"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { CountBadge } from "@suphark/ui/components/shared/count-badge"; import { EmptyState } from "@suphark/ui/components/shared/empty-state"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle, } from "@suphark/ui/components/ui/card"; import { useMediaQuery } from "@suphark/ui/hooks/use-media-query"; import { useMounted } from "@suphark/ui/hooks/use-mounted"; import { cn } from "@suphark/ui/lib/utils"; import { PlusIcon } from "lucide-react"; import type { ReactNode } from "react"; export interface RecordSectionEmpty { icon?: ReactNode; title: ReactNode; description?: ReactNode; } /** * รายการย่อยในหน้ารายละเอียด (ผู้ติดต่อ, กรรมการ, เอกสารแนบ, ผลงานอ้างอิง…): หัวการ์ด + จำนวน + ปุ่มเพิ่ม * + เนื้อหา (DataTable/SimpleTable/RecordCard ใดก็ได้) + สถานะว่างในตัว * * setOpen(true)} * empty={{ icon: , title: "ยังไม่มีผู้ติดต่อ", description: "…" }}> * * * * - `count === 0` (หรือ `isEmpty`) และมี `empty` → แสดงสถานะว่าง + ปุ่มเพิ่ม แทน children * - จอ < sm: ปุ่มเพิ่มย้ายไปท้ายการ์ดเต็มความกว้าง (แตะง่าย) · `actions` = ปุ่มอื่นข้างปุ่มเพิ่ม * - `addButton` = ส่งปุ่มเอง (เช่น FormDialog trigger) แทน `onAdd` */ export function RecordSection({ title, description, count, addLabel, onAdd, addButton, actions, empty, isEmpty, headingAs: Heading = "h2", children, className, contentClassName, }: { title: ReactNode; description?: ReactNode; count?: number; addLabel?: string; onAdd?: () => void; addButton?: ReactNode; actions?: ReactNode; empty?: RecordSectionEmpty; /** ค่าเริ่มต้น `count === 0` */ isEmpty?: boolean; headingAs?: "h2" | "h3" | "h4"; children?: ReactNode; className?: string; /** ตารางเต็มความกว้างใช้ "px-0" */ contentClassName?: string; }) { const text = useUiText().recordSection; // ปุ่มการกระทำมีสองที่ (หัวการ์ด / แถบล่างบนจอแคบ) — ก่อน mount แสดงทั้งคู่ซ่อนด้วย CSS (SSR ไม่รู้ขนาดจอ) // หลัง mount เหลือที่เดียว: ไม่งั้นปุ่มที่มีปุ่มลัด/id ของแอปถูก mount สองชุด (ปุ่มลัดทำงานซ้ำ, id ซ้ำ) const mounted = useMounted(); const narrow = useMediaQuery("(max-width: 639px)"); const showWide = !mounted || !narrow; const showNarrow = !mounted || narrow; const label = addLabel ?? text?.add ?? "เพิ่ม"; const add = addButton ?? (onAdd ? ( ) : null); const showEmpty = (isEmpty ?? count === 0) && empty !== undefined; return ( {title} {count !== undefined && ( <> {text?.count ?? "จำนวน"} )} {description && {description}} {(add || actions) && showWide && ( {actions} {!showEmpty && add} )} {showEmpty ? ( ) : ( children )} {(actions || (add && !showEmpty)) && showNarrow && (
{actions} {!showEmpty && add}
)}
); }