@abtnode/ux 1.16.11-next-8a4df821 → 1.16.11-next-ca5f18b5

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,315 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _Dialog = _interopRequireDefault(require("@arcblock/ux/lib/Dialog"));
8
- var _context = require("@arcblock/ux/lib/Locale/context");
9
- var _material = require("@mui/material");
10
- var _react = require("react");
11
- var _ListItem = _interopRequireDefault(require("@mui/material/ListItem"));
12
- var _isUrl = _interopRequireDefault(require("is-url"));
13
- var _urlJoin = _interopRequireDefault(require("url-join"));
14
- var _propTypes = _interopRequireDefault(require("prop-types"));
15
- var _Button = _interopRequireDefault(require("@arcblock/ux/lib/Button"));
16
- var _constant = require("@blocklet/constant");
17
- var _ahooks = require("ahooks");
18
- var _lodash = require("lodash");
19
- var _styled = _interopRequireDefault(require("@emotion/styled"));
20
- var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
21
- var _add = _interopRequireDefault(require("./add"));
22
- var _item = _interopRequireDefault(require("./item"));
23
- var _connect = _interopRequireDefault(require("./connect"));
24
- var _session = require("../../../lib/contexts/session");
25
- var _blocklet = require("../../../lib/contexts/blocklet");
26
- var _node = require("../../contexts/node");
27
- var _spaces = require("../../util/spaces");
28
- var _jsxRuntime = require("react/jsx-runtime");
29
- var _templateObject;
30
- /**
31
- * @typedef {{
32
- * name: string,
33
- * url: string,
34
- * protected?: boolean,
35
- * endpoint?: string
36
- * }} SpaceGateway
37
- */
38
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
- 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; }
40
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
41
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
42
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
43
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
44
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
45
- const BoxContainer = (0, _styled.default)(_material.Box)(_templateObject || (_templateObject = _taggedTemplateLiteral([""])));
46
-
47
- /**
48
- * @FIXME 希望之后放在 sdk 中 @jianchao
49
- * @description
50
- * @param {string} endpoint
51
- * @returns {string}
52
- */
53
- function getSpaceUrlFromEndpoint(endpoint) {
54
- const prefix = endpoint.replace(/\/api\/space\/.+/, '');
55
- const strArray = endpoint.replace(/\/$/, '').split('/');
56
- const spaceDid = strArray.at(-4);
57
- return (0, _urlJoin.default)(prefix, 'space', spaceDid);
58
- }
59
-
60
- /**
61
- * @description
62
- * @param {{
63
- * mode: 'add' | 'switch'
64
- * }} {mode}
65
- * @return {React.Component}
66
- */
67
- function SwitchDIDSpaces(_ref) {
68
- var _connectedGateway$nam;
69
- let {
70
- mode
71
- } = _ref;
72
- /** @type {{ blocklet: import('@abtnode/client').BlockletState }} */
73
- const {
74
- blocklet,
75
- actions: {
76
- refreshBlocklet
77
- }
78
- } = (0, _blocklet.useBlockletContext)();
79
- const {
80
- t
81
- } = (0, _context.useLocaleContext)();
82
- const [open, setOpen] = (0, _react.useState)(false);
83
- const [spaceGateways, setSpaceGateways] = (0, _react.useState)( /** @type {SpaceGateway[]} */
84
- []);
85
- const spaceBackupEndpoint = (0, _react.useMemo)(() => (0, _spaces.getSpaceBackupEndpoint)(blocklet === null || blocklet === void 0 ? void 0 : blocklet.environments), [blocklet]);
86
- const {
87
- api
88
- } = (0, _session.useSessionContext)();
89
- const {
90
- api: nodeApi
91
- } = (0, _node.useNodeContext)();
92
- const prodSpacesGateway = {
93
- name: 'Prod DID Spaces',
94
- url: 'https://www.didspaces.com/app',
95
- protected: true
96
- };
97
- const [selectedGateway, setSelectedGateway] = (0, _react.useState)(prodSpacesGateway);
98
- const [connectedGateway, setConnectedGateway] = (0, _react.useState)(null);
99
- (0, _ahooks.useAsyncEffect)(async () => {
100
- var _spaceGateways$find, _spaceGateways$find2;
101
- const {
102
- spaceGateways: _spaceGateways
103
- } = await nodeApi.getBlockletSpaceGateways({
104
- input: {
105
- did: blocklet.meta.did
106
- }
107
- });
108
- setSpaceGateways([prodSpacesGateway, ...(_spaceGateways !== null && _spaceGateways !== void 0 ? _spaceGateways : [])]);
109
- setConnectedGateway((_spaceGateways$find = _spaceGateways.find(spaceGateway => spaceBackupEndpoint.includes(spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url))) !== null && _spaceGateways$find !== void 0 ? _spaceGateways$find : {});
110
- setSelectedGateway((_spaceGateways$find2 = _spaceGateways.find(spaceGateway => spaceBackupEndpoint.includes(spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url))) !== null && _spaceGateways$find2 !== void 0 ? _spaceGateways$find2 : prodSpacesGateway);
111
- }, [blocklet]);
112
-
113
- /**
114
- *
115
- * @param {SpaceGateway} spaceGateway
116
- */
117
- const onSelectGateway = async spaceGateway => {
118
- if ((spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url) !== (selectedGateway === null || selectedGateway === void 0 ? void 0 : selectedGateway.url)) {
119
- setSelectedGateway(spaceGateway);
120
- }
121
- };
122
-
123
- /**
124
- *
125
- * @param {string} url
126
- * @returns {Promise<void>}
127
- */
128
- const onAddGateway = async url => {
129
- if (!(0, _lodash.isString)(url)) {
130
- throw new Error(t('storage.spaces.gateway.add.invalidUrl'));
131
- }
132
-
133
- // eslint-disable-next-line no-param-reassign
134
- url = url.trim().replace(/\/$/, '');
135
- if (!url.startsWith('http')) {
136
- // eslint-disable-next-line no-param-reassign
137
- url = "https://".concat(url);
138
- }
139
- if (!(0, _isUrl.default)(url)) {
140
- throw new Error(t('storage.spaces.gateway.add.invalidUrl'));
141
- }
142
- const duplicate = spaceGateways.some(s => s.url === url);
143
- if (duplicate) {
144
- throw new Error(t('storage.spaces.gateway.add.duplicate', {
145
- url: url.url
146
- }));
147
- }
148
- const {
149
- data
150
- } = await api.get((0, _urlJoin.default)(url, '/__blocklet__.js?type=json'));
151
- const spaceGateway = {
152
- name: data.appName,
153
- url
154
- };
155
- await nodeApi.addBlockletSpaceGateway({
156
- input: {
157
- did: blocklet.meta.did,
158
- spaceGateway
159
- }
160
- });
161
- setSpaceGateways(preValue => {
162
- return [...preValue, spaceGateway];
163
- });
164
- setSelectedGateway(spaceGateway);
165
- };
166
-
167
- /**
168
- *
169
- * @description
170
- * @param {SpaceGateway} spaceGateway
171
- * @param {number} index
172
- */
173
- const onDeleteGateway = async (spaceGateway, index) => {
174
- if (!spaceGateway) {
175
- return;
176
- }
177
- if (spaceBackupEndpoint === (spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.endpoint)) {
178
- _Toast.default.warning(t('storage.spaces.gateway.delete.failedForConnected'));
179
- return;
180
- }
181
- await nodeApi.deleteBlockletSpaceGateway({
182
- input: {
183
- did: blocklet.meta.did,
184
- url: spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url
185
- }
186
- });
187
- setSpaceGateways(preSpaceGateways => {
188
- return preSpaceGateways.filter(s => s.url !== spaceGateway.url);
189
- });
190
- if ((spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url) === (selectedGateway === null || selectedGateway === void 0 ? void 0 : selectedGateway.url)) {
191
- setSelectedGateway(spaceGateways[index - 1]);
192
- }
193
- };
194
-
195
- /**
196
- *
197
- *
198
- * @param {SpaceGateway} spaceGateway
199
- * @param {string} _endpoint
200
- * @return {Promise<void>}
201
- */
202
- const onConnectGateway = async (spaceGateway, _endpoint) => {
203
- var _spaceGateway$name;
204
- const {
205
- data
206
- } = await api.get((0, _urlJoin.default)(spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url, '/__blocklet__.js?type=json'));
207
- await nodeApi.updateBlockletSpaceGateway({
208
- input: {
209
- did: blocklet.meta.did,
210
- where: {
211
- url: spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url
212
- },
213
- spaceGateway: _objectSpread(_objectSpread({}, spaceGateway), {}, {
214
- name: data === null || data === void 0 ? void 0 : data.appName,
215
- endpoint: _endpoint
216
- })
217
- }
218
- });
219
- if (spaceBackupEndpoint !== _endpoint) {
220
- await nodeApi.configBlocklet({
221
- input: {
222
- did: [blocklet.meta.did],
223
- configs: [{
224
- key: _constant.BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_SPACES_URL,
225
- value: spaceGateway.url
226
- }, {
227
- key: _constant.BLOCKLET_CONFIGURABLE_KEY.BLOCKLET_APP_BACKUP_ENDPOINT,
228
- value: _endpoint
229
- }]
230
- }
231
- });
232
- }
233
- _Toast.default.success(t('storage.spaces.connectedWithName', {
234
- name: (_spaceGateway$name = spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.name) !== null && _spaceGateway$name !== void 0 ? _spaceGateway$name : 'DID Spaces'
235
- }));
236
- refreshBlocklet();
237
- setTimeout(() => setOpen(false), 1000);
238
- };
239
- const textProps = mode === 'add' ? {
240
- text: t('storage.spaces.gateway.connect.add'),
241
- tips: t('storage.spaces.gateway.connect.addTips')
242
- } : {
243
- text: t('storage.spaces.gateway.connect.switch'),
244
- tips: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
245
- children: [t('storage.spaces.gateway.connect.switchTips', {
246
- name: ''
247
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.Link, {
248
- href: getSpaceUrlFromEndpoint(spaceBackupEndpoint),
249
- target: "_blank",
250
- children: (_connectedGateway$nam = connectedGateway === null || connectedGateway === void 0 ? void 0 : connectedGateway.name) !== null && _connectedGateway$nam !== void 0 ? _connectedGateway$nam : 'Spaces'
251
- })]
252
- })
253
- };
254
- return /*#__PURE__*/(0, _jsxRuntime.jsxs)(BoxContainer, {
255
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_material.Box, {
256
- marginTop: "16px",
257
- children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.Box, {
258
- display: "inline",
259
- children: [t('storage.spaces.tips'), ": \xA0", ' ', /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.Box, {
260
- display: "inline",
261
- color: "primary.main",
262
- fontWeight: "fontWeightBold",
263
- children: textProps.tips
264
- })]
265
- })
266
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.Box, {
267
- marginTop: "16px",
268
- display: "flex",
269
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.default, {
270
- size: "small",
271
- variant: "contained",
272
- onClick: () => setOpen(true),
273
- children: textProps.text
274
- })
275
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Dialog.default, {
276
- open: open,
277
- title: t('storage.spaces.gateway.select'),
278
- disableEscapeKeyDown: true,
279
- disablePortal: false,
280
- fullWidth: true,
281
- onClose: () => setOpen(false),
282
- actions: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
283
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_add.default, {
284
- onAdd: onAddGateway
285
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_connect.default, {
286
- spaceGateway: selectedGateway,
287
- onConnect: onConnectGateway
288
- })]
289
- }),
290
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.List, {
291
- children: spaceGateways.map((spaceGateway, index) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_ListItem.default, {
292
- button: true,
293
- selected: (spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.url) === (selectedGateway === null || selectedGateway === void 0 ? void 0 : selectedGateway.url),
294
- "data-cy": "switch-did-spaces-gateway",
295
- onClick: () => onSelectGateway(spaceGateway),
296
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_material.ListItemText, {
297
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_item.default, {
298
- spaceGateway: spaceGateway,
299
- connected: spaceBackupEndpoint === (spaceGateway === null || spaceGateway === void 0 ? void 0 : spaceGateway.endpoint),
300
- onDelete: _spaceGateway => onDeleteGateway(_spaceGateway, index)
301
- })
302
- })
303
- }, spaceGateway.url))
304
- })
305
- })]
306
- });
307
- }
308
- SwitchDIDSpaces.propTypes = {
309
- mode: _propTypes.default.oneOf(['add', 'switch'])
310
- };
311
- SwitchDIDSpaces.defaultProps = {
312
- mode: 'switch'
313
- };
314
- var _default = SwitchDIDSpaces;
315
- exports.default = _default;