-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add deploy method + some refactoring
- Loading branch information
Showing
12 changed files
with
482 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { | ||
UseFormRegister, | ||
UseFormSetValue, | ||
UseFormUnregister, | ||
} from 'react-hook-form'; | ||
|
||
import { Button, FormControl, FormLabel, Input } from '@chakra-ui/react'; | ||
|
||
import { toHexString } from '../../utils/hexString'; | ||
import PreviewDataRow from '../PreviewDataRow'; | ||
|
||
import { FormValues } from './schemas'; | ||
|
||
type DeployProps = { | ||
register: UseFormRegister<FormValues>; | ||
unregister: UseFormUnregister<FormValues>; | ||
setValue: UseFormSetValue<FormValues>; | ||
}; | ||
|
||
function Deploy({ register, unregister, setValue }: DeployProps): JSX.Element { | ||
const [program, setProgram] = useState(''); | ||
const fileRef = useRef<HTMLInputElement | null>(null); | ||
|
||
useEffect(() => { | ||
register('payload.program'); | ||
return () => { | ||
unregister('payload.program'); | ||
}; | ||
}, [unregister, register]); | ||
|
||
useEffect(() => { | ||
if (program) { | ||
setValue('payload.program', program); | ||
} | ||
}, [program, setValue, unregister]); | ||
|
||
return ( | ||
<FormControl isRequired> | ||
<FormLabel fontSize="sm" mb={1}> | ||
Choose the compiled program to deploy a template: | ||
</FormLabel> | ||
<Button | ||
w="100%" | ||
variant="whiteModal" | ||
onClick={() => { | ||
if (fileRef.current) { | ||
fileRef.current.click(); | ||
} | ||
}} | ||
> | ||
Select .ELF file | ||
</Button> | ||
<Input | ||
ref={fileRef} | ||
type="file" | ||
accept=".elf" | ||
multiple={false} | ||
display="none" | ||
onChange={(evt) => { | ||
if (evt.target.files?.[0]) { | ||
const file = evt.target.files[0]; | ||
const reader = new FileReader(); | ||
reader.onloadend = () => { | ||
if (!reader.result) return; | ||
const content = | ||
typeof reader.result === 'string' | ||
? reader.result | ||
: toHexString(reader.result); | ||
setProgram(content); | ||
// To allow load the same program again: | ||
// eslint-disable-next-line no-param-reassign | ||
evt.target.value = ''; | ||
}; | ||
reader.readAsArrayBuffer(file); | ||
} | ||
}} | ||
/> | ||
<PreviewDataRow | ||
label="Program bytecode" | ||
value={program} | ||
boxProps={{ | ||
my: 2, | ||
display: program ? 'block' : 'none', | ||
}} | ||
valueProps={{ | ||
as: 'pre', | ||
fontSize: 'xx-small', | ||
wordBreak: 'break-all', | ||
whiteSpace: 'pre-wrap', | ||
maxH: '100px', | ||
overflowY: 'auto', | ||
}} | ||
/> | ||
</FormControl> | ||
); | ||
} | ||
|
||
export default Deploy; |
Oops, something went wrong.