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, }; return (
All journals
{isDraft && } {mission && ( {mission.codeName ? `${mission.codeName} · ${mission.name}` : mission.name} )}

{journal.title}

{authorName(journal.author)} {dateSource && ( <> | {DATE_FORMAT.format(new Date(dateSource))} )}
); }