@@ -87,40 +87,51 @@ declare module '@mui/material/styles' {
87
87
}
88
88
89
89
export const ImportStateButton : React . FC < { } > = ( { } ) => {
90
-
91
90
const dispatch = useDispatch ( ) ;
91
+ const inputRef = React . useRef < HTMLInputElement > ( null ) ;
92
92
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 ) => {
101
98
try {
102
99
let savedState = JSON . parse ( text ) ;
103
100
dispatch ( dfActions . loadState ( savedState ) ) ;
104
- } catch {
105
-
101
+ } catch ( error ) {
102
+ console . error ( 'Failed to parse state file:' , error ) ;
106
103
}
107
104
} ) ;
108
105
}
109
106
}
107
+ // Reset the input value to allow uploading the same file again
108
+ if ( inputRef . current ) {
109
+ inputRef . current . value = '' ;
110
+ }
110
111
} ;
111
112
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
+ ) ;
124
135
}
125
136
126
137
export const ExportStateButton : React . FC < { } > = ( { } ) => {
0 commit comments