Skip to content

Commit f134ad2

Browse files
committed
fix: add error message on upload fail
1 parent ed0e7bc commit f134ad2

1 file changed

Lines changed: 36 additions & 26 deletions

File tree

apps/playground/src/components/Header/UploadButton/UploadButton.tsx

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,46 @@ export const UploadButton = () => {
2020
const instruments = useAppStore((store) => store.instruments);
2121

2222
const handleSubmit = async (files: File[]) => {
23-
const zip = new JSZip() as JSZip & { comment?: unknown };
24-
await zip.loadAsync(files[0]!);
25-
let label: string;
2623
try {
27-
const comment = JSON.parse(String(zip.comment)) as unknown;
28-
if (isPlainObject(comment) && typeof comment.label === 'string') {
29-
label = comment.label;
30-
} else {
24+
const zip = new JSZip() as JSZip & { comment?: unknown };
25+
await zip.loadAsync(files[0]!);
26+
let label: string;
27+
try {
28+
const comment = JSON.parse(String(zip.comment)) as unknown;
29+
if (isPlainObject(comment) && typeof comment.label === 'string') {
30+
label = comment.label;
31+
} else {
32+
label = 'Unlabeled';
33+
}
34+
} catch {
3135
label = 'Unlabeled';
3236
}
33-
} catch {
34-
label = 'Unlabeled';
35-
}
36-
let suffixNumber = 1;
37-
let uniqueLabel = label;
38-
while (instruments.find((instrument) => instrument.label === uniqueLabel)) {
39-
uniqueLabel = `${label} (${suffixNumber})`;
40-
suffixNumber++;
37+
let suffixNumber = 1;
38+
let uniqueLabel = label;
39+
while (instruments.find((instrument) => instrument.label === uniqueLabel)) {
40+
uniqueLabel = `${label} (${suffixNumber})`;
41+
suffixNumber++;
42+
}
43+
const item: InstrumentRepository = {
44+
category: 'Saved',
45+
files: await loadEditorFilesFromZip(zip),
46+
id: crypto.randomUUID(),
47+
kind: null,
48+
label: uniqueLabel
49+
};
50+
addInstrument(item);
51+
setSelectedInstrument(item.id);
52+
addNotification({ type: 'success' });
53+
} catch (err) {
54+
console.error(err);
55+
addNotification({
56+
message: 'Please refer to browser console for details',
57+
title: 'Upload Failed',
58+
type: 'error'
59+
});
60+
} finally {
61+
setIsDialogOpen(false);
4162
}
42-
const item: InstrumentRepository = {
43-
category: 'Saved',
44-
files: await loadEditorFilesFromZip(zip),
45-
id: crypto.randomUUID(),
46-
kind: null,
47-
label: uniqueLabel
48-
};
49-
addInstrument(item);
50-
setSelectedInstrument(item.id);
51-
setIsDialogOpen(false);
52-
addNotification({ type: 'success' });
5363
};
5464

5565
return (

0 commit comments

Comments
 (0)