@arcblock/did-playground 1.16.1 → 1.16.2

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": "1.16.1",
3
+ "version": "1.16.2",
4
4
  "description": "React components that works with wallet-playground",
5
5
  "keywords": [
6
6
  "react",
@@ -13,7 +13,8 @@
13
13
  "main": "lib/index.js",
14
14
  "files": [
15
15
  "version",
16
- "lib"
16
+ "lib",
17
+ "src"
17
18
  ],
18
19
  "repository": {
19
20
  "type": "git",
@@ -33,9 +34,9 @@
33
34
  "url": "https://github.com/ArcBlock/wallet-playground/issues"
34
35
  },
35
36
  "dependencies": {
36
- "@arcblock/did-connect": "^1.16.1",
37
- "@arcblock/react-hooks": "^1.16.1",
38
- "@arcblock/ux": "^1.16.1",
37
+ "@arcblock/did-connect": "^1.16.2",
38
+ "@arcblock/react-hooks": "^1.16.2",
39
+ "@arcblock/ux": "^1.16.2",
39
40
  "@material-ui/core": "^4.12.3",
40
41
  "axios": "^0.21.1",
41
42
  "core-js": "^3.6.4",
@@ -56,5 +57,5 @@
56
57
  "publishConfig": {
57
58
  "access": "public"
58
59
  },
59
- "gitHead": "ca18ee446fff68422a8414dd6d4eb8b724e7ac40"
60
+ "gitHead": "b869c847f922a9e89158e7d3ba4219789e6d20d0"
60
61
  }
@@ -0,0 +1,575 @@
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
+ };
@@ -0,0 +1,285 @@
1
+ import React, { useContext, useState, useEffect } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import useWindowSize from 'react-use/lib/useWindowSize';
4
+ import useBrowser from '@arcblock/react-hooks/lib/useBrowser';
5
+ import styled from 'styled-components';
6
+
7
+ import CircularProgress from '@material-ui/core/CircularProgress';
8
+ import Dialog from '@material-ui/core/Dialog';
9
+ import DialogContent from '@material-ui/core/DialogContent';
10
+ import { withTheme } from '@material-ui/core/styles';
11
+
12
+ import DidConnect from '@arcblock/did-connect/lib/Connect';
13
+ import Button from '@arcblock/ux/lib/Button';
14
+ import { mergeProps } from '@arcblock/ux/lib/Util';
15
+
16
+ import { SessionContext } from './session';
17
+ import { actions, getMessage, getActionName, getActionParams } from './actions';
18
+
19
+ function Close({ onClose }) {
20
+ return <CloseContainer onClick={onClose}>&times;</CloseContainer>;
21
+ }
22
+
23
+ Close.propTypes = { onClose: PropTypes.func.isRequired };
24
+ const CloseContainer = styled.div`
25
+ display: ${props => (props.disableClose ? 'none' : 'block')};
26
+ position: absolute;
27
+ top: 1rem;
28
+ right: 1rem;
29
+ color: #999999;
30
+ font-size: 2rem;
31
+ line-height: 1rem;
32
+ cursor: pointer;
33
+ user-select: none;
34
+ `;
35
+
36
+ function PlaygroundAction(props) {
37
+ const newProps = mergeProps(props, PlaygroundAction, ['buttonRounded', 'extraParams', 'timeout']);
38
+ const {
39
+ autoClose,
40
+ action,
41
+ buttonText,
42
+ buttonColor,
43
+ buttonVariant,
44
+ buttonSize,
45
+ buttonRounded,
46
+ children,
47
+ disableClose,
48
+ title,
49
+ scanMessage,
50
+ successMessage,
51
+ successUrl,
52
+ successTarget,
53
+ frameProps,
54
+ confirmMessage,
55
+ extraParams,
56
+ timeout,
57
+ theme,
58
+ webWalletUrl,
59
+ ...rest
60
+ } = newProps;
61
+
62
+ const browser = useBrowser();
63
+ const { api, session } = useContext(SessionContext);
64
+ const [open, setOpen] = useState(false);
65
+ const [loading, setLoading] = useState(false);
66
+ const [dynamicParams, setDynamicParams] = useState({});
67
+ const { width } = useWindowSize();
68
+ const [success, setSuccess] = useState(false);
69
+ const [showFrame, setShowFrame] = useState(success && successUrl && successTarget === 'frame');
70
+
71
+ // 当打开或关闭组件时,重置部分状态
72
+ useEffect(
73
+ () => () => {
74
+ setSuccess(false);
75
+ setShowFrame(false);
76
+ },
77
+ [open]
78
+ );
79
+
80
+ // If this is just a login button, we do not do anything actually
81
+ if (action === 'login') {
82
+ if (session.user) {
83
+ return (
84
+ <Button
85
+ {...rest}
86
+ rounded={buttonRounded}
87
+ color={buttonColor}
88
+ variant={buttonVariant}
89
+ size={buttonSize}>
90
+ {getMessage(successMessage || `Hello ${session.user.name}`, session)}
91
+ </Button>
92
+ );
93
+ }
94
+
95
+ return (
96
+ <Button
97
+ {...rest}
98
+ rounded={buttonRounded}
99
+ color={buttonColor}
100
+ variant={buttonVariant}
101
+ size={buttonSize}
102
+ onClick={() => session.login()}>
103
+ {getMessage(buttonText || title, session)}
104
+ </Button>
105
+ );
106
+ }
107
+
108
+ const config = actions[action];
109
+ if (!actions[action]) {
110
+ throw new Error(`Unsupported playground action ${action}`);
111
+ }
112
+
113
+ const doStart = async () => {
114
+ if (typeof config.onStart === 'function') {
115
+ try {
116
+ setLoading(true);
117
+ const params = await config.onStart(api, session);
118
+ setDynamicParams(params);
119
+ setLoading(false);
120
+ } catch (err) {
121
+ console.error(
122
+ `Cannot generate dynamicParams for playground action ${getActionName(config, rest)}`
123
+ );
124
+ }
125
+ setOpen(true);
126
+ } else {
127
+ setOpen(true);
128
+ }
129
+ };
130
+
131
+ const onStart = async () => {
132
+ if (!session.user) {
133
+ session.login(doStart);
134
+ return;
135
+ }
136
+
137
+ await doStart();
138
+ };
139
+
140
+ const onClose = () => setOpen(false);
141
+
142
+ const onSuccess = () => {
143
+ setSuccess(true);
144
+ if (successUrl) {
145
+ if (successTarget === 'frame') {
146
+ setShowFrame(!!successUrl);
147
+ } else if (successTarget === '_blank') {
148
+ window.open(successUrl, '_blank');
149
+ } else {
150
+ window.open(successUrl, '_self');
151
+ }
152
+ } else if (children) {
153
+ // Do nothing
154
+ } else if (autoClose) {
155
+ setTimeout(onClose, 2000);
156
+ }
157
+ };
158
+
159
+ const renderRedirectUrlAfterSuccess = () => (
160
+ <>
161
+ <Close onClose={onClose} />
162
+ <div>
163
+ Redirecting to{' '}
164
+ <a href={successUrl} target={successTarget}>
165
+ {successUrl}
166
+ </a>
167
+ </div>
168
+ </>
169
+ );
170
+
171
+ const renderFrameAfterSuccess = () => (
172
+ <>
173
+ <Close onClose={onClose} />
174
+ <iframe
175
+ style={{ width: '100%', height: '100%' }}
176
+ allow="fullscreen"
177
+ id="successFrame"
178
+ title="successFrame"
179
+ src={successUrl}
180
+ {...frameProps}
181
+ />
182
+ </>
183
+ );
184
+
185
+ const showDidConnect = !successUrl || (successUrl && !success);
186
+
187
+ return (
188
+ <>
189
+ <Button
190
+ {...rest}
191
+ rounded={buttonRounded}
192
+ color={buttonColor}
193
+ variant={buttonVariant}
194
+ size={buttonSize}
195
+ onClick={onStart}>
196
+ {getMessage(buttonText || title, session)}{' '}
197
+ {loading && <CircularProgress size={12} color="#fff" />}
198
+ </Button>
199
+ {open && !showDidConnect && (
200
+ <Dialog
201
+ open
202
+ disableBackdropClick
203
+ disableEscapeKeyDown
204
+ fullScreen={width < theme.breakpoints.values.sm && !browser.wallet}
205
+ fullWidth={showFrame}
206
+ maxWidth={showFrame ? 'lg' : ''}>
207
+ <DialogContent
208
+ style={{
209
+ padding: success && !showFrame && successUrl ? 55 : 0,
210
+ display: 'flex',
211
+ justifyContent: 'center',
212
+ alignItems: 'center',
213
+ height: showFrame ? theme.breakpoints.values.md : '',
214
+ }}>
215
+ <Close onClose={onClose} />
216
+ {successUrl && success && !showFrame && renderRedirectUrlAfterSuccess()}
217
+ {showFrame && renderFrameAfterSuccess()}
218
+ </DialogContent>
219
+ </Dialog>
220
+ )}
221
+ <DidConnect
222
+ popup
223
+ open={open && showDidConnect}
224
+ action={getActionName(config, rest)}
225
+ checkFn={api.get}
226
+ onClose={onClose}
227
+ onSuccess={onSuccess}
228
+ checkTimeout={timeout}
229
+ // 3 layers of extraParams: user props, dynamically generated, from other props
230
+ extraParams={Object.assign(
231
+ getActionParams(config, rest, session),
232
+ dynamicParams,
233
+ extraParams
234
+ )}
235
+ webWalletUrl={webWalletUrl}
236
+ messages={{
237
+ title: getMessage(title, session),
238
+ scan: getMessage(scanMessage, session),
239
+ confirm: getMessage(confirmMessage, session),
240
+ success: children || getMessage(successMessage, session),
241
+ }}
242
+ />
243
+ </>
244
+ );
245
+ }
246
+
247
+ PlaygroundAction.propTypes = {
248
+ action: PropTypes.string.isRequired,
249
+ autoClose: PropTypes.bool,
250
+ buttonText: PropTypes.string,
251
+ buttonColor: PropTypes.string,
252
+ buttonVariant: PropTypes.string,
253
+ buttonSize: PropTypes.string,
254
+ buttonRounded: PropTypes.bool,
255
+ title: PropTypes.string.isRequired,
256
+ scanMessage: PropTypes.string,
257
+ successMessage: PropTypes.string,
258
+ confirmMessage: PropTypes.string,
259
+ extraParams: PropTypes.object,
260
+ timeout: PropTypes.number,
261
+ successUrl: PropTypes.string,
262
+ successTarget: PropTypes.oneOf(['_blank', '_self', 'frame']),
263
+ frameProps: PropTypes.object,
264
+ webWalletUrl: PropTypes.string,
265
+ };
266
+
267
+ PlaygroundAction.defaultProps = {
268
+ autoClose: true, // 只在没有 successUrl 属性下有效
269
+ buttonText: '',
270
+ buttonColor: 'primary', // primary | secondary | reverse | error
271
+ buttonVariant: 'contained', // contained | outlined | default
272
+ buttonSize: 'large', // small | large | medium
273
+ buttonRounded: false,
274
+ scanMessage: 'Scan the QRCode with your DID Wallet',
275
+ confirmMessage: 'Confirm in your DID Wallet',
276
+ successMessage: 'Operation success!',
277
+ extraParams: {},
278
+ timeout: 5 * 60 * 1000,
279
+ successUrl: '',
280
+ successTarget: '_self',
281
+ frameProps: {},
282
+ webWalletUrl: '',
283
+ };
284
+
285
+ export default withTheme(PlaygroundAction);
@@ -0,0 +1,5 @@
1
+ import { createAuthServiceSessionContext } from '@arcblock/did-connect/lib/Session';
2
+
3
+ const { SessionProvider, SessionContext, SessionConsumer, withSession } =
4
+ createAuthServiceSessionContext();
5
+ export { SessionProvider, SessionContext, SessionConsumer, withSession };
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import PlaygroundAction from './Action';
2
+ import { SessionProvider, SessionContext, SessionConsumer } from './Action/session';
3
+
4
+ export { PlaygroundAction, SessionProvider, SessionContext, SessionConsumer };