"use client";
import { useMounted } from "@suphark/ui/hooks/use-mounted";
import { formatRelativeThaiDate, formatThaiFullDate } from "@suphark/ui/lib/formatters";
import { Calendar } from "lucide-react";
interface RelativeDateProps {
date: Date | string | number | null | undefined;
showIcon?: boolean;
className?: string;
iconClassName?: string;
/** ข้อความนำหน้าใน tooltip (ค่าเริ่มต้น "วันที่") เช่น "วันที่อ้างอิงราคา" */
titlePrefix?: string;
/** ข้อความเมื่อไม่มีวันที่ */
emptyText?: string;
}
/**
* วันที่แบบสัมพัทธ์ภาษาไทย ("วันนี้", "เมื่อวาน", "3 วันที่ผ่านมา") + วันที่เต็มใน tooltip
* คำนวณเฉพาะฝั่ง client หลัง mount: "ตอนนี้" ของ server (UTC, module โหลดไว้ก่อน) กับของ browser
* ต่างกันได้ทั้งโซนเวลาและข้ามวัน → ถ้า render บน server จะ hydration mismatch (เจอจริงบน Vercel)
*/
export function RelativeDate({
date,
showIcon = true,
className = "flex items-center gap-0.5 font-medium text-muted-foreground",
iconClassName = "h-2.5 w-2.5 shrink-0 text-muted-foreground/60",
titlePrefix = "วันที่",
emptyText = "ไม่ได้ระบุ",
}: RelativeDateProps) {
const mounted = useMounted();
if (!date) {
return (
{showIcon && }
{emptyText}
);
}
// ก่อน mount แสดงตัวยึดที่ไม่ขึ้นกับเวลา (กัน layout กระโดดน้อยที่สุด)
if (!mounted) {
return (
{showIcon && }
);
}
return (
{showIcon && }
{formatRelativeThaiDate(date)}
);
}