Replace the plain articles table with a responsive card grid showing thumbnails, title, date and author. Add a slug field to the article form with live auto-suggestion, i18n-driven labels, a larger image preview and delete confirmation. Persist a user-provided slug (falls back to an auto-generated one) on create and update.
165 lines
4.7 KiB
TypeScript
165 lines
4.7 KiB
TypeScript
import Link from "next/link";
|
|
import { redirect } from "next/navigation";
|
|
import { getTranslations } from "next-intl/server";
|
|
import { deleteArticle } from "@/actions/admin-articles";
|
|
import { StatusCard } from "@/components/admin/dashboard";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export default async function AdminArticles({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<{ error?: string }>;
|
|
}) {
|
|
const { session, permissions } = await getAdminContext();
|
|
if (!canAccess(permissions, PERMS.NEWS_VIEW, session.user.rank)) {
|
|
redirect("/admin");
|
|
}
|
|
|
|
const { error } = await searchParams;
|
|
const t = await getTranslations("pages.admin.articles");
|
|
const articles = await prisma.websiteArticles
|
|
.findMany({
|
|
orderBy: { createdAt: "desc" },
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
slug: true,
|
|
image: true,
|
|
createdAt: true,
|
|
userId: true,
|
|
},
|
|
take: 50,
|
|
})
|
|
.catch(() => []);
|
|
|
|
const authorMap = new Map<number, string>();
|
|
const authorIds = articles.map((a) => a.userId).filter((id): id is number => Boolean(id));
|
|
if (authorIds.length > 0) {
|
|
const authors = await prisma.user
|
|
.findMany({
|
|
where: { id: { in: authorIds } },
|
|
select: { id: true, username: true },
|
|
})
|
|
.catch(() => []);
|
|
for (const u of authors) authorMap.set(u.id, u.username);
|
|
}
|
|
|
|
const latest = articles[0]?.createdAt
|
|
? articles[0].createdAt.toISOString().slice(0, 10)
|
|
: "—";
|
|
|
|
return (
|
|
<main>
|
|
<div className="flex items-center justify-between mb-6">
|
|
<div>
|
|
<h1 className="m-0 text-2xl font-extrabold text-[var(--admin-text)]">
|
|
{t("title")}
|
|
</h1>
|
|
<p className="m-0 text-sm text-[var(--admin-text-muted)] mt-1">
|
|
{t("subtitle")}
|
|
</p>
|
|
</div>
|
|
<Link href="/admin/articles/new" className="btn btn-primary">
|
|
+ {t("newArticle")}
|
|
</Link>
|
|
</div>
|
|
|
|
{error ? (
|
|
<p className="text-xs text-[var(--admin-error)] dark:text-[var(--admin-error)] mb-4">
|
|
Error: {error}
|
|
</p>
|
|
) : null}
|
|
|
|
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
|
<StatusCard
|
|
label={t("articlesShown")}
|
|
value={articles.length}
|
|
icon="📰"
|
|
/>
|
|
<StatusCard
|
|
label={t("latest")}
|
|
value={latest}
|
|
hint={t("mostRecentPost")}
|
|
icon="🕒"
|
|
/>
|
|
</div>
|
|
|
|
<section className="mt-6">
|
|
<h2 className="admin-section-title">{t("recentArticles")}</h2>
|
|
{articles.length === 0 ? (
|
|
<div className="admin-empty">{t("noArticles")}</div>
|
|
) : (
|
|
<div className="grid grid-cols-[repeat(auto-fill,minmax(260px,1fr))] gap-4">
|
|
{articles.map((a) => (
|
|
<div
|
|
key={String(a.id)}
|
|
className="admin-card overflow-hidden flex flex-col"
|
|
>
|
|
<div className="relative w-full aspect-[16/9] bg-[var(--admin-canvas)]">
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
src={a.image || ""}
|
|
alt={a.title}
|
|
className="w-full h-full object-cover block"
|
|
onError={(
|
|
e: React.SyntheticEvent<HTMLImageElement>,
|
|
) => {
|
|
e.currentTarget.style.visibility = "hidden";
|
|
}}
|
|
/>
|
|
</div>
|
|
<div className="p-3 flex flex-col flex-1">
|
|
<Link
|
|
href={`/admin/articles/${a.id}`}
|
|
className="font-semibold text-[var(--admin-text)] hover:text-[var(--admin-accent)] line-clamp-2"
|
|
style={{ textDecoration: "none" }}
|
|
>
|
|
{a.title}
|
|
</Link>
|
|
<p className="text-xs text-[var(--admin-text-muted)] mt-1 mb-3 flex items-center gap-2 flex-wrap">
|
|
<span>{a.createdAt ? a.createdAt.toISOString().slice(0, 10) : "—"}</span>
|
|
{authorMap.get(a.userId ?? -1) ? (
|
|
<>
|
|
<span>·</span>
|
|
<span>{authorMap.get(a.userId ?? -1)}</span>
|
|
</>
|
|
) : null}
|
|
</p>
|
|
<div className="flex gap-2 mt-auto">
|
|
<Link
|
|
href={`/admin/articles/${a.id}`}
|
|
className="btn btn-primary flex-1 text-center"
|
|
>
|
|
{t("edit")}
|
|
</Link>
|
|
<form action={deleteArticle} className="m-0">
|
|
<input type="hidden" name="id" value={String(a.id)} />
|
|
<button
|
|
type="submit"
|
|
className="btn btn-danger"
|
|
onClick={(e) => {
|
|
if (
|
|
!window.confirm(
|
|
t("confirmDelete", { title: a.title }),
|
|
)
|
|
)
|
|
e.preventDefault();
|
|
}}
|
|
>
|
|
{t("delete")}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|