"use client";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@suphark/ui/components/ui/tabs";
import {
Boxes,
Compass,
FileCode2,
Layers,
LayoutGrid,
Palette,
ShieldAlert,
Zap,
} from "lucide-react";
import * as React from "react";
import { ShowcaseOverview } from "./sections/showcase-overview";
// แท็บอื่นนอกจากภาพรวมโหลดเมื่อเปิดแท็บนั้น — เดิม import ทุกแท็บ (ปฏิทิน, ตาราง, dnd-kit, cmdk …) ~1 MB ทั้งที่เห็นทีละแท็บ
const ShowcaseDataDisplay = React.lazy(() =>
import("./sections/showcase-data-display").then((m) => ({ default: m.ShowcaseDataDisplay })),
);
const ShowcaseFeedback = React.lazy(() =>
import("./sections/showcase-feedback").then((m) => ({ default: m.ShowcaseFeedback })),
);
const ShowcaseForms = React.lazy(() =>
import("./sections/showcase-forms").then((m) => ({ default: m.ShowcaseForms })),
);
const ShowcaseFoundations = React.lazy(() =>
import("./sections/showcase-foundations").then((m) => ({ default: m.ShowcaseFoundations })),
);
const ShowcaseGuidelinesRecipes = React.lazy(() =>
import("./sections/showcase-guidelines-recipes").then((m) => ({
default: m.ShowcaseGuidelinesRecipes,
})),
);
const ShowcaseOverlaysNavigation = React.lazy(() =>
import("./sections/showcase-overlays-navigation").then((m) => ({
default: m.ShowcaseOverlaysNavigation,
})),
);
const ShowcasePrimitives = React.lazy(() =>
import("./sections/showcase-primitives").then((m) => ({ default: m.ShowcasePrimitives })),
);
function SectionLoading() {
return
;
}
export function DesignSystemShowcase({
links,
}: {
/** ลิงก์ไปหน้าตัวอย่างของแอป showcase (เช่น /shell) — ไม่ส่ง = ซ่อนปุ่ม */
links?: { shell?: string };
} = {}) {
const [activeTab, setActiveTab] = React.useState("overview");
const [searchQuery, setSearchQuery] = React.useState("");
/** สลับแท็บแล้วเลื่อนไปหัวข้อ — เลื่อนใน effect หลังแท็บใหม่ render แล้ว (ตำแหน่งจึงถูก) ชดเชย header ที่ sticky */
const [pendingAnchor, setPendingAnchor] = React.useState(null);
const tries = React.useRef(0);
const [retry, setRetry] = React.useState(0);
const jumpTo = React.useCallback((tab: string, anchor?: string) => {
tries.current = 0; // การกระโดดครั้งใหม่ได้เวลารอแท็บ lazy เต็มรอบ
setActiveTab(tab);
setPendingAnchor(anchor ?? "__top__");
}, []);
// biome-ignore lint/correctness/useExhaustiveDependencies: retry ใช้เป็นตัวกระตุ้นให้ลองหาหัวข้อซ้ำระหว่างรอแท็บ lazy
React.useEffect(() => {
if (!pendingAnchor) return;
// หน่วงเล็กน้อยให้ Tabs โฟกัส trigger ใหม่เสร็จก่อน (การโฟกัสเลื่อนหน้าไปที่แถบแท็บ) แล้วค่อยเลื่อนไปหัวข้อ
const timer = window.setTimeout(() => {
if (pendingAnchor === "__top__") {
window.scrollTo({ top: 0, behavior: "smooth" });
setPendingAnchor(null);
return;
}
const el = document.getElementById(pendingAnchor);
if (!el) {
// แท็บที่โหลดแบบ lazy อาจยัง render ไม่เสร็จ — ลองใหม่ได้ถึง ~2 วินาที
if (tries.current++ < 25) {
setRetry((r) => r + 1);
return;
}
tries.current = 0;
setPendingAnchor(null);
return;
}
tries.current = 0;
el.style.scrollMarginTop = "88px"; // ชดเชย header ที่ sticky
el.scrollIntoView({ behavior: "smooth", block: "start" });
setPendingAnchor(null);
}, 80);
return () => window.clearTimeout(timer);
}, [pendingAnchor, retry]);
const tabItems = [
{ value: "overview", label: "ภาพรวม", icon: LayoutGrid },
{ value: "foundations", label: "Tokens", icon: Palette },
{ value: "primitives", label: "Primitives", icon: Boxes },
{ value: "forms", label: "Forms", icon: Layers },
{ value: "data-display", label: "Data", icon: Zap },
{ value: "overlays", label: "Overlays", icon: Compass },
{ value: "feedback", label: "Feedback", icon: ShieldAlert },
{ value: "guidelines-recipes", label: "Rules", icon: FileCode2 },
];
return (
{/* Main Tab Navigation */}
{
if (val !== null) setActiveTab(val);
}}
className="flex flex-col gap-6"
>
{/* แถบแท็บ: จอแคบเลื่อนแนวนอน 1 แถว (ขอบไล่จางบอกว่าเลื่อนได้) · จอกว้าง wrap ได้ */}
{tabItems.map((tab) => {
const Icon = tab.icon;
return (
{tab.label}
);
})}
{/* Tab Contents */}
}>
}>
}>
}>
}>
}>
}>
);
}