shrub/pkg/interface/webterm/state.ts

39 lines
929 B
TypeScript
Raw Normal View History

import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
2021-12-06 18:05:14 +03:00
import create from 'zustand';
import produce from 'immer';
2022-03-10 01:25:10 +03:00
export type Session = {
term: Terminal,
fit: FitAddon,
hasBell: boolean,
pending: number,
subscriptionId: number | null,
} | null;
export type Sessions = { [id: string]: Session; }
export interface TermState {
sessions: Sessions,
names: string[],
selected: string,
slogstream: null | EventSource,
theme: 'auto' | 'light' | 'dark',
//TODO: figure out the type
set: any,
}
2022-03-10 01:25:10 +03:00
// eslint-disable-next-line no-unused-vars
2021-12-06 18:05:14 +03:00
const useTermState = create<TermState>((set, get) => ({
sessions: {} as Sessions,
names: [''],
selected: '', // empty string is default session
slogstream: null,
2021-12-06 18:05:14 +03:00
theme: 'auto',
2022-03-10 01:25:10 +03:00
// eslint-disable-next-line no-unused-vars
2021-12-06 18:05:14 +03:00
set: (f: (draft: TermState) => void) => {
set(produce(f));
}
} as TermState));
2021-12-06 18:05:14 +03:00
export default useTermState;