"use client"; import { useState, useEffect, useRef } from "react"; import { v4 as uuidv4 } from "uuid"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field"; import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group"; import { Block, BlockType, Page, PageMeta, PageType } from "@/lib/pages/types"; import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, GripVerticalIcon, ChevronDownIcon, ImageIcon, QuoteIcon, AlertCircleIcon, MinusIcon, ListIcon, TableIcon, TypeIcon, AlignLeftIcon, InfoIcon, DotIcon, ListOrderedIcon, LinkIcon } from "lucide-react"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { cn } from "@/lib/utils"; import { Input } from "./ui/input"; import { Separator } from "./ui/separator"; import { Alert } from "./ui/alert"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; export type EditorData = { title: string; type: PageType; slug: string; location: string; tags: string[]; blocks: Block[]; }; type EditorProps = { page?: Page | null; type?: PageType; initialBlocks?: Block[]; onChange: (data: EditorData) => void; errors?: { title?: string[]; slug?: string[] }; }; const BLOCK_ICONS: Record = { PARAGRAPH: , HEADING: , IMAGE: , QUOTE: , CALLOUT: , DIVIDER: , LIST: , TABLE: , LINK: }; const BLOCK_LABELS: Record = { PARAGRAPH: "Text", HEADING: "Heading", IMAGE: "Image", QUOTE: "Quote", CALLOUT: "Callout", DIVIDER: "Divider", LIST: "List", TABLE: "Table", LINK: "Link" }; export default function Editor({ page, initialBlocks = [], onChange, errors }: EditorProps) { const meta = (page?.meta as PageMeta) ?? {}; const [title, setTitle] = useState(page?.title ?? ""); const [type, setType] = useState(page?.type ?? "NEWS"); const [slug, setSlug] = useState(page?.slug ?? ""); const [location, setLocation] = useState(meta.location ?? ""); const [tags, setTags] = useState(meta.tags ?? []); const [tagInput, setTagInput] = useState(""); const [blocks, setBlocks] = useState(initialBlocks); const [dragIndex, setDragIndex] = useState(null); const [dropIndex, setDropIndex] = useState(null); useEffect(() => { onChange({ title, type, slug: slug || title.toLowerCase().replace(/\s+/g, "-"), location, tags, blocks, }); }, [title, slug, location, tags, blocks]); function addTag() { const trimmed = tagInput.trim(); if (!trimmed || tags.includes(trimmed)) return; setTags([...tags, trimmed]); setTagInput(""); } function removeTag(tag: string) { setTags(tags.filter((t) => t !== tag)); } function addBlock(blockType: BlockType) { setBlocks([ ...blocks, { id: uuidv4(), pageId: page?.id ?? "", parentId: null, type: blockType, position: blocks.length, content: getDefaultContent(blockType), }, ]); } function removeBlock(id: string) { setBlocks(blocks.filter((b) => b.id !== id)); } function updateBlock(id: string, content: Block["content"]) { setBlocks(blocks.map((b) => (b.id === id ? { ...b, content } : b))); } function moveBlock(from: number, to: number) { const updated = [...blocks]; const [moved] = updated.splice(from, 1); updated.splice(to, 0, moved); setBlocks(updated.map((b, i) => ({ ...b, position: i }))); } function handleDragStart(index: number) { setDragIndex(index); } function handleDragOver(e: React.DragEvent, index: number) { e.preventDefault(); setDropIndex(index); } function handleDrop(index: number) { if (dragIndex !== null && dragIndex !== index) moveBlock(dragIndex, index); setDragIndex(null); setDropIndex(null); } return (

Meta

Title { setTitle(e.target.value); if (!page) setSlug(e.target.value.toLowerCase().replace(/\s+/g, "-")); }} /> {errors?.title && ({ message: m }))} />} Page type Slug setSlug(e.target.value)} /> {errors?.slug && ({ message: m }))} />} {type === "NEWS" && ( Location setLocation(e.target.value)} /> )} {type === "NEWS" && ( Tags setTagInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); addTag(); } }} /> Add {tags.length > 0 && (
{tags.map((tag) => ( removeTag(tag)}> {tag} ))}
)}
)}

Content

{blocks.length === 0 && (

No blocks yet. Add one below.

)}
{blocks.map((block, index) => (
handleDragStart(index)} onDragOver={(e) => handleDragOver(e, index)} onDrop={() => handleDrop(index)} onDragEnd={() => { setDragIndex(null); setDropIndex(null); }} className={cn( "group relative flex flex-row gap-2 md:gap-4 items-start py-1 transition-opacity", dragIndex === index && "opacity-30", dropIndex === index && dragIndex !== index && "border-t-2 border-primary" )} >
updateBlock(block.id, content)} />
))}
{(["PARAGRAPH", "HEADING", "IMAGE", "QUOTE", "CALLOUT", "DIVIDER", "LIST", "TABLE", "LINK"] as BlockType[]).map((t) => ( addBlock(t)} className="gap-2">

{BLOCK_ICONS[t]}

{BLOCK_LABELS[t]}

))}
); } function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["content"]) => void }) { switch (block.type) { case "PARAGRAPH": return ( onChange({ type: "PARAGRAPH", text: e.target.value })} /> ); case "HEADING": const level = (block.content as any).level; return (
onChange({ type: "HEADING", text: e.target.value, level: (block.content as any).level ?? 2 })} />
); case "QUOTE": return (
onChange({ type: "QUOTE", text: e.target.value, author: (block.content as any).author })} /> onChange({ type: "QUOTE", text: (block.content as any).text, author: e.target.value })} />
); case "CALLOUT": return ( onChange({ type: "CALLOUT", title: e.target.value, text: (block.content as any).text ?? "" })} /> onChange({ type: "CALLOUT", title: (block.content as any).title ?? "", text: e.target.value })} /> ); case "IMAGE": return (
onChange({ type: "IMAGE", url: e.target.value })} /> {(block.content as any).url && (
{/* eslint-disable-next-line @next/next/no-img-element */} preview { (e.target as HTMLImageElement).style.display = "none"; }} />
)} onChange({ type: "IMAGE", url: (block.content as any).url ?? "", caption: e.target.value })} />
); case "DIVIDER": return ; case "LIST": return onChange(c)} />; case "TABLE": return onChange(c)} />; case "LINK": return (
onChange({ type: "LINK", label: e.target.value, href: (block.content as any).href ?? "", }) } /> onChange({ type: "LINK", label: (block.content as any).label ?? "", href: e.target.value, }) } />
); default: return

{block.type} — editor coming soon

; } } function ListEditor({ content, onChange }: { content: { type: "LIST"; style: "BULLET" | "ORDERED"; items: string[] }; onChange: (c: any) => void }) { const items = content.items ?? [""]; function updateItem(index: number, value: string) { const updated = [...items]; updated[index] = value; onChange({ ...content, items: updated }); } function addItem() { onChange({ ...content, items: [...items, ""] }); } function removeItem(index: number) { const updated = items.filter((_, i) => i !== index); onChange({ ...content, items: updated.length ? updated : [""] }); } function handleKeyDown(e: React.KeyboardEvent, index: number) { if (e.key === "Enter") { e.preventDefault(); const updated = [...items]; updated.splice(index + 1, 0, ""); onChange({ ...content, items: updated }); setTimeout(() => { const inputs = document.querySelectorAll(`[data-list-item]`); (inputs[index + 1] as HTMLInputElement)?.focus(); }, 0); } if (e.key === "Backspace" && items[index] === "" && items.length > 1) { e.preventDefault(); removeItem(index); } } return (
{items.map((item, index) => (
{content.style === "ORDERED" ? `${index + 1}.` : } updateItem(index, e.target.value)} onKeyDown={(e) => handleKeyDown(e, index)} /> {items.length > 1 && ( )}
))}
); } function TableEditor({ content, onChange }: { content: { type: "TABLE"; headers: string[]; rows: string[][] }; onChange: (c: any) => void }) { const headers = content.headers ?? ["Column 1"]; const rows = content.rows ?? []; function updateHeader(index: number, value: string) { const updated = [...headers]; updated[index] = value; onChange({ ...content, headers: updated }); } function addColumn() { onChange({ ...content, headers: headers.concat(""), rows: rows.map((row) => [...row, ""]) }); } function removeColumn(colIndex: number) { if (headers.length <= 1) return; onChange({ ...content, headers: headers.filter((_, i) => i !== colIndex), rows: rows.map((row) => row.filter((_, i) => i !== colIndex)), }); } function addRow() { onChange({ ...content, rows: [...rows, headers.map(() => "")] }); } function removeRow(rowIndex: number) { onChange({ ...content, rows: rows.filter((_, i) => i !== rowIndex) }); } function updateCell(rowIndex: number, colIndex: number, value: string) { onChange({ ...content, rows: rows.map((row, ri) => ri === rowIndex ? row.map((cell, ci) => (ci === colIndex ? value : cell)) : row), }); } return (
{headers.map((header, ci) => (
updateHeader(ci, e.target.value)} /> {headers.length > 1 && ( )}
))}
{rows.map((row, ri) => ( {row.map((cell, ci) => ( updateCell(ri, ci, e.target.value)} /> ))} ))}
); } function AutoResizeTextarea({ value, onChange, className, placeholder }: { value: string; onChange: (e: React.ChangeEvent) => void; className?: string; placeholder?: string }) { const ref = useRef(null); useEffect(() => { if (ref.current) { ref.current.style.height = "auto"; ref.current.style.height = `${ref.current.scrollHeight}px`; } }, [value]); return