1
0
Fork 0
polaris-task-force/src/app/(frontend)/journals/[id]/page.tsx
Z8MB1E 8a7d79c369 feat(journals): member-authored journal entries with author-owned publishing
- New Journals collection (Chronicle group): title, Lexical body, status
  draft/published, optional in-world date and mission link; authors read,
  edit, and delete their own entries at any status while every logged-in
  user reads published ones; journals:* staff permissions bypass ownership
  and gate the admin page (registered in the permission registry)
- /journals list with compose dialog and /journals/[id] reader reusing the
  Chronicle typography; edit/delete controls for authors and staff; drafts
  carry the DraftBadge and stay invisible to everyone but their author
- Server actions create/update/delete with ownership checks, publish-time
  journal:created and journal:deleted feed events (journals added to the
  GameEventLogs target collection enum via migration)
- Sidebar entry under Chronicle, command palette destination, admin panel
  icon; excerpt extractor as a pure module (src/lib/journals/excerpt.ts)
- Access-control integration spec covers draft privacy, publish visibility,
  non-author write blocking, and staff override (5 tests)
2026-10-12 14:39:48 -04:00

146 lines
4.9 KiB
TypeScript

import config from "@payload-config";
import { getPayload } from "payload";
import { headers as nextHeaders } from "next/headers";
import Link from "next/link";
import { notFound } from "next/navigation";
import { RichText } from "@payloadcms/richtext-lexical/react";
import { ArrowLeftIcon } from "lucide-react";
import type { Journal, Mission, User } from "@/payload-types";
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
import { hasPermission } from "@/utils/access-control/hasPermission";
import { cn } from "@/lib/utils";
import { BEAT_BODY_CLASSES } from "@/lib/story/richtextClasses";
import { DraftBadge } from "@/components/frontend/story/DraftBadge";
import { JournalDetailControls } from "@/components/frontend/journals/JournalDetailControls";
import type { JournalDraft, MissionOption } from "@/components/frontend/journals/JournalFormDialog";
export const metadata = {
title: "Journal Entry",
};
const DATE_FORMAT = new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
function authorName(author: number | User): string {
if (typeof author === "number") return "Unknown operator";
return author.displayName || author.username || "Unknown operator";
}
export default async function JournalDetailPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const journalId = Number(id);
if (!Number.isInteger(journalId) || journalId <= 0) notFound();
const payloadConfig = await config;
const payload = await getPayload({ config: payloadConfig });
const headers = await nextHeaders();
const { user } = await payload.auth({ headers, canSetHeaders: false });
if (!isPayloadUser(user)) return null;
// Read access is enforced by the collection: drafts of other authors throw
// here and fall through to notFound().
let journal: Journal | null = null;
try {
journal = (await payload.findByID({
collection: "journals",
id: journalId,
depth: 1,
overrideAccess: false,
user,
})) as Journal;
} catch {
journal = null;
}
if (!journal) notFound();
const authorId =
typeof journal.author === "number" ? journal.author : journal.author?.id ?? null;
const isAuthor = authorId !== null && authorId === user.id;
const [canUpdate, canDelete] = await Promise.all([
hasPermission(payload, user, "journals:update"),
hasPermission(payload, user, "journals:delete"),
]);
const canEdit = canUpdate || isAuthor;
const canDeleteEntry = canDelete || isAuthor;
const mission =
journal.mission && typeof journal.mission !== "number" ? (journal.mission as Mission) : null;
const dateSource = journal.occurredAt ?? journal.createdAt;
const isDraft = journal.status !== "published";
const missionsResult = await payload.find({
collection: "missions",
limit: 200,
depth: 0,
sort: "-id",
select: { name: true, codeName: true },
overrideAccess: false,
user,
});
const missionOptions: MissionOption[] = (missionsResult.docs as Mission[]).map((m) => ({
id: m.id,
label: m.codeName ? `${m.codeName} · ${m.name}` : m.name,
}));
const draft: JournalDraft = {
id: journal.id,
title: journal.title,
status: journal.status,
occurredAt: journal.occurredAt ? journal.occurredAt.slice(0, 10) : "",
missionId: mission?.id ?? null,
body: JSON.parse(JSON.stringify(journal.body)) as Record<string, unknown>,
};
return (
<div className="mx-auto max-w-3xl px-4 py-10">
<Link
href="/journals"
className="inline-flex items-center gap-1.5 text-sm text-muted-foreground transition-colors hover:text-foreground"
>
<ArrowLeftIcon className="size-4" />
All journals
</Link>
<header className="mt-4 flex flex-wrap items-start justify-between gap-4">
<div className="min-w-0">
<div className="flex items-center gap-2">
{isDraft && <DraftBadge />}
{mission && (
<span className="rounded border border-border px-2 py-0.5 text-xs text-muted-foreground">
{mission.codeName ? `${mission.codeName} · ${mission.name}` : mission.name}
</span>
)}
</div>
<h1 className="mt-2 font-serif text-3xl font-bold leading-tight">{journal.title}</h1>
<div className="mt-2 flex flex-wrap items-center gap-3 text-xs text-muted-foreground">
<span>{authorName(journal.author)}</span>
{dateSource && (
<>
<span className="text-border">|</span>
<span>{DATE_FORMAT.format(new Date(dateSource))}</span>
</>
)}
</div>
</div>
<JournalDetailControls
draft={draft}
missions={missionOptions}
canEdit={canEdit}
canDelete={canDeleteEntry}
/>
</header>
<article className={cn("mt-8", BEAT_BODY_CLASSES)}>
<RichText data={journal.body} />
</article>
</div>
);
}