refactor: centralize duplicated formatDate helper
Local Build and Deploy / deploy (push) Successful in 1m7s
Local Build and Deploy / deploy (push) Successful in 1m7s
21 files defined their own local formatDate (with three different output formats: date, datetime, datetime-seconds, and varying null fallbacks). Replace them with a single shared helper at src/lib/format-date.ts that takes a variant + optional fallback, preserving the exact previous output per call site (verified identical string results). Removes ~21 copies of the same logic. photos.tsx and media-grid.tsx keep their epoch-ms based formatters since those are a different input shape. Verified: tsc --noEmit clean, full test suite green (301/301).
This commit is contained in:
1 parent
3c9c1311ec
commit
b9c6001f08
20 files changed
+84
-86
No files matched your search
@@ -0,0 +1,36 @@
|
||||
export type DateFormatVariant = "date" | "datetime" | "datetime-seconds";
|
||||
|
||||
const FALLBACKS: Record<DateFormatVariant, string> = {
|
||||
date: "",
|
||||
datetime: "—",
|
||||
"datetime-seconds": "—",
|
||||
};
|
||||
|
||||
/**
|
||||
* Format a Date for display. Kept as a single shared helper so the various
|
||||
* admin/site pages no longer each redefine their own copy.
|
||||
*
|
||||
* - "date" → 2026-07-18
|
||||
* - "datetime" → 2026-07-18 18:30
|
||||
* - "datetime-seconds"→ 2026-07-18 18:30:45
|
||||
*
|
||||
* `fallback` overrides the default em-dash / empty string when the value is
|
||||
* null/undefined.
|
||||
*/
|
||||
export function formatDate(
|
||||
d: Date | null | undefined,
|
||||
variant: DateFormatVariant = "datetime",
|
||||
fallback?: string,
|
||||
): string {
|
||||
if (!d) return fallback ?? FALLBACKS[variant];
|
||||
const iso = d.toISOString();
|
||||
switch (variant) {
|
||||
case "date":
|
||||
return iso.slice(0, 10);
|
||||
case "datetime-seconds":
|
||||
return iso.slice(0, 19).replace("T", " ");
|
||||
case "datetime":
|
||||
default:
|
||||
return iso.slice(0, 16).replace("T", " ");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user