/** * Reading the moment a preview is asked for. * * The whole display pipeline is already a function of "now", so showing the * panel as it will be next Tuesday costs nothing but passing a different * instant. What it does need is a bound: an unparseable or absurd value * should not render a screen for the year 3000 and leave someone believing it. */ /** As far ahead or behind as a shop plausibly plans. */ export const PREVIEW_RANGE_DAYS = 400; export type PreviewTime = { at: Date; isNow: boolean }; export function parsePreviewTime(raw: string | null, now: Date): PreviewTime { if (!raw) { return { at: now, isNow: true }; } const parsed = new Date(raw); if (Number.isNaN(parsed.getTime())) { return { at: now, isNow: true }; } const limit = PREVIEW_RANGE_DAYS * 86_400_000; const distance = parsed.getTime() - now.getTime(); if (Math.abs(distance) > limit) { return { at: now, isNow: true }; } return { at: parsed, isNow: Math.abs(distance) < 60_000 }; }