-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
useStore.tsx
198 lines (179 loc) · 4.89 KB
/
useStore.tsx
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { deepEqual } from "fast-equals";
import * as React from "react";
import invariant from "tiny-invariant";
import { ActionInterface } from "./action/ActionInterface";
import { history } from "./action/HistoryImpl";
import type {
Action,
IKBarContext,
KBarOptions,
KBarProviderProps,
KBarQuery,
KBarState,
} from "./types";
import { VisualState } from "./types";
type useStoreProps = KBarProviderProps;
export function useStore(props: useStoreProps) {
const optionsRef = React.useRef({
animations: {
enterMs: 200,
exitMs: 100,
},
...props.options,
} as KBarOptions);
const actionsInterface = React.useMemo(
() =>
new ActionInterface(props.actions || [], {
historyManager: optionsRef.current.enableHistory ? history : undefined,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
// TODO: at this point useReducer might be a better approach to managing state.
const [state, setState] = React.useState<KBarState>({
searchQuery: "",
currentRootActionId: null,
visualState: VisualState.hidden,
actions: { ...actionsInterface.actions },
activeIndex: 0,
disabled: false,
});
const currState = React.useRef(state);
currState.current = state;
const getState = React.useCallback(() => currState.current, []);
const publisher = React.useMemo(() => new Publisher(getState), [getState]);
React.useEffect(() => {
currState.current = state;
publisher.notify();
}, [state, publisher]);
const registerActions = React.useCallback(
(actions: Action[]) => {
setState((state) => {
return {
...state,
actions: actionsInterface.add(actions),
};
});
return function unregister() {
setState((state) => {
return {
...state,
actions: actionsInterface.remove(actions),
};
});
};
},
[actionsInterface]
);
const inputRef = React.useRef<HTMLInputElement | null>(null);
return React.useMemo(() => {
const query: KBarQuery = {
setCurrentRootAction: (actionId) => {
setState((state) => ({
...state,
currentRootActionId: actionId,
}));
},
setVisualState: (cb) => {
setState((state) => ({
...state,
visualState: typeof cb === "function" ? cb(state.visualState) : cb,
}));
},
setSearch: (searchQuery) =>
setState((state) => ({
...state,
searchQuery,
})),
registerActions,
toggle: () =>
setState((state) => ({
...state,
visualState: [VisualState.animatingOut, VisualState.hidden].includes(
state.visualState
)
? VisualState.animatingIn
: VisualState.animatingOut,
})),
setActiveIndex: (cb) =>
setState((state) => ({
...state,
activeIndex: typeof cb === "number" ? cb : cb(state.activeIndex),
})),
inputRefSetter: (el: HTMLInputElement) => {
inputRef.current = el;
},
getInput: () => {
invariant(
inputRef.current,
"Input ref is undefined, make sure you attach `query.inputRefSetter` to your search input."
);
return inputRef.current;
},
disable: (disable: boolean) => {
setState((state) => ({
...state,
disabled: disable,
}));
},
};
return {
getState,
query,
options: optionsRef.current,
subscribe: (collector, cb) => publisher.subscribe(collector, cb),
} as IKBarContext;
}, [getState, publisher, registerActions]);
}
class Publisher {
getState;
subscribers: Subscriber[] = [];
constructor(getState: () => KBarState) {
this.getState = getState;
}
subscribe<C>(
collector: (state: KBarState) => C,
onChange: (collected: C) => void
) {
const subscriber = new Subscriber(
() => collector(this.getState()),
onChange
);
this.subscribers.push(subscriber);
return this.unsubscribe.bind(this, subscriber);
}
unsubscribe(subscriber: Subscriber) {
if (this.subscribers.length) {
const index = this.subscribers.indexOf(subscriber);
if (index > -1) {
return this.subscribers.splice(index, 1);
}
}
}
notify() {
this.subscribers.forEach((subscriber) => subscriber.collect());
}
}
class Subscriber {
collected: any;
collector;
onChange;
constructor(collector: () => any, onChange: (collected: any) => any) {
this.collector = collector;
this.onChange = onChange;
}
collect() {
try {
// grab latest state
const recollect = this.collector();
if (!deepEqual(recollect, this.collected)) {
this.collected = recollect;
if (this.onChange) {
this.onChange(this.collected);
}
}
} catch (error) {
console.warn(error);
}
}
}