Skip to content

Commit d3defb5

Browse files
committed
check FilePicker availability
1 parent 7cb0904 commit d3defb5

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

src/features/modals/ImportModal/index.tsx

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import type { ModalProps } from "@mantine/core";
3-
import { Modal, Group, Button, TextInput, Stack, Paper, Text } from "@mantine/core";
3+
import { Modal, Group, Button, TextInput, Stack, Paper, Text, Tooltip } from "@mantine/core";
44
import { Dropzone } from "@mantine/dropzone";
55
import { event as gaEvent } from "nextjs-google-analytics";
66
import toast from "react-hot-toast";
@@ -13,6 +13,11 @@ export const ImportModal = ({ opened, onClose }: ModalProps) => {
1313
const [url, setURL] = React.useState("");
1414
const [file, setFile] = React.useState<File | null>(null);
1515

16+
const availableFilePicker = React.useMemo(() => {
17+
if (typeof window === "undefined") return false;
18+
return "showOpenFilePicker" in window;
19+
}, []);
20+
1621
const toggleWatchMode = useWatchMode(state => state.toggleWatchMode);
1722
const setWatcherInterval = useWatchMode(state => state.setWatcherInterval);
1823
const setContents = useFile(state => state.setContents);
@@ -52,12 +57,13 @@ export const ImportModal = ({ opened, onClose }: ModalProps) => {
5257
}
5358
};
5459

55-
const enableWatcher = async () => {
56-
// El usuario debe seleccionar el archivo manualmente desde el picker
60+
const enableWatcher = React.useCallback(async () => {
61+
if (!availableFilePicker) return;
62+
5763
const [fileHandle] = await window.showOpenFilePicker({
5864
types: [
5965
{
60-
description: "Cualquier archivo",
66+
description: "Any file",
6167
accept: {
6268
"application/json": [".json"],
6369
"application/x-yaml": [".yaml", ".yml"],
@@ -67,11 +73,16 @@ export const ImportModal = ({ opened, onClose }: ModalProps) => {
6773
},
6874
},
6975
],
70-
});
76+
}).catch(() => []);
77+
78+
if (!fileHandle) return;
7179

7280
const file = await fileHandle.getFile();
7381
let lastModified = file.lastModified;
7482
const text = await file.text();
83+
const lastIndex = file.name.lastIndexOf(".");
84+
const format = file.name.substring(lastIndex + 1);
85+
setFormat(format as FileFormat);
7586
setContents({ contents: text });
7687
toggleWatchMode(true);
7788
const interval = setInterval(async () => {
@@ -87,6 +98,28 @@ export const ImportModal = ({ opened, onClose }: ModalProps) => {
8798
setFile(null);
8899
setURL("");
89100
onClose();
101+
}, [availableFilePicker, onClose, setContents, setFormat, setWatcherInterval, toggleWatchMode]);
102+
103+
const WatcherButton = () => {
104+
if (!availableFilePicker)
105+
return (
106+
<Tooltip
107+
label="Not suported in this browser"
108+
fz="xs"
109+
ta="center"
110+
maw="200"
111+
withArrow
112+
openDelay={500}
113+
>
114+
<Button disabled={true}>Watcher</Button>
115+
</Tooltip>
116+
);
117+
118+
return (
119+
<Button onClick={enableWatcher} disabled={!!(file || url)}>
120+
Watcher
121+
</Button>
122+
);
90123
};
91124

92125
return (
@@ -134,9 +167,7 @@ export const ImportModal = ({ opened, onClose }: ModalProps) => {
134167
</Paper>
135168
</Stack>
136169
<Group justify="right">
137-
<Button onClick={enableWatcher} disabled={!!(file || url)}>
138-
Watcher
139-
</Button>
170+
<WatcherButton />
140171
<Button onClick={handleImportFile} disabled={!(file || url)}>
141172
Import
142173
</Button>

0 commit comments

Comments
 (0)