1
1
import { ChangeEvent , useCallback , useRef , DragEvent , useState } from "react"
2
2
import { Link } from "react-router-dom"
3
+ import { useWasm } from "./Wasm" ;
3
4
4
5
export interface DayProps {
5
6
day : number
@@ -63,6 +64,7 @@ function StandardDayPart({part}: {part: number}) {
63
64
const [ result , setResult ] = useState < string | null > ( null ) ;
64
65
const [ file , setFile ] = useState < File | null > ( null ) ;
65
66
const [ input , setInput ] = useState < string | null > ( null ) ;
67
+ const wasm = useWasm ( ) ;
66
68
67
69
const handleFileSelect = useCallback ( ( file : File ) => {
68
70
setFile ( file ) ;
@@ -73,6 +75,10 @@ function StandardDayPart({part}: {part: number}) {
73
75
} , [ setInput ] ) ;
74
76
75
77
const handleSolve = useCallback ( async ( ) => {
78
+ if ( ! wasm ) {
79
+ return ;
80
+ }
81
+
76
82
// Get an array buffer with either the file or input text, preferring the input text
77
83
const inputBuffer = input
78
84
? new TextEncoder ( ) . encode ( input )
@@ -81,9 +87,21 @@ function StandardDayPart({part}: {part: number}) {
81
87
return ;
82
88
}
83
89
84
- alert ( `TODO: Send ${ inputBuffer ?. byteLength } byte input to Rust and get result back` ) ;
90
+ wasm . greet ( ) ;
85
91
} , [ file , input ] ) ;
86
92
93
+ const disabledReason = useCallback ( ( ) => {
94
+ if ( ! wasm ) {
95
+ return "(loading WASM...)" ;
96
+ }
97
+
98
+ if ( ! file && ! input ) {
99
+ return "(no input provided)" ;
100
+ }
101
+
102
+ return "" ;
103
+ } , [ wasm , file , input ] ) ;
104
+
87
105
return < div className = "text-center flex flex-col gap-2" >
88
106
< h3 className = "font-semibold text-lg" > Part { part } </ h3 >
89
107
< p > Upload or enter your input (entered input has priority)</ p >
@@ -94,12 +112,12 @@ function StandardDayPart({part}: {part: number}) {
94
112
className = "border-2 rounded-lg p-4 w-full h-96"
95
113
onChange = { handleTextInput } />
96
114
</ div >
97
- { file || input
115
+ { wasm && ( file || input )
98
116
? < button onClick = { handleSolve } className = "bg-green-100 hover:ring-blue-500 hover:ring-2 active:bg-white rounded p-2 m-2 text-center" >
99
117
Solve
100
118
</ button >
101
119
: < button disabled = { true } className = "bg-gray-100 rounded p-2 m-2" >
102
- Solve
120
+ Solve { disabledReason ( ) }
103
121
</ button > }
104
122
{ result && < >
105
123
< h3 className = "font-semibold text-lg" > Result</ h3 >
0 commit comments