import { useState, useRef } from 'react' import type { CreatePromptTemplateRequest } from '@opencode-manager/shared/types' import type { PromptDialog } from '@/hooks/useScheduleUrlState' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Loader2, FileText, Plus, Upload } from 'lucide-react' import { usePromptTemplates, useDeletePromptTemplate } from '@/hooks/usePromptTemplates' import { parseMarkdownTemplate } from '@/lib/schedules/markdownTemplate' import { PromptTemplateDialog } from './PromptTemplateDialog' import { DeleteDialog } from '@/components/ui/delete-dialog' import { PromptTemplateCard } from './PromptTab' interface PromptsTabProps { promptDialog: PromptDialog templateId: number | null onNew: () => void onEdit: (id: number) => void onDelete: (id: number) => void onImport: () => void onCloseDialog: () => void } export function PromptsTab({ promptDialog, templateId, onNew, onEdit, onDelete, onImport, onCloseDialog }: PromptsTabProps) { const [importValues, setImportValues] = useState | undefined>() const fileInputRef = useRef(null) const { data: templates = [], isLoading } = usePromptTemplates() const deleteMutation = useDeletePromptTemplate() const editingTemplate = templates.find((t) => t.id === templateId) const dialogOpen = promptDialog === 'new' || promptDialog === 'edit' || promptDialog === 'import' const deleteDialogOpen = promptDialog === 'delete' const handleFileImport = (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (!file) return const reader = new FileReader() reader.onload = (e) => { const content = e.target?.result as string setImportValues(parseMarkdownTemplate(content, file.name)) onImport() } reader.readAsText(file) event.target.value = '' } const handleDialogOpenChange = (open: boolean) => { if (!open) { onCloseDialog() setImportValues(undefined) } } const handleDeleteConfirm = () => { if (templateId !== null) { deleteMutation.mutate(templateId, { onSuccess: onCloseDialog }) } } const handleDeleteDialogOpenChange = (open: boolean) => { if (!open && !deleteMutation.isPending) { onCloseDialog() } } const handleDeleteCancel = () => { if (!deleteMutation.isPending) { onCloseDialog() } } return (

Prompt templates

{isLoading ? (
) : templates.length === 0 ? (

No templates yet

Create prompt templates to reuse across your schedules.

) : (
{templates.map((template) => ( onEdit(item.id)} onDelete={onDelete} /> ))}
)}
) }