import { useMutation } from 'convex/react' import { useForm } from 'react-hook-form' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/checkbox' import { Input } from '@/components/ui/input' import { api } from '../../convex/_generated/api' import type { Doc } from '../../convex/_generated/dataModel' import modulesData from '../../convex/modules.json' import { VERSIONS } from '../constants/versions' import { ModuleToggle } from './ModuleToggle' // Form values use flattened config for UI, but will be transformed to nested on submit type ProfileFormValues = Omit< Doc<'profiles'>, '_id' | '_creationTime' | 'userId' | 'flashCount' | 'updatedAt' > interface ProfileEditorProps { initialData?: Doc<'profiles'> onSave: () => void onCancel: () => void } export default function ProfileEditor({ initialData, onSave, onCancel, }: ProfileEditorProps) { const upsertProfile = useMutation(api.profiles.upsert) const { register, handleSubmit, setValue, watch, formState: { errors }, } = useForm({ defaultValues: { name: initialData?.name || '', description: initialData?.description || '', config: { version: VERSIONS[0], modulesExcluded: {}, target: '', ...initialData?.config, }, isPublic: initialData?.isPublic ?? true, }, }) const onSubmit = async (data: ProfileFormValues) => { await upsertProfile({ id: initialData?._id, name: data.name, description: data.description, config: data.config, isPublic: data.isPublic, }) onSave() } return (
{errors.name && (

{errors.name.message}

)}