import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { createProject } from '../../../../common/api/db'; import { maybeAxiosError } from '../../../../common/api/utils'; import Button from '../../../../common/components/buttons/Button'; import Input from '../../../../common/components/input/input/Input'; import { preventEscape } from '../../../../common/utils/keyEvent'; import * as Panel from '../../panel-utils/PanelUtils'; import style from './ProjectPanel.module.scss'; interface ProjectCreateFromProps { onClose: () => void; } type ProjectCreateFormValues = { title?: string; description?: string; info?: string; url?: string; custom?: { title: string; value: string }[]; }; export default function ProjectCreateForm({ onClose }: ProjectCreateFromProps) { const [error, setError] = useState(null); const { handleSubmit, register, formState: { isSubmitting, isValid }, setFocus, } = useForm({ defaultValues: { title: '' }, values: { title: '' }, resetOptions: { keepDirtyValues: true, }, }); // set focus to first field useEffect(() => { setFocus('title'); }, [setFocus]); const handleSubmitCreate = async (values: ProjectCreateFormValues) => { try { setError(null); const filename = values.title ?? 'untitled'; await createProject({ filename }); onClose(); } catch (error) { setError(maybeAxiosError(error)); } }; return ( preventEscape(event, onClose)} > Create new project {error && {error}} Project title ); }