"use client"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, } from "@suphark/ui/components/ui/breadcrumb"; import { XIcon } from "lucide-react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { Fragment, type ReactNode } from "react"; import { useBreadcrumbLabels } from "./breadcrumb-labels"; export interface HeaderBreadcrumbsProps { /** * ชื่อแสดงของแต่ละ segment: map (`{ "po-tracking": "PO Tracking" }`) หรือฟังก์ชัน * `(segment, href) => ReactNode | undefined` (เช่น t() ของ next-intl) * คืน undefined/ไม่พบ = ตัวแรกพิมพ์ใหญ่ แทน - ด้วยช่องว่าง * * ระวัง: ฟังก์ชันส่งข้าม server→client boundary ไม่ได้ ("Functions cannot be passed directly to Client Components") * ถ้า layout เป็น server component ให้ใช้ map หรือส่ง `breadcrumbs: ` ที่เป็น client component เรียก HeaderBreadcrumbs เอง */ labels?: Record | ((segment: string, href: string) => ReactNode | undefined); homeHref?: string; homeLabel?: string; /** ตัด segment นำหน้าออก เช่น `["console"]` เมื่อทุกหน้าอยู่ใต้ /console */ skip?: string[]; /** ซ่อน segment ที่ดูเหมือน id (ตัวเลข/uuid/cuid) */ hideIds?: boolean; /** * แสดง segment ที่ดูเหมือน id ด้วยคำนี้แทนรหัสดิบ (เช่น `"รายละเอียด"`) — ชื่อจริงของระเบียนอยู่ในหัวหน้าแล้ว * (เดิมขึ้น "Cmqsv5w71000f04l7t7bat8d5") · ใช้แทน `hideIds` เมื่อยังอยากให้มีลำดับชั้นครบ */ idLabel?: ReactNode; /** * ลิงก์ของ segment ที่ไม่มีหน้าของตัวเอง (กดแล้ว 404) — map ตาม path (`{ "/reports": "/reports/evaluations" }`) * หรือฟังก์ชัน `(href, segments) => string | undefined` */ hrefs?: Record | ((href: string, segments: string[]) => string | undefined); /** * ปุ่ม X ท้าย breadcrumb ถอยขึ้นหนึ่งระดับ (แบบ Vercel) — ไปที่ segment ก่อนหน้า หรือ `homeHref` ถ้ามี segment เดียว * ไม่แสดงในหน้าแรก (ไม่มี segment) · ปิดไว้เป็นค่าเริ่มต้น จึงไม่เปลี่ยนแอปเดิม */ upButton?: boolean; /** ข้อความของปุ่ม X (aria-label + tooltip) — ค่าเริ่มต้น UiText `breadcrumb.back` ("ย้อนกลับ") */ upLabel?: string; } // ตัวเลขล้วน / uuid / cuid / ตัวอักษร+ตัวเลขปน ≥16 ตัว (ต้องมีตัวเลขอย่างน้อย 1 — คำธรรมดายาว ๆ ไม่ใช่ id) const ID_LIKE = /^(\d+|[0-9a-f]{8}-[0-9a-f-]{27}|c[a-z0-9]{20,}|(?=[a-z]*\d)[0-9a-z]{16,})$/i; /** path ที่มี `%` เดี่ยว (เช่น `/files/100%`) ถอดรหัสไม่ได้ — ใช้ข้อความเดิมแทนการ throw ทั้ง layout */ function safeDecode(segment: string): string { try { return decodeURIComponent(segment); } catch { return segment; } } function labelKnown(segment: string, href: string, labels?: HeaderBreadcrumbsProps["labels"]) { const known = typeof labels === "function" ? labels(segment, href) : labels?.[segment]; return known !== undefined && known !== null && known !== ""; } function labelOf(segment: string, href: string, labels?: HeaderBreadcrumbsProps["labels"]) { const known = typeof labels === "function" ? labels(segment, href) : labels?.[segment]; if (known !== undefined && known !== null && known !== "") return known; const decoded = safeDecode(segment); return decoded.charAt(0).toUpperCase() + decoded.slice(1).replace(/-/g, " "); } /** breadcrumb อัตโนมัติจาก pathname (ยกจาก supplychain HeaderBreadcrumbs) */ export function HeaderBreadcrumbs({ labels, homeHref = "/", homeLabel: homeLabelProp, skip = [], hideIds = false, idLabel, hrefs, upButton = false, upLabel: upLabelProp, }: HeaderBreadcrumbsProps) { const text = useUiText().breadcrumb; // ข้อความตามภาษาของแอป (เดิม "Home"/"Back" อังกฤษตายตัว) const homeLabel = homeLabelProp ?? text?.home ?? "หน้าแรก"; const upLabel = upLabelProp ?? text?.back ?? "ย้อนกลับ"; const pathname = usePathname(); // ชื่อที่หน้าประกาศด้วย `BreadcrumbLabel` ชนะ `labels` ของ layout (ตรงกับข้อมูลจริงของหน้า) const declared = useBreadcrumbLabels(); const labelsOf = (segment: string, href: string) => declared[href] ?? (typeof labels === "function" ? labels(segment, href) : labels?.[segment]); const all = pathname.split("/").filter(Boolean); // segments ที่ยังคง href เต็ม แต่ไม่แสดงตัวที่อยู่ใน skip / เป็น id const shown = all .map((segment, index) => ({ segment, href: `/${all.slice(0, index + 1).join("/")}` })) .filter( ({ segment, href }) => !skip.includes(segment) && !(hideIds && ID_LIKE.test(segment) && !declared[href]), ); const fullPath = `/${all.join("/")}`; const linkOf = (href: string) => (typeof hrefs === "function" ? hrefs(href, all) : hrefs?.[href]) ?? href; const upHref = shown.length >= 2 ? (shown[shown.length - 2]?.href ?? homeHref) : homeHref; return ( }>{homeLabel} {shown.length > 0 && } {shown.map(({ segment, href }, index) => { // หน้าปัจจุบัน = segment สุดท้ายของ path จริง — ไม่ใช่ตัวสุดท้ายที่แสดง (hideIds ซ่อน id ท้าย → "Orders" ยังต้องกดกลับได้) const isLast = index === shown.length - 1 && href === fullPath; const label = idLabel !== undefined && ID_LIKE.test(segment) && !labelKnown(segment, href, labelsOf) ? idLabel : labelOf(segment, href, labelsOf); return ( {isLast ? ( {label} ) : ( }>{label} )} {/* มือถือซ่อนแค่ "หน้าแรก" — ตัวคั่นระหว่าง segment ต้องมี ไม่งั้นชื่อติดกัน ("OrdersPO-123") */} {index < shown.length - 1 && } ); })} {upButton && shown.length > 0 && ( )} ); }