"use client"; import { useMounted } from "@suphark/ui/hooks/use-mounted"; import { useTicker } from "@suphark/ui/hooks/use-ticker"; import { getDateTimeFormat } from "@suphark/ui/lib/intl-cache"; import { formatDistance, type Locale } from "date-fns"; import { enUS } from "date-fns/locale/en-US"; import { th } from "date-fns/locale/th"; import { useMemo } from "react"; interface ClientDateFormatterProps { /** Date, ISO string หรือ timestamp */ date: Date | string | number | null | undefined; /** `standard` = วันที่/เวลาตาม Intl (ค่าเริ่มต้น), `relative` = "x นาทีที่แล้ว" */ formatType?: "standard" | "relative"; /** ตัวเลือก Intl.DateTimeFormat (เฉพาะ standard) — ค่าเริ่มต้น { day: 'numeric', month: 'short', year: 'numeric' } */ options?: Intl.DateTimeFormatOptions; /** ค่าเริ่มต้น th-TH (relative ใช้ date-fns locale th/enUS ตามตัวหน้า) */ locale?: string; /** โซนเวลา (เฉพาะ standard) ค่าเริ่มต้น Asia/Bangkok */ timeZone?: string; /** ข้อความก่อน mount / วันที่ไม่ถูกต้อง ค่าเริ่มต้น "-" */ placeholder?: string; className?: string; /** relative: คำนวณใหม่ทุก N ms (ค่าเริ่มต้น 60000) — 0/undefined = ไม่อัปเดตเอง; ทุก instance แชร์ timer เดียว */ updateInterval?: number; } const getLocaleObject = (localeString: string): Locale => localeString.toLowerCase().split("-")[0] === "th" ? th : enUS; const DEFAULT_OPTIONS: Intl.DateTimeFormatOptions = { day: "numeric", month: "short", year: "numeric", }; /** * แสดงวันที่ฝั่ง client หลัง mount เท่านั้น (กัน hydration mismatch จากโซนเวลา/"ตอนนี้" ของ server) * ก่อน mount และเมื่อวันที่ไม่ถูกต้องแสดง `placeholder` */ export function ClientDateFormatter({ date, formatType = "standard", options: userOptions, locale = "th-TH", timeZone = "Asia/Bangkok", placeholder = "-", className, updateInterval = 60000, }: ClientDateFormatterProps) { const mounted = useMounted(); // relative เท่านั้นที่ต้อง tick — standard ไม่ขึ้นกับ "ตอนนี้" const tick = useTicker(mounted && formatType === "relative" ? updateInterval : 0); // key ของ options สำหรับ cache/memo (object literal จากผู้เรียกเปลี่ยน identity ทุก render) const optionsKey = userOptions ? JSON.stringify(userOptions) : ""; // "ตอนนี้" ผูกกับ tick — relative จึงคำนวณใหม่ตาม interval (ไม่ใช่ทุก render) // biome-ignore lint/correctness/useExhaustiveDependencies: tick คือสัญญาณให้อ่านเวลาใหม่ const now = useMemo(() => new Date(), [tick]); const text = useMemo(() => { if (!mounted || date == null) return placeholder; const dateObj = new Date(date); if (Number.isNaN(dateObj.getTime())) return placeholder; try { if (formatType === "relative") { return formatDistance(dateObj, now, { addSuffix: true, locale: getLocaleObject(locale) }); } const user: Intl.DateTimeFormatOptions = optionsKey ? JSON.parse(optionsKey) : {}; // Intl ห้าม dateStyle/timeStyle ปนกับ field รายตัว (day/month/year/hour…) — โยน TypeError // ผู้เรียกที่ใช้สไตล์จึงต้องไม่ถูกยัด DEFAULT_OPTIONS (day/month/year) เข้าไปด้วย const usesStyle = user.dateStyle !== undefined || user.timeStyle !== undefined; const opts: Intl.DateTimeFormatOptions = { ...(usesStyle ? {} : DEFAULT_OPTIONS), timeZone, ...user, }; return getDateTimeFormat(locale, opts).format(dateObj); } catch (error) { console.error("ClientDateFormatter:", error); return placeholder; } }, [mounted, date, formatType, locale, timeZone, optionsKey, placeholder, now]); return {text}; }