-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from UMass-Rescue/akale-add-better-compiler-e…
…rrors Add better compiler errors and fix loading screen
- Loading branch information
Showing
5 changed files
with
206 additions
and
148 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
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); | ||
} |
Oops, something went wrong.