'use client' import { useEffect, useState } from 'react' import { X, CheckSquare, BookOpen, GraduationCap, LayoutList } from 'lucide-react' import { Button } from '@/components/ui/button' import { useLanguage } from '@/lib/i18n' import { toast } from 'sonner' import type { NotesLayoutMode } from '@/components/notes-list-views' import { WIZARD_DEFAULT_VIEW, WIZARD_FIELDS_BY_GOAL, WIZARD_GOALS, type WizardFieldDef, type WizardFieldId, type WizardGoal, } from '@/lib/structured-views/wizard-templates' import { cn } from '@/lib/utils' type StructuredViewsWizardProps = { open: boolean onClose: () => void onComplete: (view: NotesLayoutMode) => void structuredModeActive: boolean enableStructuredMode: () => Promise addProperty: (name: string, type: string, options?: string[]) => Promise<{ properties: { id: string; name: string; type: string }[] } | null | undefined> setKanbanGroupProperty: (propertyId: string | null) => Promise initialGoal?: WizardGoal } const GOAL_ICONS: Record = { tasks: CheckSquare, learning: GraduationCap, reading: BookOpen, simple: LayoutList, } const VIEW_LABEL_KEYS: Record<'list' | 'gallery' | 'table' | 'kanban', string> = { list: 'structuredViews.viewList', gallery: 'structuredViews.viewGallery', table: 'structuredViews.viewTable', kanban: 'structuredViews.viewKanban', } export function StructuredViewsWizard({ open, onClose, onComplete, structuredModeActive, enableStructuredMode, addProperty, setKanbanGroupProperty, initialGoal, }: StructuredViewsWizardProps) { const { t } = useLanguage() const [step, setStep] = useState(0) const [goal, setGoal] = useState('tasks') const [selectedFields, setSelectedFields] = useState>(new Set()) const [view, setView] = useState('list') const [saving, setSaving] = useState(false) const fieldDefs = WIZARD_FIELDS_BY_GOAL[goal] useEffect(() => { if (!open) return const g = initialGoal ?? 'tasks' setStep(0) setGoal(g) setSelectedFields(new Set(WIZARD_FIELDS_BY_GOAL[g].map((f) => f.id))) setView(WIZARD_DEFAULT_VIEW[g]) }, [open, initialGoal]) useEffect(() => { setSelectedFields(new Set(fieldDefs.map((f) => f.id))) setView(WIZARD_DEFAULT_VIEW[goal]) }, [goal, fieldDefs]) const kanbanAllowed = fieldDefs.some((f) => f.type === 'select' && selectedFields.has(f.id)) if (!open) return null const toggleField = (id: WizardFieldId) => { setSelectedFields((prev) => { const next = new Set(prev) if (next.has(id)) next.delete(id) else next.add(id) return next }) } const fieldLabel = (def: WizardFieldDef) => t(`structuredViews.wizard.fields.${def.id}.name`) const parseOptions = (fieldId: WizardFieldId) => t(`structuredViews.wizard.fields.${fieldId}.options`) .split('\n') .map((l) => l.trim()) .filter(Boolean) const handleFinish = async () => { setSaving(true) try { if (!structuredModeActive) await enableStructuredMode() let kanbanPropertyId: string | null = null for (const def of fieldDefs) { if (!selectedFields.has(def.id)) continue const name = fieldLabel(def) const options = def.hasOptions ? parseOptions(def.id) : [] const schema = await addProperty(name, def.type, options) const created = schema?.properties.find((p) => p.name === name) if (created && def.type === 'select' && !kanbanPropertyId) { kanbanPropertyId = created.id } } const finalView = view === 'kanban' && !kanbanAllowed ? 'list' : view if (finalView === 'kanban' && kanbanPropertyId) { await setKanbanGroupProperty(kanbanPropertyId) } onComplete(finalView) onClose() } catch { toast.error(t('structuredViews.enableFailed')) } finally { setSaving(false) } } const goNext = () => { if (step === 0 && goal === 'simple') { setStep(2) return } setStep((s) => Math.min(s + 1, 2)) } return (

{t('structuredViews.wizard.title')}

{t('structuredViews.wizard.subtitle')}

{step === 0 && (

{t('structuredViews.wizard.stepGoal')}

{WIZARD_GOALS.map((g) => { const Icon = GOAL_ICONS[g] return ( ) })}
)} {step === 1 && fieldDefs.length > 0 && (

{t('structuredViews.wizard.stepFields')}

{t('structuredViews.wizard.fieldsHint')}

{fieldDefs.map((def) => ( ))}
)} {step === 2 && (

{t('structuredViews.wizard.stepView')}

{(['list', 'gallery', 'table', 'kanban'] as const).map((v) => { const disabled = v === 'kanban' && !kanbanAllowed return ( ) })}
{view === 'kanban' && !kanbanAllowed && (

{t('structuredViews.wizard.kanbanNeedsStatus')}

)}

{t('structuredViews.wizard.doneHint')}

)}
{step < 2 ? ( ) : ( )}
) }