feat: news
This commit is contained in:
+173
-86
@@ -1,7 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState, useEffect } from "react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
@@ -17,34 +16,50 @@ import {
|
||||
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";
|
||||
import { Globe2, Plus, Trash2, X } from "lucide-react";
|
||||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
|
||||
|
||||
export default function Editor({ page, type, initialBlocks = [] }: { page: Page | null; type: PageType; initialBlocks?: Block[] }) {
|
||||
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 router = useRouter();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
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<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 },
|
||||
});
|
||||
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();
|
||||
@@ -58,14 +73,17 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
}
|
||||
|
||||
function addBlock(type: BlockType) {
|
||||
setBlocks([...blocks, {
|
||||
id: crypto.randomUUID(),
|
||||
pageId: page?.id ?? "",
|
||||
parentId: null,
|
||||
type,
|
||||
position: blocks.length,
|
||||
content: getDefaultContent(type),
|
||||
}]);
|
||||
setBlocks([
|
||||
...blocks,
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
pageId: page?.id ?? "",
|
||||
parentId: null,
|
||||
type,
|
||||
position: blocks.length,
|
||||
content: getDefaultContent(type),
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
function removeBlock(id: string) {
|
||||
@@ -77,7 +95,7 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-8">
|
||||
<div className="flex flex-col gap-10">
|
||||
<FieldSet>
|
||||
<h2 className="underline underline-offset-8 decoration-2 mb-4">Meta</h2>
|
||||
<FieldGroup>
|
||||
@@ -90,13 +108,29 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
onChange={(e) => {
|
||||
setTitle(e.target.value);
|
||||
if (!page) {
|
||||
setSlug(e.target.value.toLowerCase().replace(/\s+/g, "-"));
|
||||
setSlug(
|
||||
e.target.value.toLowerCase().replace(/\s+/g, "-")
|
||||
);
|
||||
}
|
||||
}}
|
||||
placeholder="Enter the page title"
|
||||
placeholder="Enter page title"
|
||||
/>
|
||||
</InputGroup>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="type">Page type</FieldLabel>
|
||||
<Select defaultValue={type} onValueChange={(e) => setType(e)}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem value="PAGE">Page</SelectItem>
|
||||
<SelectItem value="NEWS">News</SelectItem>
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="slug">Slug</FieldLabel>
|
||||
<InputGroup>
|
||||
@@ -111,15 +145,15 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
<Field>
|
||||
<FieldLabel htmlFor="location">Location</FieldLabel>
|
||||
<InputGroup>
|
||||
<InputGroupAddon align="inline-start">
|
||||
<Globe2 size={14} className="text-muted-foreground" />
|
||||
</InputGroupAddon>
|
||||
<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>
|
||||
@@ -130,22 +164,23 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
value={tagInput}
|
||||
onChange={(e) => setTagInput(e.target.value)}
|
||||
onKeyDown={(e) => e.key === "Enter" && addTag()}
|
||||
placeholder="Add a tag"
|
||||
placeholder="Add a tag and press Enter"
|
||||
/>
|
||||
<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">
|
||||
<div className="flex flex-row gap-2 flex-wrap mt-3">
|
||||
{tags.map((tag) => (
|
||||
<Badge
|
||||
key={tag}
|
||||
variant="secondary"
|
||||
className="flex gap-1 items-center cursor-pointer"
|
||||
onClick={() => removeTag(tag)}
|
||||
>
|
||||
{tag}
|
||||
<X size={12} />
|
||||
<X size={11} />
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
@@ -153,11 +188,14 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
</FieldSet>
|
||||
|
||||
<FieldSet>
|
||||
<h2 className="underline underline-offset-8 decoration-2 mb-4">Content</h2>
|
||||
<div className="flex flex-col gap-4">
|
||||
<div className="flex flex-col gap-3">
|
||||
{blocks.length === 0 && (
|
||||
<p className="text-muted-foreground">No blocks yet</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
No blocks yet. Add one below.
|
||||
</p>
|
||||
)}
|
||||
{blocks.map((block) => (
|
||||
<BlockEditor
|
||||
@@ -168,113 +206,162 @@ export default function Editor({ page, type, initialBlocks = [] }: { page: Page
|
||||
/>
|
||||
))}
|
||||
</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()}
|
||||
<div className="flex flex-row gap-2 flex-wrap mt-6">
|
||||
{(
|
||||
[
|
||||
"PARAGRAPH",
|
||||
"HEADING",
|
||||
"IMAGE",
|
||||
"QUOTE",
|
||||
"CALLOUT",
|
||||
"DIVIDER",
|
||||
"LIST",
|
||||
"TABLE",
|
||||
] as BlockType[]
|
||||
).map((t) => (
|
||||
<Button
|
||||
key={t}
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => addBlock(t)}
|
||||
>
|
||||
<Plus size={12} />
|
||||
{t.charAt(0) + t.slice(1).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">
|
||||
<div className="flex flex-row gap-3 items-start group">
|
||||
<div className="flex-1 rounded-md border bg-background 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
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={onRemove}
|
||||
className="opacity-0 group-hover:opacity-100 transition-opacity mt-1 shrink-0"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function BlockInput({ block, onChange }: {
|
||||
block: Block;
|
||||
onChange: (c: Block["content"]) => void;
|
||||
}) {
|
||||
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"
|
||||
className="w-full bg-transparent resize-none outline-none text-sm leading-relaxed"
|
||||
rows={3}
|
||||
placeholder="Paragraph text..."
|
||||
placeholder="Start writing..."
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) => onChange({ type: "PARAGRAPH", text: e.target.value })}
|
||||
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..."
|
||||
className="w-full bg-transparent outline-none text-lg tracking-tight"
|
||||
placeholder="Heading"
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) => onChange({ type: "HEADING", text: e.target.value, level: 2 })}
|
||||
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"
|
||||
className="w-full bg-transparent resize-none outline-none text-sm italic leading-relaxed"
|
||||
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 })}
|
||||
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..."
|
||||
className="w-full bg-transparent outline-none text-xs 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 })}
|
||||
onChange={(e) =>
|
||||
onChange({
|
||||
type: "QUOTE",
|
||||
text: (block.content as any).text,
|
||||
author: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
case "DIVIDER":
|
||||
return <hr />;
|
||||
return <hr className="border-border" />;
|
||||
case "CALLOUT":
|
||||
return (
|
||||
<div className="flex flex-row gap-2 items-start bg-muted rounded-lg p-4">
|
||||
<span>⚠️</span>
|
||||
<div className="flex flex-row gap-3 items-start rounded-md bg-muted p-3">
|
||||
<span className="text-base leading-none mt-0.5">⚠️</span>
|
||||
<textarea
|
||||
className="w-full bg-transparent resize-none outline-none text-sm"
|
||||
className="w-full bg-transparent resize-none outline-none text-sm leading-relaxed"
|
||||
rows={2}
|
||||
placeholder="Callout text..."
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) => onChange({ type: "CALLOUT", text: e.target.value })}
|
||||
onChange={(e) =>
|
||||
onChange({ type: "CALLOUT", text: e.target.value })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
case "IMAGE":
|
||||
return (
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-sm text-muted-foreground"
|
||||
placeholder="https://example.com/image.jpg"
|
||||
value={(block.content as any).url ?? ""}
|
||||
onChange={(e) => onChange({ type: "IMAGE", url: e.target.value })}
|
||||
/>
|
||||
);
|
||||
default:
|
||||
return <p className="text-muted-foreground text-sm">{block.type} — editor coming soon</p>;
|
||||
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: "" };
|
||||
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: "" };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user