"use client"; import { Initials } from "@suphark/ui/components/shared/initials"; import { TableErrorState } from "@suphark/ui/components/shared/table-states"; import { ClientDateFormatter } from "@suphark/ui/components/shared/time/client-date-formatter"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { toDateKey } from "@suphark/ui/lib/formatters"; import { getDateTimeFormat } from "@suphark/ui/lib/intl-cache"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronDownIcon } from "lucide-react"; import { Fragment, type ReactNode, useEffect, useId, useState } from "react"; /** `danger`/`neutral` = ชื่อเดียวกับ `destructive`/`default` (ให้ตรงกับ StatusBadge/Notice/Banner) */ export type ActivityTone = | "default" | "info" | "success" | "warning" | "destructive" | "danger" | "neutral"; export interface ActivityItem { id: string; /** สิ่งที่เกิดขึ้น เช่น "แก้ไขสิทธิ์ของสมชาย" */ title: ReactNode; /** รายละเอียดเพิ่ม (ก่อน → หลัง, เหตุผล, IP) */ description?: ReactNode; /** ชื่อผู้ทำ — ไม่ส่ง = เหตุการณ์ของระบบ */ actor?: string | null; /** รูปผู้ทำ (ไม่มี = ตัวย่อชื่อ) */ actorSrc?: string | null; at: Date | string | number; /** ไอคอนแทน avatar (เหตุการณ์ของระบบ เช่น ``) */ icon?: ReactNode; tone?: ActivityTone; /** มุมขวาของแถว เช่น `` หรือปุ่มดูรายละเอียด */ action?: ReactNode; /** รายละเอียดที่พับไว้ (ขยายด้วยปุ่ม "ดูรายละเอียด") เช่น ค่าก่อน/หลัง, ไฟล์แนบ, payload */ details?: ReactNode; } const DOT_TONE: Record = { default: "bg-muted text-muted-foreground", info: "bg-info-subtle text-info-subtle-foreground", success: "bg-success-subtle text-success-subtle-foreground", warning: "bg-warning-subtle text-warning-subtle-foreground", destructive: "bg-destructive-subtle text-destructive-subtle-foreground", neutral: "bg-muted text-muted-foreground", danger: "bg-destructive-subtle text-destructive-subtle-foreground", }; /** * เส้นเวลา "ใครทำอะไรเมื่อไหร่" จาก array เดียว — audit log (Pi Auth), ประวัติงาน (Pi Done), timeline ของเอกสาร * เวลาคำนวณฝั่ง client หลัง mount (ไม่ทำ hydration mismatch) และอัปเดตเองทุกนาที * รายการที่มี `icon` ใช้ไอคอนแทน avatar; ไม่มีทั้งคู่ = ตัวย่อชื่อผู้ทำ * * */ export function ActivityFeed({ items, relative = true, locale, emptyLabel, empty, className, groupByDate = false, density = "comfortable", isLoading = false, error = false, onRetry, }: { items: ReadonlyArray; /** true = "5 นาทีที่แล้ว" (ค่าเริ่มต้น) · false = วันที่/เวลาเต็ม */ relative?: boolean; /** * ภาษาของวันที่ เช่น `"en-US"` หรือ `"th-TH-u-ca-gregory"` — ไม่ส่ง = `th-TH` (พ.ศ.) * แอปสองภาษาต้องส่งตาม locale ของผู้ใช้ ไม่งั้นวันที่จะเป็นไทยสำหรับทุกคน */ locale?: string; /** ไม่มีรายการ (ไม่ส่ง = `UiText.activity.empty`) */ emptyLabel?: ReactNode; /** ชื่อเดียวกับ `emptyLabel` (ให้ตรงกับ SimpleTable/DataList/NotificationCenter) */ empty?: ReactNode; className?: string; /** แทรกหัววัน ("วันนี้" / "เมื่อวาน" / วันที่) เมื่อวันเปลี่ยน — รายการควรเรียงตามเวลาแล้ว · คำนวณหลัง mount (กัน hydration mismatch) */ groupByDate?: boolean; /** compact = จุดเล็ก ไม่มี avatar ระยะชิด (บันทึกยาว ๆ เช่น audit log) */ density?: "comfortable" | "compact"; /** แสดงโครงร่างแทนรายการ */ isLoading?: boolean; /** โหลดไม่สำเร็จ: กล่องผิดพลาด + ปุ่มลองใหม่ (ถ้าส่ง `onRetry`) แทนรายการ */ error?: boolean; onRetry?: () => void; }) { const text = useUiText(); const extra = text.activityExtra; const compact = density === "compact"; const dayKeys = useDayKeys(groupByDate ? items : NO_ITEMS, locale); if (error) return ; if (isLoading) { return (
    {[0, 1, 2].map((i) => (
  1. ))}
); } if (items.length === 0) { return (

{empty ?? emptyLabel ?? text.activity?.empty ?? "ยังไม่มีรายการ"}

); } return (
    {items.map((item, index) => { const tone = item.tone ?? "default"; const last = index === items.length - 1; const day = dayKeys[index]; const newDay = groupByDate && day && day.key !== dayKeys[index - 1]?.key; return ( {newDay && ( // li ธรรมดา (ไม่ใช่ role="presentation") — ลูกของ
      ต้องเป็น listitem ไม่งั้น axe ฟ้อง list
    1. 0 && "pt-1", )} > {day.key === "today" ? (extra?.today ?? "วันนี้") : day.key === "yesterday" ? (extra?.yesterday ?? "เมื่อวาน") : day.label}
    2. )}
    3. {/* เส้นแนวตั้ง + จุด/avatar */}
      {compact ? (

      {item.actor && {item.actor} } {item.title}

      {item.description && (
      {item.description}
      )} {item.details && (
      {item.details}
      )}
      {item.action &&
      {item.action}
      }
    4. ); })}
    ); } /** ค่าคงที่ (ไม่สร้าง array ใหม่ทุก render — กัน effect ของ useDayKeys วนไม่จบ) */ const NO_ITEMS: ReadonlyArray = []; const DOT_SOLID: Record = { default: "bg-muted-foreground/50", info: "bg-info", success: "bg-success", warning: "bg-warning", destructive: "bg-destructive", neutral: "bg-muted-foreground/50", danger: "bg-destructive", }; /** รายละเอียดที่พับไว้ — ปุ่มมี aria-expanded/aria-controls · พื้นที่แตะ 44px บนจอสัมผัส */ function Details({ expand, collapse, children, }: { expand: string; collapse: string; children: ReactNode; }) { const [open, setOpen] = useState(false); const id = useId(); return (
    ); } /** เขตเวลาเดียวกับเวลาที่แสดงในแถว (`ClientDateFormatter` ค่าเริ่มต้น) — หัววันต้องตรงกับเวลาในแถว */ const DAY_TIME_ZONE = "Asia/Bangkok"; const DAY_MS = 24 * 60 * 60 * 1000; /** ค่าคงที่ — setKeys ค่าเดิมแล้ว React ไม่ render ซ้ำ */ const NO_KEYS: Array<{ key: string; label: string } | undefined> = []; /** * คีย์วันของแต่ละรายการ (หลัง mount เท่านั้น — "วันนี้" ขึ้นกับนาฬิกาของผู้ใช้) * จัดกลุ่มตามเวลาไทยเหมือนเวลาที่แสดง (เดิมใช้เขตเวลาของเครื่อง: นอกไทยหัววันกับเวลาในแถวไม่ตรงกัน) * และคำนวณใหม่ตอนข้ามเที่ยงคืน (เปิดหน้าค้างไว้ "วันนี้" ไม่ค้างเป็นของเมื่อวาน) */ function useDayKeys( items: ReadonlyArray, locale?: string, ): Array<{ key: string; label: string } | undefined> { const [keys, setKeys] = useState>(NO_KEYS); const [dayTick, setDayTick] = useState(0); // คีย์ขึ้นกับเวลาของแต่ละแถวเท่านั้น — แอปมักส่ง items={rows.map(…)} ใหม่ทุก render // เทียบด้วยสตริงของเวลาแทนตัว array ไม่งั้น effect (จัดรูปวันที่ทุกแถว + ตั้ง timer ใหม่) รันทุก render ของแม่ (0.98.2) const times = items.map((item) => new Date(item.at).getTime()).join(","); useEffect(() => { // dayTick เปลี่ยนตอนข้ามเที่ยงคืน → คำนวณ "วันนี้/เมื่อวาน" ใหม่ void dayTick; // ไม่จัดกลุ่ม (ได้ NO_ITEMS) / ว่าง = ไม่ต้องคำนวณและไม่ต้องตั้ง timer เที่ยงคืน if (times === "") { setKeys(NO_KEYS); return; } const now = new Date(); const today = toDateKey(now, { timeZone: DAY_TIME_ZONE }); const yesterday = toDateKey(new Date(now.getTime() - DAY_MS), { timeZone: DAY_TIME_ZONE }); const fmt = getDateTimeFormat(locale ?? "th-TH", { dateStyle: "medium", timeZone: DAY_TIME_ZONE, }); setKeys( times.split(",").map((ms) => { const d = new Date(Number(ms)); if (Number.isNaN(d.getTime())) return undefined; const k = toDateKey(d, { timeZone: DAY_TIME_ZONE }); return { key: k === today ? "today" : k === yesterday ? "yesterday" : k, label: fmt.format(d), }; }), ); // เวลาไทยไม่มี DST — เที่ยงคืนถัดไป = จุดที่ (เวลา UTC + 7 ชม.) หารวันลงตัว const sinceMidnight = (now.getTime() + 7 * 60 * 60 * 1000) % DAY_MS; const timer = window.setTimeout(() => setDayTick((t) => t + 1), DAY_MS - sinceMidnight + 1000); return () => window.clearTimeout(timer); }, [times, locale, dayTick]); return keys; }