"use client"; import { cn } from "@suphark/ui/lib/utils"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { type ReactNode, useId, useMemo, useRef, useState } from "react"; import { SearchInput } from "./form/search-input"; import { useUiText } from "./ui-text"; export interface SettingsSection { id: string; label: string; description?: ReactNode; icon?: ReactNode; /** คำค้นเพิ่ม (ทั้งไทย/อังกฤษ) — ค้นจาก label + description แบบข้อความ + keywords */ keywords?: ReadonlyArray; /** ท้ายรายการ เช่น `` / `` */ badge?: ReactNode; content: ReactNode; } /** * หน้าตั้งค่าหลายหัวข้อ (ยกจาก DueBell settings + PDF app-settings-form): * - จอกว้าง: สารบัญซ้าย (sticky) + เนื้อหาหัวข้อที่เลือกทางขวา * - มือถือ: รายการหัวข้อ → แตะแล้วเข้าหน้าหัวข้อพร้อมปุ่ม "กลับ" (master-detail) * - `searchable`: ช่องค้นหาหัวข้อเหนือสารบัญ (ค้น label / description / keywords) * * state ของหัวข้อที่เลือกอยู่ในตัว (`initialSection`) หรือคุมเองผ่าน `value`/`onValueChange` * (เช่นเก็บใน `?tab=` ของ URL) — แต่ละหัวข้อเป็น content ที่ render ไว้แล้ว ไม่ใช่หน้าแยก */ export function SettingsLayout({ sections, value: valueProp, onValueChange, initialSection, searchable = false, searchPlaceholder, className, }: { sections: ReadonlyArray; value?: string; onValueChange?: (id: string) => void; /** หัวข้อเริ่มต้นแบบ uncontrolled (ค่าเริ่มต้น: หัวข้อแรก) */ initialSection?: string; searchable?: boolean; searchPlaceholder?: string; className?: string; }) { const text = useUiText(); const headingId = useId(); const [inner, setInner] = useState(initialSection ?? sections[0]?.id ?? ""); const active = valueProp ?? inner; // มือถือ: false = เห็นรายการหัวข้อ, true = อยู่ในหน้าหัวข้อ (จอกว้างเห็นทั้งสองฝั่งเสมอ) const [mobileDetail, setMobileDetail] = useState(false); const [query, setQuery] = useState(""); const headingRef = useRef(null); const navRef = useRef(null); const visible = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return sections; return sections.filter((s) => [s.label, typeof s.description === "string" ? s.description : "", ...(s.keywords ?? [])] .join(" ") .toLowerCase() .includes(q), ); }, [sections, query]); const current = sections.find((s) => s.id === active) ?? sections[0]; const select = (id: string) => { if (valueProp === undefined) setInner(id); onValueChange?.(id); setMobileDetail(true); // มือถือ: รายการหัวข้อถูกซ่อน → ย้ายโฟกัสไปหัวข้อ ให้ screen reader รู้ว่าเนื้อหาเปลี่ยน // (จอกว้างเห็นทั้งสองฝั่ง โฟกัสอยู่ที่รายการต่อได้ — ไม่ย้าย) if (!window.matchMedia?.("(min-width: 48rem)").matches) { requestAnimationFrame(() => headingRef.current?.focus({ preventScroll: true })); } }; /** มือถือ: กลับไปรายการ — ปุ่ม "กลับ" ถูกซ่อน จึงคืนโฟกัสให้หัวข้อที่เพิ่งเปิด (ไม่งั้นโฟกัสหลุดไป body) */ const back = (id: string) => { setMobileDetail(false); requestAnimationFrame(() => { const nav = navRef.current; // หัวข้อที่เปิดอยู่ถูกกรองออกจากรายการด้วยคำค้น → โฟกัสช่องค้นหา/หัวข้อแรกแทน (ไม่ปล่อยหลุดไป body) const target = nav?.querySelector(`[data-section-id="${CSS.escape(id)}"]`) ?? nav?.querySelector("input, [data-section-id]"); target?.focus(); }); }; if (!current) return null; return (

{current.label}

{current.description && (

{current.description}

)}
{current.content}
); }