"use client"; import { ActionMenu } from "@suphark/ui/components/shared/action-menu"; import { Button } from "@suphark/ui/components/shared/button/button"; import { type RecordAccent, RecordCard, type RecordTone, } from "@suphark/ui/components/shared/record-card"; import { StatusBadge, type Tone } from "@suphark/ui/components/shared/status-badge"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { cn } from "@suphark/ui/lib/utils"; import { BellIcon, ChevronRightIcon, MoreHorizontalIcon } from "lucide-react"; import { Fragment, type ReactNode } from "react"; /** ชื่อเดียวกับ `StatusBadge` (`neutral`/`danger`) ได้ และชื่อเดิม (`default`/`destructive`) ยังใช้ได้ */ export type NotificationTone = RecordTone; /** tone ของการแจ้งเตือน → tone ของ `StatusBadge` (ชิปทั้งระบบเป็นชุดเดียว) */ const BADGE_TONE: Record = { neutral: "neutral", default: "neutral", info: "info", success: "success", warning: "warning", danger: "danger", destructive: "danger", }; const ACTION_TONE: Record = { neutral: "", default: "", info: "text-info-subtle-foreground", success: "text-success-subtle-foreground", warning: "text-warning-subtle-foreground", danger: "text-destructive", destructive: "text-destructive", }; const isDanger = (tone: NotificationTone | undefined) => tone === "danger" || tone === "destructive"; export interface NotificationChip { key: string; label: ReactNode; count: number; tone?: NotificationTone; /** กดเพื่อกรอง — ไม่ส่ง = ชิปนับอย่างเดียว */ onClick?: () => void; /** ชื่อเดิม (ยังใช้ได้) — ของใหม่ใช้ `onClick` ให้ตรงกับ ActionMenu */ onSelect?: () => void; active?: boolean; } export interface NotificationAction { key: string; label: string; /** ไม่บังคับเมื่อส่ง `render` */ onClick?: () => void; /** ชื่อเดิม (ยังใช้ได้) — ของใหม่ใช้ `onClick` ให้ตรงกับ ActionMenu */ onSelect?: () => void; icon?: ReactNode; tone?: NotificationTone; /** ปุ่มหลักของแถว (เด่นกว่าตัวอื่น) — ปกติมีตัวเดียว */ primary?: boolean; disabled?: boolean; /** * ปุ่มของแอปเอง (เช่น ``) แทนปุ่มมาตรฐาน — ใช้ได้เฉพาะ `actions` * (เมนู "…" ใช้ `label`/`onSelect`) · แอปคุม disabled ตอน `busy` เอง */ render?: ReactNode; } export interface NotificationEntry { id: string; title: ReactNode; description?: ReactNode; icon?: ReactNode; tone?: NotificationTone; /** * สีประจำแถวแบบสี CSS ตรง ๆ (`"var(--type-reminder)"`) — สำหรับสีที่สื่อ "ประเภทรายการ" ของแอป * (รายรับ/รายจ่าย/เตือน) ไม่ใช่สถานะ · ไม่ส่ง = ตาม `tone` */ accent?: RecordAccent; /** * สถานะ/กำหนดเวลาที่ **แอปฟอร์แมตมาแล้ว** (เช่น `` หรือ "23 ก.ย. · พรุ่งนี้") * แพ็กเกจไม่ฟอร์แมตวันที่เอง เพราะปี พ.ศ./ภาษาเป็นของแอป */ status?: ReactNode; /** บรรทัดเล็กใต้ชื่อ เช่น กลุ่ม · หมวดหมู่ */ meta?: ReactNode; /** ส่วนรายละเอียด (ฟิลด์ของโดเมน) — มีเส้นคั่น เยื้องตรงคอลัมน์ข้อความ */ details?: ReactNode; /** ซ้ายของแถบล่าง — เรื่องเฉพาะโดเมน เช่นยอดเงิน (ซ่อนได้) หรือความคืบหน้ารอบ */ footer?: ReactNode; /** `variant="quiet"`: เน้นแถวที่ต้องจัดการก่อน (พื้นสี accent/tone จาง ๆ) — ปกติใช้แถวเดียว */ highlight?: boolean; /** `variant="quiet"`: แถบความคืบหน้า 0–100 ใต้ยอด (`footer`) */ progress?: number; /** ยังไม่ได้อ่าน — จุดหน้าชื่อ + พื้นจาง */ unread?: boolean; /** หัวข้อกลุ่มคั่น (`variant="quiet"`) — แถวติดกันที่ `section` เดียวกันอยู่ใต้หัวเดียว เช่น "ต้องจัดการก่อน" */ section?: string; /** ปุ่มในแถว (ปุ่มที่ `primary` จะเด่นสุด) */ actions?: ReadonlyArray; /** คำสั่งรองใน "…" */ menuActions?: ReadonlyArray; /** คลิกทั้งแถว (ปุ่มในแถว stopPropagation ให้แล้ว) */ onClick?: () => void; /** ชื่อเดิม (ยังใช้ได้) — ของใหม่ใช้ `onClick` ให้ตรงกับ ActionMenu */ onSelect?: () => void; } export interface NotificationCenterProps { /** * รายการที่จะแสดง — **แพ็กเกจไม่ตัดรายการเอง** ถ้าจะจำกัดจำนวนให้แอป slice มาก่อน * แล้วส่ง `viewAll` เพื่อพาไปหน้ารวม (ตัวเลขใน `counts` จึงตรงกับสิ่งที่ผู้ใช้เห็นเสมอ) */ items: ReadonlyArray; /** ชิปนับด้านบน (`StatusBadge`) — กี่กลุ่มก็ได้ (เกินกำหนด / รอตรวจสอบ / ใกล้ถึงกำหนด …) */ counts?: ReadonlyArray; /** หัวข้อ — ส่ง node ได้ (เช่น label ตัวเล็กตัวพิมพ์ใหญ่ + ไอคอน แบบหัวกลุ่มของแอป) */ title?: ReactNode; /** แท็กของหัวข้อ (ค่าเริ่มต้น h2) */ headingAs?: "h2" | "h3" | "h4" | "p"; /** ทับสไตล์หัวข้อ (ค่าเริ่มต้น `font-semibold text-sm`) */ headingClassName?: string; description?: ReactNode; /** ไม่มีรายการ — ส่ง element เองได้ทั้งก้อน */ empty?: ReactNode; /** * ของแอปที่วางใต้หัว (และใต้แท็บของ `quiet`) ก่อนรายการ เช่นการ์ด "เกินกำหนด N รายการ" — แสดงแม้ `items` ว่าง * (อยู่เหนือ `empty`) · อยู่ใน `
` เดียวกันจึงเลื่อนไปพร้อมรายการ · ไม่มีกรอบ/ระยะเพิ่ม แอปจัดสไตล์ node เอง */ beforeList?: ReactNode; /** ปุ่มคำสั่งของทั้งกล่องที่หัว เช่น "อ่านทั้งหมดแล้ว" (ซ้ายของ "ดูทั้งหมด") — ไม่ต้องยืม `viewAll` มาใช้ผิดความหมาย */ headerAction?: ReactNode; /** ลิงก์/ปุ่ม "ดูทั้งหมด" มุมขวาบน */ viewAll?: { label?: ReactNode; onClick?: () => void; /** ชื่อเดิม (ยังใช้ได้) — ของใหม่ใช้ `onClick` ให้ตรงกับ ActionMenu */ onSelect?: () => void; }; /** กำลังทำคำสั่งลัดอยู่ — ปิดปุ่มทั้งหมดกันกดซ้ำ */ busy?: boolean; /** ชื่อเดียวกับ `busy` (ให้ตรงกับ Button/ActionButton) */ isLoading?: boolean; /** * `card` (ค่าเริ่มต้น) = ชิปนับ + การ์ดแยกใบ · `quiet` = "Quiet ledger": ชิปนับเป็นแท็บขีดเส้นใต้ (ตัวเลข tone danger เป็นสีแดง) * รายการเป็นแถวเรียบในกรอบเดียว (`RecordCard variant="quiet"`) มีหัวกลุ่มคั่นด้วย `section` — เห็นได้มากกว่าเท่าตัว */ variant?: "card" | "quiet"; /** * เรนเดอร์แถวเอง (การ์ดของแอป) — ปกติไม่ต้องใช้: แถวมาตรฐานคือ `RecordCard` ตัวเดียวกับที่แอปใช้ทำรายการ * อยู่แล้ว · คืน node ที่จะอยู่ใน `
  • ` */ renderItem?: (item: NotificationEntry, context: { busy: boolean }) => ReactNode; className?: string; } /** * ศูนย์การแจ้งเตือน: หัวข้อ + ชิปนับ (`StatusBadge`) + รายการ (`RecordCard`) พร้อมปุ่มลัดต่อรายการ (ยกจาก DueBell) * ทุกอย่างที่เป็นของโดเมน — วันที่/สถานะ/ยอดเงิน/ความคืบหน้า — ส่งเข้ามาเป็น node ที่แอปเตรียมแล้ว * วางในหน้าได้ตรง ๆ หรือใส่ใน `SimpleDialog`/`SimpleSheet` เป็นกล่องแจ้งเตือนก็ได้ * * ({ id: b.id, title: b.title, accent: typeAccent(b.type), status: , * footer: , actions: [{ key: "pay", label: "จ่ายเลย", primary: true, onSelect: … }] }))} * viewAll={{ onSelect: () => router.push("/alerts") }} /> */ export function NotificationCenter({ items, counts, title, headingAs: Heading = "h2", headingClassName, description, empty, viewAll, headerAction, beforeList, busy: busyProp, isLoading, variant = "card", renderItem, className, }: NotificationCenterProps) { const busy = busyProp ?? isLoading ?? false; const quiet = variant === "quiet"; const text = useUiText(); const heading = title ?? text.notifications?.title ?? "การแจ้งเตือน"; return (
    {heading} {description &&

    {description}

    } {counts && counts.length > 0 && !quiet && (
    {counts.map((chip) => ( ))}
    )}
    {(headerAction || viewAll) && (
    {headerAction} {viewAll && ( )}
    )}
    {quiet && counts && counts.length > 0 && } {beforeList} {items.length === 0 ? ( (empty ?? (
    )) ) : (
      {items.map((item, index) => ( {quiet && item.section && item.section !== items[index - 1]?.section && (
    • {item.section}
    • )} {renderItem ? (
    • {renderItem(item, { busy })}
    • ) : ( )}
      ))}
    )}
    ); } function CountChip({ chip }: { chip: NotificationChip }) { const content = ( <> {chip.label} {chip.count} ); const tone = BADGE_TONE[chip.tone ?? "neutral"]; const onChipClick = chip.onClick ?? chip.onSelect; if (!onChipClick) return {content}; return ( } className={cn( "relative cursor-pointer transition-[filter] after:absolute after:-inset-y-3 after:inset-x-0 after:content-[''] hover:brightness-95", chip.active && "ring-2 ring-ring/40", )} > {content} ); } /** แท็บนับแบบขีดเส้นใต้ (`variant="quiet"`) — ตัวเลขของ tone danger เป็นสีแดงตัวหนา */ function QuietTabs({ counts }: { counts: ReadonlyArray }) { return (
    {counts.map((chip) => { const body = ( <> {chip.label} {chip.count} ); const tab = cn( "-mb-px border-b-2 px-0.5 pt-2 pb-2.5 text-sm pointer-coarse:py-3", chip.active ? "border-primary font-semibold" : "border-transparent font-medium text-muted-foreground", ); const onChipClick = chip.onClick ?? chip.onSelect; return onChipClick ? ( ) : ( {body} ); })}
    ); } function NotificationRow({ item, busy, quiet, }: { item: NotificationEntry; busy: boolean; quiet: boolean; }) { const text = useUiText(); const hasActions = Boolean(item.actions?.length || item.menuActions?.length); return ( {item.actions?.map((action) => action.render != null ? ( {action.render} ) : ( ), )} {item.menuActions && item.menuActions.length > 0 && ( } items={item.menuActions.map((action) => ({ label: action.label, icon: action.icon, destructive: isDanger(action.tone), disabled: busy || action.disabled, onClick: () => (action.onClick ?? action.onSelect)?.(), }))} /> )} ) : undefined } /> ); }