diff --git a/app/components/assets/form/model-source-field.tsx b/app/components/assets/form/model-source-field.tsx new file mode 100644 index 0000000..a7a69ee --- /dev/null +++ b/app/components/assets/form/model-source-field.tsx @@ -0,0 +1,64 @@ +import { useFormContext, useWatch } from 'react-hook-form'; +import { Link2 } from 'lucide-react'; +import { Field } from '~/components/ui/field'; +import { IconInput } from '~/components/ui/icon-input'; +import { SegmentedControl } from '~/components/ui/segmented-control'; +import { FilePicker } from '~/components/assets/form/file-picker'; +import { MODEL_FIELDS, MODEL_SOURCE_OPTIONS, type ModelFieldName } from '~/data/assets/constants'; +import type { AssetFormValues } from '~/schemas/assets/asset-form'; + +export function ModelSourceField({ name, required }: { name: ModelFieldName; required?: boolean }) { + const { control, register, setValue, trigger, formState } = useFormContext(); + const source = useWatch({ control, name }); + const config = MODEL_FIELDS[name]; + const fieldErrors = formState.errors[name]; + const error = fieldErrors?.file?.message ?? fieldErrors?.url?.message; + const inputId = `${name}-source`; + + // Re-validates the whole source once the form has been submitted, so errors for the other mode clear + const update = (patch: Partial) => { + setValue(name, { ...source, ...patch }); + if (formState.isSubmitted) trigger(name); + }; + + return ( + update({ mode })} + /> + } + > + {source.mode === 'file' ? ( + update({ file })} + onClearExisting={() => update({ existing: '' })} + /> + ) : ( + + )} + + ); +}