"use client"; import { ActivityFeed, type ActivityItem } from "@suphark/ui/components/shared/activity-feed"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { errorMessage, failedResult, isNavigationError } from "@suphark/ui/lib/action-result"; import { cn } from "@suphark/ui/lib/utils"; import { RotateCcwIcon } from "lucide-react"; import { type ReactNode, useMemo, useState } from "react"; import { toast } from "sonner"; import { ActionHistory } from "./semantic-icons"; export interface VersionHistoryItem { id: string; at: Date | string | number; /** สิ่งที่เปลี่ยนในรุ่นนี้ เช่น "ปรับส่วนลดท้ายบิล" */ label: ReactNode; /** ผู้บันทึกรุ่นนี้ — ไม่ส่ง = ระบบ */ actor?: string | null; actorSrc?: string | null; /** * ไอคอนแทน avatar (เช่น ``) — ไม่ส่งและไม่มี `actor` = ไอคอนประวัติ * (ไม่ใช่วงตัวย่อ "?" ของคนที่ไม่มีชื่อ — ระบบที่เก็บแค่ id ผู้บันทึก) */ icon?: ReactNode; /** รุ่นที่ใช้อยู่ — มีป้าย "รุ่นปัจจุบัน" และไม่มีปุ่มกู้คืน */ current?: boolean; /** ค่าก่อน/หลัง (`ChangeDiff`) — พับไว้ใต้ปุ่ม "ดูรายละเอียด" */ details?: ReactNode; /** กู้คืนรุ่นนี้ไม่ได้ (เช่น ไฟล์หมดอายุ) */ restorable?: boolean; } export interface VersionHistoryProps { items: ReadonlyArray; /** * กู้คืน — ไม่ส่ง = อ่านอย่างเดียว · throw หรือคืน `{ ok: false, error }` = toast แจ้งและคงแถวยืนยันไว้ให้ลองใหม่ * ถามยืนยันในแถวเอง (ไม่เปิด dialog ซ้อน dialog/sheet ที่แสดงประวัติอยู่) */ onRestore?: (id: string) => unknown; /** * ปุ่มเพิ่มต่อรุ่น (ดาวน์โหลด, ลบรุ่น …) — แสดงต่อจากปุ่มกู้คืน ซ่อนระหว่างถามยืนยันกู้คืนของแถวนั้น * actions={(v) => !v.current && } */ actions?: (item: VersionHistoryItem) => ReactNode; /** จัดกลุ่มตามวัน "วันนี้/เมื่อวาน/วันที่" (ค่าเริ่มต้น true) */ groupByDate?: boolean; locale?: string; isLoading?: boolean; error?: boolean; onRetry?: () => void; /** ไม่มีรุ่นเลย */ empty?: ReactNode; className?: string; } /** * ประวัติรุ่น + ปุ่มกู้คืน (config revisions, files versions, pdf version history, duebell activity log ทำเองคนละแบบ) * ใช้ `ActivityFeed` เป็นฐาน: avatar ผู้บันทึก, จัดกลุ่มตามวัน, รายละเอียดพับได้ * * ({ id: r.id, at: r.createdAt, label: r.note, actor: r.user.name, * current: r.id === currentId, details: }))} onRestore={restoreRevision} /> */ export function VersionHistory({ items, onRestore, actions, groupByDate = true, locale, isLoading, error, onRetry, empty, className, }: VersionHistoryProps) { const text = useUiText(); const t = text.versions; const [confirming, setConfirming] = useState(null); const [pending, setPending] = useState(false); const restore = async (id: string) => { if (!onRestore) return; setPending(true); try { const failed = failedResult(await onRestore(id), text.actionFailed ?? "ดำเนินการไม่สำเร็จ"); if (failed) { toast.error(failed); return; } setConfirming(null); toast.success(t?.restored ?? "กู้คืนแล้ว"); } catch (err) { if (isNavigationError(err)) return; toast.error(errorMessage(err, text.actionFailed ?? "ดำเนินการไม่สำเร็จ")); } finally { setPending(false); } }; // ต้อง memo: ActivityFeed คำนวณวันใหม่ทุกครั้งที่ array เปลี่ยน (array ใหม่ทุก render = วนไม่จบ) // biome-ignore lint/correctness/useExhaustiveDependencies: restore สร้างใหม่ทุก render แต่อ่านแค่ props/state ที่อยู่ใน deps แล้ว const feed = useMemo( () => items.map((item) => { const canRestore = Boolean(onRestore) && !item.current && item.restorable !== false; const asking = confirming === item.id; const extra = asking ? null : actions?.(item); const restoreButton = canRestore && !asking ? ( ) : null; return { id: item.id, at: item.at, actor: item.actor, actorSrc: item.actorSrc, icon: item.icon ?? (item.actor ? undefined : ), details: item.details, title: ( {item.label} {item.current && ( {t?.current ?? "รุ่นปัจจุบัน"} )} ), action: restoreButton || extra ? ( {restoreButton} {extra} ) : undefined, description: asking ? (
{t?.confirm ?? "กู้คืนรุ่นนี้? รุ่นปัจจุบันจะถูกเก็บไว้เป็นรุ่นหนึ่งในประวัติ"}
) : undefined, }; }), [items, onRestore, actions, confirming, pending, t, text.cancel], ); return ( ); }