'use client' import { useState } from 'react' import { createKbEntry, updateKbEntry } from '@/lib/actions' import { toast } from 'sonner' import type { Enums } from '@/types/database' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import type { KbEntry } from '../types' // ─── KB Entry Modal ──────────────────────────────────────────────────────────── export function KbModal({ entry, onClose, onSaved, }: { entry: KbEntry | null onClose: () => void onSaved: (entry: KbEntry) => void }) { const isEdit = !!entry const [title, setTitle] = useState(entry?.title ?? '') const [content, setContent] = useState(entry?.content ?? '') const [area, setArea] = useState(entry?.area ?? '') const [visibility, setVisibility] = useState(entry?.visibility ?? 'all_members') const [isPinned, setIsPinned] = useState(entry?.is_pinned ?? false) const [isProcess, setIsProcess] = useState(entry?.tags?.includes('process') ?? false) const [saving, setSaving] = useState(false) async function handleSave(e: React.FormEvent) { e.preventDefault() if (!title.trim() || !content.trim()) return setSaving(true) const tags = isProcess ? ['process'] : [] let result if (isEdit) { result = await updateKbEntry(entry.id, { title, content, area, visibility, is_pinned: isPinned, tags }) } else { result = await createKbEntry({ title, content, area, visibility, is_pinned: isPinned, tags }) } setSaving(false) if ('error' in result && result.error) { toast.error(result.error) return } toast.success(isEdit ? 'Entry updated' : 'Entry created') if (!isEdit && 'data' in result && result.data) onSaved(result.data as KbEntry) else if (isEdit) onSaved({ ...entry, title, content, area, visibility, is_pinned: isPinned, tags }) onClose() } return ( { if (!o) onClose() }}> {isEdit ? 'Edit Entry' : 'New KB Entry'}
setTitle(e.target.value)} className="w-full bg-background border border-border rounded px-3 py-2 font-sans text-sm text-foreground focus:outline-none focus:border-primary transition" placeholder="e.g. How to open the space" required />