@@ -7,14 +7,16 @@ import { SafeParseReturnType, z } from "zod";
7
7
import { useAppSelector } from "@/store/hooks" ;
8
8
import { selectInitialState } from "@/store/debugger/debuggerSlice" ;
9
9
import { cn , getAsChunks , getAsPageMap } from "@/lib/utils.ts" ;
10
- import { TriangleAlert , UploadCloud } from "lucide-react" ;
10
+ import { UploadCloud } from "lucide-react" ;
11
11
import { Button } from "../ui/button" ;
12
12
import { useCallback , useEffect , useMemo , useState } from "react" ;
13
13
import { Input } from "../ui/input" ;
14
14
import { decodeSpiWithMetadata } from "@/utils/spi" ;
15
15
16
16
type ProgramFileUploadProps = {
17
17
onFileUpload : ( val : ProgramUploadFileOutput ) => void ;
18
+ setError : ( e ?: string ) => void ;
19
+ isError : boolean ;
18
20
close ?: ( ) => void ;
19
21
} ;
20
22
@@ -70,6 +72,9 @@ function loadFileFromUint8Array(
70
72
onFileUpload : ( d : ProgramUploadFileOutput ) => void ,
71
73
initialState : ExpectedState ,
72
74
) {
75
+ // reset error state
76
+ setError ( undefined ) ;
77
+
73
78
// Try to parse file as a JSON first
74
79
let jsonFile = null ;
75
80
let stringContent = "" ;
@@ -104,7 +109,7 @@ function loadFileFromUint8Array(
104
109
return ;
105
110
}
106
111
107
- onFileUpload ( mapUploadFileInputToOutput ( jsonFile ) ) ;
112
+ onFileUpload ( mapUploadFileInputToOutput ( jsonFile , "JSON test" ) ) ;
108
113
return ;
109
114
}
110
115
@@ -123,6 +128,8 @@ function loadFileFromUint8Array(
123
128
onFileUpload ( {
124
129
program : Array . from ( code ) ,
125
130
name : `${ loadedFileName } [spi]` ,
131
+ isSpi : true ,
132
+ kind : "JAM SPI" ,
126
133
initial : {
127
134
regs : Array . from ( registers ) . map ( ( x ) => BigInt ( x as number | bigint ) ) as RegistersArray ,
128
135
pc : 0 ,
@@ -146,20 +153,21 @@ function loadFileFromUint8Array(
146
153
onFileUpload ( {
147
154
program : Array . from ( uint8Array ) ,
148
155
name : `${ loadedFileName } [generic]` ,
156
+ isSpi : false ,
157
+ kind : "Generic PVM" ,
149
158
initial : initialState ,
150
159
} ) ;
151
160
} else {
152
161
setError ( "Unrecognized program format (see console)." ) ;
153
162
}
154
163
}
155
164
156
- export const ProgramFileUpload : React . FC < ProgramFileUploadProps > = ( { onFileUpload, close } ) => {
165
+ export const ProgramFileUpload : React . FC < ProgramFileUploadProps > = ( { isError , onFileUpload, close, setError } ) => {
157
166
const initialState = useAppSelector ( selectInitialState ) ;
158
167
const spiArgs = useAppSelector ( ( state ) => state . debugger . spiArgs ) ;
159
168
160
169
const [ isUpload , setIsUpload ] = useState ( true ) ;
161
170
const [ manualInput , setManualInput ] = useState ( "" ) ;
162
- const [ error , setError ] = useState < string > ( ) ;
163
171
const [ loadedFileName , setLoadedFileName ] = useState < string | undefined > ( undefined ) ;
164
172
165
173
const spiArgsAsBytes = useMemo ( ( ) => {
@@ -173,18 +181,17 @@ export const ProgramFileUpload: React.FC<ProgramFileUploadProps> = ({ onFileUplo
173
181
useEffect ( ( ) => {
174
182
// reset the state of upload
175
183
if ( isUpload ) {
176
- setLoadedFileName ( undefined ) ;
177
- setError ( undefined ) ;
178
184
return ;
179
185
}
186
+
180
187
if ( manualInput === "" ) {
181
188
setError ( undefined ) ;
182
189
return ;
183
190
}
184
191
185
192
const buffer = new TextEncoder ( ) . encode ( manualInput ) ;
186
193
loadFileFromUint8Array ( "pasted" , buffer , spiArgsAsBytes , setError , onFileUpload , initialState ) ;
187
- } , [ manualInput , isUpload , initialState , onFileUpload , spiArgsAsBytes ] ) ;
194
+ } , [ manualInput , isUpload , initialState , onFileUpload , spiArgsAsBytes , setError ] ) ;
188
195
189
196
const handleFileRead = ( e : ProgressEvent < FileReader > ) => {
190
197
setError ( undefined ) ;
@@ -227,9 +234,11 @@ export const ProgramFileUpload: React.FC<ProgramFileUploadProps> = ({ onFileUplo
227
234
open ( ) ;
228
235
} , [ open ] ) ;
229
236
230
- const toggleUpload = useCallback ( ( ) => {
231
- setIsUpload ( ( upload ) => ! upload ) ;
232
- } , [ ] ) ;
237
+ const setNoUpload = useCallback ( ( ) => {
238
+ setIsUpload ( false ) ;
239
+ setLoadedFileName ( undefined ) ;
240
+ setError ( undefined ) ;
241
+ } , [ setError ] ) ;
233
242
234
243
const isLoaded = loadedFileName !== undefined ;
235
244
@@ -253,14 +262,14 @@ export const ProgramFileUpload: React.FC<ProgramFileUploadProps> = ({ onFileUplo
253
262
value = { manualInput }
254
263
onChange = { ( e ) => setManualInput ( e . target . value ) }
255
264
className = { cn ( "flex-auto text-xs" , {
256
- "focus-visible:ring-red-500 ring-red-500 ring-2" : error ,
265
+ "focus-visible:ring-red-500 ring-red-500 ring-2" : isError ,
257
266
} ) }
258
267
/>
259
268
) }
260
269
</ div >
261
270
< div className = "flex space-x-2" >
262
271
{ isUpload && (
263
- < Button className = "text-[10px] py-1 h-9" variant = "ghost" onClick = { toggleUpload } >
272
+ < Button className = "text-[10px] py-1 h-9" variant = "ghost" onClick = { setNoUpload } >
264
273
Paste manually
265
274
</ Button >
266
275
) }
@@ -270,12 +279,6 @@ export const ProgramFileUpload: React.FC<ProgramFileUploadProps> = ({ onFileUplo
270
279
</ div >
271
280
< input { ...getInputProps ( ) } className = "hidden" />
272
281
</ div >
273
-
274
- { error && (
275
- < p className = "flex items-center text-destructive-foreground mt-3 text-[11px] whitespace-pre-line" >
276
- < TriangleAlert className = "mr-2" height = "18px" /> { error }
277
- </ p >
278
- ) }
279
282
</ div >
280
283
) ;
281
284
} ;
0 commit comments