Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 61 additions & 61 deletions src/renderer/components/InputField.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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' }, () => (
<TextField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'textarea' }, () => (
<TextAreaField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'file' }, () => (
<FileField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'directory' }, () => (
<DirectoryField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'batchtext' }, () => (
<BatchTextField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'batchfile' }, () => (
<BatchFileField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'batchdirectory' }, () => (
<BatchDirectoryField
value={value}
onChange={onChange}
disabled={disabled}
/>
))
.with(
{
inputType: {
inputType: 'newfile',
},
},
() => (
<NewFileField
inputSchema={inputSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
),
)
.exhaustive();
} catch (e) {
if (e instanceof NonExhaustiveError) {
log.error('Received an unexpected input schema.', inputSchema);
return <div>Unsupported input type.</div>;
}
throw e;
}
}

export default function InputField({
inputSchema,
value,
Expand All @@ -31,66 +90,7 @@ export default function InputField({
{inputSchema.subtitle && (
<p className="text-xs mt-1">{inputSchema.subtitle}</p>
)}
{match(inputSchema)
.with({ inputType: 'text' }, () => (
<TextField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'textarea' }, () => (
<TextAreaField
value={value}
onChange={onChange}
disabled={disabled}
/>
))
.with({ inputType: 'file' }, () => (
<FileField value={value} onChange={onChange} disabled={disabled} />
))
.with({ inputType: 'directory' }, () => (
<DirectoryField
value={value}
onChange={onChange}
disabled={disabled}
/>
))
.with({ inputType: 'batchtext' }, () => (
<BatchTextField
value={value}
onChange={onChange}
disabled={disabled}
/>
))
.with({ inputType: 'batchfile' }, () => (
<BatchFileField
value={value}
onChange={onChange}
disabled={disabled}
/>
))
.with({ inputType: 'batchdirectory' }, () => (
<BatchDirectoryField
value={value}
onChange={onChange}
disabled={disabled}
/>
))
.with(
{
inputType: {
inputType: 'newfile',
},
},
() => (
<NewFileField
inputSchema={inputSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
),
)
.otherwise(() => (
<div>Unsupported input type.</div>
))}
{getFieldByInputSchema(inputSchema, value, onChange, disabled)}
</div>
);
}
156 changes: 106 additions & 50 deletions src/renderer/components/ParameterField.tsx
Original file line number Diff line number Diff line change
@@ -1,99 +1,155 @@
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';
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 (
<div>
<h2 className="font-semibold text-sm xl:text-base mb-2">
{parameterSchema.label}
</h2>
{parameterSchema.subtitle && (
<p className="text-xs mt-1">{parameterSchema.subtitle}</p>
)}
{match(paramValue)
.with({ parameterType: 'text' }, () => {
return (
<h2 className="font-semibold text-sm xl:text-base mb-2">{label}</h2>
{subtitle && <p className="text-xs mt-1">{subtitle}</p>}
{children}
</div>
);
}

function getFieldByParameterSchema(
parameterSchema: ParameterSchema,
value: any,
onChange: (value: any) => void,
disabled: boolean,
) {
try {
return match(parameterSchema.value)
.with({ parameterType: 'text' }, () => {
return (
<LabelAndSubtitle
label={parameterSchema.label}
subtitle={parameterSchema.subtitle}
>
<TextField
parameterSchema={parameterSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
);
})
.with({ parameterType: 'int' }, () => {
return (
</LabelAndSubtitle>
);
})
.with({ parameterType: 'int' }, () => {
return (
<LabelAndSubtitle
label={parameterSchema.label}
subtitle={parameterSchema.subtitle}
>
<IntField
parameterSchema={parameterSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
);
})
.with({ parameterType: 'float' }, () => {
return (
</LabelAndSubtitle>
);
})
.with({ parameterType: 'float' }, () => {
return (
<LabelAndSubtitle
label={parameterSchema.label}
subtitle={parameterSchema.subtitle}
>
<FloatField
parameterSchema={parameterSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
);
})
.with({ parameterType: 'enum' }, () => {
return (
</LabelAndSubtitle>
);
})
.with({ parameterType: 'enum' }, () => {
return (
<LabelAndSubtitle
label={parameterSchema.label}
subtitle={parameterSchema.subtitle}
>
<EnumField
parameterSchema={parameterSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
);
})
.with({ parameterType: 'ranged_int' }, () => {
return (
</LabelAndSubtitle>
);
})
.with({ parameterType: 'ranged_int' }, () => {
return (
<LabelAndSubtitle
label={parameterSchema.label}
subtitle={parameterSchema.subtitle}
>
<RangedIntField
parameterSchema={parameterSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
);
})
.with({ parameterType: 'ranged_float' }, () => {
return (
</LabelAndSubtitle>
);
})
.with({ parameterType: 'ranged_float' }, () => {
return (
<LabelAndSubtitle
label={parameterSchema.label}
subtitle={parameterSchema.subtitle}
>
<RangedFloatField
parameterSchema={parameterSchema}
value={value}
onChange={onChange}
disabled={disabled}
/>
);
})
.otherwise(() => {
return (
<div>Unsupported parameter type: {paramValue.parameterType}</div>
);
})}
</div>
);
</LabelAndSubtitle>
);
})
.exhaustive();
} catch (e) {
if (e instanceof NonExhaustiveError) {
log.error('Received an unexpected parameter schema.', parameterSchema);
return (
<div>
Unsupported parameter type: {parameterSchema.value.parameterType}
</div>
);
}
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);
}
Loading
Loading