-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
168 lines (142 loc) · 3.99 KB
/
index.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
import React, { useState, useContext, useRef } from "react";
import { get, set } from "lodash";
import produce from "immer";
const MISSING = {};
type StateTreePath = Array<string | number>;
type StateTreeAPI<StateTreeType> = {
stateTree: StateTreeType;
replaceStateTree: (nextStateTree: StateTreeType) => void;
markGeneratedPath: (path: StateTreePath) => void;
checkIfGeneratedPathInUse: (path: StateTreePath) => boolean;
};
const defaultStateTreeAPI = {
stateTree: null,
replaceStateTree: () => {},
markGeneratedPath: () => {},
checkIfGeneratedPathInUse: () => false,
};
const StateTree = React.createContext<StateTreeAPI<any>>(defaultStateTreeAPI);
const StateTreePath = React.createContext<StateTreePath>([]);
export function StateTreeProvider<StateTreeType extends Object>({
children,
// @ts-ignore
initialValue = {},
onUpdate = () => {},
}: {
children?: React.ReactNode;
initialValue?: StateTreeType;
onUpdate?: (stateTree: StateTreeType) => void;
}) {
const [stateTree, rawReplaceStateTree] = useState(initialValue);
const generatedPaths = useRef({});
return (
<StateTree.Provider
value={{
stateTree,
replaceStateTree(nextStateTree) {
rawReplaceStateTree(nextStateTree);
onUpdate(nextStateTree);
},
markGeneratedPath(path) {
set(generatedPaths.current, path, true);
},
checkIfGeneratedPathInUse(path) {
return get(generatedPaths.current, path, false);
},
}}
>
{children}
</StateTree.Provider>
);
}
export function StateKey({
value,
children,
}: {
value: string | number;
children: React.ReactNode;
}) {
const parentPath = useContext(StateTreePath);
return (
<StateTreePath.Provider value={parentPath.concat(value)}>
{children}
</StateTreePath.Provider>
);
}
export function StateListKey({
value,
children,
}: {
value: string;
children: React.ReactNode;
}) {
return (
<StateKey value={value}>
{React.Children.map(children, (child, index) => {
return (
<StateKey key={index} value={index}>
{child}
</StateKey>
);
})}
</StateKey>
);
}
export function useEntireStateTree() {
const { stateTree, replaceStateTree } = useContext(StateTree);
return { stateTree, replaceStateTree };
}
let defaultStateTreeKey: string | number | null = null;
export function nextStateTreeKey(nextKey: string | number) {
defaultStateTreeKey = nextKey;
}
export function useStateTree<T>(
initialValue: T,
key = defaultStateTreeKey
): [T, (nextValue: T) => void] {
defaultStateTreeKey = null;
const keyRef = useRef(key);
const stateTreeApi = useContext(StateTree);
if (stateTreeApi === defaultStateTreeAPI) {
// If they aren't using a StateTreeProvider, fall back to useState
return useState(initialValue);
}
const {
stateTree,
replaceStateTree,
markGeneratedPath,
checkIfGeneratedPathInUse,
} = stateTreeApi;
const stateTreePath = useContext(StateTreePath);
if (keyRef.current == null) {
let generatedKey;
let index: number = 0;
const valueAtStateTreePath = get(stateTree, stateTreePath, MISSING);
if (valueAtStateTreePath !== MISSING) {
index = Object.keys(valueAtStateTreePath).length;
}
do {
generatedKey = "$" + index;
index++;
} while (checkIfGeneratedPathInUse(stateTreePath.concat(generatedKey)));
markGeneratedPath(stateTreePath.concat(generatedKey));
keyRef.current = generatedKey;
}
if (keyRef.current == null) {
throw new Error("Expected keyRef to be initialized, but it was not");
}
const targetPath = stateTreePath.concat(keyRef.current);
const updateState = (nextValue: T) => {
replaceStateTree(
produce(stateTree, (draft) => {
set(draft, targetPath, nextValue);
})
);
};
let value = get(stateTree, targetPath, MISSING);
if (value === MISSING) {
updateState(initialValue);
value = initialValue;
}
return [value, updateState];
}