@arcblock/ux 2.7.15 → 2.7.17

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.
Files changed (57) hide show
  1. package/es/Dialog/confirm.js +10 -8
  2. package/es/Img/index.js +7 -7
  3. package/es/SessionManager/account-item.js +133 -0
  4. package/es/SessionManager/add-account-item.js +100 -0
  5. package/es/SessionManager/federated-login-detecter.js +37 -33
  6. package/es/SessionManager/index.js +119 -259
  7. package/es/SessionManager/manage-accounts.js +156 -0
  8. package/es/SessionManager/manage-blocklet.js +70 -0
  9. package/es/SessionManager/menu-accordion.js +104 -0
  10. package/es/SessionManager/translation.js +52 -0
  11. package/es/SessionManager/use-config.js +34 -0
  12. package/es/SessionManager/user-info.js +147 -0
  13. package/es/SessionManager/user-popper.js +10 -53
  14. package/es/SessionManager/utils.js +2 -0
  15. package/es/Typography/index.js +89 -0
  16. package/es/Util/federated.js +65 -0
  17. package/es/Util/index.js +7 -0
  18. package/lib/Dialog/confirm.js +9 -7
  19. package/lib/Img/index.js +7 -7
  20. package/lib/SessionManager/account-item.js +141 -0
  21. package/lib/SessionManager/add-account-item.js +108 -0
  22. package/lib/SessionManager/federated-login-detecter.js +38 -33
  23. package/lib/SessionManager/index.js +122 -272
  24. package/lib/SessionManager/manage-accounts.js +168 -0
  25. package/lib/SessionManager/manage-blocklet.js +86 -0
  26. package/lib/SessionManager/menu-accordion.js +112 -0
  27. package/lib/SessionManager/translation.js +59 -0
  28. package/lib/SessionManager/use-config.js +41 -0
  29. package/lib/SessionManager/user-info.js +163 -0
  30. package/lib/SessionManager/user-popper.js +8 -8
  31. package/lib/SessionManager/utils.js +16 -0
  32. package/lib/Typography/index.js +100 -0
  33. package/lib/Util/federated.js +85 -0
  34. package/lib/Util/index.js +11 -2
  35. package/package.json +12 -5
  36. package/src/Dialog/confirm.js +9 -6
  37. package/src/Img/index.js +5 -5
  38. package/src/SessionManager/account-item.jsx +109 -0
  39. package/src/SessionManager/add-account-item.jsx +97 -0
  40. package/src/SessionManager/federated-login-detecter.jsx +42 -29
  41. package/src/SessionManager/index.jsx +131 -259
  42. package/src/SessionManager/manage-accounts.jsx +157 -0
  43. package/src/SessionManager/manage-blocklet.jsx +70 -0
  44. package/src/SessionManager/menu-accordion.jsx +94 -0
  45. package/src/SessionManager/translation.js +52 -0
  46. package/src/SessionManager/use-config.js +33 -0
  47. package/src/SessionManager/user-info.jsx +116 -0
  48. package/src/SessionManager/user-popper.jsx +6 -51
  49. package/src/SessionManager/utils.js +3 -0
  50. package/src/Typography/index.jsx +79 -0
  51. package/src/Util/federated.js +73 -0
  52. package/src/Util/index.js +8 -0
  53. /package/src/Avatar/{did-motif.js → did-motif.jsx} +0 -0
  54. /package/src/Avatar/{index.js → index.jsx} +0 -0
  55. /package/src/Header/{auto-hidden.js → auto-hidden.jsx} +0 -0
  56. /package/src/Header/{header.js → header.jsx} +0 -0
  57. /package/src/Header/{responsive-header.js → responsive-header.jsx} +0 -0
@@ -1,60 +1,27 @@
1
- /* eslint-disable react/no-array-index-key */
1
+ /* eslint-disable react/prop-types */
2
2
  /* eslint-disable react/jsx-no-bind */
3
- import { useMemo, useRef, useState } from 'react';
3
+ import { useEffect, useMemo, useRef, useState } from 'react';
4
4
  import PropTypes from 'prop-types';
5
- import { Box, IconButton, MenuList, MenuItem, SvgIcon, Button, Chip, Link, CircularProgress } from '@mui/material';
6
- import SwitchProfileIcon from '@mui/icons-material/PersonOutline';
7
- import BindWalletIcon from '@mui/icons-material/Link';
8
- import SwitchPassportIcon from '@mui/icons-material/VpnKeyOutlined';
9
- import ShieldCheck from 'mdi-material-ui/ShieldCheck';
5
+ import { Box, IconButton, MenuList, Button, CircularProgress, Divider } from '@mui/material';
10
6
  import AccountIcon from '@arcblock/icons/lib/Account';
11
- import OpenInIcon from '@arcblock/icons/lib/OpenIn';
12
- import DisconnectIcon from '@arcblock/icons/lib/Disconnect';
13
- import SwitchDidIcon from '@arcblock/icons/lib/Switch';
14
- import useBrowser from '@arcblock/react-hooks/lib/useBrowser';
15
7
  import noop from 'lodash/noop';
16
- import isEmpty from 'lodash/isEmpty';
8
+ import cloneDeep from 'lodash/cloneDeep';
9
+ import { useLatest, useMemoizedFn } from 'ahooks';
17
10
  import DidAvatar from '../Avatar';
18
- import DidAddress from '../Address';
19
- import { getUserAvatar } from '../Util';
11
+ import { getUserAvatar, sleep } from '../Util';
20
12
  import FederatedLoginDetecter from './federated-login-detecter';
21
13
  import UserPopper from './user-popper';
14
+ import UserInfo from './user-info';
15
+ import { translate } from '../Locale/util';
16
+ import ManageAccounts from './manage-accounts';
17
+ import ManageBlocklet from './manage-blocklet';
18
+ import { translations } from './translation';
19
+ import { getConnectedAccounts, getSourceProvider } from './utils';
20
+ import useConfig from './use-config';
21
+ import { getCurrentApp, getFederatedApp } from '../Util/federated';
22
22
  import { jsx as _jsx } from "react/jsx-runtime";
23
23
  import { jsxs as _jsxs } from "react/jsx-runtime";
24
24
  import { Fragment as _Fragment } from "react/jsx-runtime";
25
- const translations = {
26
- en: {
27
- account: 'account',
28
- switchDid: 'Switch DID',
29
- switchTo: 'Switch to',
30
- switchProfile: 'Switch Profile',
31
- switchPassport: 'Switch Passport',
32
- disconnect: 'Disconnect',
33
- connect: 'Connect',
34
- openInWallet: 'Open DID Wallet',
35
- alreadyBindOAuth: 'Already bind Auth0',
36
- bind: 'Bind ',
37
- thirdParty: 'Third Party Login',
38
- connectedWith: 'Connected with'
39
- },
40
- zh: {
41
- account: '账号',
42
- switchDid: '切换账户',
43
- switchTo: '切换至',
44
- switchProfile: '切换用户信息',
45
- switchPassport: '切换通行证',
46
- disconnect: '退出',
47
- connect: '登录',
48
- openInWallet: '打开 DID 钱包',
49
- // NOTE: 目前只有 Auth0,展示出具体的第三方名字会更好
50
- alreadyBindOAuth: '已绑定 Auth0 账号',
51
- bind: '绑定',
52
- thirdParty: '第三方登录',
53
- connectedWith: '连接至'
54
- }
55
- };
56
- const getConnectedAccounts = user => user?.connectedAccounts || user?.extraConfigs?.connectedAccounts || [];
57
- const getSourceProvider = user => user?.sourceProvider || user?.extraConfigs?.sourceProvider || 'wallet';
58
25
  function SessionManager({
59
26
  session,
60
27
  locale,
@@ -74,32 +41,32 @@ function SessionManager({
74
41
  menuRender,
75
42
  dark,
76
43
  size,
44
+ showManageBlocklet,
77
45
  ...rest
78
46
  }) {
79
- const translation = translations[locale] || translations.en;
47
+ const latestSession = useLatest(session);
48
+ const {
49
+ connectAccount,
50
+ config,
51
+ setConfig
52
+ } = useConfig();
53
+ const t = useMemoizedFn((key, data = {}) => {
54
+ return translate(translations, key, locale, 'en', data);
55
+ });
80
56
  const userAnchorRef = useRef(null);
81
- // eslint-disable-next-line react/prop-types
82
57
  const {
83
58
  logoutOAuth,
84
- bindOAuth,
85
- configs: oauthConfigs,
86
59
  switchOAuthPassport
87
60
  } = session.useOAuth();
88
61
  const [userOpen, setUserOpen] = useState(false);
89
62
 
90
63
  // base64 img maybe have some blank char, need encodeURIComponent to transform it
91
64
  const avatar = getUserAvatar(session.user?.avatar?.replace(/\s/g, encodeURIComponent(' ')));
92
- const currentRole = useMemo(() => session.user?.passports?.find(item => item.name === session.user.role), [session.user]);
93
- const browser = useBrowser();
94
- // eslint-disable-next-line react/prop-types
95
- const {
96
- walletDid
97
- } = session.useDid({
98
- session
99
- });
100
65
  const isRawWalletAccount = getSourceProvider(session.user) === 'wallet';
101
66
  const connectedAccounts = getConnectedAccounts(session.user);
102
- const federatedAccount = connectedAccounts.find(item => item.provider === 'federated');
67
+ const isFirstLoading = useMemo(() => {
68
+ return session?.initialized === false && session?.loading === true;
69
+ }, [session?.initialized, session?.loading]);
103
70
  let hasBindWallet = false;
104
71
  let hasBindAccount = false;
105
72
  if (isRawWalletAccount) {
@@ -111,19 +78,38 @@ function SessionManager({
111
78
  hasBindAccount = true;
112
79
  hasBindWallet = true;
113
80
  }
114
- const oauthConfigList = Object.entries(oauthConfigs).map(([key, value]) => {
115
- return {
116
- ...value,
117
- provider: key
118
- };
119
- }).filter(item => item.enabled);
120
- const isFirstLoading = useMemo(() => {
121
- // eslint-disable-next-line react/prop-types
122
- return session?.initialized === false && session?.loading === true;
123
- // eslint-disable-next-line react/prop-types
124
- }, [session?.initialized, session?.loading]);
125
- const masterSiteInfo = window.blocklet?.settings?.federated?.master;
126
- const currentSiteInfo = window.blocklet;
81
+ const _connectAccount = useMemoizedFn(async () => {
82
+ // HACK: 强制等待组件渲染一轮,以拿到最新的 session 值
83
+ await sleep();
84
+ if (!latestSession.current?.user) {
85
+ return;
86
+ }
87
+ if (typeof window === 'undefined') {
88
+ return;
89
+ }
90
+ const blocklet = window?.blocklet;
91
+ const currentApp = getCurrentApp(blocklet);
92
+ const masterApp = getFederatedApp(blocklet);
93
+ const targetApp = latestSession.current.user.sourceAppPid ? masterApp : currentApp;
94
+ if (targetApp) {
95
+ const loginAccount = {
96
+ did: latestSession.current.user.did,
97
+ avatar: latestSession.current.user.avatar,
98
+ provider: latestSession.current.provider,
99
+ ...targetApp
100
+ };
101
+ connectAccount(loginAccount);
102
+ }
103
+ });
104
+
105
+ // HACK: 用于处理 统一登录 的自动登录情况,添加一个已连接的账号
106
+ // 同时可用于以前的站点,会自动生成一个已连接的账号
107
+ useEffect(() => {
108
+ if (session.user) {
109
+ _connectAccount();
110
+ }
111
+ // eslint-disable-next-line react-hooks/exhaustive-deps
112
+ }, [session.user]);
127
113
  if (!session.user) {
128
114
  return /*#__PURE__*/_jsxs(_Fragment, {
129
115
  children: [showText ? /*#__PURE__*/_jsxs(Button, {
@@ -144,7 +130,7 @@ function SessionManager({
144
130
  style: {
145
131
  lineHeight: '25px'
146
132
  },
147
- children: translation.connect
133
+ children: t('connect')
148
134
  })]
149
135
  }) : /*#__PURE__*/_jsx(IconButton, {
150
136
  ref: userAnchorRef,
@@ -185,15 +171,21 @@ function SessionManager({
185
171
  function onToggleUser() {
186
172
  setUserOpen(prevOpen => !prevOpen);
187
173
  }
174
+ function close() {
175
+ setUserOpen(false);
176
+ }
188
177
  function onCloseUser(e) {
189
178
  if (userAnchorRef.current && userAnchorRef.current.contains(e.target)) {
190
179
  return;
191
180
  }
192
- setUserOpen(false);
181
+ close();
193
182
  }
194
183
  function _onLogin() {
195
184
  if (!isFirstLoading) {
196
- session.login(onLogin);
185
+ session.login((...args) => {
186
+ _connectAccount();
187
+ onLogin(...args);
188
+ });
197
189
  }
198
190
  }
199
191
  function _onLogout() {
@@ -206,7 +198,7 @@ function SessionManager({
206
198
  }).catch(err => {
207
199
  console.error(err);
208
200
  }).finally(() => {
209
- setUserOpen(false);
201
+ close();
210
202
  });
211
203
  });
212
204
  }
@@ -214,29 +206,10 @@ function SessionManager({
214
206
  * @name 切换账户
215
207
  * @description 该功能仅在登录后才能使用,目前仅用于切换普通登录和统一登录的账户,所以会增加一些与统一登录相关的逻辑
216
208
  */
217
- function _onSwitchDid() {
218
- const {
219
- provider,
220
- user
221
- } = session;
222
- if (!user) return;
223
- let targetProvider = provider;
224
- if (provider === 'federated') {
225
- targetProvider = 'wallet';
226
- } else if (['auth0', 'wallet'].includes(provider)) {
227
- targetProvider = 'federated';
228
- }
229
- session.switchDid((...args) => {
230
- setUserOpen(false);
231
- onSwitchDid(...args);
232
- }, {
233
- provider: targetProvider,
234
- providerMode: 'paramsFirst'
235
- });
236
- }
209
+
237
210
  function _onSwitchProfile() {
238
211
  session.switchProfile((...args) => {
239
- setUserOpen(false);
212
+ close();
240
213
  onSwitchProfile(...args);
241
214
  });
242
215
  }
@@ -245,28 +218,16 @@ function SessionManager({
245
218
  user,
246
219
  provider
247
220
  } = session;
248
- if (!isRawWalletAccount && provider !== 'federated') {
221
+ if (['auth0'].includes(provider)) {
249
222
  switchOAuthPassport(user);
250
223
  } else {
251
- setUserOpen(false);
224
+ close();
252
225
  session.switchPassport((...args) => {
253
- setUserOpen(false);
226
+ close();
254
227
  onSwitchPassport(...args);
255
228
  });
256
229
  }
257
230
  }
258
- function _onBindWallet() {
259
- setUserOpen(false);
260
- // FIXME: @zhanghan 暂时切换回 isRawWalletAccount 的方式来判断,在 did-connect 改版时,简化这里的关系判断
261
- if (!isRawWalletAccount) {
262
- session.bindWallet((...args) => {
263
- setUserOpen(false);
264
- onBindWallet(...args);
265
- });
266
- } else {
267
- bindOAuth();
268
- }
269
- }
270
231
  return /*#__PURE__*/_jsxs(_Fragment, {
271
232
  children: [/*#__PURE__*/_jsx(IconButton, {
272
233
  ref: userAnchorRef,
@@ -295,150 +256,47 @@ function SessionManager({
295
256
  sx: {
296
257
  p: 0
297
258
  },
298
- children: [/*#__PURE__*/_jsxs("div", {
299
- className: "session-manager-user",
300
- children: [/*#__PURE__*/_jsxs("div", {
301
- className: "session-manager-user-name",
302
- role: "button",
303
- "aria-label": "User info panel",
304
- children: [/*#__PURE__*/_jsx("span", {
305
- children: session.user.fullName
306
- }), !!showRole && (currentRole?.title || session.user?.role.toUpperCase()) && /*#__PURE__*/_jsx(Chip, {
307
- label: currentRole?.title || session.user?.role.toUpperCase(),
308
- size: "small",
309
- variant: "outlined",
310
- sx: {
311
- height: 'auto',
312
- marginRight: 0
313
- },
314
- icon: /*#__PURE__*/_jsx(SvgIcon, {
315
- component: ShieldCheck,
316
- size: "small"
317
- })
318
- })]
319
- }), /*#__PURE__*/_jsxs("div", {
320
- className: "session-manager-id-list",
321
- "aria-label": "User DID list",
322
- children: [walletDid && /*#__PURE__*/_jsx("div", {
323
- className: "session-manager-id-item",
324
- children: /*#__PURE__*/_jsx(DidAddress, {
325
- responsive: false,
326
- children: walletDid
327
- })
328
- }), federatedAccount && /*#__PURE__*/_jsx("div", {
329
- className: "session-manager-id-item",
330
- children: /*#__PURE__*/_jsx(DidAddress, {
331
- responsive: false,
332
- children: federatedAccount.did
333
- })
334
- }), session?.user?.email && /*#__PURE__*/_jsx("div", {
335
- className: "session-manager-id-item",
336
- children: /*#__PURE__*/_jsx(DidAddress, {
337
- responsive: false,
338
- children: session.user.email
339
- })
340
- })]
341
- })]
342
- }), Array.isArray(menu) && menu.map((menuItem, index) => {
343
- const {
344
- svgIcon,
345
- ...menuProps
346
- } = menuItem;
347
- return /*#__PURE__*/_jsxs(MenuItem, {
348
- className: "session-manager-menu-item",
349
- ...menuProps,
350
- icon: undefined,
351
- label: undefined,
352
- children: [svgIcon ? svgIcon && /*#__PURE__*/_jsx(SvgIcon, {
353
- component: svgIcon,
354
- className: "session-manager-menu-icon"
355
- }) : menuItem.icon, menuItem.label]
356
- }, index);
357
- }), menuRender({
358
- classes: {
359
- menuItem: 'session-manager-menu-item',
360
- menuIcon: 'session-manager-menu-icon'
259
+ children: [/*#__PURE__*/_jsx(UserInfo, {
260
+ session: session,
261
+ size: size,
262
+ locale: locale,
263
+ onSwitchProfile: () => {
264
+ close();
265
+ _onSwitchProfile();
266
+ },
267
+ onSwitchPassport: () => {
268
+ close();
269
+ _onSwitchPassport();
270
+ },
271
+ close: close,
272
+ switchProfile: switchProfile,
273
+ hasBindWallet: hasBindWallet
274
+ }), /*#__PURE__*/_jsx(Divider, {}), /*#__PURE__*/_jsx(ManageAccounts, {
275
+ session: session,
276
+ locale: locale,
277
+ onBindWallet: onBindWallet,
278
+ onSwitchDid: onSwitchDid,
279
+ connectAccount: _connectAccount,
280
+ close: close,
281
+ hasBindAccount: hasBindAccount,
282
+ onLogout: _onLogout,
283
+ expanded: config.expandAccount,
284
+ onExpand: value => {
285
+ const cloneConfig = cloneDeep(config);
286
+ cloneConfig.expandAccount = value;
287
+ setConfig(cloneConfig);
361
288
  }
362
- }), !browser.wallet && /*#__PURE__*/_jsxs(MenuItem, {
363
- component: "a",
364
- className: "session-manager-menu-item",
365
- "data-cy": "sessionManager-openInWallet",
366
- href: "https://www.abtwallet.io/",
367
- "aria-label": translation.openInWallet,
368
- target: "_blank",
369
- children: [/*#__PURE__*/_jsx(SvgIcon, {
370
- component: OpenInIcon,
371
- className: "session-manager-menu-icon"
372
- }), translation.openInWallet]
373
- }), !isEmpty(masterSiteInfo) && !!switchDid && /*#__PURE__*/_jsxs(MenuItem, {
374
- className: "session-manager-menu-item",
375
- onClick: _onSwitchDid,
376
- "aria-label": translation.switchDid,
377
- "data-cy": "sessionManager-switch-trigger",
378
- children: [/*#__PURE__*/_jsx(SvgIcon, {
379
- component: SwitchDidIcon,
380
- className: "session-manager-menu-icon"
381
- }), /*#__PURE__*/_jsxs(Box, {
382
- sx: {
383
- whiteSpace: 'normal',
384
- wordBreak: 'break-all'
385
- },
386
- children: [translation.switchTo, session.provider === 'federated' ? /*#__PURE__*/_jsx(Link, {
387
- mx: 1,
388
- href: currentSiteInfo.appUrl,
389
- underline: "hover",
390
- target: "_blank",
391
- title: currentSiteInfo.appName,
392
- "aria-label": "Open current site url",
393
- children: currentSiteInfo.appName
394
- }) : /*#__PURE__*/_jsx(Link, {
395
- mx: 1,
396
- href: masterSiteInfo.appUrl,
397
- underline: "hover",
398
- target: "_blank",
399
- title: masterSiteInfo.appName,
400
- "aria-label": "Open federated master site url",
401
- children: masterSiteInfo.appName
402
- }), translation.account]
403
- })]
404
- }), !!switchProfile && hasBindWallet && session.provider !== 'federated' && /*#__PURE__*/_jsxs(MenuItem, {
405
- className: "session-manager-menu-item",
406
- onClick: _onSwitchProfile,
407
- "aria-label": translation.switchProfile,
408
- "data-cy": "sessionManager-switch-profile-trigger",
409
- children: [/*#__PURE__*/_jsx(SvgIcon, {
410
- component: SwitchProfileIcon,
411
- className: "session-manager-menu-icon"
412
- }), translation.switchProfile]
413
- }), !!switchPassport && /*#__PURE__*/_jsxs(MenuItem, {
414
- className: "session-manager-menu-item",
415
- onClick: _onSwitchPassport,
416
- "aria-label": translation.switchPassport,
417
- "data-cy": "sessionManager-switch-passport-trigger",
418
- children: [/*#__PURE__*/_jsx(SvgIcon, {
419
- component: SwitchPassportIcon,
420
- className: "session-manager-menu-icon"
421
- }), translation.switchPassport]
422
- }), oauthConfigList.length > 0 && !hasBindAccount && session.provider !== 'federated' && /*#__PURE__*/_jsxs(MenuItem, {
423
- className: "session-manager-menu-item",
424
- onClick: _onBindWallet,
425
- "aria-label": !isRawWalletAccount ? `${translation.bind}DID Wallet` : `${translation.bind}${translation.thirdParty}`,
426
- "data-cy": "sessionManager-bind-trigger",
427
- children: [/*#__PURE__*/_jsx(SvgIcon, {
428
- component: BindWalletIcon,
429
- className: "session-manager-menu-icon"
430
- }), !isRawWalletAccount ? `${translation.bind}DID Wallet` : `${translation.bind}${translation.thirdParty}`]
431
- }), /*#__PURE__*/_jsxs(MenuItem, {
432
- className: "session-manager-menu-item",
433
- onClick: _onLogout,
434
- disabled: disableLogout,
435
- "aria-label": "Logout account",
436
- "data-cy": "sessionManager-logout-trigger",
437
- children: [/*#__PURE__*/_jsx(SvgIcon, {
438
- component: DisconnectIcon,
439
- className: "session-manager-menu-icon"
440
- }), translation.disconnect]
441
- })]
289
+ }), showManageBlocklet ? [/*#__PURE__*/_jsx(Divider, {}, "divider"), /*#__PURE__*/_jsx(ManageBlocklet, {
290
+ menu: menu,
291
+ menuRender: menuRender,
292
+ locale: locale,
293
+ expanded: config.expandBlocklet,
294
+ onExpand: value => {
295
+ const cloneConfig = cloneDeep(config);
296
+ cloneConfig.expandBlocklet = value;
297
+ setConfig(cloneConfig);
298
+ }
299
+ }, "manageBlocklet")] : null]
442
300
  })
443
301
  })]
444
302
  });
@@ -446,7 +304,7 @@ function SessionManager({
446
304
  SessionManager.propTypes = {
447
305
  session: PropTypes.shape({
448
306
  federatedMaster: PropTypes.object,
449
- provider: PropTypes.oneOf(['wallet', 'federated', 'auth0', '']),
307
+ provider: PropTypes.oneOf(['wallet', 'auth0', '']),
450
308
  user: PropTypes.shape({
451
309
  did: PropTypes.string.isRequired,
452
310
  role: PropTypes.string.isRequired,
@@ -490,7 +348,8 @@ SessionManager.propTypes = {
490
348
  menu: PropTypes.array,
491
349
  menuRender: PropTypes.func,
492
350
  dark: PropTypes.bool,
493
- size: PropTypes.number
351
+ size: PropTypes.number,
352
+ showManageBlocklet: PropTypes.bool
494
353
  };
495
354
  SessionManager.defaultProps = {
496
355
  locale: 'en',
@@ -509,6 +368,7 @@ SessionManager.defaultProps = {
509
368
  onSwitchPassport: noop,
510
369
  onBindWallet: noop,
511
370
  dark: false,
512
- size: 24
371
+ size: 24,
372
+ showManageBlocklet: true
513
373
  };
514
374
  export default SessionManager;
@@ -0,0 +1,156 @@
1
+ /* eslint-disable react/prop-types */
2
+ /* eslint-disable react/jsx-no-bind */
3
+ import { useMemoizedFn, useUpdate } from 'ahooks';
4
+ import { MenuItem, SvgIcon } from '@mui/material';
5
+ import { Icon } from '@iconify/react';
6
+ import AccountIcon from '@arcblock/icons/lib/Account';
7
+ import LinkIcon from '@iconify-icons/mdi/link';
8
+ import DisconnectIcon from '@arcblock/icons/lib/Disconnect';
9
+ import AccountItem from './account-item';
10
+ import MenuAccordion from './menu-accordion';
11
+ import { translations } from './translation';
12
+ import { translate } from '../Locale/util';
13
+ import { useConfirm } from '../Dialog/confirm';
14
+ import { getSourceProvider } from './utils';
15
+ import AddAccountItem from './add-account-item';
16
+ import useConfig from './use-config';
17
+ import { jsx as _jsx } from "react/jsx-runtime";
18
+ import { Fragment as _Fragment } from "react/jsx-runtime";
19
+ import { jsxs as _jsxs } from "react/jsx-runtime";
20
+ export default function ManageAccounts({
21
+ session,
22
+ locale,
23
+ onBindWallet,
24
+ onSwitchDid,
25
+ connectAccount,
26
+ close,
27
+ hasBindAccount,
28
+ disableLogout,
29
+ onLogout,
30
+ expanded,
31
+ onExpand
32
+ }) {
33
+ const {
34
+ bindOAuth,
35
+ configs: oauthConfigs
36
+ } = session.useOAuth();
37
+ const t = useMemoizedFn((key, data = {}) => {
38
+ return translate(translations, key, locale, 'en', data);
39
+ });
40
+ const {
41
+ confirmApi,
42
+ confirmHolder
43
+ } = useConfirm();
44
+ const {
45
+ deleteAccount,
46
+ config
47
+ } = useConfig();
48
+ const update = useUpdate();
49
+ const onChoose = useMemoizedFn((account, {
50
+ active
51
+ }) => {
52
+ if (active) {
53
+ return;
54
+ }
55
+ close();
56
+ session.switchDid((...args) => {
57
+ connectAccount();
58
+ onSwitchDid(...args);
59
+ }, {
60
+ provider: account.provider,
61
+ sourceAppPid: account.sourceAppPid
62
+ });
63
+ });
64
+ const oauthConfigList = Object.entries(oauthConfigs).map(([key, value]) => {
65
+ return {
66
+ ...value,
67
+ provider: key
68
+ };
69
+ }).filter(item => item.enabled);
70
+ const isRawWalletAccount = getSourceProvider(session.user) === 'wallet';
71
+ const onDelete = useMemoizedFn((account, {
72
+ active
73
+ }) => {
74
+ if (active) {
75
+ return;
76
+ }
77
+ confirmApi.open({
78
+ title: t('deleteAccountTitle'),
79
+ content: t('deleteAccountContent'),
80
+ confirmButtonText: t('confirm'),
81
+ cancelButtonText: t('cancel'),
82
+ onConfirm(done) {
83
+ deleteAccount(account);
84
+ update();
85
+ done();
86
+ }
87
+ });
88
+ });
89
+ const onAdd = useMemoizedFn(app => {
90
+ close();
91
+ session.switchDid((...args) => {
92
+ connectAccount();
93
+ update();
94
+ onSwitchDid(...args);
95
+ }, {
96
+ sourceAppPid: app.sourceAppPid,
97
+ provider: app.provider
98
+ });
99
+ });
100
+ function _onBindWallet() {
101
+ close();
102
+ // FIXME: @zhanghan 暂时切换回 isRawWalletAccount 的方式来判断,在 did-connect 改版时,简化这里的关系判断
103
+ if (!isRawWalletAccount) {
104
+ session.bindWallet(onBindWallet);
105
+ } else {
106
+ bindOAuth();
107
+ }
108
+ }
109
+ return /*#__PURE__*/_jsxs(_Fragment, {
110
+ children: [/*#__PURE__*/_jsxs(MenuAccordion, {
111
+ expanded: expanded,
112
+ onChange: onExpand,
113
+ locale: locale,
114
+ title: /*#__PURE__*/_jsxs(_Fragment, {
115
+ children: [/*#__PURE__*/_jsx(AccountIcon, {
116
+ className: "session-manager-menu-icon",
117
+ style: {
118
+ width: 24,
119
+ height: 24
120
+ }
121
+ }), t('accounts')]
122
+ }),
123
+ children: [config.accounts.map(item => /*#__PURE__*/_jsx(AccountItem, {
124
+ account: item,
125
+ locale: locale,
126
+ active: session.user.did === item.did,
127
+ onDelete: onDelete,
128
+ onChoose: onChoose
129
+ }, item.did)), /*#__PURE__*/_jsx(AddAccountItem, {
130
+ locale: locale,
131
+ onAdd: onAdd
132
+ }), oauthConfigList.length > 0 && !hasBindAccount && /*#__PURE__*/_jsxs(MenuItem, {
133
+ className: "session-manager-menu-item",
134
+ onClick: _onBindWallet,
135
+ "aria-label": !isRawWalletAccount ? `${t('bind')}DID Wallet` : `${t('bind')}${t('thirdParty')}`,
136
+ "data-cy": "sessionManager-bind-trigger",
137
+ children: [/*#__PURE__*/_jsx(Icon, {
138
+ icon: LinkIcon,
139
+ width: 24,
140
+ height: 24,
141
+ className: "session-manager-menu-icon"
142
+ }), !isRawWalletAccount ? `${t('bind')}DID Wallet` : `${t('bind')}${t('thirdParty')}`]
143
+ }), /*#__PURE__*/_jsxs(MenuItem, {
144
+ className: "session-manager-menu-item",
145
+ onClick: onLogout,
146
+ disabled: disableLogout,
147
+ "aria-label": "Logout account",
148
+ "data-cy": "sessionManager-logout-trigger",
149
+ children: [/*#__PURE__*/_jsx(SvgIcon, {
150
+ component: DisconnectIcon,
151
+ className: "session-manager-menu-icon"
152
+ }), t('disconnect')]
153
+ })]
154
+ }), confirmHolder]
155
+ });
156
+ }