'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { useMutation } from 'convex/react'; import { toast } from 'sonner'; import { api } from '@spoon/backend/convex/_generated/api.js'; import { Button, Input, Label, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Textarea, } from '@spoon/ui'; const options = { provider: ['github', 'gitea', 'gitlab', 'other'], visibility: ['unknown', 'public', 'private', 'internal'], maintenanceMode: ['watch', 'auto_pr', 'paused'], syncCadence: ['daily', 'weekly', 'manual'], productionRefStrategy: ['default_branch', 'latest_release', 'tag_pattern'], } as const; type FormState = { name: string; description: string; provider: (typeof options.provider)[number]; upstreamOwner: string; upstreamRepo: string; upstreamDefaultBranch: string; upstreamUrl: string; forkOwner: string; forkRepo: string; forkUrl: string; visibility: (typeof options.visibility)[number]; maintenanceMode: (typeof options.maintenanceMode)[number]; syncCadence: (typeof options.syncCadence)[number]; productionRefStrategy: (typeof options.productionRefStrategy)[number]; }; const initialState: FormState = { name: '', description: '', provider: 'github', upstreamOwner: '', upstreamRepo: '', upstreamDefaultBranch: 'main', upstreamUrl: '', forkOwner: '', forkRepo: '', forkUrl: '', visibility: 'unknown', maintenanceMode: 'watch', syncCadence: 'daily', productionRefStrategy: 'default_branch', }; const TextField = ({ id, label, value, onChange, required, }: { id: keyof FormState; label: string; value: string; onChange: (value: string) => void; required?: boolean; }) => (
onChange(event.target.value)} />
); export const NewSpoonForm = () => { const router = useRouter(); const createManual = useMutation(api.spoons.createManual); const [form, setForm] = useState(initialState); const [submitting, setSubmitting] = useState(false); const update = (key: K, value: FormState[K]) => { setForm((current) => ({ ...current, [key]: value })); }; const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setSubmitting(true); try { await createManual({ ...form, description: form.description || undefined, forkOwner: form.forkOwner || undefined, forkRepo: form.forkRepo || undefined, forkUrl: form.forkUrl || undefined, }); toast.success('Spoon created.'); router.push('/spoons'); } catch (error) { console.error(error); toast.error('Could not create Spoon.'); } finally { setSubmitting(false); } }; return (
update('name', value)} />