"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; 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 { savePageAction } from "@/lib/pages/actions"; import { Globe2, Loader2, Plus, Trash2, X } from "lucide-react"; export default function Editor({ page, type, initialBlocks = [] }: { page: Page | null; type: PageType; initialBlocks?: Block[] }) { const meta = (page?.meta as PageMeta) ?? {}; const router = useRouter(); const [isPending, startTransition] = useTransition(); const [title, setTitle] = useState(page?.title ?? ""); 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); function handleSave(status: "DRAFT" | "PUBLISHED") { startTransition(async () => { await savePageAction({ id: page?.id ?? null, slug: slug || title.toLowerCase().replace(/\s+/g, "-"), title, type: "NEWS", status, blocks, meta: { location, tags }, }); }); } 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 the page title" /> 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" /> Add {tags.length > 0 && (
{tags.map((tag) => ( removeTag(tag)} > {tag} ))}
)}

Content

{blocks.length === 0 && (

No blocks yet

)} {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 (