payment-kit 1.17.6 → 1.17.7

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/blocklet.yml CHANGED
@@ -14,7 +14,7 @@ repository:
14
14
  type: git
15
15
  url: git+https://github.com/blocklet/payment-kit.git
16
16
  specVersion: 1.2.8
17
- version: 1.17.6
17
+ version: 1.17.7
18
18
  logo: logo.png
19
19
  files:
20
20
  - dist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payment-kit",
3
- "version": "1.17.6",
3
+ "version": "1.17.7",
4
4
  "scripts": {
5
5
  "dev": "blocklet dev --open",
6
6
  "eject": "vite eject",
@@ -53,7 +53,7 @@
53
53
  "@arcblock/validator": "^1.19.3",
54
54
  "@blocklet/js-sdk": "^1.16.37",
55
55
  "@blocklet/logger": "^1.16.37",
56
- "@blocklet/payment-react": "1.17.6",
56
+ "@blocklet/payment-react": "1.17.7",
57
57
  "@blocklet/sdk": "^1.16.37",
58
58
  "@blocklet/ui-react": "^2.11.27",
59
59
  "@blocklet/uploader": "^0.1.64",
@@ -120,7 +120,7 @@
120
120
  "devDependencies": {
121
121
  "@abtnode/types": "^1.16.37",
122
122
  "@arcblock/eslint-config-ts": "^0.3.3",
123
- "@blocklet/payment-types": "1.17.6",
123
+ "@blocklet/payment-types": "1.17.7",
124
124
  "@types/cookie-parser": "^1.4.7",
125
125
  "@types/cors": "^2.8.17",
126
126
  "@types/debug": "^4.1.12",
@@ -166,5 +166,5 @@
166
166
  "parser": "typescript"
167
167
  }
168
168
  },
169
- "gitHead": "50e93ab4340be53c9d9bd18718c6db26048578c5"
169
+ "gitHead": "f2a439af19e405746a6663b4dbef8a96231a1076"
170
170
  }
@@ -1,20 +1,57 @@
1
- /* eslint-disable no-nested-ternary */
2
1
  import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
3
2
  import Toast from '@arcblock/ux/lib/Toast';
4
3
  import { api, formatError } from '@blocklet/payment-react';
5
- import type { TPaymentCurrency, TPaymentMethod } from '@blocklet/payment-types';
4
+ import type { TPaymentCurrency, TPaymentMethodExpanded } from '@blocklet/payment-types';
6
5
  import { AddOutlined } from '@mui/icons-material';
7
- import { Button, CircularProgress } from '@mui/material';
6
+ import {
7
+ Button,
8
+ CircularProgress,
9
+ TextField,
10
+ Autocomplete,
11
+ Box,
12
+ Typography,
13
+ Avatar,
14
+ Stack,
15
+ Divider,
16
+ } from '@mui/material';
8
17
  import { useSetState } from 'ahooks';
9
18
  import { FormProvider, useForm } from 'react-hook-form';
10
19
  import { dispatch } from 'use-bus';
11
-
20
+ import { useEffect } from 'react';
12
21
  import DrawerForm from '../drawer-form';
13
22
  import PaymentCurrencyForm from './form';
14
23
 
15
- export default function PaymentCurrencyAdd({ method, onClose }: { method: TPaymentMethod; onClose: () => void }) {
24
+ const loadTokenList = () =>
25
+ import(
26
+ /* webpackChunkName: "payment-token-list" */
27
+ './tokenList.json'
28
+ );
29
+ interface TokenInfo {
30
+ address: string;
31
+ chainId: number;
32
+ decimals: number;
33
+ name: string;
34
+ symbol: string;
35
+ logoURI?: string;
36
+ }
37
+
38
+ export default function PaymentCurrencyAdd({
39
+ method,
40
+ onClose,
41
+ }: {
42
+ method: TPaymentMethodExpanded;
43
+ onClose: () => void;
44
+ }) {
16
45
  const { t } = useLocaleContext();
17
- const [state, setState] = useSetState({ loading: false });
46
+ const [state, setState] = useSetState<{
47
+ loading: boolean;
48
+ tokenListLoading: boolean;
49
+ availableTokens: TokenInfo[];
50
+ }>({
51
+ loading: false,
52
+ tokenListLoading: false,
53
+ availableTokens: [],
54
+ });
18
55
 
19
56
  const methods = useForm<TPaymentCurrency>({
20
57
  defaultValues: {
@@ -25,7 +62,39 @@ export default function PaymentCurrencyAdd({ method, onClose }: { method: TPayme
25
62
  contract: '',
26
63
  },
27
64
  });
28
- const { handleSubmit } = methods;
65
+ const { handleSubmit, setValue } = methods;
66
+
67
+ const showTokenSelect = ['ethereum', 'base'].includes(method.type);
68
+
69
+ // @ts-ignore
70
+ const chainId = method?.settings?.[method.type]?.chain_id || '';
71
+
72
+ useEffect(() => {
73
+ if (showTokenSelect && !state.availableTokens.length) {
74
+ setState({ tokenListLoading: true });
75
+ loadTokenList().then((module) => {
76
+ const availableTokens = showTokenSelect
77
+ ? // @ts-ignore
78
+ ((module.default?.[chainId] as TokenInfo[]) || []).filter(
79
+ (token: TokenInfo) => !(method.payment_currencies || []).find((c: any) => c.contract === token.address)
80
+ )
81
+ : [];
82
+ setState({
83
+ availableTokens,
84
+ tokenListLoading: false,
85
+ });
86
+ });
87
+ }
88
+ }, [showTokenSelect, chainId]);
89
+
90
+ const handleTokenSelect = (token: TokenInfo | null) => {
91
+ if (token) {
92
+ setValue('name', token.name);
93
+ setValue('description', `${token.name} (${token.symbol})`);
94
+ setValue('logo', token.logoURI || '');
95
+ setValue('contract', token.address);
96
+ }
97
+ };
29
98
 
30
99
  const onSubmit = (data: TPaymentCurrency) => {
31
100
  setState({ loading: true });
@@ -54,10 +123,64 @@ export default function PaymentCurrencyAdd({ method, onClose }: { method: TPayme
54
123
  width={640}
55
124
  addons={
56
125
  <Button variant="contained" size="small" onClick={handleSubmit(onSubmit)} disabled={state.loading}>
57
- {state.loading ? <CircularProgress size="small" /> : t('admin.paymentCurrency.save')}
126
+ {state.loading ? <CircularProgress size={20} /> : t('admin.paymentCurrency.save')}
58
127
  </Button>
59
128
  }>
60
129
  <FormProvider {...methods}>
130
+ {showTokenSelect && (
131
+ <Box sx={{ mb: 3 }}>
132
+ <Autocomplete
133
+ disablePortal
134
+ options={state.availableTokens}
135
+ loading={state.tokenListLoading}
136
+ getOptionLabel={(option: TokenInfo) => option.symbol}
137
+ onChange={(_, value) => handleTokenSelect(value)}
138
+ renderInput={(params) => (
139
+ <TextField
140
+ {...params}
141
+ label={t('admin.paymentCurrency.quickAdd')}
142
+ placeholder={t('admin.paymentCurrency.searchToken')}
143
+ fullWidth
144
+ />
145
+ )}
146
+ renderOption={(props, option) => (
147
+ <Box
148
+ component="li"
149
+ {...props}
150
+ sx={{
151
+ display: 'flex',
152
+ alignItems: 'center',
153
+ gap: 1,
154
+ cursor: 'pointer',
155
+ '&:hover': {
156
+ backgroundColor: 'action.hover',
157
+ },
158
+ }}>
159
+ <Avatar
160
+ src={option.logoURI || option.symbol}
161
+ alt={option.symbol}
162
+ sx={{
163
+ width: 24,
164
+ height: 24,
165
+ }}
166
+ />
167
+ <Stack>
168
+ <Typography fontWeight={500}>{option.name}</Typography>
169
+ <Typography variant="caption" color="text.secondary">
170
+ {option.symbol}
171
+ </Typography>
172
+ </Stack>
173
+ </Box>
174
+ )}
175
+ isOptionEqualToValue={(option, value) => option.address === value.address}
176
+ />
177
+ <Divider sx={{ my: 3 }}>
178
+ <Typography variant="caption" color="text.secondary">
179
+ {t('admin.paymentCurrency.orManualInput')}
180
+ </Typography>
181
+ </Divider>
182
+ </Box>
183
+ )}
61
184
  <PaymentCurrencyForm />
62
185
  </FormProvider>
63
186
  </DrawerForm>