@blocklet/ui-react
Version:
Some useful front-end web components that can be used in Blocklets.
133 lines (132 loc) • 4.47 kB
JavaScript
import { jsx as r, jsxs as z } from "react/jsx-runtime";
import "iconify-icon";
import { use as D, useMemo as w, useLayoutEffect as A } from "react";
import e from "prop-types";
import { SessionContext as M } from "@arcblock/did-connect-react/lib/Session";
import { useLocaleContext as j } from "@arcblock/ux/lib/Locale/context";
import H from "@arcblock/ux/lib/Img";
import O from "@arcblock/ux/lib/Layout/dashboard";
import S from "@arcblock/ux/lib/hooks/use-blocklet-logo";
import { SessionManagerProps as _, BlockletMetaProps as E } from "../types.js";
import { mapRecursive as U, flatRecursive as q, matchPaths as F } from "../utils.js";
import { formatBlockletInfo as G, getLocalizedNavigation as J, filterNavByRole as K, publicPath as m } from "../blocklets.js";
import Q from "../common/header-addons.js";
import { useWalletHiddenTopbar as V } from "../common/wallet-hidden-topbar.js";
import X from "./app-shell/app-header.js";
import { default as yo } from "./app-shell/app-badge.js";
import { AppInfoProvider as Y } from "./app-shell/app-info-context.js";
import { useAppInfo as Ao } from "./app-shell/app-info-context.js";
function Z({
meta: a = void 0,
fallbackUrl: s = m,
invalidPathFallback: l = null,
headerAddons: L = void 0,
sessionManagerProps: P = {
showRole: !0,
// dashboard 默认退出登录行为: 跳转到 (root) blocklet 首页
onLogout: () => {
window.location.href = m;
}
},
links: d = [],
showDomainWarningDialog: T = !0,
appPath: k = void 0,
appTab: I = void 0,
onAppTabChange: N = void 0,
children: B = void 0,
...h
}) {
V();
const c = D(M)?.session?.user, g = c?.role, { locale: v, defaultLocale: x } = j() || {}, n = w(() => {
const t = Object.assign({}, window.blocklet, a);
try {
return G(t);
} catch (i) {
return console.error("Failed to format blocklet info", i, t), t;
}
}, [a]), { localizedNav: b, flattened: p, matchedIndex: y } = w(() => {
let t = J({
navigation: n?.navigation?.dashboard,
locale: v,
defaultLocale: x
}) || [];
t = K(t, g), t = U(
t,
(o) => {
let u = null;
return o.icon && (o.icon.startsWith("http") || o.icon.startsWith("data:") ? u = /* @__PURE__ */ r(H, { src: o.icon }) : u = /* @__PURE__ */ r("iconify-icon", { height: "100%", width: "100%", icon: o.icon })), {
id: o.id,
title: o.title,
url: o.link,
icon: u,
// https://github.com/ArcBlock/ux/issues/755#issuecomment-1208692620
external: !0,
children: o.items
};
},
"items"
);
const i = q(t).filter((o) => !!o.url), f = F(i.map((o) => o.url));
return f !== -1 && (i[f].active = !0), { localizedNav: t, flattened: i, matchedIndex: f };
}, [n, v, x, g]), C = typeof d == "function" ? d(b) : [...b, ...d], W = S({
meta: a
});
if (A(() => {
c && !p?.length && s && (window.location.href = s);
}, [s]), A(() => {
c && p?.length && y === -1 && l && l();
}, [l, p, y]), !n.appName)
return null;
const { appName: R } = n;
return /* @__PURE__ */ r(
O,
{
title: R,
fullWidth: !0,
sidebarWidth: 128,
legacy: !1,
links: C,
...h,
headerProps: {
homeLink: m,
logo: /* @__PURE__ */ r("img", { src: W, alt: "logo" }),
addons: /* @__PURE__ */ r(
Q,
{
formattedBlocklet: n,
addons: L,
sessionManagerProps: P,
showDomainWarningDialog: T
}
),
...h.headerProps
},
children: /* @__PURE__ */ z(Y, { path: k, currentTab: I, meta: a, children: [
/* @__PURE__ */ r(X, { onTabChange: N }),
B
] })
}
);
}
Z.propTypes = {
meta: E,
// 如果当前用户没有权限访问任何导航菜单, 则自动跳转到 fallbackUrl, 默认值为 publicPath, 设置为 null 表示禁用自动跳转
fallbackUrl: e.string,
// 当前路径未匹配任何 nav links 时的 fallback, 默认行为跳转到首个可用的 nav link
invalidPathFallback: e.func,
headerAddons: e.oneOfType([e.func, e.node]),
sessionManagerProps: _,
links: e.oneOfType([e.array, e.func]),
showDomainWarningDialog: e.bool,
appPath: e.string,
appTab: e.string,
onAppTabChange: e.func,
children: e.node
};
export {
yo as AppBadge,
X as AppHeader,
Y as AppInfoProvider,
Z as default,
Ao as useAppInfo
};