@arcblock/ux 2.9.6 → 2.9.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-prototype-builtins */
2
- import { createContext, useState, useEffect, useContext } from 'react';
2
+ import { createContext, useState, useEffect, useContext, useRef } from 'react';
3
3
  import get from 'lodash/get';
4
4
  import PropTypes from 'prop-types';
5
5
  import Cookie from 'js-cookie';
@@ -63,11 +63,15 @@ function LocaleProvider({
63
63
  locale,
64
64
  fallbackLocale,
65
65
  translations,
66
+ onLoadingTranslation,
66
67
  languages,
67
68
  ...rest
68
69
  }) {
69
70
  const langs = getLanguages(languages);
70
- const [currentLocale, setCurrentLocale] = useState(locale || getLocale(langs));
71
+ // eslint-disable-next-line prefer-const
72
+ let [currentLocale, setCurrentLocale] = useState(locale || getLocale(langs));
73
+ const lastCurrentLocale = useRef(fallbackLocale);
74
+ const [, setForceUpdate] = useState(0);
71
75
  const changeLocale = newLocale => {
72
76
  setCurrentLocale(newLocale);
73
77
  setLocale(newLocale);
@@ -79,6 +83,25 @@ function LocaleProvider({
79
83
  }
80
84
  // eslint-disable-next-line react-hooks/exhaustive-deps
81
85
  }, [locale]);
86
+
87
+ // handle async loading of translations
88
+ if (currentLocale && translations[currentLocale] && typeof translations[currentLocale] === 'function') {
89
+ const tmpLocale = currentLocale;
90
+ try {
91
+ Promise.resolve(translations[tmpLocale]()).then(res => {
92
+ if (res && typeof res === 'object') {
93
+ translations[tmpLocale] = res;
94
+ setForceUpdate(x => x > 999 ? 0 : x + 1);
95
+ }
96
+ });
97
+ } catch (err) {
98
+ console.error(err);
99
+ }
100
+ onLoadingTranslation(tmpLocale, langs);
101
+ currentLocale = lastCurrentLocale.current;
102
+ } else {
103
+ lastCurrentLocale.current = currentLocale;
104
+ }
82
105
  const t = (key, data) => translate(translations, key, currentLocale, fallbackLocale, data);
83
106
  return /*#__PURE__*/_jsx(Provider, {
84
107
  value: {
@@ -98,6 +121,7 @@ function useLocaleContext() {
98
121
  LocaleProvider.propTypes = {
99
122
  children: PropTypes.any.isRequired,
100
123
  translations: PropTypes.object.isRequired,
124
+ onLoadingTranslation: PropTypes.func,
101
125
  locale: PropTypes.string,
102
126
  // 会影响 translate(key) 的结果 - 当 key 无效时查找 fallbackLocale 对应的翻译结果
103
127
  fallbackLocale: PropTypes.string,
@@ -109,6 +133,7 @@ LocaleProvider.propTypes = {
109
133
  LocaleProvider.defaultProps = {
110
134
  locale: '',
111
135
  fallbackLocale: '',
112
- languages: []
136
+ languages: [],
137
+ onLoadingTranslation: () => {}
113
138
  };
114
139
  export { LocaleProvider, Consumer as LocaleConsumer, LocaleContext, useLocaleContext, setLocale, getLocale };
package/es/Locale/util.js CHANGED
@@ -8,6 +8,9 @@ export const translate = (translations, key, locale, fallbackLocale = 'en', data
8
8
  }
9
9
  return key;
10
10
  }
11
+ if (typeof translations[locale] === 'function' || translations[locale].then) {
12
+ return replace(translations[fallbackLocale][key], data);
13
+ }
11
14
  return replace(translations[locale][key], data);
12
15
  };
13
16
  export const t = translate;
@@ -39,7 +39,7 @@ export default function SessionBlocklet({
39
39
  const onTogglePopper = useMemoizedFn((value = !currentState.open) => {
40
40
  currentState.open = value;
41
41
  });
42
- if (sessionMenuList.length === 0) {
42
+ if (sessionMenuList.length === 0 || !session?.user) {
43
43
  return null;
44
44
  }
45
45
  return /*#__PURE__*/_jsxs(_Fragment, {
@@ -15,7 +15,7 @@ var _browserLang = _interopRequireDefault(require("./browser-lang"));
15
15
  var _util = require("./util");
16
16
  var _Util = require("../Util");
17
17
  var _jsxRuntime = require("react/jsx-runtime");
18
- const _excluded = ["children", "locale", "fallbackLocale", "translations", "languages"];
18
+ const _excluded = ["children", "locale", "fallbackLocale", "translations", "onLoadingTranslation", "languages"];
19
19
  /* eslint-disable no-prototype-builtins */
20
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
21
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -90,11 +90,15 @@ function LocaleProvider(_ref) {
90
90
  locale,
91
91
  fallbackLocale,
92
92
  translations,
93
+ onLoadingTranslation,
93
94
  languages
94
95
  } = _ref,
95
96
  rest = _objectWithoutProperties(_ref, _excluded);
96
97
  const langs = getLanguages(languages);
97
- const [currentLocale, setCurrentLocale] = (0, _react.useState)(locale || getLocale(langs));
98
+ // eslint-disable-next-line prefer-const
99
+ let [currentLocale, setCurrentLocale] = (0, _react.useState)(locale || getLocale(langs));
100
+ const lastCurrentLocale = (0, _react.useRef)(fallbackLocale);
101
+ const [, setForceUpdate] = (0, _react.useState)(0);
98
102
  const changeLocale = newLocale => {
99
103
  setCurrentLocale(newLocale);
100
104
  setLocale(newLocale);
@@ -106,6 +110,25 @@ function LocaleProvider(_ref) {
106
110
  }
107
111
  // eslint-disable-next-line react-hooks/exhaustive-deps
108
112
  }, [locale]);
113
+
114
+ // handle async loading of translations
115
+ if (currentLocale && translations[currentLocale] && typeof translations[currentLocale] === 'function') {
116
+ const tmpLocale = currentLocale;
117
+ try {
118
+ Promise.resolve(translations[tmpLocale]()).then(res => {
119
+ if (res && typeof res === 'object') {
120
+ translations[tmpLocale] = res;
121
+ setForceUpdate(x => x > 999 ? 0 : x + 1);
122
+ }
123
+ });
124
+ } catch (err) {
125
+ console.error(err);
126
+ }
127
+ onLoadingTranslation(tmpLocale, langs);
128
+ currentLocale = lastCurrentLocale.current;
129
+ } else {
130
+ lastCurrentLocale.current = currentLocale;
131
+ }
109
132
  const t = (key, data) => (0, _util.translate)(translations, key, currentLocale, fallbackLocale, data);
110
133
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Provider, {
111
134
  value: _objectSpread({
@@ -124,6 +147,7 @@ function useLocaleContext() {
124
147
  LocaleProvider.propTypes = {
125
148
  children: _propTypes.default.any.isRequired,
126
149
  translations: _propTypes.default.object.isRequired,
150
+ onLoadingTranslation: _propTypes.default.func,
127
151
  locale: _propTypes.default.string,
128
152
  // 会影响 translate(key) 的结果 - 当 key 无效时查找 fallbackLocale 对应的翻译结果
129
153
  fallbackLocale: _propTypes.default.string,
@@ -135,5 +159,6 @@ LocaleProvider.propTypes = {
135
159
  LocaleProvider.defaultProps = {
136
160
  locale: '',
137
161
  fallbackLocale: '',
138
- languages: []
162
+ languages: [],
163
+ onLoadingTranslation: () => {}
139
164
  };
@@ -19,6 +19,9 @@ const translate = function translate(translations, key, locale) {
19
19
  }
20
20
  return key;
21
21
  }
22
+ if (typeof translations[locale] === 'function' || translations[locale].then) {
23
+ return replace(translations[fallbackLocale][key], data);
24
+ }
22
25
  return replace(translations[locale][key], data);
23
26
  };
24
27
  exports.translate = translate;
@@ -52,7 +52,7 @@ function SessionBlocklet(_ref) {
52
52
  let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : !currentState.open;
53
53
  currentState.open = value;
54
54
  });
55
- if (sessionMenuList.length === 0) {
55
+ if (sessionMenuList.length === 0 || !(session !== null && session !== void 0 && session.user)) {
56
56
  return null;
57
57
  }
58
58
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ux",
3
- "version": "2.9.6",
3
+ "version": "2.9.8",
4
4
  "description": "Common used react components for arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -340,11 +340,11 @@
340
340
  "peerDependencies": {
341
341
  "react": ">=18.1.0"
342
342
  },
343
- "gitHead": "b00fb27f0798846fdb8cccaaa432ee802c14333b",
343
+ "gitHead": "8241dd27ee0de2332133aee2316c61bb792dad79",
344
344
  "dependencies": {
345
345
  "@arcblock/did-motif": "^1.1.13",
346
- "@arcblock/icons": "^2.9.6",
347
- "@arcblock/react-hooks": "^2.9.6",
346
+ "@arcblock/icons": "^2.9.8",
347
+ "@arcblock/react-hooks": "^2.9.8",
348
348
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
349
349
  "@emotion/react": "^11.10.4",
350
350
  "@emotion/styled": "^11.10.4",
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-prototype-builtins */
2
- import { createContext, useState, useEffect, useContext } from 'react';
2
+ import { createContext, useState, useEffect, useContext, useRef } from 'react';
3
3
  import get from 'lodash/get';
4
4
  import PropTypes from 'prop-types';
5
5
  import Cookie from 'js-cookie';
@@ -61,9 +61,12 @@ const getLanguages = (arg) => {
61
61
 
62
62
  const LocaleContext = createContext();
63
63
  const { Provider, Consumer } = LocaleContext;
64
- function LocaleProvider({ children, locale, fallbackLocale, translations, languages, ...rest }) {
64
+ function LocaleProvider({ children, locale, fallbackLocale, translations, onLoadingTranslation, languages, ...rest }) {
65
65
  const langs = getLanguages(languages);
66
- const [currentLocale, setCurrentLocale] = useState(locale || getLocale(langs));
66
+ // eslint-disable-next-line prefer-const
67
+ let [currentLocale, setCurrentLocale] = useState(locale || getLocale(langs));
68
+ const lastCurrentLocale = useRef(fallbackLocale);
69
+ const [, setForceUpdate] = useState(0);
67
70
 
68
71
  const changeLocale = (newLocale) => {
69
72
  setCurrentLocale(newLocale);
@@ -78,6 +81,26 @@ function LocaleProvider({ children, locale, fallbackLocale, translations, langua
78
81
  // eslint-disable-next-line react-hooks/exhaustive-deps
79
82
  }, [locale]);
80
83
 
84
+ // handle async loading of translations
85
+ if (currentLocale && translations[currentLocale] && typeof translations[currentLocale] === 'function') {
86
+ const tmpLocale = currentLocale;
87
+ try {
88
+ Promise.resolve(translations[tmpLocale]()).then((res) => {
89
+ if (res && typeof res === 'object') {
90
+ translations[tmpLocale] = res;
91
+ setForceUpdate((x) => (x > 999 ? 0 : x + 1));
92
+ }
93
+ });
94
+ } catch (err) {
95
+ console.error(err);
96
+ }
97
+
98
+ onLoadingTranslation(tmpLocale, langs);
99
+ currentLocale = lastCurrentLocale.current;
100
+ } else {
101
+ lastCurrentLocale.current = currentLocale;
102
+ }
103
+
81
104
  const t = (key, data) => translate(translations, key, currentLocale, fallbackLocale, data);
82
105
 
83
106
  return <Provider value={{ locale: currentLocale, changeLocale, t, languages: langs, ...rest }}>{children}</Provider>;
@@ -91,6 +114,7 @@ function useLocaleContext() {
91
114
  LocaleProvider.propTypes = {
92
115
  children: PropTypes.any.isRequired,
93
116
  translations: PropTypes.object.isRequired,
117
+ onLoadingTranslation: PropTypes.func,
94
118
  locale: PropTypes.string,
95
119
  // 会影响 translate(key) 的结果 - 当 key 无效时查找 fallbackLocale 对应的翻译结果
96
120
  fallbackLocale: PropTypes.string,
@@ -101,6 +125,7 @@ LocaleProvider.defaultProps = {
101
125
  locale: '',
102
126
  fallbackLocale: '',
103
127
  languages: [],
128
+ onLoadingTranslation: () => {},
104
129
  };
105
130
 
106
131
  export { LocaleProvider, Consumer as LocaleConsumer, LocaleContext, useLocaleContext, setLocale, getLocale };
@@ -10,7 +10,9 @@ export const translate = (translations, key, locale, fallbackLocale = 'en', data
10
10
  }
11
11
  return key;
12
12
  }
13
-
13
+ if (typeof translations[locale] === 'function' || translations[locale].then) {
14
+ return replace(translations[fallbackLocale][key], data);
15
+ }
14
16
  return replace(translations[locale][key], data);
15
17
  };
16
18
 
@@ -48,7 +48,7 @@ export default function SessionBlocklet({ session, locale, size }) {
48
48
  currentState.open = value;
49
49
  });
50
50
 
51
- if (sessionMenuList.length === 0) {
51
+ if (sessionMenuList.length === 0 || !session?.user) {
52
52
  return null;
53
53
  }
54
54