-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(dashboard): optimize execute
WfRun
components
- Loading branch information
1 parent
c4b2d52
commit bfc0462
Showing
9 changed files
with
144 additions
and
171 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
63 changes: 63 additions & 0 deletions
63
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/BaseFormField.tsx
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,63 @@ | ||
import { getVariableValue } from '@/app/utils' | ||
import { Label } from '@/components/ui/label' | ||
import { FormFieldProp } from '@/types' | ||
import { WfRunVariableAccessLevel } from 'littlehorse-client/proto' | ||
import { CircleAlert } from 'lucide-react' | ||
import { useFormContext } from 'react-hook-form' | ||
|
||
export const accessLevels: { [key in WfRunVariableAccessLevel]: string } = { | ||
PUBLIC_VAR: 'Public', | ||
INHERITED_VAR: 'Inherited', | ||
PRIVATE_VAR: 'Private', | ||
UNRECOGNIZED: '', | ||
} | ||
|
||
type FormFieldWrapperProps = { | ||
variables: FormFieldProp['variable'] | ||
children: React.ReactNode | ||
} | ||
|
||
export const BaseFormField = ({ variables, children }: FormFieldWrapperProps) => { | ||
const { | ||
formState: { errors }, | ||
} = useFormContext() | ||
|
||
if (!variables?.varDef?.name) return null | ||
|
||
const { | ||
varDef: { name, defaultValue }, | ||
required, | ||
accessLevel, | ||
} = variables | ||
|
||
const hasDefaultValue = !!defaultValue | ||
|
||
return ( | ||
<div> | ||
<div className="mb-2 flex justify-between"> | ||
<Label htmlFor={name} className="center flex items-center gap-2"> | ||
{name} | ||
<span className="rounded bg-green-300 p-1 text-xs">{accessLevels[accessLevel]}</span> | ||
{required ? ( | ||
<span className="rounded bg-red-300 p-1 text-xs">Required</span> | ||
) : ( | ||
<span className="rounded bg-gray-300 p-1 text-xs">Optional</span> | ||
)} | ||
{hasDefaultValue && ( | ||
<span className="rounded bg-blue-300 p-1 text-xs"> | ||
Default: {getVariableValue(defaultValue)?.toString()} | ||
</span> | ||
)} | ||
</Label> | ||
</div> | ||
|
||
<div className="w-full">{children}</div> | ||
{errors[name] && ( | ||
<p className="mt-2 flex items-center gap-1 text-sm text-destructive"> | ||
<CircleAlert size={16} /> | ||
{errors[name]?.message?.toString()} | ||
</p> | ||
)} | ||
</div> | ||
) | ||
} |
20 changes: 7 additions & 13 deletions
20
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/FormFields.tsx
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,16 +1,10 @@ | ||
import React, { FC } from 'react' | ||
import { FormFieldProp } from '@/types' | ||
import { FC } from 'react' | ||
import { FormComponent } from './formType' | ||
import { VariableType, ThreadVarDef } from 'littlehorse-client/proto' | ||
import { FieldValues, UseFormRegister, FormState } from 'react-hook-form' | ||
|
||
type Prop = { | ||
variables: ThreadVarDef | ||
register: UseFormRegister<FieldValues> | ||
formState: FormState<FieldValues> | ||
} | ||
export const FormFields: FC<Prop> = props => { | ||
const type = props.variables?.varDef?.type as VariableType | ||
if (!type) return | ||
const Component = FormComponent[type] | ||
return <Component {...props} /> | ||
export const FormFields: FC<FormFieldProp> = ({ variable: variables, register, formState }) => { | ||
if (!variables?.varDef?.type) return null | ||
|
||
const Component = FormComponent[variables.varDef.type] | ||
return <Component variable={variables} register={register} formState={formState} /> | ||
} |
72 changes: 19 additions & 53 deletions
72
dashboard/src/app/(authenticated)/(diagram)/components/Forms/components/FormInput.tsx
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,77 +1,43 @@ | ||
import { VARIABLE_TYPES } from '@/app/constants' | ||
import { Input } from '@/components/ui/input' | ||
import { Label } from '@/components/ui/label' | ||
import { FormFieldProp } from '@/types' | ||
import { VariableType } from 'littlehorse-client/proto' | ||
import { CircleAlert } from 'lucide-react' | ||
import { FC, useState } from 'react' | ||
import { accessLevels } from '../../../wfSpec/[...props]/components/Variables' | ||
import { FC } from 'react' | ||
import { useFormContext } from 'react-hook-form' | ||
import { BaseFormField } from './BaseFormField' | ||
|
||
export const FormInput: FC<FormFieldProp> = props => { | ||
const [isDisabled, setIsDisabled] = useState(false) | ||
if (!props.variables?.varDef?.name) return null | ||
const { register } = useFormContext() | ||
|
||
if (!props.variable?.varDef?.name) return null | ||
|
||
const { | ||
variables: { | ||
variable: { | ||
varDef: { type, name }, | ||
required, | ||
accessLevel, | ||
}, | ||
register, | ||
formState: { errors }, | ||
} = props | ||
|
||
const variableToType = (variable: VariableType) => { | ||
switch (variable) { | ||
case VariableType.INT: | ||
return 'number' | ||
case VariableType.DOUBLE: | ||
return 'number' | ||
case VariableType.BYTES: | ||
return 'number' | ||
case VariableType.STR: | ||
return 'text' | ||
default: | ||
return 'text' | ||
} | ||
} | ||
|
||
const setValue = (value: number | string) => { | ||
if (value === null) return value | ||
return variableToType(type) === 'number' ? parseFloat(value?.toString()) || undefined : value || undefined | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className="mb-2 flex justify-between"> | ||
<Label htmlFor={name} className="flex items-center gap-2"> | ||
{name} | ||
<span className="rounded bg-green-300 p-1 text-xs">{accessLevels[accessLevel]}</span> | ||
{required ? ( | ||
<span className="rounded bg-red-300 p-1 text-xs">Required</span> | ||
) : ( | ||
<span className="rounded bg-gray-300 p-1 text-xs">Optional</span> | ||
)} | ||
</Label> | ||
</div> | ||
<BaseFormField variables={props.variable}> | ||
<Input | ||
type={variableToType(type)} | ||
className={errors[name] ? 'mb-1 mt-2 border-red-700' : 'mb-4 mt-1'} | ||
type={type === VariableType.DOUBLE || type === VariableType.INT ? 'number' : 'text'} | ||
id={name} | ||
step={type === VariableType.DOUBLE ? '0.01' : undefined} | ||
disabled={isDisabled} | ||
placeholder={`Enter ${VARIABLE_TYPES[type]?.toLowerCase()} value`} | ||
{...register(name, { | ||
required: required ? `${name} is required` : false, | ||
setValueAs: setValue, | ||
setValueAs: value => { | ||
if (value === '') return undefined | ||
|
||
if ((type === VariableType.DOUBLE || type === VariableType.INT) && value) { | ||
return parseFloat(value) | ||
} | ||
|
||
return value | ||
}, | ||
})} | ||
/> | ||
{errors[name] && ( | ||
<p className="mb-3 flex items-center gap-1 text-sm text-red-700"> | ||
<CircleAlert size={16} /> | ||
{errors[name]?.message} | ||
</p> | ||
)} | ||
</div> | ||
</BaseFormField> | ||
) | ||
} |
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
Oops, something went wrong.