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
@@ -1,4 +1,5 @@
|
||||
import type { Metadata } from "next";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
@@ -45,9 +46,6 @@ const REACTIONS: { key: string; emoji: string }[] = [
|
||||
{ key: "wow", emoji: "😮" },
|
||||
];
|
||||
|
||||
function formatDate(d: Date | null | undefined): string {
|
||||
return d ? d.toISOString().slice(0, 10) : "";
|
||||
}
|
||||
|
||||
export default async function ArticlePage({
|
||||
params,
|
||||
@@ -262,7 +260,7 @@ export default async function ArticlePage({
|
||||
<span>{t("userNumber", { id: c.userId })}</span>
|
||||
)}
|
||||
{c.createdAt ? (
|
||||
<span> · {formatDate(c.createdAt)}</span>
|
||||
<span> · {formatDate(c.createdAt, "date", "")}</span>
|
||||
) : null}
|
||||
</p>
|
||||
<p style={{ margin: 0, whiteSpace: "pre-wrap" }}>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import Image from "next/image";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
@@ -9,9 +10,6 @@ export const revalidate = 300;
|
||||
|
||||
export const metadata = { title: "News" };
|
||||
|
||||
function formatDate(d: Date | null): string {
|
||||
return d ? d.toISOString().slice(0, 10) : "";
|
||||
}
|
||||
|
||||
export default async function NewsPage() {
|
||||
const t = await getTranslations("pages.news");
|
||||
@@ -99,7 +97,7 @@ export default async function NewsPage() {
|
||||
className="muted"
|
||||
style={{ margin: "0.35rem 0 0.6rem", fontSize: "0.78rem" }}
|
||||
>
|
||||
{formatDate(a.createdAt)}
|
||||
{formatDate(a.createdAt, "date", "")}
|
||||
</p>
|
||||
<p
|
||||
style={{
|
||||
|
||||
Reference in new issue
Block a user