@blocklet/ui-react
Version:
Some useful front-end web components that can be used in Blocklets.
121 lines (120 loc) • 3.76 kB
JavaScript
import { jsx as s, jsxs as i, Fragment as t } from "react/jsx-runtime";
import F from "react";
import { useCreation as f, useMemoizedFn as U } from "ahooks";
import { useTheme as C, Link as E } from "@mui/material";
import { WELLKNOWN_SERVICE_PATH_PREFIX as M } from "@abtnode/constant";
import { joinURL as R, withQuery as S } from "ufo";
import D from "lodash/isEmpty";
import { getActivityLink as I } from "../utils.js";
const P = {
comment: (r, o) => o && o > 1 ? /* @__PURE__ */ i(t, { children: [
"left ",
o,
" comments on your ",
r
] }) : /* @__PURE__ */ i(t, { children: [
"commented on your ",
r
] }),
like: (r) => /* @__PURE__ */ i(t, { children: [
"liked your ",
r
] }),
follow: () => /* @__PURE__ */ s(t, { children: "followed you" }),
tips: (r) => /* @__PURE__ */ i(t, { children: [
"gave tips to your ",
r
] }),
mention: (r) => /* @__PURE__ */ i(t, { children: [
"mentioned you in ",
r
] }),
assign: () => /* @__PURE__ */ s(t, { children: "assigned you a task" })
};
function a({ user: r, color: o = void 0 }) {
const { palette: d } = C(), h = S(R(M, "user"), { did: r.did }), u = o || d.text.primary;
return /* @__PURE__ */ s(
E,
{
href: h,
color: u,
target: "_blank",
onClick: (p) => {
p.customPreventRedirect = !0;
},
sx: {
fontWeight: 600,
textDecoration: "none",
"&:hover": { cursor: "pointer" }
},
children: r.fullName || "System"
}
);
}
a.displayName = "UserLink";
function H({ activity: r, users: o, actors: d, extra: h = {}, mountPoint: u = "" }) {
const { palette: p } = C(), { type: m, target: g } = r || {}, { type: y } = g || {}, { linkColor: c = p.text.primary } = h || {}, k = f(() => {
if (!Array.isArray(o)) return /* @__PURE__ */ new Map();
const e = /* @__PURE__ */ new Map();
return o.forEach((l) => {
l?.did && !e.has(l.did) && e.set(l.did, l);
}), e;
}, [o]), n = f(() => Array.isArray(d) ? d.map((e) => e ? k.get(e) || { did: e, fullName: "System" } : null).filter(Boolean) : [], [d, k]), L = U(() => {
if (!Array.isArray(n) || n.length === 0)
return null;
if (n.length === 1)
return /* @__PURE__ */ s(a, { user: n[0], color: c });
const e = n.slice(0, -1), l = n[n.length - 1];
if (n.length === 2)
return /* @__PURE__ */ i(t, { children: [
/* @__PURE__ */ s(a, { user: e[0], color: c }),
" and ",
/* @__PURE__ */ s(a, { user: l, color: c })
] });
const _ = n.length > 3, N = _ ? n.slice(0, 2) : e;
return /* @__PURE__ */ i(t, { children: [
N.map((T, w) => /* @__PURE__ */ i(F.Fragment, { children: [
/* @__PURE__ */ s(a, { user: T, color: c }),
w < N.length - 1 ? ", " : ""
] }, T.did)),
_ ? `, and ${n.length - 2} others` : /* @__PURE__ */ i(t, { children: [
", and ",
/* @__PURE__ */ s(a, { user: l, color: c })
] })
] });
}), A = U(() => {
const e = m ? P[m] : null;
return e ? e(y, o.length) : null;
}), v = f(() => {
const e = L(), l = A();
return !e || !l ? null : /* @__PURE__ */ i(t, { children: [
e,
" ",
l
] });
}, [m, y, n, L, A]), x = f(() => {
if (!r) return null;
const e = I(r);
return e?.targetLink ? R(u, e.targetLink) : null;
}, [r, u]);
return !m || D(g) ? null : /* @__PURE__ */ i(t, { children: [
v,
" ",
x && /* @__PURE__ */ s(
E,
{
href: x,
color: c,
target: "_blank",
sx: { textDecoration: "none" },
onClick: (e) => {
e.customPreventRedirect = !0;
},
children: g.name
}
)
] });
}
export {
H as default
};