@blocklet/ui-react 3.2.2 → 3.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,73 +1,73 @@
1
- import { jsx as i } from "react/jsx-runtime";
2
- import O from "prop-types";
3
- import { useMemo as _, useCallback as h, useEffect as $ } from "react";
1
+ import { jsx as s } from "react/jsx-runtime";
2
+ import U from "prop-types";
3
+ import { useMemo as y, useCallback as I, useEffect as $ } from "react";
4
4
  import { IconButton as g, Badge as B } from "@mui/material";
5
5
  import { useSnackbar as D } from "notistack";
6
- import S from "@arcblock/icons/lib/Notification";
7
- import { useCreation as s } from "ahooks";
8
- import { WELLKNOWN_SERVICE_PATH_PREFIX as V, EVENTS as y } from "@abtnode/constant";
9
- import x from "@arcblock/react-hooks/lib/useBrowser";
10
- import { compareVersions as U } from "@arcblock/ux/lib/Util";
11
- import { joinURL as j, withQuery as I } from "ufo";
6
+ import P from "@arcblock/icons/lib/Notification";
7
+ import { useCreation as c } from "ahooks";
8
+ import { WELLKNOWN_SERVICE_PATH_PREFIX as S, EVENTS as L } from "@abtnode/constant";
9
+ import V from "@arcblock/react-hooks/lib/useBrowser";
10
+ import { compareVersions as x } from "@arcblock/ux/lib/Util";
11
+ import { joinURL as j, withQuery as A } from "ufo";
12
12
  import { useListenWsClient as F } from "./ws.js";
13
13
  import H from "../Notifications/Snackbar.js";
14
- const b = j(V, "user", "notifications"), K = (r) => I(b, {
15
- id: r.id,
16
- severity: r.severity || "all",
17
- componentDid: r.source === "system" ? "system" : r.componentDid || "all"
14
+ const E = j(S, "user", "notifications"), K = (o) => A(E, {
15
+ id: o.id,
16
+ severity: o.severity || "all",
17
+ componentDid: o.source === "system" ? "system" : o.componentDid || "all"
18
18
  });
19
- function P({ session: r = {} }) {
20
- const { unReadCount: c, user: w, setUnReadCount: n } = r, e = s(() => w?.did, [w]), { enqueueSnackbar: E } = D(), l = x(), k = _(() => l.arcSphere || l.wallet, [l]), N = s(() => window.blocklet?.serverVersion, []), t = F("user"), d = s(
21
- () => `${window.blocklet.did}/${e}/${y.NOTIFICATION_BLOCKLET_CREATE}`,
19
+ function W({ session: o = {} }) {
20
+ const { unReadCount: l, user: n, setUnReadCount: i } = o, e = c(() => n?.did, [n]), { enqueueSnackbar: k } = D(), d = V(), u = y(() => !e || n?.isPreviewUser || e === window.blocklet.did, [e, n]), N = y(() => d.arcSphere || d.wallet, [d]), T = c(() => window.blocklet?.serverVersion, []), t = F("user"), a = c(
21
+ () => `${window.blocklet.did}/${e}/${L.NOTIFICATION_BLOCKLET_CREATE}`,
22
22
  [e]
23
- ), u = s(
24
- () => `${window.blocklet.did}/${e}/${y.NOTIFICATION_BLOCKLET_READ}`,
23
+ ), m = c(
24
+ () => `${window.blocklet.did}/${e}/${L.NOTIFICATION_BLOCKLET_READ}`,
25
25
  [e]
26
- ), a = h(
27
- (o) => {
28
- const { receivers: f } = o ?? {}, { receiver: p } = f[0] ?? {};
29
- if (p === e) {
30
- n((v) => v + 1);
31
- const C = U(N, "1.16.42-beta-20250407");
32
- if (!k && o.source === "component" && C) {
33
- const v = K(o), { severity: T, description: L } = o || {}, A = ["error", "warning"].includes(T) || o.sticky;
34
- E(L, {
35
- variant: T,
36
- autoHideDuration: A ? null : 5e3,
26
+ ), f = I(
27
+ (r) => {
28
+ const { receivers: v } = r ?? {}, { receiver: w } = v[0] ?? {};
29
+ if (w === e) {
30
+ i((b) => b + 1);
31
+ const C = x(T, "1.16.42-beta-20250407");
32
+ if (!N && r.source === "component" && C) {
33
+ const b = K(r), { severity: h, description: R } = r || {}, O = ["error", "warning"].includes(h) || r.sticky;
34
+ k(R, {
35
+ variant: h,
36
+ autoHideDuration: O ? null : 5e3,
37
37
  // eslint-disable-next-line react/no-unstable-nested-components
38
- content: (R) => /* @__PURE__ */ i(H, { viewAllUrl: v, keyId: R, notification: o })
38
+ content: (_) => /* @__PURE__ */ s(H, { viewAllUrl: b, keyId: _, notification: r })
39
39
  });
40
40
  }
41
41
  }
42
42
  },
43
- [e, n, E, N, k]
44
- ), m = h(
45
- (o) => {
46
- const { receiver: f, readCount: p } = o ?? {};
47
- f === e && n((C) => Math.max(C - p, 0));
43
+ [e, i, k, T, N]
44
+ ), p = I(
45
+ (r) => {
46
+ const { receiver: v, readCount: w } = r ?? {};
47
+ v === e && i((C) => Math.max(C - w, 0));
48
48
  },
49
- [e, n]
49
+ [e, i]
50
50
  );
51
- return $(() => (t && (t.on(d, a), t.on(u, m)), () => {
52
- t && (t.off(d, a), t.off(u, m));
53
- }), [t, n, a, d, m, u]), !r.user || !b ? null : /* @__PURE__ */ i(
51
+ return $(() => (t && !u && (t.on(a, f), t.on(m, p)), () => {
52
+ t && !u && (t.off(a, f), t.off(m, p));
53
+ }), [t, i, f, a, p, m, u]), !o.user || !E ? null : /* @__PURE__ */ s(
54
54
  g,
55
55
  {
56
56
  size: "medium",
57
57
  variant: "outlined",
58
- href: I(b, { read: c <= 0 }),
58
+ href: A(E, { read: l <= 0 }),
59
59
  sx: {
60
60
  "&:hover": {
61
61
  borderRadius: "50%"
62
62
  }
63
63
  },
64
- children: /* @__PURE__ */ i(B, { badgeContent: c, color: "error", invisible: c === 0, children: /* @__PURE__ */ i(S, { style: { width: "auto", height: 24 } }) })
64
+ children: /* @__PURE__ */ s(B, { badgeContent: l, color: "error", invisible: l === 0, children: /* @__PURE__ */ s(P, { style: { width: "auto", height: 24 } }) })
65
65
  }
66
66
  );
67
67
  }
68
- P.propTypes = {
69
- session: O.object
68
+ W.propTypes = {
69
+ session: U.object
70
70
  };
71
71
  export {
72
- P as default
72
+ W as default
73
73
  };
package/lib/common/ws.js CHANGED
@@ -1,26 +1,33 @@
1
- import { use as s } from "react";
2
- import { WsClient as o } from "@arcblock/ws";
3
- import { SessionContext as n } from "@arcblock/did-connect-react/lib/Session";
4
- import { joinURL as f } from "ufo";
5
- import { BLOCKLET_SERVICE_PATH_PREFIX as m } from "@arcblock/ux/lib/Util/constant";
1
+ import { use as c } from "react";
2
+ import { WsClient as f } from "@arcblock/ws";
3
+ import { useCreation as u, usePrevious as m, useUnmount as a } from "ahooks";
4
+ import { SessionContext as l } from "@arcblock/did-connect-react/lib/Session";
5
+ import { joinURL as C } from "ufo";
6
+ import { BLOCKLET_SERVICE_PATH_PREFIX as d } from "@arcblock/ux/lib/Util/constant";
6
7
  const e = {};
7
- function a(t = "admin") {
8
+ function w(t = "admin") {
8
9
  let r = "/";
9
10
  !window?.blocklet && window?.env?.apiPrefix && (r = window.env.apiPrefix);
10
- const i = f(r, m, t);
11
- return new o(i, {
11
+ const s = C(r, d, t);
12
+ return new f(s, {
12
13
  heartbeatIntervalMs: 100 * 1e3
13
14
  });
14
15
  }
15
- function c(t = "admin") {
16
- return e[t] || (e[t] = a(t)), e[t];
16
+ function x(t = "admin") {
17
+ return e[t] || (e[t] = w(t)), e[t];
17
18
  }
18
- const P = (t = "admin") => {
19
- const r = s(n), { session: i } = r ?? {};
20
- return !e[t] && i.user && (e[t] = c(t), e[t].connect()), e[t];
19
+ const L = (t = "admin") => {
20
+ const r = c(l), { session: s } = r ?? {}, n = u(() => s.user?.did, [s.user]), o = m(n);
21
+ return a(() => {
22
+ Object.keys(e).forEach((i) => {
23
+ e[i]?.isConnected() && e[i]?.disconnect(), delete e[i];
24
+ });
25
+ }), n ? (o && o !== n && Object.keys(e).forEach((i) => {
26
+ e[i] && (e[i].isConnected() && e[i].disconnect(), e[i].connect());
27
+ }), !e[t] && s.user && (e[t] = x(t), e[t].connect()), e[t]) : null;
21
28
  };
22
29
  export {
23
- a as create,
24
- c as default,
25
- P as useListenWsClient
30
+ w as create,
31
+ x as default,
32
+ L as useListenWsClient
26
33
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/ui-react",
3
- "version": "3.2.2",
3
+ "version": "3.2.3",
4
4
  "description": "Some useful front-end web components that can be used in Blocklets.",
5
5
  "keywords": [
6
6
  "react",
@@ -35,9 +35,9 @@
35
35
  "dependencies": {
36
36
  "@abtnode/constant": "1.17.1",
37
37
  "@abtnode/util": "1.17.1",
38
- "@arcblock/bridge": "3.2.2",
39
- "@arcblock/icons": "3.2.2",
40
- "@arcblock/react-hooks": "3.2.2",
38
+ "@arcblock/bridge": "3.2.3",
39
+ "@arcblock/icons": "3.2.3",
40
+ "@arcblock/react-hooks": "3.2.3",
41
41
  "@arcblock/ws": "^1.27.3",
42
42
  "@blocklet/constant": "1.17.1",
43
43
  "@blocklet/did-space-react": "^1.2.1",
@@ -83,7 +83,7 @@
83
83
  "access": "public"
84
84
  },
85
85
  "devDependencies": {
86
- "@arcblock/did-connect-react": "3.2.2",
86
+ "@arcblock/did-connect-react": "3.2.3",
87
87
  "@types/dompurify": "^3.2.0",
88
88
  "@types/ua-parser-js": "^0.7.39",
89
89
  "@types/validator": "^13.15.2",
@@ -91,5 +91,5 @@
91
91
  "jest": "^29.7.0",
92
92
  "unbuild": "^2.0.0"
93
93
  },
94
- "gitHead": "d6da30f57d7f5f855462aa75b4189fb8be48fecc"
94
+ "gitHead": "bbd2b3c8caecf454ee738676f1ca5fe0e71fdb6a"
95
95
  }
@@ -28,6 +28,10 @@ export default function NotificationAddon({ session = {} }) {
28
28
  const { enqueueSnackbar } = useSnackbar();
29
29
  const browser = useBrowser();
30
30
 
31
+ const isPreviewUser = useMemo(() => {
32
+ return !userDid || user?.isPreviewUser || userDid === window.blocklet.did;
33
+ }, [userDid, user]);
34
+
31
35
  // 在 ArcSphere 和 Wallet 端隐藏, 消息的 toast 提示,因为有 native 的通知
32
36
  const hiddenNotificationToast = useMemo(() => {
33
37
  return browser.arcSphere || browser.wallet;
@@ -87,17 +91,17 @@ export default function NotificationAddon({ session = {} }) {
87
91
  );
88
92
 
89
93
  useEffect(() => {
90
- if (wsClient) {
94
+ if (wsClient && !isPreviewUser) {
91
95
  wsClient.on(listenEvent, listenCallback);
92
96
  wsClient.on(readListenEvent, readListenCallback);
93
97
  }
94
98
  return () => {
95
- if (wsClient) {
99
+ if (wsClient && !isPreviewUser) {
96
100
  wsClient.off(listenEvent, listenCallback);
97
101
  wsClient.off(readListenEvent, readListenCallback);
98
102
  }
99
103
  };
100
- }, [wsClient, setUnReadCount, listenCallback, listenEvent, readListenCallback, readListenEvent]);
104
+ }, [wsClient, setUnReadCount, listenCallback, listenEvent, readListenCallback, readListenEvent, isPreviewUser]);
101
105
 
102
106
  if (!session.user || !viewAllUrl) {
103
107
  return null;
package/src/common/ws.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { use } from 'react';
2
2
  import { WsClient } from '@arcblock/ws';
3
+ import { useCreation, usePrevious, useUnmount } from 'ahooks';
3
4
  import { SessionContext } from '@arcblock/did-connect-react/lib/Session';
4
5
  import { joinURL } from 'ufo';
5
6
  import { BLOCKLET_SERVICE_PATH_PREFIX } from '@arcblock/ux/lib/Util/constant';
@@ -29,6 +30,35 @@ export const useListenWsClient = (endpoint = 'admin') => {
29
30
  const sessionCtx = use(SessionContext);
30
31
  const { session } = sessionCtx ?? {};
31
32
 
33
+ const userDid = useCreation(() => session.user?.did, [session.user]);
34
+
35
+ const previousUserDid = usePrevious(userDid);
36
+
37
+ useUnmount(() => {
38
+ Object.keys(client).forEach((key) => {
39
+ if (client[key]?.isConnected()) {
40
+ client[key]?.disconnect();
41
+ }
42
+ delete client[key];
43
+ });
44
+ });
45
+
46
+ if (!userDid) {
47
+ return null;
48
+ }
49
+
50
+ // 同步检查:如果用户切换了,断开并重新连接所有客户端
51
+ if (previousUserDid && previousUserDid !== userDid) {
52
+ Object.keys(client).forEach((key) => {
53
+ if (client[key]) {
54
+ if (client[key].isConnected()) {
55
+ client[key].disconnect();
56
+ }
57
+ // 重新连接
58
+ client[key].connect();
59
+ }
60
+ });
61
+ }
32
62
  if (!client[endpoint] && session.user) {
33
63
  client[endpoint] = getWsClient(endpoint);
34
64
  client[endpoint].connect();