import { useLocal } from "lib/utils/use-local"; import { cx } from "class-variance-authority"; import { ArrowRight } from "lucide-react"; import { FC } from "react"; import { Skeleton } from "../ui/skeleton"; import { formatName } from "lib/gen/utils"; export const Detail: FC<{ detail: ( item: any ) => Record void)]>; on_load: | Record | ((arg: { params: any; bind: (fn: (on_load: any) => void) => void; }) => Promise); mode: "standard" | "compact" | "inline"; }> = ({ detail: _detail, mode, on_load }) => { const local = useLocal({ status: "init" as "init" | "loading" | "ready", detail: null as any, pathname: "", mode: mode, on_load, bound: false, resolved_value: null as any, }); local.on_load = on_load; let detail = _detail; if (typeof detail !== "function") { detail = (load: any) => { const result: any = {}; if (typeof load === "object" && !!load) { for (const [k, v] of Object.entries(load)) { if (k !== "id") if (typeof v !== "object") result[k] = [formatName(k), v]; } } return result; }; } let values = {}; if (local.status !== "loading") { if (typeof on_load === "function") { if (local.resolved_value) { values = detail(local.resolved_value); local.resolved_value = null; local.status = "ready"; } else { const result = on_load({} as any); if (result instanceof Promise) { local.status = "loading"; values = detail({}); result.then((e) => { local.resolved_value = e; local.status = "ready"; local.render(); }); } else { values = detail(result); local.status = "ready"; } } } else { values = detail(on_load); local.status = "ready"; } } if (typeof values !== "object" || values === null) return null; const entries = Object.entries(values); return (
3 && mode === "compact" && css` flex-direction: row !important; flex-wrap: wrap !important; border: 1px solid #ddd; border-radius: 10px; margin-top: 10px; margin-bottom: 10px; &::before { content: " "; position: absolute; left: 49%; bottom: 0px; top: 0px; border-right: 1px solid #ddd; } > div { border-top: 0px; padding-right: 10px; padding-left: 10px; width: 49% !important; } ` )} > {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; let [label, sample, link] = data; if (typeof on_load === "object" && on_load[name]) { sample = on_load[name]; } if (typeof link === "string") { preload(link); } if (mode === "standard") { return (
{label}
); } else if (mode === "compact") { return (
{label}
); } else { return (
{label}
); } })}
); }; const Linkable: FC<{ sample?: string; link?: string | (() => void); mode: "standard" | "compact" | "inline"; status: "init" | "loading" | "ready"; }> = ({ sample, link, status, mode }) => { const loading = ( ); if (!link) { if (status !== "ready") return status; return sample || "-"; } return (
{ if (typeof link === "function") { link(); } else { if (link.startsWith("http://") || link.startsWith("https://")) { window.open(link, "_blank"); } if (!isEditor) { navigate(link); } } }} > {status === "ready" ? ( <>
{sample || "-"}
) : (
{loading}
)}
); };