UNPKG

@blocklet/ui-react

Version:

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

120 lines (119 loc) 3.99 kB
import { jsxs as l, Fragment as D, jsx as s } from "react/jsx-runtime"; import { use as L } from "react"; import { Box as a, Typography as p, Button as w } from "@mui/material"; import { useLocaleContext as E } from "@arcblock/ux/lib/Locale/context"; import { translate as I } from "@arcblock/ux/lib/Locale/util"; import { useMemoizedFn as d, useCreation as R } from "ahooks"; import { useConfirm as v } from "@arcblock/ux/lib/Dialog"; import { SessionContext as S } from "@arcblock/did-connect/lib/Session"; import { LOGIN_PROVIDER as _ } from "@blocklet/constant"; import m from "@arcblock/ux/lib/Toast"; import { translations as B } from "../libs/locales.js"; import { client as y } from "../../libs/client.js"; function N() { const { confirmApi: g, confirmHolder: x } = v(), { locale: f } = E(), { session: n, connectApi: u } = L(S), e = d((r, o = {}) => I(B, r, f, "en", o)), h = R(() => { if (["true", !0].includes(window?.blocklet?.ALLOW_SKIP_DESTROY_MYSELF_VERIFY)) return !1; const r = n?.user?.connectedAccounts || [], o = [_.WALLET]; return !!r.some((t) => o.includes(t.provider)); }, [n?.user]), A = d(() => new Promise((r, o) => { const t = n?.user?.did; u.open({ locale: f, action: "destroy-myself", forceConnected: !0, saveConnect: !1, autoConnect: !1, // 暂不允许使用 passkey 进行验证 passkeyBehavior: "none", extraParams: { removeUserDid: t }, messages: { title: e("destroyMyself.title"), scan: e("destroyMyself.scan"), confirm: e("destroyMyself.confirm"), success: e("destroyMyself.success") }, // @ts-expect-error 该 did-connect 后端api 将结果塞在了 result 中,并做了加密,所以已经变成一个字符串了 onSuccess: ({ result: c }, C = (i) => i) => { const i = C(c); r(i); }, onClose: () => { u.close(), o(new Error(e("destroyMyself.abort"))); } }); })), M = d(() => { g.open({ title: e("dangerZone.deleteAccount"), content: e("dangerZone.deleteAccountDescription"), confirmButtonText: e("common.confirm"), confirmButtonProps: { color: "error" }, cancelButtonText: e("common.cancel"), async onConfirm(r) { let o; try { if (h) o = await A(); else if (y?.user?.destroyMyself instanceof Function) o = await y.user.destroyMyself(); else { m.error(e("notImplemented")); return; } o?.did === n?.user?.did ? n.logout(r) : m.error(e("destroyMyself.error")); } catch (t) { const c = t?.response?.data.error || t?.message || e("destroyMyself.error"); m.error(c); } } }); }); return /* @__PURE__ */ l(D, { children: [ /* @__PURE__ */ s(a, { children: /* @__PURE__ */ l( a, { sx: { display: "flex", alignItems: "center", gap: 1, justifyContent: "space-between", flexWrap: "wrap" }, children: [ /* @__PURE__ */ l(a, { children: [ /* @__PURE__ */ s( p, { variant: "h6", sx: { fontSize: "0.875rem !important", fontWeight: "bold" }, children: e("dangerZone.deleteAccount") } ), /* @__PURE__ */ s( p, { variant: "caption", sx: { color: "text.secondary" }, children: e("dangerZone.deleteAccountDescription") } ) ] }), /* @__PURE__ */ s(w, { variant: "contained", color: "error", size: "small", onClick: M, children: e("dangerZone.delete") }) ] } ) }), x ] }); } export { N as default };