import config from "@payload-config"; import { getPayload } from "payload"; import { headers as nextHeaders } from "next/headers"; import { RichTextView } from "@/components/frontend/RichTextView"; import type { StoryBeat, StoryBeatState, StoryVoiceover } from "@/payload-types"; import { isPayloadUser } from "@/utils/access-control/isPayloadUser"; import hasRoles from "@/utils/access-control/hasRoles"; import { cn } from "@/lib/utils"; import { BEAT_BODY_CLASSES } from "@/lib/story/richtextClasses"; import { BeatReactions } from "@/components/frontend/story/BeatReactions"; import { BeatReadMarker } from "@/components/frontend/story/BeatReadMarker"; import { BeatVoiceover } from "@/components/frontend/story/BeatVoiceover"; import { DraftBadge } from "@/components/frontend/story/DraftBadge"; import { BookView } from "@/components/frontend/story/BookView"; import { StoryViewer, type StoryViewerBeat } from "@/components/frontend/story/StoryViewer"; import { StoryTableOfContents } from "@/components/frontend/story/StoryTableOfContents"; import { chronicleDisplayOrder } from "@/lib/story/sortBeats"; import type { StoryReaction } from "@/lib/story/reactions"; export const metadata = { title: "Chronicle", }; type ReactionCounts = Record; const DATE_FORMAT = new Intl.DateTimeFormat("en-US", { year: "numeric", month: "long", day: "numeric", }); function emptyCounts(): ReactionCounts { return { like: 0, heart: 0, salute: 0 }; } function voiceoverDoc(beat: StoryBeat): StoryVoiceover | null { return beat.voiceover && typeof beat.voiceover !== "number" ? beat.voiceover : null; } export default async function StoryPage() { 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; // Story staff (admin/developer) preview drafts on /story; everyone else sees // published entries only. The collection's own read access is the backstop: // overrideAccess stays false, so a non-staff request can never leak a draft. const isStaff = hasRoles(["admin", "developer"], user); const beatsResult = await payload.find({ collection: "story-beats", ...(isStaff ? {} : { where: { status: { equals: "published" } } }), limit: 200, depth: 1, overrideAccess: false, user, }); const { beats: displayBeats, chapterById } = chronicleDisplayOrder( beatsResult.docs as StoryBeat[], ); const statesResult = await payload.find({ collection: "story-beat-states", where: { user: { equals: user.id } }, limit: 500, depth: 0, overrideAccess: false, user, }); const stateByBeat = new Map(); for (const state of statesResult.docs as StoryBeatState[]) { const beatId = typeof state.beat === "number" ? state.beat : state.beat.id; stateByBeat.set(beatId, state); } const beatIds = displayBeats.map((beat) => beat.id); const counts = new Map(); for (const id of beatIds) counts.set(id, emptyCounts()); if (beatIds.length > 0) { const reactionDocs = await payload.find({ collection: "story-beat-states", where: { and: [{ beat: { in: beatIds } }, { reaction: { not_equals: null } }], }, limit: 1000, depth: 0, overrideAccess: true, }); for (const doc of reactionDocs.docs as StoryBeatState[]) { const beatId = typeof doc.beat === "number" ? doc.beat : doc.beat.id; const beatCounts = counts.get(beatId); if (doc.reaction && beatCounts) beatCounts[doc.reaction] += 1; } } const readerBeats: StoryViewerBeat[] = displayBeats.map((beat) => ({ id: beat.id, title: beat.title, chapter: chapterById.get(beat.id) ?? 0, dateLabel: beat.occurredAt ? DATE_FORMAT.format(new Date(beat.occurredAt)) : null, body: beat.body, initialRead: Boolean(stateByBeat.get(beat.id)?.readAt), voiceoverUrl: voiceoverDoc(beat)?.url ?? null, voiceoverAiGenerated: voiceoverDoc(beat)?.aiGenerated ?? false, draft: beat.status !== "published", })); return (

The Unit Chronicle

The Chronicle

Every operation leaves a mark. These are the entries of our chronicle, told in order.

{displayBeats.length > 0 && (
)}
{displayBeats.length === 0 ? (

No chronicle entries to show yet. Check back soon.

) : (
    {displayBeats.map((beat) => { const state = stateByBeat.get(beat.id); const isDraft = beat.status !== "published"; const mission = beat.mission && typeof beat.mission !== "number" ? beat.mission : null; const missionLabel = mission ? mission.codeName ? `${mission.codeName} ยท ${mission.name}` : mission.name : null; const voiceoverDocForBeat = voiceoverDoc(beat); const voiceoverUrl = voiceoverDocForBeat?.url ?? null; return (
  1. Chapter {chapterById.get(beat.id) ?? 0}

    {isDraft ? ( ) : ( )}

    {beat.title}

    {beat.occurredAt && ( {DATE_FORMAT.format(new Date(beat.occurredAt))} )} {missionLabel && ( {missionLabel} )}
    {voiceoverUrl && (
    )}
    {!isDraft && ( )}
  2. ); })}
)}
{displayBeats.length > 0 && ( )}
); }