@blocklet/ui-react
Version:
Some useful front-end web components that can be used in Blocklets.
84 lines (83 loc) • 2.61 kB
JavaScript
import { jsx as C } from "react/jsx-runtime";
import { createContext as k, useState as L, useMemo as M, useContext as P } from "react";
import { useCreation as b, useMemoizedFn as T } from "ahooks";
import { withoutTrailingSlash as w } from "ufo";
import N from "lodash/isPlainObject";
import { WELLKNOWN_BLOCKLET_ADMIN_PATH as O } from "@abtnode/constant";
import { useLocaleContext as S } from "@arcblock/ux/lib/Locale/context";
const A = k({
inService: !1,
currentTab: "",
TabComponent: null,
navItem: void 0,
updateAppInfo: () => {
}
}), h = (l, r, c = "en") => {
const n = l?.[r];
return N(n) ? n[c] || n.en : n;
}, j = (l, r = "", c = "en") => {
const n = r === "/" ? ["/"] : r.split("/").filter(Boolean);
let e = null, d = 0;
return l.forEach((s) => {
const m = h(s, "link", c) || "", i = m === "/" ? ["/"] : m.split("/").filter(Boolean);
if (i.length > n.length)
return;
let p = !0;
for (let u = 0; u < i.length; u++)
if (i[u] !== n[u]) {
p = !1;
break;
}
p && i.length > d && (e = s, d = i.length);
}), e;
};
function z({
path: l = window?.location?.pathname || "",
currentTab: r = "",
meta: c = void 0,
children: n = null
}) {
const { locale: e = "en" } = S() || {}, d = w(l), s = d.startsWith(O), m = b(() => Object.assign({}, window.blocklet, c), [c]), i = b(() => {
const o = m.navigation?.filter(
(a) => Array.isArray(a.section) ? a.section.includes("dashboard") : a.section === "dashboard"
), f = o?.flatMap((a) => a.items || []), t = j(f, d, e);
if (!t)
return;
const v = o.find((a) => a.id === t.parent);
let g = h(t, "title", e) || "";
return s || (g = `${h(v, "title", e) || ""} - ${g}`), {
id: t.id,
parent: t.parent,
title: g,
link: h(t, "link", e) || "",
description: h(t, "description", e) || ""
};
}, [l, s, e, m]), [p, u] = L({}), I = T((o) => {
u((f) => ({ ...f, ...o }));
}), x = M(
() => {
const { tabs: o = [] } = p, f = o.find((g) => g.value === r);
let t = r, v = null;
return !f && o.length > 0 ? (t = o[0].value, v = o[0].render || null) : v = f?.render || null, {
...p,
inService: s,
navItem: i,
currentTab: t,
TabComponent: v,
updateAppInfo: I
};
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[p, i, s, r, I]
);
return /* @__PURE__ */ C(A.Provider, { value: x, children: n });
}
function D() {
return P(A);
}
export {
z as AppInfoProvider,
j as findNavItem,
h as getI18nVal,
D as useAppInfo
};