import { useLocal } from "lib/utils/use-local"; import { FC, ReactNode, useEffect } from "react"; import { Skeleton } from "../ui/skeleton"; export const Header: FC<{ text: string; back_url: string | (() => void); props: any; actions: () => Promise<[string, string | (() => void), ReactNode][]>; action_mode: "auto" | "buttons" | "sub-header" | "dropdown"; edit_url: string; }> = ({ text, back_url, props, edit_url, actions, action_mode }) => { const local = useLocal({ loading: false, actions: [] as [string, string | (() => void), ReactNode][], }); useEffect(() => { (async () => { if (typeof actions === "function") { local.loading = true; local.render(); const res_actions = await actions(); if (Array.isArray(res_actions)) { local.actions = res_actions; preload( res_actions .filter((e) => typeof e[1] === "string") .map((e) => e[1]) as string[] ); } local.loading = false; local.render(); } })(); }, [actions, action_mode]); const back = () => { if (isEditor) return; if (typeof back_url === "function") { const url = back_url(); if (typeof url === "string") { if (url === "back") { history.back(); return; } navigate(url); } } else { if (back_url === "back") { history.back(); return; } navigate(back_url); } }; return (
{typeof back_url === "string" && preload([back_url])} {back_url && (
)}
{text}
{local.loading && ( )} {!local.loading && ( <> {local.actions.map((e) => { const [label, to, icon] = e; return (
{ if (isEditor) return; if (typeof to === "string") { navigate(to); } else if (typeof to === "function") { to(); } }} >
{icon}
{label}
); })} )} {/* {edit_url && edit_url !== "" && params.id !== "_" && (
{ if (isEditor) return; if (typeof edit_url === "string") { navigate(edit_url); } }} >
)} */}
); };