76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
"use client";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@/components/ui/dialog";
|
|
import { deletePageAction } from "@/lib/pages/actions";
|
|
import { PencilIcon, XIcon } from "lucide-react";
|
|
import Link from "next/link";
|
|
import { useState } from "react";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { PageType } from "@/lib/pages/types";
|
|
import { useAction } from "@/hooks/useAction";
|
|
|
|
type Draft = { id: string; type: PageType; title: string; slug: string; updatedAt: Date | null };
|
|
|
|
function DraftItem({ draft, onDeleted }: { draft: Draft; onDeleted: (id: string) => void }) {
|
|
const { run, isPending } = useAction(deletePageAction, {
|
|
successMessage: "Draft deleted",
|
|
onSuccess: () => onDeleted(draft.id),
|
|
});
|
|
|
|
return (
|
|
<div className="w-full flex flex-row items-center justify-between">
|
|
<h3 className="w-64 truncate">
|
|
{draft.title}
|
|
</h3>
|
|
<div className="flex flex-row items-center gap-2">
|
|
<Link href={`/${draft.type.toLocaleLowerCase()}/${draft.slug}/edit`}>
|
|
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
|
</Link>
|
|
<Button disabled={isPending} variant="secondary" size="icon-sm" onClick={() => run(draft.id, draft.type)}>
|
|
{isPending ? <Spinner /> : <XIcon />}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function DraftsDialog({ drafts }: { drafts: Draft[]}) {
|
|
const [items, setItems] = useState(drafts);
|
|
|
|
function handleDeleted(id: string) {
|
|
setItems((prev) => prev.filter((d) => d.id !== id));
|
|
}
|
|
|
|
return (
|
|
<Dialog>
|
|
<DialogTrigger asChild>
|
|
<Button variant="secondary" className="flex flex-row items-center gap-2">Drafts<div className="flex items-center justify-center bg-black/50 text-white w-3.5 h-3.5 text-xs rounded-full">{items.length}</div></Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Drafts</DialogTitle>
|
|
<DialogDescription>Here are drafts of your pages</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
{items.length > 0 ? (
|
|
<div className="flex flex-col items-center gap-2">
|
|
{items.map((draft) => (
|
|
<DraftItem key={draft.id} draft={draft} onDeleted={handleDeleted} />
|
|
))}
|
|
</div>
|
|
) : (
|
|
<h4 className="text-muted-foreground">There are no drafts yet...</h4>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|