import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' import { useEffect, useState } 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { getRepoDisplayName } from '@/lib/utils' import type { SkillFileInfo, CreateSkillRequest, UpdateSkillRequest, SkillScope } from '@opencode-manager/shared' import type { Repo } from '@/api/types' import { listRepos } from '@/api/repos' import { useQuery } from '@tanstack/react-query' const skillFormSchema = z.object({ name: z.string() .min(1, 'Skill name is required') .max(64, 'Skill name must be 64 characters or less') .regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, 'Must be lowercase letters, numbers, and hyphens only'), description: z.string().min(1, 'Description is required').max(1024, 'Description must be 1024 characters or less'), body: z.string().min(1, 'Skill body is required'), scope: z.enum(['global', 'project']), }) type SkillFormValues = z.infer interface SkillDialogProps { open: boolean onOpenChange: (open: boolean) => void onSubmit: (data: CreateSkillRequest | (UpdateSkillRequest & { name: string; scope: SkillScope; repoId?: number })) => void editingSkill?: SkillFileInfo | null } export function SkillDialog({ open, onOpenChange, onSubmit, editingSkill }: SkillDialogProps) { const { data: repos = [] } = useQuery({ queryKey: ['repos'], queryFn: listRepos, enabled: open, staleTime: 5 * 60 * 1000, }) const [selectedRepoId, setSelectedRepoId] = useState(undefined) const getDefaultValues = (skill?: SkillFileInfo | null): SkillFormValues => { return { name: skill?.name || '', description: skill?.description || '', body: skill?.body || '', scope: skill?.scope || 'global', } } const form = useForm({ resolver: zodResolver(skillFormSchema), defaultValues: getDefaultValues(editingSkill) }) useEffect(() => { if (open) { form.reset(getDefaultValues(editingSkill)) if (editingSkill?.repoId) { setSelectedRepoId(editingSkill.repoId) } } }, [open, editingSkill, form]) const handleSubmit = (values: SkillFormValues) => { if (!editingSkill && values.scope === 'project' && !selectedRepoId) { form.setError('scope', { message: 'Please select a repository for project-scoped skills' }) return } if (editingSkill) { onSubmit({ name: editingSkill.name, scope: editingSkill.scope, repoId: editingSkill.scope === 'project' ? editingSkill.repoId : undefined, description: values.description, body: values.body, }) } else { onSubmit({ name: values.name, description: values.description, body: values.body, scope: values.scope, repoId: values.scope === 'project' ? selectedRepoId : undefined, }) } form.reset() onOpenChange(false) } const handleOpenChange = (isOpen: boolean) => { if (!isOpen) { form.reset() } onOpenChange(isOpen) } const scope = form.watch('scope') return ( {editingSkill ? 'Edit Skill' : 'Create Skill'}
( Skill Name Use lowercase letters, numbers, and hyphens only )} /> ( Description