1
0
Fork 0
polaris-task-force/src/app/(frontend)/story/page.tsx
Z8MB1E 34c0d5681f fix(rich-text): render full lexical formatting incl. colours across all fields
RichText authored in the admin panel was rendered as plain text on the
public site: headings/lists/quotes were flattened by Tailwind preflight
(no @tailwindcss/typography installed), and Payload's default JSX
converter ignored text colours.

- src/lib/lexical: shared editor factory (ptfLexicalEditor) + shared
  colour palette (textState) + colour converter (richTextConverters)
- RichTextView: canonical .lexical-content renderer (colour-aware),
  swapped into every frontend render site
- All rich-text editors route through the factory, so every field
  inherits the coloured editor (incl. the previously-unedited ones)
- .lexical-content CSS extended with checklist/table/heading rules,
  wrapped in @layer components so per-page utilities still override;
  dropped the now-dead per-page prose hacks
- Remove the now-dead briefingConverters in favour of the shared
  converter; add tests/int/rich-text-view.int.spec.ts
2026-11-18 13:10:26 -05:00

212 lines
8.5 KiB
TypeScript

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<StoryReaction, number>;
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<number, StoryBeatState>();
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<number, ReactionCounts>();
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 (
<div className="mx-auto max-w-3xl px-4 py-10 xl:max-w-[96rem]">
<div className="xl:grid xl:grid-cols-[minmax(0,1fr)_minmax(0,48rem)_minmax(0,1fr)] xl:gap-8">
<div className="min-w-0 xl:col-start-2">
<header className="mb-12 text-center">
<p className="text-xs uppercase tracking-[0.3em] text-muted-foreground">
The Unit Chronicle
</p>
<h1 className="mt-2 font-serif text-4xl font-bold">The Chronicle</h1>
<p className="mt-3 text-muted-foreground">
Every operation leaves a mark. These are the entries of our chronicle, told in order.
</p>
{displayBeats.length > 0 && (
<div className="mt-5 flex flex-wrap justify-center gap-2">
<StoryViewer beats={readerBeats} />
<BookView beats={readerBeats} />
</div>
)}
</header>
{displayBeats.length === 0 ? (
<p className="text-center text-muted-foreground">
No chronicle entries to show yet. Check back soon.
</p>
) : (
<ol className="space-y-16">
{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 (
<li
key={beat.id}
id={`beat-${beat.id}`}
data-beat-id={beat.id}
className="relative scroll-mt-20 border-l border-border pl-8"
>
<span className="absolute -left-[9px] top-1.5 size-4 rounded-full border-2 border-background bg-primary" />
<div className="flex items-center justify-between gap-4">
<p className="text-xs uppercase tracking-widest text-muted-foreground">
Chapter {chapterById.get(beat.id) ?? 0}
</p>
{isDraft ? (
<DraftBadge />
) : (
<BeatReadMarker beatId={beat.id} initialRead={Boolean(state?.readAt)} />
)}
</div>
<h2 className="mt-2 font-serif text-2xl font-semibold">{beat.title}</h2>
<div className="mt-1 flex flex-wrap items-center gap-3 text-xs text-muted-foreground">
{beat.occurredAt && (
<span>{DATE_FORMAT.format(new Date(beat.occurredAt))}</span>
)}
{missionLabel && (
<span className="rounded border border-border px-2 py-0.5">
{missionLabel}
</span>
)}
</div>
{voiceoverUrl && (
<div className="mt-4">
<BeatVoiceover
src={voiceoverUrl}
label={beat.title}
aiGenerated={voiceoverDocForBeat?.aiGenerated ?? false}
/>
</div>
)}
<div className="mt-4">
<RichTextView data={beat.body} className={cn(BEAT_BODY_CLASSES)} />
</div>
{!isDraft && (
<BeatReactions
beatId={beat.id}
initialReaction={state?.reaction ?? null}
initialCounts={counts.get(beat.id) ?? emptyCounts()}
/>
)}
</li>
);
})}
</ol>
)}
</div>
{displayBeats.length > 0 && (
<aside className="hidden xl:col-start-1 xl:row-start-1 xl:block xl:pt-48 xl:min-w-max">
<StoryTableOfContents beats={readerBeats} />
</aside>
)}
</div>
</div>
);
}