@arcblock/ux 2.5.12 → 2.5.13

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.
@@ -23,10 +23,14 @@ var _Switch = _interopRequireDefault(require("@arcblock/icons/lib/Switch"));
23
23
 
24
24
  var _PersonOutline = _interopRequireDefault(require("@mui/icons-material/PersonOutline"));
25
25
 
26
+ var _Link = _interopRequireDefault(require("@mui/icons-material/Link"));
27
+
26
28
  var _VpnKeyOutlined = _interopRequireDefault(require("@mui/icons-material/VpnKeyOutlined"));
27
29
 
28
30
  var _useBrowser = _interopRequireDefault(require("@arcblock/react-hooks/lib/useBrowser"));
29
31
 
32
+ var _OAuth = require("@arcblock/did-connect/lib/OAuth");
33
+
30
34
  var _Theme = require("../Theme");
31
35
 
32
36
  var _Avatar = _interopRequireDefault(require("../Avatar"));
@@ -37,7 +41,7 @@ var _jsxRuntime = require("react/jsx-runtime");
37
41
 
38
42
  var _templateObject;
39
43
 
40
- const _excluded = ["session", "locale", "showText", "showRole", "switchDid", "switchProfile", "switchPassport", "disableLogout", "onLogin", "onLogout", "onSwitchDid", "onSwitchProfile", "onSwitchPassport", "menu", "menuRender", "dark", "size"],
44
+ const _excluded = ["session", "locale", "showText", "showRole", "switchDid", "switchProfile", "switchPassport", "disableLogout", "onLogin", "onLogout", "onSwitchDid", "onSwitchProfile", "onSwitchPassport", "onBindWallet", "menu", "menuRender", "dark", "size"],
41
45
  _excluded2 = ["svgIcon"];
42
46
 
43
47
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -72,9 +76,19 @@ const messages = {
72
76
  openInWallet: 'Open DID Wallet'
73
77
  }
74
78
  };
79
+ const translations = {
80
+ en: {
81
+ alreadyBindOAuth: 'Already bind OAuth',
82
+ bind: 'Bind '
83
+ },
84
+ zh: {
85
+ alreadyBindOAuth: '已绑定 OAuth 账号',
86
+ bind: '绑定'
87
+ }
88
+ };
75
89
 
76
90
  function SessionManager(_ref) {
77
- var _session$user, _session$user$avatar, _session$user3, _session$user4;
91
+ var _session$user, _session$user$avatar, _session$user3, _session$user4, _session$user5, _session$user6, _session$user7, _session$user8, _session$user9;
78
92
 
79
93
  let {
80
94
  session,
@@ -90,6 +104,7 @@ function SessionManager(_ref) {
90
104
  onSwitchDid,
91
105
  onSwitchProfile,
92
106
  onSwitchPassport,
107
+ onBindWallet,
93
108
  menu,
94
109
  menuRender,
95
110
  dark,
@@ -98,6 +113,11 @@ function SessionManager(_ref) {
98
113
  rest = _objectWithoutProperties(_ref, _excluded);
99
114
 
100
115
  const userAnchorRef = (0, _react.useRef)(null);
116
+ const {
117
+ logoutOAuth,
118
+ bindOAuth,
119
+ configs: oauthConfigs
120
+ } = (0, _OAuth.useOAuth)();
101
121
  const [userOpen, setUserOpen] = (0, _react.useState)(false); // base64 img maybe have some blank char, need encodeURIComponent to transform it
102
122
 
103
123
  const avatar = (_session$user = session.user) === null || _session$user === void 0 ? void 0 : (_session$user$avatar = _session$user.avatar) === null || _session$user$avatar === void 0 ? void 0 : _session$user$avatar.replace(/\s/g, encodeURIComponent(' '));
@@ -107,6 +127,14 @@ function SessionManager(_ref) {
107
127
  return (_session$user2 = session.user) === null || _session$user2 === void 0 ? void 0 : (_session$user2$passpo = _session$user2.passports) === null || _session$user2$passpo === void 0 ? void 0 : _session$user2$passpo.find(item => item.name === session.user.role);
108
128
  }, [session.user]);
109
129
  const browser = (0, _useBrowser.default)();
130
+ const hasBindOAuth = ((_session$user3 = session.user) === null || _session$user3 === void 0 ? void 0 : _session$user3.source) === 'wallet' && ((_session$user4 = session.user) === null || _session$user4 === void 0 ? void 0 : _session$user4.derivedAccount);
131
+ const isDerivedAccount = ((_session$user5 = session.user) === null || _session$user5 === void 0 ? void 0 : _session$user5.source) === 'derived';
132
+ const oauthConfigList = Object.entries(oauthConfigs).map(_ref2 => {
133
+ let [key, value] = _ref2;
134
+ return _objectSpread(_objectSpread({}, value), {}, {
135
+ provider: key
136
+ });
137
+ }).filter(item => item.enabled);
110
138
 
111
139
  if (!session.user) {
112
140
  return showText ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.Button, _objectSpread(_objectSpread({
@@ -159,8 +187,14 @@ function SessionManager(_ref) {
159
187
 
160
188
  function _onLogout() {
161
189
  session.logout(function () {
162
- setUserOpen(false);
163
- onLogout(...arguments);
190
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
191
+ args[_key] = arguments[_key];
192
+ }
193
+
194
+ logoutOAuth().then(() => {
195
+ setUserOpen(false);
196
+ onLogout(...args);
197
+ });
164
198
  });
165
199
  }
166
200
 
@@ -186,6 +220,19 @@ function SessionManager(_ref) {
186
220
  });
187
221
  }
188
222
 
223
+ function _onBindWallet() {
224
+ setUserOpen(false);
225
+
226
+ if (session.user.source === 'derived') {
227
+ session.bindWallet(function () {
228
+ setUserOpen(false);
229
+ onBindWallet(...arguments);
230
+ });
231
+ } else {
232
+ bindOAuth();
233
+ }
234
+ }
235
+
189
236
  const message = messages[locale] || messages.en;
190
237
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
191
238
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_material.IconButton, _objectSpread(_objectSpread({
@@ -247,8 +294,8 @@ function SessionManager(_ref) {
247
294
  className: "session-manager-user-name",
248
295
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
249
296
  children: session.user.fullName
250
- }), !!showRole && ((currentRole === null || currentRole === void 0 ? void 0 : currentRole.title) || ((_session$user3 = session.user) === null || _session$user3 === void 0 ? void 0 : _session$user3.role.toUpperCase())) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.Chip, {
251
- label: (currentRole === null || currentRole === void 0 ? void 0 : currentRole.title) || ((_session$user4 = session.user) === null || _session$user4 === void 0 ? void 0 : _session$user4.role.toUpperCase()),
297
+ }), !!showRole && ((currentRole === null || currentRole === void 0 ? void 0 : currentRole.title) || ((_session$user6 = session.user) === null || _session$user6 === void 0 ? void 0 : _session$user6.role.toUpperCase())) && /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.Chip, {
298
+ label: (currentRole === null || currentRole === void 0 ? void 0 : currentRole.title) || ((_session$user7 = session.user) === null || _session$user7 === void 0 ? void 0 : _session$user7.role.toUpperCase()),
252
299
  size: "small",
253
300
  variant: "outlined",
254
301
  sx: {
@@ -262,7 +309,7 @@ function SessionManager(_ref) {
262
309
  })]
263
310
  }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Address.default, {
264
311
  responsive: false,
265
- children: session.user.did
312
+ children: isDerivedAccount ? (_session$user8 = session.user) === null || _session$user8 === void 0 ? void 0 : _session$user8.email : session.user.did
266
313
  })]
267
314
  }), Array.isArray(menu) && menu.map((menuItem, index) => {
268
315
  const {
@@ -320,6 +367,21 @@ function SessionManager(_ref) {
320
367
  component: _VpnKeyOutlined.default,
321
368
  className: "session-manager-menu-icon"
322
369
  }), message.switchPassport]
370
+ }), oauthConfigList.length > 0 && !hasBindOAuth && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.MenuItem, {
371
+ className: "session-manager-menu-item",
372
+ onClick: _onBindWallet,
373
+ "data-cy": "sessionManager-bind-trigger",
374
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_material.SvgIcon, {
375
+ component: _Link.default,
376
+ className: "session-manager-menu-icon"
377
+ }), ((_session$user9 = session.user) === null || _session$user9 === void 0 ? void 0 : _session$user9.source) === 'derived' ? "".concat(translations[locale].bind, "DID Wallet") : "".concat(translations[locale].bind, "OAuth")]
378
+ }), oauthConfigList.length > 0 && hasBindOAuth && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.MenuItem, {
379
+ className: "session-manager-menu-item",
380
+ disabled: true,
381
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_material.SvgIcon, {
382
+ component: _Link.default,
383
+ className: "session-manager-menu-icon"
384
+ }), translations[locale].alreadyBindOAuth]
323
385
  }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.MenuItem, {
324
386
  className: "session-manager-menu-item",
325
387
  onClick: _onLogout,
@@ -343,17 +405,22 @@ SessionManager.propTypes = {
343
405
  did: _propTypes.default.string.isRequired,
344
406
  role: _propTypes.default.string.isRequired,
345
407
  fullName: _propTypes.default.string,
408
+ email: _propTypes.default.string,
346
409
  avatar: _propTypes.default.string,
347
410
  passports: _propTypes.default.arrayOf(_propTypes.default.shape({
348
411
  name: _propTypes.default.string.isRequired,
349
412
  title: _propTypes.default.string.isRequired
350
- }))
413
+ })),
414
+ source: _propTypes.default.oneOf(['derived', 'wallet', undefined]),
415
+ derivedAccount: _propTypes.default.object,
416
+ connectedAccounts: _propTypes.default.array
351
417
  }),
352
418
  login: _propTypes.default.func.isRequired,
353
419
  logout: _propTypes.default.func.isRequired,
354
420
  switchDid: _propTypes.default.func.isRequired,
355
421
  switchProfile: _propTypes.default.func.isRequired,
356
- switchPassport: _propTypes.default.func.isRequired
422
+ switchPassport: _propTypes.default.func.isRequired,
423
+ bindWallet: _propTypes.default.func.isRequired
357
424
  }).isRequired,
358
425
  locale: _propTypes.default.string,
359
426
  showText: _propTypes.default.bool,
@@ -367,6 +434,7 @@ SessionManager.propTypes = {
367
434
  onSwitchDid: _propTypes.default.func,
368
435
  onSwitchProfile: _propTypes.default.func,
369
436
  onSwitchPassport: _propTypes.default.func,
437
+ onBindWallet: _propTypes.default.func,
370
438
  menu: _propTypes.default.array,
371
439
  menuRender: _propTypes.default.func,
372
440
  dark: _propTypes.default.bool,
@@ -390,18 +458,19 @@ SessionManager.defaultProps = {
390
458
  onSwitchDid: noop,
391
459
  onSwitchProfile: noop,
392
460
  onSwitchPassport: noop,
461
+ onBindWallet: noop,
393
462
  dark: false,
394
463
  size: 24
395
464
  };
396
- const StyledPopper = (0, _Theme.styled)(_material.Popper)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n z-index: ", ";\n .MuiList-root {\n width: 280px;\n }\n .session-manager-user {\n font-size: 12px;\n flex-direction: column;\n align-items: flex-start;\n padding: 24px 24px 10px;\n }\n .session-manager-user-name {\n font-size: 20px;\n color: ", ";\n font-weight: bold;\n margin-bottom: 10px;\n display: flex;\n align-items: center;\n justify-content: space-between;\n }\n .session-manager-menu-item {\n padding: 18.5px 24px;\n color: #777;\n font-size: 16px;\n &:hover {\n background-color: #fbfbfb;\n }\n }\n .session-manager-menu-icon {\n color: #999;\n margin-right: 16px;\n }\n"])), _ref2 => {
465
+ const StyledPopper = (0, _Theme.styled)(_material.Popper)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n z-index: ", ";\n .MuiList-root {\n width: 280px;\n }\n .session-manager-user {\n font-size: 12px;\n flex-direction: column;\n align-items: flex-start;\n padding: 24px 24px 10px;\n }\n .session-manager-user-name {\n font-size: 20px;\n color: ", ";\n font-weight: bold;\n margin-bottom: 10px;\n display: flex;\n align-items: center;\n justify-content: space-between;\n }\n .session-manager-menu-item {\n padding: 18.5px 24px;\n color: #777;\n font-size: 16px;\n &:hover {\n background-color: #fbfbfb;\n }\n }\n .session-manager-menu-icon {\n color: #999;\n margin-right: 16px;\n }\n"])), _ref3 => {
397
466
  let {
398
467
  theme
399
- } = _ref2;
468
+ } = _ref3;
400
469
  return theme.zIndex.tooltip;
401
- }, _ref3 => {
470
+ }, _ref4 => {
402
471
  let {
403
472
  $dark
404
- } = _ref3;
473
+ } = _ref4;
405
474
  return $dark ? '#aaa' : '#222';
406
475
  });
407
476
  var _default = SessionManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ux",
3
- "version": "2.5.12",
3
+ "version": "2.5.13",
4
4
  "description": "Common used react components for arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -47,11 +47,11 @@
47
47
  "react": ">=18.1.0",
48
48
  "react-ga": "^2.7.0"
49
49
  },
50
- "gitHead": "b125a20f22ff5b3e57ebb117fb0cbe4120ceb44f",
50
+ "gitHead": "400e7898ea61963ddcdc17db55e815a321b61566",
51
51
  "dependencies": {
52
52
  "@arcblock/did-motif": "^1.1.10",
53
- "@arcblock/icons": "^2.5.12",
54
- "@arcblock/react-hooks": "^2.5.12",
53
+ "@arcblock/icons": "^2.5.13",
54
+ "@arcblock/react-hooks": "^2.5.13",
55
55
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
56
56
  "@emotion/react": "^11.10.4",
57
57
  "@emotion/styled": "^11.10.4",
@@ -9,10 +9,12 @@ import OpenInIcon from '@arcblock/icons/lib/OpenIn';
9
9
  import DisconnectIcon from '@arcblock/icons/lib/Disconnect';
10
10
  import SwitchDidIcon from '@arcblock/icons/lib/Switch';
11
11
  import SwitchProfileIcon from '@mui/icons-material/PersonOutline';
12
+ import BindWalletIcon from '@mui/icons-material/Link';
12
13
  import SwitchPassportIcon from '@mui/icons-material/VpnKeyOutlined';
13
14
  import useBrowser from '@arcblock/react-hooks/lib/useBrowser';
14
- import { styled } from '../Theme';
15
+ import { useOAuth } from '@arcblock/did-connect/lib/OAuth';
15
16
 
17
+ import { styled } from '../Theme';
16
18
  import DidAvatar from '../Avatar';
17
19
  import DidAddress from '../Address';
18
20
 
@@ -34,6 +36,16 @@ const messages = {
34
36
  openInWallet: 'Open DID Wallet',
35
37
  },
36
38
  };
39
+ const translations = {
40
+ en: {
41
+ alreadyBindOAuth: 'Already bind OAuth',
42
+ bind: 'Bind ',
43
+ },
44
+ zh: {
45
+ alreadyBindOAuth: '已绑定 OAuth 账号',
46
+ bind: '绑定',
47
+ },
48
+ };
37
49
 
38
50
  function SessionManager({
39
51
  session,
@@ -49,6 +61,7 @@ function SessionManager({
49
61
  onSwitchDid,
50
62
  onSwitchProfile,
51
63
  onSwitchPassport,
64
+ onBindWallet,
52
65
  menu,
53
66
  menuRender,
54
67
  dark,
@@ -56,6 +69,7 @@ function SessionManager({
56
69
  ...rest
57
70
  }) {
58
71
  const userAnchorRef = useRef(null);
72
+ const { logoutOAuth, bindOAuth, configs: oauthConfigs } = useOAuth();
59
73
  const [userOpen, setUserOpen] = useState(false);
60
74
 
61
75
  // base64 img maybe have some blank char, need encodeURIComponent to transform it
@@ -66,6 +80,15 @@ function SessionManager({
66
80
  );
67
81
  const browser = useBrowser();
68
82
 
83
+ const hasBindOAuth = session.user?.source === 'wallet' && session.user?.derivedAccount;
84
+ const isDerivedAccount = session.user?.source === 'derived';
85
+
86
+ const oauthConfigList = Object.entries(oauthConfigs)
87
+ .map(([key, value]) => {
88
+ return { ...value, provider: key };
89
+ })
90
+ .filter((item) => item.enabled);
91
+
69
92
  if (!session.user) {
70
93
  return showText ? (
71
94
  <Button
@@ -101,8 +124,10 @@ function SessionManager({
101
124
  }
102
125
  function _onLogout() {
103
126
  session.logout((...args) => {
104
- setUserOpen(false);
105
- onLogout(...args);
127
+ logoutOAuth().then(() => {
128
+ setUserOpen(false);
129
+ onLogout(...args);
130
+ });
106
131
  });
107
132
  }
108
133
  function _onSwitchDid() {
@@ -125,6 +150,18 @@ function SessionManager({
125
150
  });
126
151
  }
127
152
 
153
+ function _onBindWallet() {
154
+ setUserOpen(false);
155
+ if (session.user.source === 'derived') {
156
+ session.bindWallet((...args) => {
157
+ setUserOpen(false);
158
+ onBindWallet(...args);
159
+ });
160
+ } else {
161
+ bindOAuth();
162
+ }
163
+ }
164
+
128
165
  const message = messages[locale] || messages.en;
129
166
 
130
167
  return (
@@ -188,7 +225,9 @@ function SessionManager({
188
225
  />
189
226
  )}
190
227
  </div>
191
- <DidAddress responsive={false}>{session.user.did}</DidAddress>
228
+ <DidAddress responsive={false}>
229
+ {isDerivedAccount ? session.user?.email : session.user.did}
230
+ </DidAddress>
192
231
  </div>
193
232
  {Array.isArray(menu) &&
194
233
  menu.map((menuItem, index) => {
@@ -253,6 +292,24 @@ function SessionManager({
253
292
  {message.switchPassport}
254
293
  </MenuItem>
255
294
  )}
295
+ {oauthConfigList.length > 0 && !hasBindOAuth && (
296
+ <MenuItem
297
+ className="session-manager-menu-item"
298
+ onClick={_onBindWallet}
299
+ data-cy="sessionManager-bind-trigger">
300
+ <SvgIcon component={BindWalletIcon} className="session-manager-menu-icon" />
301
+ {session.user?.source === 'derived'
302
+ ? `${translations[locale].bind}DID Wallet`
303
+ : `${translations[locale].bind}OAuth`}
304
+ </MenuItem>
305
+ )}
306
+ {oauthConfigList.length > 0 && hasBindOAuth && (
307
+ <MenuItem className="session-manager-menu-item" disabled>
308
+ <SvgIcon component={BindWalletIcon} className="session-manager-menu-icon" />
309
+ {translations[locale].alreadyBindOAuth}
310
+ </MenuItem>
311
+ )}
312
+
256
313
  <MenuItem
257
314
  className="session-manager-menu-item"
258
315
  onClick={_onLogout}
@@ -276,6 +333,7 @@ SessionManager.propTypes = {
276
333
  did: PropTypes.string.isRequired,
277
334
  role: PropTypes.string.isRequired,
278
335
  fullName: PropTypes.string,
336
+ email: PropTypes.string,
279
337
  avatar: PropTypes.string,
280
338
  passports: PropTypes.arrayOf(
281
339
  PropTypes.shape({
@@ -283,12 +341,16 @@ SessionManager.propTypes = {
283
341
  title: PropTypes.string.isRequired,
284
342
  })
285
343
  ),
344
+ source: PropTypes.oneOf(['derived', 'wallet', undefined]),
345
+ derivedAccount: PropTypes.object,
346
+ connectedAccounts: PropTypes.array,
286
347
  }),
287
348
  login: PropTypes.func.isRequired,
288
349
  logout: PropTypes.func.isRequired,
289
350
  switchDid: PropTypes.func.isRequired,
290
351
  switchProfile: PropTypes.func.isRequired,
291
352
  switchPassport: PropTypes.func.isRequired,
353
+ bindWallet: PropTypes.func.isRequired,
292
354
  }).isRequired,
293
355
  locale: PropTypes.string,
294
356
  showText: PropTypes.bool,
@@ -302,6 +364,7 @@ SessionManager.propTypes = {
302
364
  onSwitchDid: PropTypes.func,
303
365
  onSwitchProfile: PropTypes.func,
304
366
  onSwitchPassport: PropTypes.func,
367
+ onBindWallet: PropTypes.func,
305
368
  menu: PropTypes.array,
306
369
  menuRender: PropTypes.func,
307
370
  dark: PropTypes.bool,
@@ -325,6 +388,7 @@ SessionManager.defaultProps = {
325
388
  onSwitchDid: noop,
326
389
  onSwitchProfile: noop,
327
390
  onSwitchPassport: noop,
391
+ onBindWallet: noop,
328
392
  dark: false,
329
393
  size: 24,
330
394
  };