"use client"; import { ActionMenu, type ActionMenuCommand, type ActionMenuItem, } from "@suphark/ui/components/shared/action-menu"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiDefaults } from "@suphark/ui/components/shared/ui-defaults"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, } from "@suphark/ui/components/ui/breadcrumb"; import { buttonVariants } from "@suphark/ui/components/ui/button"; import { useMediaQuery } from "@suphark/ui/hooks/use-media-query"; import { useMounted } from "@suphark/ui/hooks/use-mounted"; import { errorMessage, failedResult, isNavigationError } from "@suphark/ui/lib/action-result"; import { getNumberFormat } from "@suphark/ui/lib/intl-cache"; import { cn } from "@suphark/ui/lib/utils"; import { ArrowLeftIcon, MoreHorizontal } from "lucide-react"; import { type ElementType, Fragment, type ReactNode, useState } from "react"; import { toast } from "sonner"; export interface DocumentHeaderCrumb { label: ReactNode; /** ไม่ส่ง = หน้าปัจจุบัน (ไม่เป็นลิงก์) */ href?: string; } export interface DocumentHeaderMeta { label: ReactNode; value: ReactNode; /** ซ่อนบนจอ < sm (ข้อมูลรองที่ไม่จำเป็นบนมือถือ) */ hideOnMobile?: boolean; } export interface DocumentHeaderTotal { label: ReactNode; /** ตัวเลข = จัดรูปแบบด้วย `format` (ค่าเริ่มต้น ฿ + ทศนิยม 2 ตำแหน่ง) · ReactNode = แสดงตามที่ส่ง */ value: number | ReactNode; /** สีของค่า เช่น ต่ำกว่าราคากลาง = success */ tone?: "default" | "success" | "danger" | "muted"; } /** ปุ่มรองข้างปุ่มหลัก (จอ ≥ md) — จอ < md ย้ายเข้าเมนู ⋯ ให้เอง (ทั้งหัวปกติและแถบล่างของ `stickyActionsOnMobile`) */ export type DocumentHeaderAction = Omit; /** ฿ + ทศนิยม 2 ตำแหน่ง · ติดลบ = −฿1,000.00 (ลบหน้าสกุลเงิน แบบเดียวกับ AmountSummary) · -0/-0.004 = ฿0.00 */ const defaultFormat = (n: number) => { const text = getNumberFormat("th-TH", { minimumFractionDigits: 2, maximumFractionDigits: 2, }).format(Math.abs(n)); return `${n < 0 && /[1-9]/.test(text) ? "−" : ""}฿${text}`; }; const TOTAL_TONE: Record, string> = { default: "", success: "text-success", danger: "text-destructive", muted: "text-muted-foreground", }; /** * หัวหน้ารายละเอียดเอกสาร (ใบขอซื้อ, ใบสั่งซื้อ, งานประกวดราคา ฯลฯ): breadcrumb, ปุ่มกลับ, เลขเอกสาร, ชื่อ, * สถานะ, ข้อมูลย่อ, ยอดรวม, ปุ่มหลัก + ปุ่มรอง + เมนู ⋯ — หัวหน้าทั่วไปที่มีแค่ชื่อ/ปุ่มใช้ `PageHeader` * * รอเปรียบเทียบราคา} * meta={[{ label: "ผู้รับผิดชอบ", value: "วิภา ส." }, { label: "ปิดรับซอง", value: "31 ต.ค. 2569" }]} * totals={[{ label: "ราคากลาง", value: 4850000 }, { label: "ต่ำสุด", value: 4512300 }]} * primaryAction={} * secondaryActions={[{ label: "ส่งออก Excel", icon: , onClick: exportXlsx }]} * actions={[{ label: "ยกเลิกงาน", destructive: true, confirm: "ยกเลิกงานนี้?", onClick: cancel }]} * stickyActionsOnMobile * /> * * `stickyActionsOnMobile` = จอ < md ปุ่มหลัก + ⋯ ไปอยู่แถบล่าง (สูง 44px, พ้น `MobileTabBar`, เว้นที่ท้ายหน้าให้เอง) * แอป Next ส่ง `linkComponent={Link}` (barrel ไม่พึ่ง next/link) */ export function DocumentHeader({ breadcrumb, backHref, backLabel, number, title, status, meta, totals, format = defaultFormat, primaryAction, secondaryActions, actions, stickyActionsOnMobile = false, linkComponent: linkComponentProp, headingAs: Heading = "h1", children, className, }: { breadcrumb?: ReadonlyArray; /** ปุ่มลูกศรกลับหน้ารายการ (จอ ≥ sm) */ backHref?: string; /** aria-label ของปุ่มกลับ (ค่าเริ่มต้น UiText.documentHeader.back) */ backLabel?: string; /** เลขเอกสาร (ตัว mono เล็กเหนือชื่อ) */ number?: ReactNode; title: ReactNode; /** ป้ายสถานะ (StatusBadge) ข้างชื่อ */ status?: ReactNode; meta?: ReadonlyArray; totals?: ReadonlyArray; /** จัดรูปแบบ `totals[].value` ที่เป็นตัวเลข */ format?: (value: number) => string; /** ปุ่มหลัก 1 ปุ่ม (Button / ConfirmButton / SubmitButton…) */ primaryAction?: ReactNode; secondaryActions?: ReadonlyArray; /** เมนู ⋯ (ActionMenu items — รองรับ confirm/destructive/separator) */ actions?: ReadonlyArray; stickyActionsOnMobile?: boolean; linkComponent?: ElementType; headingAs?: "h1" | "h2"; /** เนื้อหาเพิ่มใต้หัว เช่น `DocumentFlow` หรือ `Notice` */ children?: ReactNode; className?: string; }) { const defaultLink = useUiDefaults().linkComponent; const LinkComponent = linkComponentProp ?? defaultLink ?? "a"; const text = useUiText(); const t = text.documentHeader; // ปุ่มหลัก/เมนูมีสองที่เมื่อ stickyActionsOnMobile (หัว / แถบล่างจอ < md) — ก่อน mount แสดงทั้งคู่ซ่อนด้วย CSS // หลัง mount เหลือที่เดียว: ไม่งั้น primaryAction ของแอปถูก mount สองชุด (ปุ่มลัดทำงานซ้ำ, id ซ้ำ) const mounted = useMounted(); const narrow = useMediaQuery("(max-width: 767px)"); const showWide = !stickyActionsOnMobile || !mounted || !narrow; const showNarrow = !mounted || narrow; const secondary = secondaryActions ?? []; const menu = actions ?? []; // จอ < md (หัวปกติและแถบล่าง): ปุ่มรองรวมเข้าเมนู ⋯ ก่อนคำสั่งอื่น const mobileMenu: ActionMenuItem[] = [ ...secondary, ...(secondary.length > 0 && menu.length > 0 ? [{ type: "separator" as const }] : []), ...menu, ]; const hasDesktopActions = Boolean(primaryAction) || secondary.length > 0 || menu.length > 0; return (
{breadcrumb && breadcrumb.length > 0 && ( {breadcrumb.map((crumb, i) => ( // biome-ignore lint/suspicious/noArrayIndexKey: ลำดับ breadcrumb คงที่ — label เป็น ReactNode {i > 0 && } {crumb.href ? ( }> {crumb.label} ) : ( {crumb.label} )} ))} )}
{backHref && ( )} {/* basis-48: ชื่อกว้างอย่างน้อย 12rem — ไม่พอ = กล่องปุ่มขึ้นบรรทัดใหม่ แทนการบีบชื่อเหลือคำละบรรทัด */}
{number && ( {number} )}
{title} {status}
{hasDesktopActions && showWide && (
{/* จอ < md ปุ่มรองย้ายเข้าเมนู ⋯ (ด้วย CSS — ไม่กระพริบตอน hydrate) ชื่อเอกสารจึงไม่ถูกบีบ */} {secondary.map((action, i) => ( // biome-ignore lint/suspicious/noArrayIndexKey: ลำดับปุ่มคงที่ — label เป็น ReactNode ))} {primaryAction} {secondary.length > 0 ? ( <> {menu.length > 0 && ( )} ) : ( menu.length > 0 && )}
)}
{meta && meta.length > 0 && (
{meta.map((item, i) => (
{item.label}
{item.value}
))}
)} {totals && totals.length > 0 && (
{totals.map((item, i) => ( // biome-ignore lint/suspicious/noArrayIndexKey: ลำดับยอดคงที่ตามที่แอปส่งมา
{item.label}
{typeof item.value === "number" ? format(item.value) : item.value}
))}
)} {children} {stickyActionsOnMobile && showNarrow && (primaryAction || mobileMenu.length > 0) && (
{primaryAction && (
{primaryAction}
)} {mobileMenu.length > 0 && ( } /> )}
)}
); } /** ปุ่มรองบนจอกว้าง: onClick (รอ async + toast เมื่อพลาด) หรือ href */ function InlineAction({ action, linkComponent: LinkComponent, }: { action: DocumentHeaderAction; linkComponent: ElementType; }) { const text = useUiText(); const [pending, setPending] = useState(false); if (action.href && !action.disabled) { const Link = action.external ? "a" : LinkComponent; return ( {action.icon} {action.label} ); } const fallback = text.actionFailed ?? "ดำเนินการไม่สำเร็จ"; return ( ); }