UNPKG

@blocklet/ui-react

Version:

Some useful front-end web components that can be used in Blocklets.

87 lines (86 loc) 2.92 kB
import h from "lodash/isEmpty"; import m from "dompurify"; import { toTextList as v, Link as f, getLink as p } from "@abtnode/util/lib/notification-preview/highlight"; import { isSameAddr as d } from "@abtnode/util/lib/notification-preview/func"; import { getBlockletSDK as T } from "@blocklet/js-sdk"; const b = (t) => { if (!t || !t.length) return []; const e = []; let r = null, s = []; if (t.forEach((a) => { if (!a.activity || !a.activity.type || !r) { r && (s.length === 1 ? e.push(s[0]) : (r.items = s, e.push(r))), a.activity && a.activity.type ? (r = a, s = [a]) : (e.push(a), r = null, s = []); return; } const n = s[0].activity, c = n?.type, l = n?.target?.type, i = n?.target?.id, u = a.activity.type, o = a.activity.target?.type, y = a.activity.target?.id; if ( // activity.type 必须相同且不为null或undefined c && u && c === u && // 如果都没有target,可以合并 (!n?.target && !a.activity.target || // 如果都有target,则target.type和target.id都必须相同 n?.target && a.activity.target && l === o && i === y) ) s.push(a); else { if (s.length === 1) e.push(s[0]); else { const g = Object.assign({}, r, { items: s }); e.push(g); } r = a, s = [a]; } }), r) if (s.length === 1) e.push(s[0]); else { const a = Object.assign({}, r, { items: s }); e.push(a); } return e; }, j = (t) => !h(t.activity) && !!t.activity?.type && !!t.activity?.actor, O = (t) => { const { severity: e } = t || {}; return !!e && ["normal", "success", "info"].includes(e); }, P = (t) => { if (!t) return null; const { meta: e, target: r } = t; return { metaLink: e?.id, targetLink: r?.id }; }, k = async (t, e) => { try { return await T().user.getProfileUrl({ did: t, locale: e }); } catch (r) { console.error(r); return; } }, L = async (t, e) => { const { type: r, did: s } = t; return d(r, "did") ? await k(s, e) || p(t) : p(t); }, D = async (t, e, r = !0) => { const s = v(t); return await Promise.all( s.map(async (n) => { if (n instanceof f) { if (r) { const c = await L(n, e), { type: l, chainId: i, did: u } = n; return d(l, "dapp") ? `<em class="dapp" data-type="${l}" data-chain-id="${i}" data-did="${u}">${n.text}</em>` : c ? `<a target="_blank" rel="noopener noreferrer" class="link" href="${c}">${n.text}</a>` : `<em class="common" data-type="${l}" data-chain-id="${i}" data-did="${u}">${n.text}</em>`; } return n.text; } return n; }) ).then((n) => n.join("")); }, E = (t) => m.sanitize(t, { ALLOWED_ATTR: ["href", "target", "rel", "class", "id", "style"] }); export { O as canAutoRead, P as getActivityLink, j as isIncludeActivity, b as mergeAdjacentNotifications, E as sanitize, D as toClickableSpan };