"use client"; import { StatusBadge, type Tone } from "@suphark/ui/components/shared/status-badge"; import { ClientDateFormatter } from "@suphark/ui/components/shared/time/client-date-formatter"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { UserAvatars } from "@suphark/ui/components/shared/user-avatars"; import { cn } from "@suphark/ui/lib/utils"; import { CheckIcon, XIcon } from "lucide-react"; import type { ReactNode } from "react"; export type ApprovalStepStatus = "done" | "current" | "rejected" | "skipped" | "pending"; export interface ApprovalApprover { id?: string; name: string; src?: string | null; /** คนนี้อนุมัติแล้ว (ขั้นที่ต้องอนุมัติหลายคน) */ approved?: boolean; /** คนนี้ไม่อนุมัติ */ rejected?: boolean; } export interface ApprovalStep { id: string; title: ReactNode; status: ApprovalStepStatus; approvers?: ReadonlyArray; /** `all` = ทุกคนต้องอนุมัติ · `any` = คนใดคนหนึ่ง · ReactNode = ป้ายเงื่อนไขเอง */ policy?: "all" | "any" | ReactNode; /** เวลาที่ขั้นนี้จบ (อนุมัติ/ไม่อนุมัติ) */ at?: Date | string | number | null; /** ขั้นที่กำลังรอ: เริ่มรอเมื่อไหร่ (แสดงแบบสัมพัทธ์) */ waitingSince?: Date | string | number | null; /** เหตุผลที่ข้าม เช่น "ยอดต่ำกว่า 5 ล้าน" */ reason?: ReactNode; /** ความเห็นของผู้อนุมัติ/ผู้ปฏิเสธ */ comment?: ReactNode; /** เนื้อหาเพิ่มท้ายขั้น (ปุ่ม อนุมัติ/ไม่อนุมัติ ของผู้ใช้ปัจจุบัน ฯลฯ) */ extra?: ReactNode; } const STATUS_TONE: Record = { done: "success", current: "info", rejected: "danger", skipped: "neutral", pending: "neutral", }; /** * ลำดับการอนุมัติแนวตั้ง: แต่ละขั้นมีสถานะ (เสร็จ/กำลังรอ/ไม่อนุมัติ/ข้าม/ยังไม่ถึง), ผู้อนุมัติหลายคน, * เงื่อนไข "ทุกคน/คนใดคนหนึ่ง", เวลา และความเห็น — ไม่ใช่ตัวนำทาง (ใช้ `Stepper` ถ้าแต่ละขั้นเป็นหน้า) * * * * `density="compact"` = ไม่มี avatar/ป้ายเงื่อนไข สำหรับ popover หรือการ์ดแคบ */ export function ApprovalTimeline({ steps, density = "default", locale, className, "aria-label": ariaLabel, }: { steps: ReadonlyArray; density?: "default" | "compact"; /** ภาษาของวันที่/เวลา (ค่าเริ่มต้น `th-TH` พ.ศ.) — แอปสองภาษาส่งตามผู้ใช้ เหมือน `ActivityFeed locale` */ locale?: string; className?: string; "aria-label"?: string; }) { const text = useUiText().approval; const compact = density === "compact"; const statusLabel: Record = { done: text?.done ?? "อนุมัติแล้ว", current: text?.current ?? "กำลังรอ", rejected: text?.rejected ?? "ไม่อนุมัติ", skipped: text?.skipped ?? "ข้าม", pending: text?.pending ?? "ยังไม่ถึง", }; return (
    {steps.map((step, index) => { const last = index === steps.length - 1; const approvers = step.approvers ?? []; const approvedCount = approvers.filter((a) => a.approved).length; const showProgress = step.status === "current" && approvers.length > 1; const policy = step.policy === "all" ? (text?.policyAll ?? "ทุกคนต้องอนุมัติ") : step.policy === "any" ? (text?.policyAny ?? "คนใดคนหนึ่ง") : step.policy; return (
  1. {!last && ( )}
    {step.title} {(step.status !== "pending" || !compact) && ( {statusLabel[step.status]} {showProgress && ` · ${(text?.progress ?? "{approved} จาก {total}") .replace("{approved}", String(approvedCount)) .replace("{total}", String(approvers.length))}`} {step.status === "skipped" && step.reason ? <> — {step.reason} : null} )} {!compact && policy && step.status !== "skipped" && ( {policy} )} {(step.at || step.waitingSince) && ( {!step.at && (text?.waitingSince ?? "รอตั้งแต่")} )}
    {approvers.length > 0 && (
    {/* ชื่อทุกคนอยู่ในรายการข้าง ๆ แล้ว — ซ่อนกลุ่มรูปจากโปรแกรมอ่านจอ ไม่ให้อ่านชื่อซ้ำสองรอบ */} {!compact && ( )} {approvers.map((a, i) => ( {i > 0 && " · "} {a.name} {a.approved && ( )} {a.rejected && ( )} ))}
    )} {step.comment && (
    {step.comment}
    )} {step.extra}
  2. ); })}
); } function StepDot({ status, index }: { status: ApprovalStepStatus; index: number }) { return ( {status === "done" ? : status === "rejected" ? : index + 1} ); }