This repository was archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuseInformativePanels.ts
More file actions
204 lines (192 loc) · 6.77 KB
/
useInformativePanels.ts
File metadata and controls
204 lines (192 loc) · 6.77 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
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
import {useState,useCallback,useEffect} from 'react'
import { useMounted } from '@Utils/Hooks';
import {OperationState} from '../PanelCard/PanelCard';
// @ts-ignore
import worker from 'workerize-loader!./worker'; // eslint-disable-line
const NO_BASE64STRINGFILE = 'NO_BASE64STRINGFILE';
interface IPanel {
/** whether it's loading file, is completed, is failure */
operationState: OperationState;
/** name of file associated with the informative panel */
name: string;
/** worker; will do the job of reading the file */
worker: Worker | null;
/** the file associated with the informative panel */
file: File | null;
}
interface IInformativePanels {
/** array of panels */
panels: IPanel[];
/** names of files already uploaded, or failed, or cancelled */
makeItDisappear: string[];
/** names of files for which we want to start workers */
startWorkers: string[];
}
export const useInformativePanels = (
isTestIsFailure: boolean,
onFile: (base64StringFile: string) => void,
messageDuration: number,
): readonly [
IPanel[],
(acceptedFiles: File[]) => void,
(name: string) => () => void,
] => {
const isMounted = useMounted();
const [informativePanels, setInformativePanels] =
useState<IInformativePanels>({
panels: [],
makeItDisappear: [],
startWorkers: [],
});
/**
* set end state
*/
const prepareForEndInformativePanel = useCallback(
(operationState: OperationState, informativePanel: IPanel): void => {
setInformativePanels((prev) => ({
...prev,
panels: prev.panels.map((panel) => {
if (panel.name === informativePanel.name)
return {
...panel,
operationState,
};
return panel;
}),
makeItDisappear: [
...prev.makeItDisappear,
informativePanel.name,
],
}));
},
[],
);
/**
* terminate worker and set state of informative panel to success or failure and
* send order to remove informative panel in the future. also do whatever user
* wants to do with the file read in case of success
*/
const onWorkerMessage = useCallback(
(e: any) => {
const { base64StringFile, name } = e.data;
if (base64StringFile === undefined) {
return;
}
const informativePanel = informativePanels.panels.find(
(panel) => panel.name === name,
);
if (informativePanel) {
if (
base64StringFile === NO_BASE64STRINGFILE ||
isTestIsFailure
) {
prepareForEndInformativePanel(
OperationState.isFailure,
informativePanel,
);
} else {
onFile(base64StringFile);
prepareForEndInformativePanel(
OperationState.isSuccess,
informativePanel,
);
}
}
},
[
informativePanels.panels,
isTestIsFailure,
onFile,
prepareForEndInformativePanel,
],
);
// start workers after files have been droped and array of informative panels
// are loaded
useEffect(() => {
if (informativePanels.startWorkers.length) {
informativePanels.startWorkers.forEach((name) => {
const informativePanel = informativePanels.panels.find(
(panel) => panel.name === name,
);
if (informativePanel && informativePanel.worker) {
informativePanel.worker.onmessage = onWorkerMessage;
informativePanel.worker?.postMessage({
file: informativePanel.file,
});
}
});
setInformativePanels((prev) => ({
...prev,
startWorkers: [],
}));
}
}, [informativePanels.startWorkers.length]);
// make disappear informative panels in the future
useEffect(() => {
if (informativePanels.makeItDisappear.length) {
informativePanels.makeItDisappear.forEach((name) => {
setTimeout(() => {
if (isMounted.current) {
setInformativePanels((prev) => ({
...prev,
panels: prev.panels.filter((panel) => {
if (panel.name === name) {
panel.worker?.terminate();
return false;
}
return true;
}),
makeItDisappear: prev.makeItDisappear.filter(
(name_) => name_ !== name,
),
}));
}
}, messageDuration);
});
}
}, [informativePanels.makeItDisappear.length]);
// terminate workers on clean up function
useEffect(
() => () => {
if (!isMounted.current) {
informativePanels.panels.forEach((panel) =>
panel.worker?.terminate(),
);
}
},
[informativePanels.panels],
);
/**
* load array of informative panels and send order to start workers
*/
const onDrop = useCallback((acceptedFiles: File[]) => {
const newInformativePanels = acceptedFiles.map((file) => {
const workerInstance = worker();
return {
operationState: OperationState.isLoading,
name: file.name,
worker: workerInstance,
file,
};
});
const fileNames = acceptedFiles.map((file) => file.name);
setInformativePanels((prev) => ({
...prev,
panels: [...prev.panels, ...newInformativePanels],
startWorkers: [...fileNames],
}));
}, []);
const onCancelUploading = (name: string) => () => {
setInformativePanels((prev) => ({
...prev,
panels: prev.panels.filter((panel) => {
if (panel.name === name) {
panel.worker?.terminate();
return false;
}
return true;
}),
}));
};
return [informativePanels.panels, onDrop, onCancelUploading] as const;
};