fix(journals): render in-world dates in UTC so the day never shifts
This commit is contained in:
parent
420bc06f84
commit
3cc97bf791
4 changed files with 34 additions and 14 deletions
|
|
@ -10,6 +10,7 @@ import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
||||||
import { hasPermission } from "@/utils/access-control/hasPermission";
|
import { hasPermission } from "@/utils/access-control/hasPermission";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { BEAT_BODY_CLASSES } from "@/lib/story/richtextClasses";
|
import { BEAT_BODY_CLASSES } from "@/lib/story/richtextClasses";
|
||||||
|
import { formatJournalDate } from "@/lib/journals/dates";
|
||||||
import { DraftBadge } from "@/components/frontend/story/DraftBadge";
|
import { DraftBadge } from "@/components/frontend/story/DraftBadge";
|
||||||
import { JournalDetailControls } from "@/components/frontend/journals/JournalDetailControls";
|
import { JournalDetailControls } from "@/components/frontend/journals/JournalDetailControls";
|
||||||
import type { JournalDraft, MissionOption } from "@/components/frontend/journals/JournalFormDialog";
|
import type { JournalDraft, MissionOption } from "@/components/frontend/journals/JournalFormDialog";
|
||||||
|
|
@ -18,12 +19,6 @@ export const metadata = {
|
||||||
title: "Journal Entry",
|
title: "Journal Entry",
|
||||||
};
|
};
|
||||||
|
|
||||||
const DATE_FORMAT = new Intl.DateTimeFormat("en-US", {
|
|
||||||
year: "numeric",
|
|
||||||
month: "long",
|
|
||||||
day: "numeric",
|
|
||||||
});
|
|
||||||
|
|
||||||
function authorName(author: number | User): string {
|
function authorName(author: number | User): string {
|
||||||
if (typeof author === "number") return "Unknown operator";
|
if (typeof author === "number") return "Unknown operator";
|
||||||
return author.displayName || author.username || "Unknown operator";
|
return author.displayName || author.username || "Unknown operator";
|
||||||
|
|
@ -127,7 +122,7 @@ export default async function JournalDetailPage({
|
||||||
{dateSource && (
|
{dateSource && (
|
||||||
<>
|
<>
|
||||||
<span className="text-border">|</span>
|
<span className="text-border">|</span>
|
||||||
<span>{DATE_FORMAT.format(new Date(dateSource))}</span>
|
<span>{formatJournalDate(dateSource)}</span>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { headers as nextHeaders } from "next/headers";
|
||||||
import type { Journal, Mission, User } from "@/payload-types";
|
import type { Journal, Mission, User } from "@/payload-types";
|
||||||
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
||||||
import { extractExcerpt } from "@/lib/journals/excerpt";
|
import { extractExcerpt } from "@/lib/journals/excerpt";
|
||||||
|
import { formatJournalDate } from "@/lib/journals/dates";
|
||||||
import { JournalsList, type JournalCard } from "@/components/frontend/journals/JournalsList";
|
import { JournalsList, type JournalCard } from "@/components/frontend/journals/JournalsList";
|
||||||
import type { MissionOption } from "@/components/frontend/journals/JournalFormDialog";
|
import type { MissionOption } from "@/components/frontend/journals/JournalFormDialog";
|
||||||
|
|
||||||
|
|
@ -11,12 +12,6 @@ export const metadata = {
|
||||||
title: "Journals",
|
title: "Journals",
|
||||||
};
|
};
|
||||||
|
|
||||||
const DATE_FORMAT = new Intl.DateTimeFormat("en-US", {
|
|
||||||
year: "numeric",
|
|
||||||
month: "long",
|
|
||||||
day: "numeric",
|
|
||||||
});
|
|
||||||
|
|
||||||
function missionLabel(mission: Mission): string {
|
function missionLabel(mission: Mission): string {
|
||||||
return mission.codeName ? `${mission.codeName} · ${mission.name}` : mission.name;
|
return mission.codeName ? `${mission.codeName} · ${mission.name}` : mission.name;
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +65,7 @@ export default async function JournalsPage() {
|
||||||
title: journal.title,
|
title: journal.title,
|
||||||
status: journal.status,
|
status: journal.status,
|
||||||
authorName: authorName(journal.author),
|
authorName: authorName(journal.author),
|
||||||
dateLabel: dateSource ? DATE_FORMAT.format(new Date(dateSource)) : null,
|
dateLabel: formatJournalDate(dateSource),
|
||||||
missionLabel: mission ? missionLabel(mission) : null,
|
missionLabel: mission ? missionLabel(mission) : null,
|
||||||
excerpt: extractExcerpt(journal.body),
|
excerpt: extractExcerpt(journal.body),
|
||||||
};
|
};
|
||||||
|
|
|
||||||
20
src/lib/journals/dates.ts
Normal file
20
src/lib/journals/dates.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/**
|
||||||
|
* Journal date display. `occurredAt` is an in-world *calendar day* stored as UTC
|
||||||
|
* midnight, and the `createdAt` fallback is a UTC instant; both are rendered in
|
||||||
|
* UTC so a machine behind UTC never shows the previous day. Pure and
|
||||||
|
* deterministic (independent of the server/browser timezone).
|
||||||
|
*/
|
||||||
|
|
||||||
|
const UTC_DATE_FORMAT = new Intl.DateTimeFormat("en-US", {
|
||||||
|
year: "numeric",
|
||||||
|
month: "long",
|
||||||
|
day: "numeric",
|
||||||
|
timeZone: "UTC",
|
||||||
|
});
|
||||||
|
|
||||||
|
export function formatJournalDate(value: string | Date | null | undefined): string | null {
|
||||||
|
if (!value) return null;
|
||||||
|
const date = value instanceof Date ? value : new Date(value);
|
||||||
|
if (Number.isNaN(date.getTime())) return null;
|
||||||
|
return UTC_DATE_FORMAT.format(date);
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from
|
||||||
import type { Journal, User } from "@/payload-types";
|
import type { Journal, User } from "@/payload-types";
|
||||||
import { createJournal, deleteJournal, updateJournal } from "@/app/(frontend)/journals/actions";
|
import { createJournal, deleteJournal, updateJournal } from "@/app/(frontend)/journals/actions";
|
||||||
import { extractExcerpt, journalBodyLength } from "@/lib/journals/excerpt";
|
import { extractExcerpt, journalBodyLength } from "@/lib/journals/excerpt";
|
||||||
|
import { formatJournalDate } from "@/lib/journals/dates";
|
||||||
import { recordContribution } from "@/lib/contributions";
|
import { recordContribution } from "@/lib/contributions";
|
||||||
|
|
||||||
// The actions read next/headers and call next/cache outside a request scope.
|
// The actions read next/headers and call next/cache outside a request scope.
|
||||||
|
|
@ -254,3 +255,12 @@ describe("journal text helpers", () => {
|
||||||
expect(out).toBe(`${"x".repeat(180)}…`);
|
expect(out).toBe(`${"x".repeat(180)}…`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("formatJournalDate", () => {
|
||||||
|
it("renders the calendar day in UTC regardless of the runner timezone", () => {
|
||||||
|
expect(formatJournalDate("2156-05-10T00:00:00.000Z")).toBe("May 10, 2156");
|
||||||
|
expect(formatJournalDate(null)).toBeNull();
|
||||||
|
expect(formatJournalDate(undefined)).toBeNull();
|
||||||
|
expect(formatJournalDate("not-a-date")).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue