"use client"; import { useState, useEffect } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Field, 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 { Globe2, Plus, Trash2, X } from "lucide-react"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; 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; }; export default function Editor({ page, initialBlocks = [], onChange, }: 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); 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(type: BlockType) { setBlocks([ ...blocks, { id: crypto.randomUUID(), pageId: page?.id ?? "", parentId: null, type, position: blocks.length, content: getDefaultContent(type), }, ]); } 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))); } return (

Meta

Title { setTitle(e.target.value); if (!page) { setSlug( e.target.value.toLowerCase().replace(/\s+/g, "-") ); } }} placeholder="Enter page title" /> Page type Slug setSlug(e.target.value)} placeholder="page-slug" /> Location setLocation(e.target.value)} placeholder="Liberty City, European Federation" /> Tags setTagInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && addTag()} placeholder="Add a tag and press Enter" /> Add {tags.length > 0 && (
{tags.map((tag) => ( removeTag(tag)} > {tag} ))}
)}

Content

{blocks.length === 0 && (

No blocks yet. Add one below.

)} {blocks.map((block) => ( updateBlock(block.id, content)} onRemove={() => removeBlock(block.id)} /> ))}
{( [ "PARAGRAPH", "HEADING", "IMAGE", "QUOTE", "CALLOUT", "DIVIDER", "LIST", "TABLE", ] as BlockType[] ).map((t) => ( ))}
); } function BlockEditor({ block, onChange, onRemove }: { block: Block; onChange: (content: Block["content"]) => void; onRemove: () => void }) { return (
); } function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["content"]) => void }) { switch (block.type) { case "PARAGRAPH": return (