@arcblock/did-playground 3.4.14 → 3.5.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/did-playground",
3
- "version": "3.4.14",
3
+ "version": "3.5.0",
4
4
  "description": "React components that works with wallet-playground",
5
5
  "keywords": [
6
6
  "react",
@@ -30,8 +30,8 @@
30
30
  "url": "https://github.com/ArcBlock/wallet-playground/issues"
31
31
  },
32
32
  "dependencies": {
33
- "@arcblock/did-connect-react": "3.4.14",
34
- "@arcblock/react-hooks": "3.4.14",
33
+ "@arcblock/did-connect-react": "3.5.0",
34
+ "@arcblock/react-hooks": "3.5.0",
35
35
  "ahooks": "^3.8.5",
36
36
  "axios": "^1.10.0",
37
37
  "mustache": "^4.2.0"
@@ -43,8 +43,11 @@
43
43
  "@mui/material": "^7.2.0",
44
44
  "react": "^19.0.0"
45
45
  },
46
+ "files": [
47
+ "lib"
48
+ ],
46
49
  "publishConfig": {
47
50
  "access": "public"
48
51
  },
49
- "gitHead": "d663390d3c6a9b2d8d71790c770dd0025ae309de"
52
+ "gitHead": "1cfc816004525cf1b352ec2b64d459f4769f0237"
50
53
  }
@@ -1,575 +0,0 @@
1
- import mustache from 'mustache';
2
-
3
- async function createSwapOrder(api) {
4
- const res = await api.post('/api/did/swap', {});
5
- return { tid: res.data.traceId };
6
- }
7
-
8
- const getValidPayAmount = (payAmount, price) => {
9
- if (Number(payAmount) >= 0) {
10
- return payAmount;
11
- }
12
- if (Number(price) >= 0) {
13
- return price;
14
- }
15
- return 1;
16
- };
17
-
18
- export const getMessage = (message = '', session = {}) => {
19
- try {
20
- return mustache.render(
21
- message,
22
- {
23
- user: session.user || {},
24
- token: session.token || {},
25
- balance: session.balance || {},
26
- },
27
- {},
28
- ['(%', '%)']
29
- );
30
- } catch (err) {
31
- console.error('Cannot render message', { message, session });
32
- return message;
33
- }
34
- };
35
-
36
- // https://github.com/ArcBlock/gatsby-extensions/issues/56
37
- export const actions = {
38
- // Currency
39
- receive_local_token: {
40
- action: 'receive_token',
41
- extraParams: (props) => ({ chain: 'local', amount: props.amount || 1 }),
42
- },
43
- receive_foreign_token: {
44
- action: 'receive_token',
45
- extraParams: (props) => ({ chain: 'foreign', amount: props.amount || 1 }),
46
- },
47
- send_local_token: {
48
- action: 'send_token',
49
- extraParams: (props) => ({ chain: 'local', amount: props.amount || 1 }),
50
- },
51
- send_foreign_token: {
52
- action: 'send_token',
53
- extraParams: (props) => ({ chain: 'foreign', amount: props.amount || 1 }),
54
- },
55
- exchange_to_foreign_token: {
56
- action: 'swap_token',
57
- onStart: createSwapOrder,
58
- extraParams: (props) => ({ action: 'buy', rate: props.exchangeRate, amount: props.amount || 1 }),
59
- },
60
- exchange_to_local_token: {
61
- action: 'swap_token',
62
- onStart: createSwapOrder,
63
- extraParams: (props) => ({ action: 'sell', rate: props.exchangeRate, amount: props.amount || 1 }),
64
- },
65
-
66
- exchange_to_foreign_token_v2: {
67
- action: 'swap_token_v2',
68
- onStart: createSwapOrder,
69
- extraParams: (props) => ({ action: 'buy', rate: props.exchangeRate, amount: props.amount || 1 }),
70
- },
71
- exchange_to_local_token_v2: {
72
- action: 'swap_token_v2',
73
- onStart: createSwapOrder,
74
- extraParams: (props) => ({ action: 'sell', rate: props.exchangeRate, amount: props.amount || 1 }),
75
- },
76
-
77
- // Cross chain assets and tokens
78
- buy_foreign_certificate_with_local_token: {
79
- action: 'swap_asset',
80
- onStart: createSwapOrder,
81
- extraParams: (props, session) => ({
82
- action: 'buy',
83
- type: 'certificate',
84
- pfc: 'local',
85
- price: props.price || 1,
86
- name: getMessage(props.name, session),
87
- desc: getMessage(props.description, session),
88
- loc: getMessage(props.location, session),
89
- bg: props.backgroundUrl,
90
- logo: props.logoUrl,
91
- }),
92
- },
93
- buy_foreign_badge_with_local_token: {
94
- action: 'swap_asset',
95
- onStart: createSwapOrder,
96
- extraParams: (props, session) => ({
97
- action: 'buy',
98
- type: 'badge',
99
- pfc: 'local',
100
- price: props.price || 1,
101
- name: getMessage(props.name, session),
102
- desc: getMessage(props.description, session),
103
- loc: getMessage(props.location, session),
104
- bg: props.backgroundUrl,
105
- logo: props.logoUrl,
106
- svg: props.svg,
107
- }),
108
- },
109
- buy_foreign_ticket_with_local_token: {
110
- action: 'swap_asset',
111
- onStart: createSwapOrder,
112
- extraParams: (props, session) => ({
113
- action: 'buy',
114
- type: 'ticket',
115
- pfc: 'local',
116
- price: props.price || 1,
117
- name: getMessage(props.name, session),
118
- desc: getMessage(props.description, session),
119
- loc: getMessage(props.location, session),
120
- bg: props.backgroundUrl,
121
- logo: props.logoUrl,
122
- }),
123
- },
124
- sell_foreign_certificate_for_local_token: {
125
- action: 'swap_asset',
126
- onStart: createSwapOrder,
127
- extraParams: (props, session) => ({
128
- action: 'sell',
129
- type: 'certificate',
130
- pfc: 'local',
131
- price: props.price || 1,
132
- name: getMessage(props.name, session),
133
- }),
134
- },
135
- sell_foreign_badge_for_local_token: {
136
- action: 'swap_asset',
137
- onStart: createSwapOrder,
138
- extraParams: (props, session) => ({
139
- action: 'sell',
140
- type: 'badge',
141
- pfc: 'local',
142
- price: props.price || 1,
143
- name: getMessage(props.name, session),
144
- }),
145
- },
146
- sell_foreign_ticket_for_local_token: {
147
- action: 'swap_asset',
148
- onStart: createSwapOrder,
149
- extraParams: (props, session) => ({
150
- action: 'sell',
151
- type: 'ticket',
152
- pfc: 'local',
153
- price: props.price || 1,
154
- name: getMessage(props.name, session),
155
- }),
156
- },
157
-
158
- buy_foreign_certificate_with_local_token_v2: {
159
- action: 'swap_asset_v2',
160
- extraParams: (props, session) => ({
161
- action: 'buy',
162
- type: 'certificate',
163
- pfc: 'local',
164
- price: props.price || 1,
165
- name: getMessage(props.name, session),
166
- desc: getMessage(props.description, session),
167
- loc: getMessage(props.location, session),
168
- bg: props.backgroundUrl,
169
- logo: props.logoUrl,
170
- }),
171
- },
172
- buy_foreign_badge_with_local_token_v2: {
173
- action: 'swap_asset_v2',
174
- extraParams: (props, session) => ({
175
- action: 'buy',
176
- type: 'badge',
177
- pfc: 'local',
178
- price: props.price || 1,
179
- name: getMessage(props.name, session),
180
- desc: getMessage(props.description, session),
181
- loc: getMessage(props.location, session),
182
- bg: props.backgroundUrl,
183
- logo: props.logoUrl,
184
- svg: props.svg,
185
- }),
186
- },
187
- buy_foreign_ticket_with_local_token_v2: {
188
- action: 'swap_asset_v2',
189
- extraParams: (props, session) => ({
190
- action: 'buy',
191
- type: 'ticket',
192
- pfc: 'local',
193
- price: props.price || 1,
194
- name: getMessage(props.name, session),
195
- desc: getMessage(props.description, session),
196
- loc: getMessage(props.location, session),
197
- bg: props.backgroundUrl,
198
- logo: props.logoUrl,
199
- }),
200
- },
201
- sell_foreign_certificate_for_local_token_v2: {
202
- action: 'swap_asset_v2',
203
- extraParams: (props, session) => ({
204
- action: 'sell',
205
- type: 'certificate',
206
- pfc: 'local',
207
- price: props.price || 1,
208
- name: getMessage(props.name, session),
209
- }),
210
- },
211
- sell_foreign_badge_for_local_token_v2: {
212
- action: 'swap_asset_v2',
213
- extraParams: (props, session) => ({
214
- action: 'sell',
215
- type: 'badge',
216
- pfc: 'local',
217
- price: props.price || 1,
218
- name: getMessage(props.name, session),
219
- }),
220
- },
221
- sell_foreign_ticket_for_local_token_v2: {
222
- action: 'swap_asset_v2',
223
- extraParams: (props, session) => ({
224
- action: 'sell',
225
- type: 'ticket',
226
- pfc: 'local',
227
- price: props.price || 1,
228
- name: getMessage(props.name, session),
229
- }),
230
- },
231
-
232
- buy_local_certificate_with_foreign_token: {
233
- action: 'swap_asset',
234
- onStart: createSwapOrder,
235
- extraParams: (props, session) => ({
236
- action: 'buy',
237
- type: 'certificate',
238
- pfc: 'foreign',
239
- price: props.price || 1,
240
- name: getMessage(props.name, session),
241
- desc: getMessage(props.description, session),
242
- loc: getMessage(props.location, session),
243
- bg: props.backgroundUrl,
244
- logo: props.logoUrl,
245
- }),
246
- },
247
- buy_local_badge_with_foreign_token: {
248
- action: 'swap_asset',
249
- onStart: createSwapOrder,
250
- extraParams: (props, session) => ({
251
- action: 'buy',
252
- type: 'badge',
253
- pfc: 'foreign',
254
- price: props.price || 0,
255
- name: getMessage(props.name, session),
256
- desc: getMessage(props.description, session),
257
- loc: getMessage(props.location, session),
258
- bg: props.backgroundUrl,
259
- logo: props.logoUrl,
260
- svg: props.svg,
261
- }),
262
- },
263
- buy_local_ticket_with_foreign_token: {
264
- action: 'swap_asset',
265
- onStart: createSwapOrder,
266
- extraParams: (props, session) => ({
267
- action: 'buy',
268
- type: 'ticket',
269
- pfc: 'foreign',
270
- price: props.price || 1,
271
- name: getMessage(props.name, session),
272
- desc: getMessage(props.description, session),
273
- loc: getMessage(props.location, session),
274
- bg: props.backgroundUrl,
275
- logo: props.logoUrl,
276
- }),
277
- },
278
- sell_local_certificate_for_foreign_token: {
279
- action: 'swap_asset',
280
- onStart: createSwapOrder,
281
- extraParams: (props, session) => ({
282
- action: 'sell',
283
- type: 'certificate',
284
- pfc: 'foreign',
285
- price: props.price || 1,
286
- name: getMessage(props.name, session),
287
- }),
288
- },
289
- sell_local_badge_for_foreign_token: {
290
- action: 'swap_asset',
291
- onStart: createSwapOrder,
292
- extraParams: (props, session) => ({
293
- action: 'sell',
294
- type: 'vc',
295
- pfc: 'foreign',
296
- price: props.price || 1,
297
- name: getMessage(props.name, session),
298
- }),
299
- },
300
- sell_local_ticket_for_foreign_token: {
301
- action: 'swap_asset',
302
- onStart: createSwapOrder,
303
- extraParams: (props, session) => ({
304
- action: 'sell',
305
- type: 'ticket',
306
- pfc: 'foreign',
307
- price: props.price || 1,
308
- name: getMessage(props.name, session),
309
- }),
310
- },
311
-
312
- buy_local_certificate_with_foreign_token_v2: {
313
- action: 'swap_asset_v2',
314
- extraParams: (props, session) => ({
315
- pa: getValidPayAmount(props.payAmount, props.price),
316
- pt: 'token',
317
- ra: props.receiveAmount || 1,
318
- rt: 'certificate',
319
- name: getMessage(props.name, session),
320
- desc: getMessage(props.description, session),
321
- loc: getMessage(props.location, session),
322
- bg: props.backgroundUrl,
323
- logo: props.logoUrl,
324
- }),
325
- },
326
- buy_local_badge_with_foreign_token_v2: {
327
- action: 'swap_asset_v2',
328
- extraParams: (props, session) => ({
329
- pa: getValidPayAmount(props.payAmount, props.price),
330
- pt: 'token',
331
- ra: props.receiveAmount || 1,
332
- rt: 'badge',
333
- name: getMessage(props.name, session),
334
- desc: getMessage(props.description, session),
335
- loc: getMessage(props.location, session),
336
- bg: props.backgroundUrl,
337
- logo: props.logoUrl,
338
- svg: props.svg,
339
- }),
340
- },
341
- buy_local_ticket_with_foreign_token_v2: {
342
- action: 'swap_asset_v2',
343
- extraParams: (props, session) => ({
344
- pa: getValidPayAmount(props.payAmount, props.price),
345
- pt: 'token',
346
- ra: props.receiveAmount || 1,
347
- rt: 'badge',
348
- name: getMessage(props.name, session),
349
- desc: getMessage(props.description, session),
350
- loc: getMessage(props.location, session),
351
- bg: props.backgroundUrl,
352
- logo: props.logoUrl,
353
- }),
354
- },
355
- sell_local_certificate_for_foreign_token_v2: {
356
- action: 'swap_asset_v2',
357
- extraParams: (props, session) => ({
358
- pa: props.payAmount || 1,
359
- pt: 'certificate',
360
- ra: getValidPayAmount(props.receiveAmount, props.price),
361
- rt: 'token',
362
- name: getMessage(props.name, session),
363
- }),
364
- },
365
- sell_local_badge_for_foreign_token_v2: {
366
- action: 'swap_asset_v2',
367
- extraParams: (props, session) => ({
368
- pa: props.payAmount || 1,
369
- pt: 'badge',
370
- ra: getValidPayAmount(props.receiveAmount, props.price),
371
- rt: 'token',
372
- name: getMessage(props.name, session),
373
- }),
374
- },
375
- sell_local_ticket_for_foreign_token_v2: {
376
- action: 'swap_asset_v2',
377
- extraParams: (props, session) => ({
378
- pa: props.payAmount || 1,
379
- pt: 'ticket',
380
- ra: getValidPayAmount(props.receiveAmount, props.price),
381
- rt: 'token',
382
- name: getMessage(props.name, session),
383
- }),
384
- },
385
-
386
- // Exchange Scenarios
387
- buy_local_certificate_with_local_token: {
388
- action: 'exchange_assets',
389
- extraParams: (props, session) => ({
390
- pa: getValidPayAmount(props.payAmount, props.price),
391
- pt: 'token',
392
- ra: props.receiveAmount || 1,
393
- rt: 'certificate',
394
- name: getMessage(props.name, session),
395
- desc: getMessage(props.description, session),
396
- loc: getMessage(props.location, session),
397
- bg: props.backgroundUrl,
398
- logo: props.logoUrl,
399
- }),
400
- },
401
- sell_local_certificate_for_local_token: {
402
- action: 'exchange_assets',
403
- extraParams: (props, session) => ({
404
- pa: props.payAmount || 1,
405
- pt: 'certificate',
406
- ra: getValidPayAmount(props.receiveAmount, props.price),
407
- rt: 'token',
408
- name: getMessage(props.name, session),
409
- }),
410
- },
411
- buy_local_badge_with_local_token: {
412
- action: 'exchange_assets',
413
- extraParams: (props, session) => ({
414
- pa: getValidPayAmount(props.payAmount, props.price),
415
- pt: 'token',
416
- ra: props.receiveAmount || 1,
417
- rt: 'badge',
418
- name: getMessage(props.name, session),
419
- desc: getMessage(props.description, session),
420
- loc: getMessage(props.location, session),
421
- bg: props.backgroundUrl,
422
- logo: props.logoUrl,
423
- svg: props.svg,
424
- }),
425
- },
426
- sell_local_badge_for_local_token: {
427
- action: 'exchange_assets',
428
- extraParams: (props, session) => ({
429
- pa: props.payAmount || 1,
430
- pt: 'badge',
431
- ra: getValidPayAmount(props.receiveAmount, props.price),
432
- rt: 'token',
433
- name: getMessage(props.name, session),
434
- }),
435
- },
436
- buy_local_ticket_with_local_token: {
437
- action: 'exchange_assets',
438
- extraParams: (props, session) => ({
439
- pa: getValidPayAmount(props.payAmount, props.price),
440
- pt: 'token',
441
- ra: props.receiveAmount || 1,
442
- rt: 'ticket',
443
- name: getMessage(props.name, session),
444
- desc: getMessage(props.description, session),
445
- loc: getMessage(props.location, session),
446
- bg: props.backgroundUrl,
447
- logo: props.logoUrl,
448
- }),
449
- },
450
- sell_local_ticket_for_local_token: {
451
- action: 'exchange_assets',
452
- extraParams: (props, session) => ({
453
- pa: props.payAmount || 1,
454
- pt: 'ticket',
455
- ra: getValidPayAmount(props.receiveAmount, props.price),
456
- rt: 'token',
457
- name: getMessage(props.name, session),
458
- }),
459
- },
460
- buy_local_ticket_with_local_certificate: {
461
- action: 'exchange_assets',
462
- extraParams: (props, session) => ({
463
- pa: props.payAmount || 1,
464
- pt: 'certificate',
465
- ra: props.receiveAmount || 1,
466
- rt: 'ticket',
467
- name: getMessage(props.name, session),
468
- }),
469
- },
470
- buy_local_certificate_with_local_ticket: {
471
- action: 'exchange_assets',
472
- extraParams: (props, session) => ({
473
- pa: props.payAmount || 1,
474
- pt: 'ticket',
475
- ra: props.receiveAmount || 1,
476
- rt: 'certificate',
477
- name: getMessage(props.name, session),
478
- }),
479
- },
480
-
481
- consume_local_asset: {
482
- action: 'consume_asset',
483
- extraParams: ({ type, typeUrl, name, did }, session) => ({
484
- pfc: 'local',
485
- type,
486
- tu: typeUrl,
487
- name: getMessage(name, session),
488
- did,
489
- }),
490
- },
491
- consume_foreign_asset: {
492
- action: 'consume_asset',
493
- extraParams: ({ type, typeUrl, name, did }, session) => ({
494
- pfc: 'foreign',
495
- type,
496
- tu: typeUrl,
497
- name: getMessage(name, session),
498
- did,
499
- }),
500
- },
501
- consume_local_asset_by_name: {
502
- action: 'consume_asset',
503
- extraParams: ({ name }, session) => ({
504
- pfc: 'local',
505
- name: getMessage(name, session),
506
- }),
507
- },
508
- consume_foreign_asset_by_name: {
509
- action: 'consume_asset',
510
- extraParams: ({ name }, session) => ({
511
- pfc: 'foreign',
512
- name: getMessage(name, session),
513
- }),
514
- },
515
- consume_local_asset_by_did: {
516
- action: 'consume_asset',
517
- extraParams: ({ did }) => ({
518
- pfc: 'local',
519
- did,
520
- }),
521
- },
522
- consume_foreign_asset_by_did: {
523
- action: 'consume_asset',
524
- extraParams: ({ did }) => ({
525
- pfc: 'foreign',
526
- did,
527
- }),
528
- },
529
-
530
- claim_signature: {
531
- action: 'claim_signature',
532
- extraParams: ({ type }) => ({ type }),
533
- },
534
-
535
- consume_email_vc: {
536
- action: 'consume_vc',
537
- extraParams: {},
538
- },
539
- };
540
-
541
- export const getActionName = (config, props) => {
542
- if (typeof config === 'string') {
543
- return config;
544
- }
545
-
546
- if (typeof config.action === 'string') {
547
- return config.action;
548
- }
549
-
550
- if (typeof config.action === 'function') {
551
- return config.action(props);
552
- }
553
-
554
- throw new Error('Cannot determine playground button action');
555
- };
556
-
557
- export const getActionParams = (config, props, session) => {
558
- if (typeof config === 'string') {
559
- return {};
560
- }
561
-
562
- if (!config.extraParams) {
563
- return {};
564
- }
565
-
566
- if (typeof config.extraParams === 'function') {
567
- return config.extraParams(props, session);
568
- }
569
-
570
- if (typeof config.extraParams === 'object') {
571
- return config.extraParams;
572
- }
573
-
574
- return {};
575
- };
@@ -1,302 +0,0 @@
1
- import { use, useState, useEffect } from 'react';
2
- import PropTypes from 'prop-types';
3
- import { useSize } from 'ahooks';
4
- import useBrowser from '@arcblock/react-hooks/lib/useBrowser';
5
- import { styled, useTheme } from '@arcblock/ux/lib/Theme';
6
- import isUndefined from 'lodash/isUndefined';
7
-
8
- import { CircularProgress, Dialog, DialogContent } from '@mui/material';
9
-
10
- import DidConnect from '@arcblock/did-connect-react/lib/Connect';
11
- import Button from '@arcblock/ux/lib/Button';
12
- import { mergeProps } from '@arcblock/ux/lib/Util';
13
-
14
- import { SessionContext } from './session';
15
- import { actions, getMessage, getActionName, getActionParams } from './actions';
16
-
17
- function Close({ onClose }) {
18
- return <CloseContainer onClick={onClose}>&times;</CloseContainer>;
19
- }
20
-
21
- Close.propTypes = { onClose: PropTypes.func.isRequired };
22
- const CloseContainer = styled('div')`
23
- display: ${(props) => (props.disableClose ? 'none' : 'block')};
24
- position: absolute;
25
- top: 1rem;
26
- right: 1rem;
27
- color: #999999;
28
- font-size: 2rem;
29
- line-height: 1rem;
30
- cursor: pointer;
31
- user-select: none;
32
- `;
33
-
34
- function PlaygroundAction({ ...rawProps }) {
35
- const props = Object.assign({}, rawProps);
36
- if (isUndefined(props.autoClose)) {
37
- props.autoClose = true;
38
- }
39
- if (isUndefined(props.buttonText)) {
40
- props.buttonText = '';
41
- }
42
- if (isUndefined(props.buttonColor)) {
43
- props.buttonColor = 'primary';
44
- }
45
- if (isUndefined(props.buttonVariant)) {
46
- props.buttonVariant = 'contained';
47
- }
48
- if (isUndefined(props.buttonSize)) {
49
- props.buttonSize = 'large';
50
- }
51
- if (isUndefined(props.buttonRounded)) {
52
- props.buttonRounded = false;
53
- }
54
- if (isUndefined(props.scanMessage)) {
55
- props.scanMessage = 'Scan the QR Code with your DID Wallet';
56
- }
57
- if (isUndefined(props.confirmMessage)) {
58
- props.confirmMessage = 'Confirm in your DID Wallet';
59
- }
60
- if (isUndefined(props.successMessage)) {
61
- props.successMessage = 'Operation success!';
62
- }
63
- if (isUndefined(props.extraParams)) {
64
- props.extraParams = {};
65
- }
66
- if (isUndefined(props.timeout)) {
67
- props.timeout = 5 * 60 * 1000;
68
- }
69
- if (isUndefined(props.successUrl)) {
70
- props.successUrl = '';
71
- }
72
- if (isUndefined(props.successTarget)) {
73
- props.successTarget = '_self';
74
- }
75
- if (isUndefined(props.frameProps)) {
76
- props.frameProps = {};
77
- }
78
- if (isUndefined(props.webWalletUrl)) {
79
- props.webWalletUrl = '';
80
- }
81
- const newProps = mergeProps(props, PlaygroundAction, ['buttonRounded', 'extraParams', 'timeout']);
82
- const {
83
- autoClose,
84
- action,
85
- buttonText,
86
- buttonColor,
87
- buttonVariant,
88
- buttonSize,
89
- buttonRounded,
90
- children,
91
- disableClose,
92
- title,
93
- scanMessage,
94
- successMessage,
95
- successUrl,
96
- successTarget,
97
- frameProps,
98
- confirmMessage,
99
- extraParams,
100
- timeout,
101
- webWalletUrl,
102
- ...rest
103
- } = newProps;
104
-
105
- const theme = useTheme();
106
- const browser = useBrowser();
107
- const { api, session } = use(SessionContext);
108
- const [open, setOpen] = useState(false);
109
- const [loading, setLoading] = useState(false);
110
- const [dynamicParams, setDynamicParams] = useState({});
111
- const size = useSize(document.body);
112
- const [success, setSuccess] = useState(false);
113
- const [showFrame, setShowFrame] = useState(success && successUrl && successTarget === 'frame');
114
-
115
- const width = size?.width || 0;
116
-
117
- // 当打开或关闭组件时,重置部分状态
118
- useEffect(
119
- () => () => {
120
- setSuccess(false);
121
- setShowFrame(false);
122
- },
123
- [open]
124
- );
125
-
126
- // If this is just a login button, we do not do anything actually
127
- if (action === 'login') {
128
- if (session.user) {
129
- return (
130
- <Button {...rest} rounded={buttonRounded} color={buttonColor} variant={buttonVariant} size={buttonSize}>
131
- {getMessage(successMessage || `Hello ${session.user.name}`, session)}
132
- </Button>
133
- );
134
- }
135
-
136
- return (
137
- <Button
138
- {...rest}
139
- rounded={buttonRounded}
140
- color={buttonColor}
141
- variant={buttonVariant}
142
- size={buttonSize}
143
- onClick={() => session.login()}>
144
- {getMessage(buttonText || title, session)}
145
- </Button>
146
- );
147
- }
148
-
149
- const config = actions[action];
150
- if (!actions[action]) {
151
- throw new Error(`Unsupported playground action ${action}`);
152
- }
153
-
154
- const doStart = async () => {
155
- if (typeof config.onStart === 'function') {
156
- try {
157
- setLoading(true);
158
- const params = await config.onStart(api, session);
159
- setDynamicParams(params);
160
- setLoading(false);
161
- } catch (err) {
162
- console.error(`Cannot generate dynamicParams for playground action ${getActionName(config, rest)}`);
163
- }
164
- setOpen(true);
165
- } else {
166
- setOpen(true);
167
- }
168
- };
169
-
170
- const onStart = async () => {
171
- if (!session.user) {
172
- session.login(doStart);
173
- return;
174
- }
175
-
176
- await doStart();
177
- };
178
-
179
- const onClose = () => setOpen(false);
180
-
181
- const onSuccess = () => {
182
- setSuccess(true);
183
- if (successUrl) {
184
- if (successTarget === 'frame') {
185
- setShowFrame(!!successUrl);
186
- } else if (successTarget === '_blank') {
187
- // 这里是安全的
188
- window.open(successUrl, '_blank');
189
- } else {
190
- // 这里是安全的
191
- window.open(successUrl, '_self');
192
- }
193
- } else if (children) {
194
- // Do nothing
195
- } else if (autoClose) {
196
- setTimeout(onClose, 2000);
197
- }
198
- };
199
-
200
- const renderRedirectUrlAfterSuccess = () => (
201
- <>
202
- <Close onClose={onClose} />
203
- <div>
204
- Redirecting to{' '}
205
- <a href={successUrl} target={successTarget}>
206
- {successUrl}
207
- </a>
208
- </div>
209
- </>
210
- );
211
-
212
- const renderFrameAfterSuccess = () => (
213
- <>
214
- <Close onClose={onClose} />
215
- <iframe
216
- style={{ width: '100%', height: '100%' }}
217
- allow="fullscreen"
218
- id="successFrame"
219
- title="successFrame"
220
- src={successUrl}
221
- {...frameProps}
222
- />
223
- </>
224
- );
225
-
226
- const showDidConnect = !successUrl || (successUrl && !success);
227
-
228
- return (
229
- <>
230
- <Button
231
- {...rest}
232
- rounded={buttonRounded}
233
- color={buttonColor}
234
- variant={buttonVariant}
235
- size={buttonSize}
236
- onClick={onStart}>
237
- {getMessage(buttonText || title, session)} {loading && <CircularProgress size={12} sx={{ color: '#fff' }} />}
238
- </Button>
239
- {open && !showDidConnect && (
240
- <Dialog
241
- open
242
- disableEscapeKeyDown
243
- fullScreen={width < theme.breakpoints.values.sm && !(browser.wallet || browser.arcSphere)}
244
- fullWidth={showFrame}
245
- maxWidth={showFrame ? 'lg' : ''}>
246
- <DialogContent
247
- style={{
248
- padding: success && !showFrame && successUrl ? 55 : 0,
249
- display: 'flex',
250
- justifyContent: 'center',
251
- alignItems: 'center',
252
- height: showFrame ? theme.breakpoints.values.md : '',
253
- }}>
254
- <Close onClose={onClose} />
255
- {successUrl && success && !showFrame && renderRedirectUrlAfterSuccess()}
256
- {showFrame && renderFrameAfterSuccess()}
257
- </DialogContent>
258
- </Dialog>
259
- )}
260
- <DidConnect
261
- popup
262
- open={open && showDidConnect}
263
- action={getActionName(config, rest)}
264
- checkFn={api.get}
265
- onClose={onClose}
266
- onSuccess={onSuccess}
267
- checkTimeout={timeout}
268
- // 3 layers of extraParams: user props, dynamically generated, from other props
269
- extraParams={Object.assign(getActionParams(config, rest, session), dynamicParams, extraParams)}
270
- webWalletUrl={webWalletUrl}
271
- messages={{
272
- title: getMessage(title, session),
273
- scan: getMessage(scanMessage, session),
274
- confirm: getMessage(confirmMessage, session),
275
- success: children || getMessage(successMessage, session),
276
- }}
277
- />
278
- </>
279
- );
280
- }
281
-
282
- PlaygroundAction.propTypes = {
283
- action: PropTypes.string.isRequired,
284
- autoClose: PropTypes.bool,
285
- buttonText: PropTypes.string,
286
- buttonColor: PropTypes.string,
287
- buttonVariant: PropTypes.string,
288
- buttonSize: PropTypes.string,
289
- buttonRounded: PropTypes.bool,
290
- title: PropTypes.string.isRequired,
291
- scanMessage: PropTypes.string,
292
- successMessage: PropTypes.string,
293
- confirmMessage: PropTypes.string,
294
- extraParams: PropTypes.object,
295
- timeout: PropTypes.number,
296
- successUrl: PropTypes.string,
297
- successTarget: PropTypes.oneOf(['_blank', '_self', 'frame']),
298
- frameProps: PropTypes.object,
299
- webWalletUrl: PropTypes.string,
300
- };
301
-
302
- export default PlaygroundAction;
@@ -1,4 +0,0 @@
1
- import { createAuthServiceSessionContext } from '@arcblock/did-connect-react/lib/Session';
2
-
3
- const { SessionProvider, SessionContext, SessionConsumer, withSession } = createAuthServiceSessionContext();
4
- export { SessionProvider, SessionContext, SessionConsumer, withSession };
package/src/index.js DELETED
@@ -1,4 +0,0 @@
1
- import PlaygroundAction from './Action';
2
- import { SessionProvider, SessionContext, SessionConsumer } from './Action/session';
3
-
4
- export { PlaygroundAction, SessionProvider, SessionContext, SessionConsumer };
package/version DELETED
@@ -1 +0,0 @@
1
- 1.10.1
package/vite.config.mjs DELETED
@@ -1,29 +0,0 @@
1
- import { defineConfig } from 'vite';
2
- import svgr from 'vite-plugin-svgr';
3
- import react from '@vitejs/plugin-react';
4
- import noBundlePlugin from 'vite-plugin-no-bundle';
5
- import fg from 'fast-glob';
6
-
7
- export default defineConfig({
8
- plugins: [
9
- react({ jsxRuntime: 'automatic' }),
10
- svgr({
11
- include: ['**/*.svg', '**/*.svg?react'],
12
- }),
13
- noBundlePlugin({
14
- root: 'src',
15
- copy: ['**/*.png', '**/*.gif', '**/*.jpg', '**/*.jpeg', '**/*.d.ts'],
16
- }),
17
- ],
18
- build: {
19
- lib: {
20
- entry: fg.sync('src/**/*.{tsx,ts,jsx,js}', {
21
- ignore: ['**/stories/**', '**/demo/**', '**/*.d.ts', '**/*.stories.*'],
22
- }),
23
- formats: ['es'],
24
- fileName: (format, entryName) => `${entryName}.js`,
25
- },
26
- outDir: 'lib',
27
- emptyOutDir: true,
28
- },
29
- });