diff --git a/src/renderer/components/InputField.tsx b/src/renderer/components/InputField.tsx index eb754c8..6a9ce6a 100644 --- a/src/renderer/components/InputField.tsx +++ b/src/renderer/components/InputField.tsx @@ -1,5 +1,6 @@ import { InputSchema } from 'src/shared/generated_models'; -import { match } from 'ts-pattern'; +import { match, NonExhaustiveError } from 'ts-pattern'; +import log from 'electron-log/renderer'; import DirectoryField from './input_fields/DirectoryField'; import FileField from './input_fields/FileField'; import TextField from './input_fields/TextField'; @@ -17,6 +18,64 @@ type InputFieldProps = { disabled?: boolean; }; +function getFieldByInputSchema( + inputSchema: InputSchema, + value: any, + onChange: (value: any) => void, + disabled: boolean, +) { + try { + return match(inputSchema) + .with({ inputType: 'text' }, () => ( + + )) + .with({ inputType: 'textarea' }, () => ( + + )) + .with({ inputType: 'file' }, () => ( + + )) + .with({ inputType: 'directory' }, () => ( + + )) + .with({ inputType: 'batchtext' }, () => ( + + )) + .with({ inputType: 'batchfile' }, () => ( + + )) + .with({ inputType: 'batchdirectory' }, () => ( + + )) + .with( + { + inputType: { + inputType: 'newfile', + }, + }, + () => ( + + ), + ) + .exhaustive(); + } catch (e) { + if (e instanceof NonExhaustiveError) { + log.error('Received an unexpected input schema.', inputSchema); + return
Unsupported input type.
; + } + throw e; + } +} + export default function InputField({ inputSchema, value, @@ -31,66 +90,7 @@ export default function InputField({ {inputSchema.subtitle && (

{inputSchema.subtitle}

)} - {match(inputSchema) - .with({ inputType: 'text' }, () => ( - - )) - .with({ inputType: 'textarea' }, () => ( - - )) - .with({ inputType: 'file' }, () => ( - - )) - .with({ inputType: 'directory' }, () => ( - - )) - .with({ inputType: 'batchtext' }, () => ( - - )) - .with({ inputType: 'batchfile' }, () => ( - - )) - .with({ inputType: 'batchdirectory' }, () => ( - - )) - .with( - { - inputType: { - inputType: 'newfile', - }, - }, - () => ( - - ), - ) - .otherwise(() => ( -
Unsupported input type.
- ))} + {getFieldByInputSchema(inputSchema, value, onChange, disabled)} ); } diff --git a/src/renderer/components/ParameterField.tsx b/src/renderer/components/ParameterField.tsx index 73c3611..e2a60e6 100644 --- a/src/renderer/components/ParameterField.tsx +++ b/src/renderer/components/ParameterField.tsx @@ -1,5 +1,7 @@ import { ParameterSchema } from 'src/shared/generated_models'; -import { match } from 'ts-pattern'; +import { match, NonExhaustiveError } from 'ts-pattern'; +import log from 'electron-log/renderer'; +import { ReactNode } from 'react'; import TextField from './parameter_fields/TextField'; import IntField from './parameter_fields/IntField'; import FloatField from './parameter_fields/FloatField'; @@ -7,93 +9,147 @@ import EnumField from './parameter_fields/EnumField'; import RangedIntField from './parameter_fields/RangedIntField'; import RangedFloatField from './parameter_fields/RangedFloatField'; -export default function ParameterField({ - parameterSchema, - value, - onChange, - disabled = false, +function LabelAndSubtitle({ + children, + label, + subtitle, }: { - parameterSchema: ParameterSchema; - value: any; - onChange: (value: any) => void; - // eslint-disable-next-line - disabled?: boolean; + children: ReactNode; + label: string; + subtitle: string | null; }) { - const { value: paramValue } = parameterSchema; return (
-

- {parameterSchema.label} -

- {parameterSchema.subtitle && ( -

{parameterSchema.subtitle}

- )} - {match(paramValue) - .with({ parameterType: 'text' }, () => { - return ( +

{label}

+ {subtitle &&

{subtitle}

} + {children} +
+ ); +} + +function getFieldByParameterSchema( + parameterSchema: ParameterSchema, + value: any, + onChange: (value: any) => void, + disabled: boolean, +) { + try { + return match(parameterSchema.value) + .with({ parameterType: 'text' }, () => { + return ( + - ); - }) - .with({ parameterType: 'int' }, () => { - return ( + + ); + }) + .with({ parameterType: 'int' }, () => { + return ( + - ); - }) - .with({ parameterType: 'float' }, () => { - return ( + + ); + }) + .with({ parameterType: 'float' }, () => { + return ( + - ); - }) - .with({ parameterType: 'enum' }, () => { - return ( + + ); + }) + .with({ parameterType: 'enum' }, () => { + return ( + - ); - }) - .with({ parameterType: 'ranged_int' }, () => { - return ( + + ); + }) + .with({ parameterType: 'ranged_int' }, () => { + return ( + - ); - }) - .with({ parameterType: 'ranged_float' }, () => { - return ( + + ); + }) + .with({ parameterType: 'ranged_float' }, () => { + return ( + - ); - }) - .otherwise(() => { - return ( -
Unsupported parameter type: {paramValue.parameterType}
- ); - })} - - ); +
+ ); + }) + .exhaustive(); + } catch (e) { + if (e instanceof NonExhaustiveError) { + log.error('Received an unexpected parameter schema.', parameterSchema); + return ( +
+ Unsupported parameter type: {parameterSchema.value.parameterType} +
+ ); + } + throw e; + } +} + +export default function ParameterField({ + parameterSchema, + value, + onChange, + disabled = false, +}: { + parameterSchema: ParameterSchema; + value: any; + onChange: (value: any) => void; + // eslint-disable-next-line + disabled?: boolean; +}) { + return getFieldByParameterSchema(parameterSchema, value, onChange, disabled); } diff --git a/src/renderer/jobs/PreviewResponseBody.tsx b/src/renderer/jobs/PreviewResponseBody.tsx index cc9a351..5ea7ed7 100644 --- a/src/renderer/jobs/PreviewResponseBody.tsx +++ b/src/renderer/jobs/PreviewResponseBody.tsx @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import { ResponseBody } from 'src/shared/generated_models'; -import { match } from 'ts-pattern'; +import { match, NonExhaustiveError } from 'ts-pattern'; import MarkdownView from '../components/response_body/text_views/MarkdownView'; import DirectoryView from '../components/response_body/directory_views/DirectoryView'; import BatchDirectoryView from '../components/response_body/directory_views/BatchDirectoryView'; @@ -14,29 +14,34 @@ export default function PreviewResponseBody({ }: { response: ResponseBody; }) { - return match(response) - .with({ output_type: 'file' }, (fileResponse) => { - return ; - }) - .with({ output_type: 'directory' }, (directoryResponse) => { - return ; - }) - .with({ output_type: 'markdown' }, (markdownResponse) => { - return ; - }) - .with({ output_type: 'text' }, (textResponse) => { - return ; - }) - .with({ output_type: 'batchfile' }, (batchFileResponse) => { - return ; - }) - .with({ output_type: 'batchtext' }, (batchTextResponse) => { - return ; - }) - .with({ output_type: 'batchdirectory' }, (batchDirectoryResponse) => { - return ; - }) - .otherwise(() => { + try { + return match(response) + .with({ output_type: 'file' }, (fileResponse) => { + return ; + }) + .with({ output_type: 'directory' }, (directoryResponse) => { + return ; + }) + .with({ output_type: 'markdown' }, (markdownResponse) => { + return ; + }) + .with({ output_type: 'text' }, (textResponse) => { + return ; + }) + .with({ output_type: 'batchfile' }, (batchFileResponse) => { + return ; + }) + .with({ output_type: 'batchtext' }, (batchTextResponse) => { + return ; + }) + .with({ output_type: 'batchdirectory' }, (batchDirectoryResponse) => { + return ; + }) + .exhaustive(); + } catch (e) { + if (e instanceof NonExhaustiveError) { return
Unknown Response
; - }); + } + throw e; + } } diff --git a/src/renderer/models/ModelRunTask.tsx b/src/renderer/models/ModelRunTask.tsx index efdc3e2..6633c46 100644 --- a/src/renderer/models/ModelRunTask.tsx +++ b/src/renderer/models/ModelRunTask.tsx @@ -39,6 +39,14 @@ export default function ModelRunTask() { return
Invalid Model UID or Task ID.
; } + if (taskSchemaIsLoading) { + return ; + } + + if (taskSchemaError) { + return
Error loading task schema
; + } + if (!thisApiRoute || !taskSchema) { return (
); } - if (taskSchemaIsLoading) { - return ; - } - if (taskSchemaError) { - return
Error loading task schema
; - } const onSubmit = async (data: any) => { const runJobArgs: RunJobArgs = { @@ -168,11 +170,6 @@ export default function ModelRunTask() { ))}
- //
- //

Not Runnable

- //
    - //
- //
)}