import { FC } from "react"; import { Tabs, TabsList, TabsTrigger } from "../ui/tabs"; import { useLocal } from "@/utils/use-local"; export const Tab: FC<{ tabs: (arg: { count: (string | number)[] }) => { label: string; navigate: string; count: string; width?: number; color?: string; onClick?: () => Promise | void; }[]; active: string; body: any; on_load?: () => any; PassProp: any; }> = ({ tabs, active, body, PassProp, on_load }) => { const local = useLocal( { activeIndex: active, count: [] as (number | string)[], mode: "number" as "number" | "hash", status: "init" as "init" | "load" | "ready", }, () => { if (local.mode === "hash") { } if (local.status === "init") { if (typeof on_load === "function" && !isEditor) { local.status = "load"; const res = on_load(); if (typeof res === "object" && res instanceof Promise) { res.then((value) => { local.count = value; local.status = "ready"; local.render(); }); } else { local.count = res; local.status = "ready"; } } else { local.status = "ready"; } } } ); const all_tabs = tabs({ count: local.count || [] }); if (local.activeIndex === "#") { local.mode = "hash"; } if (!parseInt(local.activeIndex)) { if (local.mode === "hash") { local.activeIndex = location.hash.substring(1); if (!parseInt(local.activeIndex)) { local.activeIndex = "0"; } } else { local.activeIndex = "0"; } } return (
{all_tabs.map((e, idx) => { if (e.navigate) { preload(e.navigate); } let has_count = local.status !== "ready" || typeof e.count === "number" || typeof e.count === "string"; return ( { local.activeIndex = idx.toString(); if (local.mode === "hash") { location.hash = `#${local.activeIndex}`; } local.render(); if (e.navigate) { navigate(e.navigate); } }} className={cx( css` padding: 0 !important; margin: 0 0 0 ${idx !== 0 ? "5px" : 0}; `, e.width ? css` max-width: ${e.width}px; overflow: hidden; ` : "c-flex-1" )} >
{e.label}
{has_count && (
{local.status != "ready" ? "..." : e.count}
)}
); })}
{body}
); };