-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
TerminalRedux.ts
66 lines (53 loc) 路 1.42 KB
/
TerminalRedux.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2022-2023 Datalayer Inc. All rights reserved.
*
* MIT License
*/
import { useSelector } from "react-redux";
import actionCreatorFactory from "typescript-fsa";
import { reducerWithInitialState } from "typescript-fsa-reducers";
import TerminalAdapter from "./TerminalAdapter";
/* State */
export type ITerminal = boolean;
export interface ITerminalState {
dark: ITerminal;
adapter?: TerminalAdapter,
}
export const terminalInitialState: ITerminalState = {
dark: false,
}
/* Selectors */
export const selectTerminal = (): ITerminalState =>
useSelector((state: ITerminalState) => {
if ((state as any).terminal) {
return (state as any).terminal;
}
return terminalInitialState;
}
);
/* Actions */
export enum TerminalActionType {
UPDATE = "terminal/UPDATE",
}
const actionCreator = actionCreatorFactory('jupyterReact');
export const terminalActions = {
update: actionCreator<Partial<ITerminalState>>(TerminalActionType.UPDATE),
}
/* Reducers */
export const terminalReducer = reducerWithInitialState(terminalInitialState)
.case(terminalActions.update, (state: ITerminalState, update: Partial<ITerminalState>) => {
if (state.adapter) {
if (update.dark !== undefined) {
if (update.dark) {
state.adapter.setTheme('dark');
} else {
state.adapter.setTheme('light');
}
}
}
return {
...state,
...update,
}
}
);