forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-methods-visual-editor.tsx
More file actions
474 lines (442 loc) · 14.9 KB
/
Copy pathpayment-methods-visual-editor.tsx
File metadata and controls
474 lines (442 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Lightbulb, Pencil, Plus, Search, Trash2 } from 'lucide-react'
import { useState, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { StaticDataTable } from '@/components/data-table/static/static-data-table'
import { StaticRowActions } from '@/components/data-table/static/static-row-actions'
import { ReactIconByName } from '@/components/react-icon-by-name'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover'
import { safeJsonParseWithValidation } from '../utils/json-parser'
import { isArray } from '../utils/json-validators'
import {
PaymentMethodDialog,
type PaymentMethodData,
} from './payment-method-dialog'
type PaymentMethodsVisualEditorProps = {
value: string
onChange: (value: string) => void
}
const PAYMENT_TYPE_ICON_NAMES: Record<string, string> = {
alipay: 'SiAlipay',
stripe: 'SiStripe',
waffo_pancake: 'LuCreditCard',
wxpay: 'SiWechat',
}
function getDefaultIconName(type: string) {
return PAYMENT_TYPE_ICON_NAMES[type] ?? ''
}
function getEffectiveIconName(method: PaymentMethodData) {
return method.icon || getDefaultIconName(method.type)
}
export function PaymentMethodsVisualEditor({
value,
onChange,
}: PaymentMethodsVisualEditorProps) {
const { t } = useTranslation()
const paymentTemplates = [
{
name: t('Epay Alipay'),
template: {
icon: getDefaultIconName('alipay'),
name: '支付宝',
type: 'alipay',
},
},
{
name: t('Epay WeChat Pay'),
template: {
icon: getDefaultIconName('wxpay'),
name: '微信',
type: 'wxpay',
},
},
{
name: t('Stripe'),
template: {
icon: getDefaultIconName('stripe'),
min_topup: '10',
name: 'Stripe',
type: 'stripe',
},
},
{
name: 'Waffo Pancake',
template: {
icon: getDefaultIconName('waffo_pancake'),
name: 'Waffo Pancake',
type: 'waffo_pancake',
},
},
{
name: t('Custom Epay method'),
template: {
icon: 'LuCreditCard',
min_topup: '50',
name: '自定义1',
type: 'custom1',
},
},
]
const [searchText, setSearchText] = useState('')
const [dialogOpen, setDialogOpen] = useState(false)
const [editData, setEditData] = useState<PaymentMethodData | null>(null)
const paymentMethods = useMemo(() => {
const parsed = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
validatorMessage: 'Payment methods must be a JSON array',
context: 'payment methods',
})
return parsed.filter(
(item): item is PaymentMethodData =>
typeof item === 'object' &&
item !== null &&
'name' in item &&
'type' in item &&
typeof item.name === 'string' &&
typeof item.type === 'string' &&
(!('icon' in item) || typeof item.icon === 'string') &&
(!('min_topup' in item) || typeof item.min_topup === 'string') &&
(!('color' in item) || typeof item.color === 'string')
)
}, [value])
const filteredMethods = useMemo(() => {
if (!searchText) return paymentMethods
const lowerSearch = searchText.toLowerCase()
return paymentMethods.filter(
(method) =>
method.name.toLowerCase().includes(lowerSearch) ||
method.type.toLowerCase().includes(lowerSearch) ||
getEffectiveIconName(method).toLowerCase().includes(lowerSearch)
)
}, [paymentMethods, searchText])
const handleSave = (data: PaymentMethodData) => {
const parsed = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
silent: true,
})
const updatedArray = [...parsed]
if (editData) {
const index = updatedArray.findIndex(
(item): item is PaymentMethodData =>
typeof item === 'object' &&
item !== null &&
'name' in item &&
'type' in item &&
item.name === editData.name &&
item.type === editData.type
)
if (index !== -1) {
updatedArray[index] = data
} else {
updatedArray.push(data)
}
} else {
updatedArray.push(data)
}
onChange(JSON.stringify(updatedArray, null, 2))
}
const handleDelete = (method: PaymentMethodData) => {
const parsed = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
silent: true,
})
const updatedArray = parsed.filter(
(item) =>
!(
typeof item === 'object' &&
item !== null &&
'name' in item &&
'type' in item &&
item.name === method.name &&
item.type === method.type
)
)
onChange(JSON.stringify(updatedArray, null, 2))
}
const handleEdit = (method: PaymentMethodData) => {
setEditData(method)
setDialogOpen(true)
}
const handleAdd = () => {
setEditData(null)
setDialogOpen(true)
}
const handleInsertTemplate = (template: PaymentMethodData) => {
const parsed = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
silent: true,
})
// Check if template already exists
const exists = parsed.some(
(item) =>
typeof item === 'object' &&
item !== null &&
'type' in item &&
'name' in item &&
item.type === template.type &&
item.name === template.name
)
if (!exists) {
parsed.push(template)
onChange(JSON.stringify(parsed, null, 2))
}
}
return (
<div className='space-y-4'>
<div className='flex flex-col gap-3 sm:flex-row sm:items-center'>
<div className='relative flex-1'>
<Search className='text-muted-foreground absolute top-2.5 left-2.5 h-4 w-4' />
<Input
placeholder={t('Search payment methods...')}
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
className='pl-9'
/>
</div>
<div className='flex gap-2'>
<Popover>
<PopoverTrigger
render={
<Button variant='outline' className='flex-1 sm:flex-none' />
}
>
<Lightbulb className='h-4 w-4 sm:mr-2' />
<span className='sm:inline'>{t('Templates')}</span>
</PopoverTrigger>
<PopoverContent className='w-60'>
<div className='space-y-2'>
<p className='text-muted-foreground text-xs'>
{t('Quick insert payment entries')}
</p>
<div className='space-y-1'>
{paymentTemplates.map((item) => (
<Button
key={item.name}
type='button'
variant='ghost'
className='w-full justify-start text-sm'
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
handleInsertTemplate(item.template)
}}
>
<Plus className='mr-2 h-3 w-3' />
{item.name}
</Button>
))}
</div>
</div>
</PopoverContent>
</Popover>
<Button
type='button'
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
handleAdd()
}}
className='flex-1 sm:flex-none'
>
<Plus className='h-4 w-4 sm:mr-2' />
<span className='sm:inline'>{t('Add method')}</span>
</Button>
</div>
</div>
{filteredMethods.length === 0 ? (
<div className='text-muted-foreground rounded-lg border border-dashed p-8 text-center text-sm'>
{searchText
? t('No payment methods match your search')
: t(
'No payment methods configured. Click "Add method" or use templates to get started.'
)}
</div>
) : (
<div className='rounded-md border'>
{/* Desktop table view */}
<StaticDataTable
className='hidden rounded-none border-0 md:block'
data={filteredMethods}
getRowKey={(method, index) => `${method.type}-${index}`}
columns={[
{
id: 'name',
header: t('Name'),
cellClassName: 'font-medium',
cell: (method) => method.name,
},
{
id: 'type',
header: t('Payment type key'),
cell: (method) => (
<code className='bg-muted rounded px-1.5 py-0.5 text-sm'>
{method.type}
</code>
),
},
{
id: 'icon',
header: t('Icon'),
cell: (method) => {
const iconName = getEffectiveIconName(method)
return iconName ? (
<div className='flex items-center gap-2'>
<ReactIconByName
name={iconName}
className='text-muted-foreground size-5 shrink-0'
title={iconName}
/>
<span className='text-muted-foreground truncate font-mono text-sm'>
{iconName}
</span>
</div>
) : (
<span className='text-muted-foreground text-sm'>—</span>
)
},
},
{
id: 'min-top-up',
header: t('Min Top-up'),
cell: (method) =>
method.min_topup ? (
<span className='font-mono text-sm'>
{method.min_topup}
</span>
) : (
<span className='text-muted-foreground text-sm'>—</span>
),
},
{
id: 'actions',
header: t('Actions'),
className: 'text-right',
cellClassName: 'text-right',
cell: (method) => (
<StaticRowActions
editLabel={t('Edit')}
deleteLabel={t('Delete')}
menuLabel={t('Open menu')}
onEdit={() => handleEdit(method)}
onDelete={() => handleDelete(method)}
/>
),
},
]}
/>
{/* Mobile card view */}
<div className='divide-y md:hidden'>
{filteredMethods.map((method) => {
const iconName = getEffectiveIconName(method)
const methodKey = [
method.type,
method.name,
method.icon,
method.min_topup,
method.color,
]
.filter(Boolean)
.join('-')
return (
<div key={methodKey} className='p-4'>
<div className='mb-3 flex items-start justify-between'>
<div className='flex-1'>
<div className='mb-1 font-medium'>{method.name}</div>
<code className='bg-muted rounded px-1.5 py-0.5 text-xs'>
{method.type}
</code>
</div>
<div className='flex gap-1'>
<Button
type='button'
variant='ghost'
size='sm'
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
handleEdit(method)
}}
>
<Pencil className='h-4 w-4' />
</Button>
<Button
type='button'
variant='ghost'
size='sm'
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
handleDelete(method)
}}
>
<Trash2 className='h-4 w-4' />
</Button>
</div>
</div>
<div className='space-y-2 text-sm'>
<div className='flex items-center gap-2'>
<span className='text-muted-foreground min-w-20'>
{t('Icon')}
</span>
{iconName ? (
<div className='flex min-w-0 items-center gap-2'>
<ReactIconByName
name={iconName}
className='text-muted-foreground size-5 shrink-0'
title={iconName}
/>
<span className='text-muted-foreground truncate font-mono text-xs'>
{iconName}
</span>
</div>
) : (
<span className='text-muted-foreground text-xs'>—</span>
)}
</div>
{method.min_topup && (
<div className='flex items-center gap-2'>
<span className='text-muted-foreground min-w-20'>
{t('Min Top-up:')}
</span>
<span className='font-mono'>{method.min_topup}</span>
</div>
)}
</div>
</div>
)
})}
</div>
</div>
)}
<PaymentMethodDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
onSave={handleSave}
editData={editData}
/>
</div>
)
}