"use client"; import { ConfirmDialog } from "@suphark/ui/components/shared/confirm-dialog"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { Tooltip, TooltipContent, TooltipTrigger } from "@suphark/ui/components/ui/tooltip"; import { useLongPress } from "@suphark/ui/hooks/use-long-press"; import { cn } from "@suphark/ui/lib/utils"; import { PinOff } from "lucide-react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { type ReactNode, useRef, useState } from "react"; import { isNavActive } from "./types"; export interface PinnedLinkItem { href: string; title: string; /** element เช่น `` — ส่งจาก server ได้ (รูปแบบเดียวกับ `AppNavItem.icon`) */ icon?: ReactNode; /** ไฮไลต์เฉพาะ path ตรงตัว (ค่าเริ่มต้น prefix) */ exact?: boolean; } /** * แถบเมนูที่ปักหมุดบน header — คู่กับปุ่ม pin ของ `AppNav` (`AppShell pin`) ใส่ที่ `header.center` * จอกว้าง (lg) แสดงไอคอน + ชื่อ · จอแคบเหลือไอคอน (ชื่อใน tooltip/aria-label) · เลื่อนแนวนอนเมื่อล้น * หน้าปัจจุบันไฮไลต์ · `onUnpin` = ปุ่มเลิกปักหมุดมุมขวาบนเมื่อ hover/โฟกัส (ถามยืนยันก่อน — ปิดด้วย `confirmUnpin={false}`) * จอสัมผัส (ไม่มี hover) ไม่แสดงปุ่มทับไอคอน — กดค้างที่ทางลัดเพื่อเลิกปักหมุด หรือใช้ปุ่ม pin ในเมนูข้าง * ยกจาก supplychain `PinnedMenus` * * ```tsx * const pinned = flattenNav(nav).filter((i) => pinnedHrefs.includes(i.href)); * }} … /> * ``` */ export function PinnedLinks({ items, onUnpin, confirmUnpin = true, className, "aria-label": ariaLabel, }: { items: ReadonlyArray; onUnpin?: (href: string) => void; confirmUnpin?: boolean; className?: string; "aria-label"?: string; }) { const pathname = usePathname(); const text = useUiText().pinnedLinks; // แยกรายการที่ถามกับสถานะเปิด — ปิดแล้วข้อความยังเป็นชื่อเดิมระหว่าง animation (ไม่กะพริบเป็น "") const [confirming, setConfirming] = useState(null); const [confirmOpen, setConfirmOpen] = useState(false); const navRef = useRef(null); // ลิงก์ที่จะรับโฟกัสหลังเลิกปักหมุด — ปุ่มที่เปิดกล่องหายไปพร้อมรายการ โฟกัสจะหลุดไป body const focusAfterUnpin = useRef(null); const neighborOf = (href: string) => { const i = items.findIndex((item) => item.href === href); return (items[i + 1] ?? items[i - 1])?.href ?? null; }; const focusNeighbor = () => { const href = focusAfterUnpin.current; focusAfterUnpin.current = null; if (!href) return null; return ( [...(navRef.current?.querySelectorAll("a[href]") ?? [])].find( (a) => a.getAttribute("href") === href, ) ?? null ); }; if (items.length === 0) return null; const unpinLabel = text?.unpin ?? "เลิกปักหมุด"; const requestUnpin = (item: PinnedLinkItem) => { if (!onUnpin) return; if (!confirmUnpin) { const next = neighborOf(item.href); onUnpin(item.href); // ไม่มีกล่องยืนยัน — ย้ายโฟกัสหลังรายการถูกเอาออก (render รอบถัดไป) requestAnimationFrame(() => { if (next) { focusAfterUnpin.current = next; focusNeighbor()?.focus(); } }); return; } setConfirming(item); setConfirmOpen(true); }; return ( ); } /** * เปิดกล่องหลังการยกนิ้ว "จบทั้งชุด" — หลัง touchend เบราว์เซอร์ยิง mousedown/mouseup/click จำลองที่จุดเดิม * ถ้ากล่องเปิดอยู่แล้ว click จำลองไปโดนปุ่มยืนยันกลางจอ = เลิกปักหมุดเองโดยไม่ได้ตั้งใจ (เจอใน Chrome จริง) * → `preventDefault` ที่ touchend ตัด event จำลองทั้งชุด แล้วค่อยเปิดกล่อง · ปากกา/ไม่มี touchend เปิดเองหลัง 300 ms */ function openAfterRelease(doc: Document, open: () => void) { let done = false; const run = () => { if (done) return; done = true; doc.removeEventListener("touchend", onTouchEnd, true); setTimeout(open, 0); }; const onTouchEnd = (event: TouchEvent) => { if (event.cancelable) event.preventDefault(); run(); }; doc.addEventListener("touchend", onTouchEnd, { capture: true, once: true }); setTimeout(run, 300); } /** ทางลัดหนึ่งรายการ — ลิงก์ + ปุ่มเลิกปักหมุด (hover/โฟกัส) + กดค้างด้วยนิ้ว = เลิกปักหมุด */ function PinnedLinkChip({ item, active, unpinLabel, onUnpin, }: { item: PinnedLinkItem; active: boolean; unpinLabel: string; onUnpin?: () => void; }) { // กดค้างเฉพาะนิ้ว/ปากกา — เมาส์ใช้ปุ่มที่โผล่ตอน hover // เปิดกล่องตอน "ยกนิ้ว" หลังกดค้างครบ — เปิดระหว่างนิ้วยังแตะ การยกนิ้วบนฉากหลังถูกนับเป็นแตะนอกกล่อง ปิดทันที const armed = useRef(false); const longPress = useLongPress( () => { armed.current = true; }, { enabled: Boolean(onUnpin) }, ); return (
  • { armed.current = false; if (event.pointerType !== "mouse") longPress.handlers.onPointerDown(event); }} onPointerUp={(event) => { longPress.handlers.onPointerUp(); if (!armed.current) return; armed.current = false; event.preventDefault(); openAfterRelease(event.currentTarget.ownerDocument, () => onUnpin?.()); }} onPointerCancel={() => { longPress.handlers.onPointerCancel(); armed.current = false; }} onClick={(event) => { // นิ้วที่ยกหลังกดค้างสำเร็จ = ไม่เปิดลิงก์ if (longPress.fired.current) event.preventDefault(); }} className={cn( "flex h-8 items-center gap-1.5 rounded-md border px-2.5 font-medium text-xs no-underline outline-none transition-colors select-none [-webkit-touch-callout:none] hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring/50 [&_svg]:size-4 [&_svg]:shrink-0", active ? "border-primary/40 bg-primary/10 text-primary" : "border-border bg-background text-foreground", )} /> } > {item.icon && ( )} {/* มีไอคอน = จอแคบซ่อนชื่อ (tooltip บอกแทน) · ไม่มีไอคอน = แสดงชื่อเสมอ */} {item.title} {item.title} {onUnpin && ( )}
  • ); }