-
Notifications
You must be signed in to change notification settings - Fork 43
/
AsyncData.story.tsx
212 lines (187 loc) · 4.93 KB
/
AsyncData.story.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* eslint-disable max-depth */
import {boolean, number, withKnobs} from '@storybook/addon-knobs';
import {storiesOf} from '@storybook/react';
import React, {FC, useCallback, useMemo, useRef, useState} from 'react';
import AutoSizer from 'react-virtualized-auto-sizer';
import {
FixedSizeNodeData,
FixedSizeNodePublicState,
FixedSizeTree,
TreeWalker,
TreeWalkerValue,
} from '../src';
import {NodeComponentProps} from '../src/Tree';
import {AsyncTaskScheduler} from './utils';
document.body.style.margin = '0';
document.body.style.display = 'flex';
document.body.style.minHeight = '100vh';
const root = document.getElementById('root')!;
root.style.margin = '10px 0 0 10px';
root.style.flex = '1';
type TreeNode = Readonly<{
children: TreeNode[];
downloaded: boolean;
id: number;
name: string;
}>;
type TreeData = FixedSizeNodeData &
Readonly<{
downloaded: boolean;
download: () => Promise<void>;
isLeaf: boolean;
name: string;
nestingLevel: number;
}>;
let nodeId = 0;
const createNode = (
downloadedIds: readonly number[],
depth: number = 0,
): TreeNode => {
const id = nodeId;
const node: TreeNode = {
children: [],
downloaded: downloadedIds.includes(id),
id,
name: `test-${nodeId}`,
};
nodeId += 1;
if (depth === 5) {
return node;
}
for (let i = 0; i < 10; i++) {
node.children.push(createNode(downloadedIds, depth + 1));
}
return node;
};
const defaultTextStyle = {marginLeft: 10};
const defaultButtonStyle = {fontFamily: 'Courier New'};
type NodeMeta = Readonly<{
nestingLevel: number;
node: TreeNode;
}>;
const getNodeData = (
node: TreeNode,
nestingLevel: number,
download: () => Promise<void>,
): TreeWalkerValue<TreeData, NodeMeta> => ({
data: {
download,
downloaded: node.downloaded,
id: node.id.toString(),
isLeaf: node.children.length === 0,
isOpenByDefault: false,
name: node.name,
nestingLevel,
},
nestingLevel,
node,
});
const Node: FC<NodeComponentProps<
TreeData,
FixedSizeNodePublicState<TreeData>
>> = ({
data: {download, downloaded, isLeaf, name, nestingLevel},
isOpen,
style,
setOpen,
}) => {
const [isLoading, setLoading] = useState(false);
return (
<div
style={{
...style,
alignItems: 'center',
display: 'flex',
marginLeft: nestingLevel * 30 + (isLeaf ? 48 : 0),
}}
>
{!isLeaf && (
<div>
<button
type="button"
onClick={async () => {
if (!downloaded) {
setLoading(true);
await download();
await setOpen(!isOpen);
setLoading(false);
} else {
await setOpen(!isOpen);
}
}}
style={defaultButtonStyle}
>
{isLoading ? '⌛' : isOpen ? '-' : '+'}
</button>
</div>
)}
<div style={defaultTextStyle}>{name}</div>
</div>
);
};
type TreePresenterProps = Readonly<{
disableAsync: boolean;
itemSize: number;
}>;
const TreePresenter: FC<TreePresenterProps> = ({disableAsync, itemSize}) => {
const [downloadedIds, setDownloadedIds] = useState<readonly number[]>([]);
const scheduler = useRef<AsyncTaskScheduler<number>>(
new AsyncTaskScheduler((ids) => {
setDownloadedIds(ids);
}),
);
const rootNode = useMemo(() => {
nodeId = 0;
return createNode(downloadedIds);
}, [downloadedIds]);
const createDownloader = (node: TreeNode) => (): Promise<void> =>
new Promise((resolve) => {
const timeoutId = setTimeout(() => {
scheduler.current.finalize();
}, 2000);
scheduler.current.add(node.id, resolve, () => clearTimeout(timeoutId));
});
const treeWalker = useCallback(
function* treeWalker(): ReturnType<TreeWalker<TreeData, NodeMeta>> {
yield getNodeData(rootNode, 0, createDownloader(rootNode));
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (true) {
const parentMeta = yield;
if (parentMeta.data.downloaded) {
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < parentMeta.node.children.length; i++) {
yield getNodeData(
parentMeta.node.children[i],
parentMeta.nestingLevel + 1,
createDownloader(parentMeta.node.children[i]),
);
}
}
}
},
[rootNode],
);
return (
<AutoSizer disableWidth>
{({height}) => (
<FixedSizeTree
treeWalker={treeWalker}
itemSize={itemSize}
height={height}
async={!disableAsync}
width="100%"
>
{Node}
</FixedSizeTree>
)}
</AutoSizer>
);
};
storiesOf('Tree', module)
.addDecorator(withKnobs)
.add('Async data', () => (
<TreePresenter
disableAsync={boolean('Disable async', false)}
itemSize={number('Row height', 30)}
/>
));