280 lines
9.6 KiB
TypeScript
280 lines
9.6 KiB
TypeScript
"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<string[]>(meta.tags ?? []);
|
|
const [tagInput, setTagInput] = useState("");
|
|
const [blocks, setBlocks] = useState<Block[]>(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 (
|
|
<div className="flex flex-col gap-8">
|
|
<FieldSet>
|
|
<h2 className="underline underline-offset-8 decoration-2 mb-4">Meta</h2>
|
|
<FieldGroup>
|
|
<Field>
|
|
<FieldLabel htmlFor="title">Title</FieldLabel>
|
|
<InputGroup>
|
|
<InputGroupInput
|
|
id="title"
|
|
value={title}
|
|
onChange={(e) => {
|
|
setTitle(e.target.value);
|
|
if (!page) {
|
|
setSlug(e.target.value.toLowerCase().replace(/\s+/g, "-"));
|
|
}
|
|
}}
|
|
placeholder="Enter the page title"
|
|
/>
|
|
</InputGroup>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="slug">Slug</FieldLabel>
|
|
<InputGroup>
|
|
<InputGroupInput
|
|
id="slug"
|
|
value={slug}
|
|
onChange={(e) => setSlug(e.target.value)}
|
|
placeholder="page-slug"
|
|
/>
|
|
</InputGroup>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="location">Location</FieldLabel>
|
|
<InputGroup>
|
|
<InputGroupInput
|
|
id="location"
|
|
value={location}
|
|
onChange={(e) => setLocation(e.target.value)}
|
|
placeholder="Liberty City, European Federation"
|
|
/>
|
|
<InputGroupAddon align="inline-start">
|
|
<Globe2 size={16} className="text-muted-foreground" />
|
|
</InputGroupAddon>
|
|
</InputGroup>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="tags">Tags</FieldLabel>
|
|
<InputGroup>
|
|
<InputGroupInput
|
|
id="tags"
|
|
value={tagInput}
|
|
onChange={(e) => setTagInput(e.target.value)}
|
|
onKeyDown={(e) => e.key === "Enter" && addTag()}
|
|
placeholder="Add a tag"
|
|
/>
|
|
<InputGroupAddon align="inline-end">
|
|
<InputGroupButton onClick={addTag}>Add</InputGroupButton>
|
|
</InputGroupAddon>
|
|
</InputGroup>
|
|
{tags.length > 0 && (
|
|
<div className="flex flex-row gap-2 flex-wrap mt-2">
|
|
{tags.map((tag) => (
|
|
<Badge
|
|
key={tag}
|
|
className="flex gap-1 items-center cursor-pointer"
|
|
onClick={() => removeTag(tag)}
|
|
>
|
|
{tag}
|
|
<X size={12} />
|
|
</Badge>
|
|
))}
|
|
</div>
|
|
)}
|
|
</Field>
|
|
</FieldGroup>
|
|
</FieldSet>
|
|
<FieldSet>
|
|
<h2 className="underline underline-offset-8 decoration-2 mb-4">Content</h2>
|
|
<div className="flex flex-col gap-4">
|
|
{blocks.length === 0 && (
|
|
<p className="text-muted-foreground">No blocks yet</p>
|
|
)}
|
|
{blocks.map((block) => (
|
|
<BlockEditor
|
|
key={block.id}
|
|
block={block}
|
|
onChange={(content) => updateBlock(block.id, content)}
|
|
onRemove={() => removeBlock(block.id)}
|
|
/>
|
|
))}
|
|
</div>
|
|
<div className="flex flex-row gap-4 flex-wrap mt-8">
|
|
{(["PARAGRAPH", "HEADING", "IMAGE", "QUOTE", "CALLOUT", "DIVIDER", "LIST", "TABLE"] as BlockType[]).map((t) => (
|
|
<Button key={t} variant="outline" onClick={() => addBlock(t)}>
|
|
<Plus size={14} />{t.toLowerCase()}
|
|
</Button>
|
|
))}
|
|
</div>
|
|
</FieldSet>
|
|
<div className="flex flex-row gap-4">
|
|
<Button variant="outline" disabled={isPending} onClick={() => handleSave("DRAFT")}>
|
|
{isPending ? <Loader2 size={14} className="animate-spin" /> : null}
|
|
Save as draft
|
|
</Button>
|
|
<Button disabled={isPending} onClick={() => handleSave("PUBLISHED")}>
|
|
{isPending ? <Loader2 size={14} className="animate-spin" /> : null}
|
|
Publish
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function BlockEditor({ block, onChange, onRemove }: { block: Block; onChange: (content: Block["content"]) => void; onRemove: () => void }) {
|
|
return (
|
|
<div className="flex flex-row gap-4 items-start group">
|
|
<div className="flex-1 rounded-lg border p-4">
|
|
<BlockInput block={block} onChange={onChange} />
|
|
</div>
|
|
<Button variant="ghost" size="icon" onClick={onRemove} className="opacity-0 group-hover:opacity-100 duration-200">
|
|
<Trash2 size={16} />
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function BlockInput({ block, onChange }: {
|
|
block: Block;
|
|
onChange: (c: Block["content"]) => void;
|
|
}) {
|
|
switch (block.type) {
|
|
case "PARAGRAPH":
|
|
return (
|
|
<textarea
|
|
className="w-full bg-transparent resize-none outline-none text-sm"
|
|
rows={3}
|
|
placeholder="Paragraph text..."
|
|
value={(block.content as any).text ?? ""}
|
|
onChange={(e) => onChange({ type: "PARAGRAPH", text: e.target.value })}
|
|
/>
|
|
);
|
|
case "HEADING":
|
|
return (
|
|
<input
|
|
className="w-full bg-transparent outline-none text-xl font-semibold tracking-tight"
|
|
placeholder="Heading..."
|
|
value={(block.content as any).text ?? ""}
|
|
onChange={(e) => onChange({ type: "HEADING", text: e.target.value, level: 2 })}
|
|
/>
|
|
);
|
|
case "QUOTE":
|
|
return (
|
|
<div className="flex flex-col gap-2 border-l-2 pl-4">
|
|
<textarea
|
|
className="w-full bg-transparent resize-none outline-none text-sm"
|
|
rows={2}
|
|
placeholder="Quote text..."
|
|
value={(block.content as any).text ?? ""}
|
|
onChange={(e) => onChange({ type: "QUOTE", text: e.target.value, author: (block.content as any).author })}
|
|
/>
|
|
<input
|
|
className="w-full bg-transparent outline-none text-sm text-muted-foreground"
|
|
placeholder="Author..."
|
|
value={(block.content as any).author ?? ""}
|
|
onChange={(e) => onChange({ type: "QUOTE", text: (block.content as any).text, author: e.target.value })}
|
|
/>
|
|
</div>
|
|
);
|
|
case "DIVIDER":
|
|
return <hr />;
|
|
case "CALLOUT":
|
|
return (
|
|
<div className="flex flex-row gap-2 items-start bg-muted rounded-lg p-4">
|
|
<span>⚠️</span>
|
|
<textarea
|
|
className="w-full bg-transparent resize-none outline-none text-sm"
|
|
rows={2}
|
|
placeholder="Callout text..."
|
|
value={(block.content as any).text ?? ""}
|
|
onChange={(e) => onChange({ type: "CALLOUT", text: e.target.value })}
|
|
/>
|
|
</div>
|
|
);
|
|
default:
|
|
return <p className="text-muted-foreground text-sm">{block.type} — editor coming soon</p>;
|
|
}
|
|
}
|
|
|
|
function getDefaultContent(type: BlockType): Block["content"] {
|
|
switch (type) {
|
|
case "PARAGRAPH": return { type: "PARAGRAPH", text: "" };
|
|
case "HEADING": return { type: "HEADING", text: "", level: 2 };
|
|
case "QUOTE": return { type: "QUOTE", text: "" };
|
|
case "CALLOUT": return { type: "CALLOUT", text: "" };
|
|
case "DIVIDER": return { type: "DIVIDER" };
|
|
case "LIST": return { type: "LIST", style: "BULLET", items: [] };
|
|
case "TABLE": return { type: "TABLE", headers: [], rows: [] };
|
|
case "IMAGE": return { type: "IMAGE", url: "" };
|
|
default: return { type: "PARAGRAPH", text: "" };
|
|
}
|
|
} |