Skip to content

Commit 20a2ecf

Browse files
committed
fix the import button issue
1 parent 67362c5 commit 20a2ecf

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/app/App.tsx

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,51 @@ declare module '@mui/material/styles' {
8787
}
8888

8989
export const ImportStateButton: React.FC<{}> = ({ }) => {
90-
9190
const dispatch = useDispatch();
91+
const inputRef = React.useRef<HTMLInputElement>(null);
9292

93-
let $uploadStateFile = React.createRef<HTMLInputElement>();
94-
95-
let handleFileUpload = (event: React.FormEvent<HTMLElement>): void => {
96-
const target: any = event.target;
97-
if (target && target.files) {
98-
for (let file of target.files) {
99-
//const file: File = target.files[0];
100-
(file as File).text().then((text) => {
93+
const handleFileUpload = (event: React.ChangeEvent<HTMLInputElement>): void => {
94+
const files = event.target.files;
95+
if (files) {
96+
for (let file of files) {
97+
file.text().then((text) => {
10198
try {
10299
let savedState = JSON.parse(text);
103100
dispatch(dfActions.loadState(savedState));
104-
} catch {
105-
101+
} catch (error) {
102+
console.error('Failed to parse state file:', error);
106103
}
107104
});
108105
}
109106
}
107+
// Reset the input value to allow uploading the same file again
108+
if (inputRef.current) {
109+
inputRef.current.value = '';
110+
}
110111
};
111112

112-
113-
return <Tooltip title="load a saved session">
114-
<Button variant="text" color="primary"
115-
//endIcon={<InputIcon />}
116-
>
117-
<Input inputProps={{ accept: '.dfstate', multiple: false }} id="upload-data-file"
118-
type="file" sx={{ display: 'none' }} aria-hidden={true}
119-
ref={$uploadStateFile} onChange={handleFileUpload}
120-
/>
121-
Import
122-
</Button>
123-
</Tooltip>;
113+
return (
114+
<Tooltip title="load a saved session">
115+
<Button
116+
variant="text"
117+
color="primary"
118+
onClick={() => inputRef.current?.click()}
119+
>
120+
<Input
121+
inputProps={{
122+
accept: '.dfstate',
123+
multiple: false
124+
}}
125+
id="upload-data-file"
126+
type="file"
127+
sx={{ display: 'none' }}
128+
inputRef={inputRef}
129+
onChange={handleFileUpload}
130+
/>
131+
Import
132+
</Button>
133+
</Tooltip>
134+
);
124135
}
125136

126137
export const ExportStateButton: React.FC<{}> = ({ }) => {

0 commit comments

Comments
 (0)