import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Textarea } from "@/components/ui/textarea"; import { Switch } from "@/components/ui/switch"; import { configModelRef, formatOpenCodeModelRef } from "@opencode-manager/shared/opencode"; import type { OpenCodeCommandConfig } from "@opencode-manager/shared/opencode"; import type { OpenCodeCommandPatch } from "@/api/types/settings"; const commandFormSchema = z.object({ name: z .string() .min(1, "Command name is required") .regex( /^[a-z0-9-]+$/, "Must be lowercase letters, numbers, and hyphens only", ), template: z.string().min(1, "Template is required"), description: z.string().optional(), agent: z.string().optional(), model: z.string().optional(), subagent: z.boolean(), }); type CommandFormValues = z.infer; interface CommandDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onSubmit: (name: string, command: OpenCodeCommandPatch) => void; editingCommand?: { name: string; command: OpenCodeCommandConfig } | null; } /** * Renders the create/edit command dialog with name, template, description, * agent, model, and subagent fields, locking the name while editing an existing * command. */ export function CommandDialog({ open, onOpenChange, onSubmit, editingCommand, }: CommandDialogProps) { /** * Builds the form defaults for creating or editing a command, rendering any stored model * reference in the `provider/model` display format and falling back to empty values when there is * no command to edit. */ const getDefaultValues = ( command?: { name: string; command: OpenCodeCommandConfig } | null, ): CommandFormValues => { const modelRef = configModelRef(command?.command.model); return { name: command?.name || "", template: command?.command.template || "", description: command?.command.description || "", agent: command?.command.agent || "", model: modelRef ? formatOpenCodeModelRef(modelRef) : "", subagent: command?.command.subagent || false, }; }; const form = useForm({ resolver: zodResolver(commandFormSchema), defaultValues: getDefaultValues(editingCommand), }); useEffect(() => { if (open) { form.reset(getDefaultValues(editingCommand)); } }, [open, editingCommand, form]); /** * Submits the validated command form as a patch that keeps the stored * command's other fields, nulls emptied optional fields, and then resets and * closes the dialog. */ const handleSubmit = (values: CommandFormValues) => { const command: OpenCodeCommandPatch = { ...editingCommand?.command, template: values.template, description: values.description || null, agent: values.agent || null, model: values.model?.trim() || null, subagent: values.subagent ? true : null, }; onSubmit(values.name, command); form.reset(); onOpenChange(false); }; const handleOpenChange = (isOpen: boolean) => { if (!isOpen) { form.reset(); } onOpenChange(isOpen); }; return ( {editingCommand ? "Edit Command" : "Create Command"}
( Command Name Use lowercase letters, numbers, and hyphens only )} /> ( Description