-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathFilePicker.tsx
More file actions
50 lines (46 loc) · 1.44 KB
/
FilePicker.tsx
File metadata and controls
50 lines (46 loc) · 1.44 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
// adapted from https://github.com/Jaaneek/useFilePicker
import { Dispatch, SetStateAction } from "react";
import { useFilePicker } from "use-file-picker";
import {
FileAmountLimitValidator,
FileSizeValidator,
} from "use-file-picker/validators";
export interface FilePickerProps {
readonly setUpload: Dispatch<SetStateAction<string>>;
readonly setFile: Dispatch<SetStateAction<Blob | undefined>>;
}
export default function FilePicker({ setUpload, setFile }: FilePickerProps) {
const { openFilePicker, loading, errors } = useFilePicker({
readAs: "DataURL",
accept: [".html"],
multiple: false,
validators: [
new FileAmountLimitValidator({ max: 1 }),
new FileSizeValidator({ maxFileSize: 2 * 1024 * 1024 /* 2 MB */ }),
],
onFilesSuccessfullySelected: ({ plainFiles }) => {
// this callback is called when there were no validation errors
const userFile = plainFiles[0];
setUpload(userFile.name);
// API call to add the html file of bookmarks
// api.import()...
setFile(userFile);
// handle adding each bookmark returned to frontend or rerender
},
});
if (loading) {
return <div>Loading...</div>;
}
if (errors.length) {
return <div>Error...</div>;
}
return (
<button
className="btn btn-outline-info"
onClick={() => openFilePicker()}
title="Import Bookmarks"
>
<i className="bi bi-arrow-bar-up"> Upload File</i>
</button>
);
}