import { useLocal } from "@/utils/use-local"; import { cx } from "class-variance-authority"; import { ArrowRight } from "lucide-react"; import { FC, useEffect } from "react"; import { Skeleton } from "../ui/skeleton"; export const Detail: FC<{ detail: (item: any) => Record; on_load: (arg: { params: any }) => Promise; mode: "standard" | "compact" | "inline"; }> = ({ detail, mode, on_load }) => { const local = useLocal({ status: "init" as "init" | "loading" | "ready", detail: null as any, pathname: "", mode: mode, on_load, }); if (!isEditor) { if ( location.pathname !== local.pathname || mode !== local.mode || local.on_load !== on_load ) { local.status = "init"; local.on_load = on_load; local.mode = mode; } useEffect(() => { if (local.status === "init" && typeof on_load === "function") { local.status = "loading"; if (location.pathname === "") { local.detail = detail({}); } else { local.detail = detail({}); local.pathname = location.pathname; } local.render(); const res = on_load({ params: {} }); if (typeof res === "object" && res instanceof Promise) { res.then((item) => { local.detail = detail(item); local.status = "ready"; local.render(); }); } else { local.detail = detail(res); local.status = "ready"; local.render(); } } }, [local.status]); } let values = {}; if (!isEditor && typeof on_load === "function") { values = local.detail || {}; } else { values = detail(null); local.status = "ready"; } const entries = Object.entries(values); return (
{entries.map(([name, data], idx) => { const is_first = idx === 0; const is_last = idx === entries.length - 1; if ( typeof data !== "object" || !data || (typeof data === "object" && !Array.isArray(data)) ) return null; const [label, sample, link] = data; if (link) { preload(link); } if (mode === "standard") { return (
{label}
); } else if (mode === "compact") { return (
{label}
); } else { return (
{label}
); } })}
); }; const Linkable: FC<{ sample?: string; link?: string; mode: "standard" | "compact" | "inline"; status: "init" | "loading" | "ready"; }> = ({ sample, link, status, mode }) => { const loading = ( ); if (!link) { if (status !== "ready") return loading; return sample || "-"; } return (
{ if (link.startsWith("http://") || link.startsWith("https://")) { window.open(link, "_blank"); } if (!isEditor) { navigate(link); } }} > {status === "ready" ? ( <>
{sample || "-"}
) : (
{loading}
)}
); };