-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRoot.tsx
More file actions
102 lines (90 loc) · 2.79 KB
/
Copy pathRoot.tsx
File metadata and controls
102 lines (90 loc) · 2.79 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Component } from 'solid-js';
import { createStore, produce } from 'solid-js/store';
import { createKbdShortcuts } from './createKbdShortcuts';
import { getActiveParentAction } from './actionUtils/actionUtils';
import { rootParentActionId } from './constants';
import { Provider } from './StoreContext';
import { RootProps, StoreState, StoreMethods, StoreContext, DynamicContextMap } from './types';
const RootInternal: Component = () => {
createKbdShortcuts();
return null;
};
export const Root: Component<RootProps> = (p) => {
const initialActions = p.actions || {};
const initialActionsContext = p.actionsContext || {};
const initialVisibleActions = p.initialVisibleActions || 'root';
const [state, setState] = createStore<StoreState>({
visibility: 'closed',
searchText: '',
activeParentActionIdList: [rootParentActionId],
actions: initialActions,
actionsContext: {
root: initialActionsContext,
dynamic: {},
},
components: p.components,
initialVisibleActions: initialVisibleActions,
});
const storeMethods: StoreMethods = {
setSearchText(newValue) {
setState('searchText', newValue);
},
setActionsContext(actionId, newData) {
// @ts-expect-error need to figure out nested store setters.
setState('actionsContext', 'dynamic', actionId, newData);
},
resetActionsContext(actionId) {
setState(
'actionsContext',
'dynamic',
produce<DynamicContextMap>((dynamicContext) => {
delete dynamicContext[actionId];
})
);
},
openPalette() {
setState('visibility', 'opened');
},
closePalette() {
setState('visibility', 'closed');
const hasActiveParent = state.activeParentActionIdList.length > 1;
if (hasActiveParent) {
storeMethods.setSearchText('');
storeMethods.resetParentAction();
}
},
togglePalette() {
setState('visibility', (prev) => (prev === 'opened' ? 'closed' : 'opened'));
},
selectParentAction(parentActionId) {
if (parentActionId === rootParentActionId) {
return;
}
setState('activeParentActionIdList', (old) => {
return [...old, parentActionId];
});
storeMethods.setSearchText('');
},
revertParentAction() {
setState('activeParentActionIdList', (old) => {
const { isRoot } = getActiveParentAction(old);
if (isRoot) {
return old;
}
const copiedList = [...old];
copiedList.pop();
return copiedList;
});
},
resetParentAction() {
setState('activeParentActionIdList', [rootParentActionId]);
},
};
const store: StoreContext = [state, storeMethods];
return (
<Provider value={store}>
<RootInternal />
{p.children}
</Provider>
);
};