forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamount-options-visual-editor.tsx
More file actions
168 lines (150 loc) · 5.1 KB
/
Copy pathamount-options-visual-editor.tsx
File metadata and controls
168 lines (150 loc) · 5.1 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
/*
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 { Plus, X } from 'lucide-react'
import { useState, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { StatusBadge } from '@/components/status-badge'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { safeJsonParseWithValidation } from '../utils/json-parser'
import { isArray } from '../utils/json-validators'
type AmountOptionsVisualEditorProps = {
value: string
onChange: (value: string) => void
}
export function AmountOptionsVisualEditor({
value,
onChange,
}: AmountOptionsVisualEditorProps) {
const { t } = useTranslation()
const [newAmount, setNewAmount] = useState('')
const amounts = useMemo(() => {
const parsed = safeJsonParseWithValidation<unknown[]>(value, {
fallback: [],
validator: isArray,
validatorMessage: t('Amount options must be a JSON array'),
context: 'amount options',
})
return parsed
.filter((item) => typeof item === 'number' || !isNaN(Number(item)))
.map(Number)
.sort((a, b) => a - b)
}, [value, t])
const handleAdd = () => {
const amount = parseFloat(newAmount)
if (isNaN(amount) || amount <= 0) {
return
}
try {
const updatedAmounts = [...amounts, amount]
.filter((v, i, a) => a.indexOf(v) === i) // Remove duplicates
.sort((a, b) => a - b)
onChange(JSON.stringify(updatedAmounts, null, 2))
setNewAmount('')
} catch (_error) {
// eslint-disable-next-line no-console
console.error('Failed to add amount:', _error)
}
}
const handleRemove = (amount: number) => {
try {
const updatedAmounts = amounts.filter((a) => a !== amount)
onChange(JSON.stringify(updatedAmounts, null, 2))
} catch (_error) {
// eslint-disable-next-line no-console
console.error('Failed to remove amount:', _error)
}
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.preventDefault()
handleAdd()
}
}
return (
<div className='space-y-4'>
<div>
<p className='text-muted-foreground mb-3 text-sm'>
{t('Preset recharge amounts displayed to users')}
</p>
{amounts.length === 0 ? (
<div className='text-muted-foreground rounded-lg border border-dashed p-6 text-center text-sm'>
{t(
'No amount options configured. Add amounts below to get started.'
)}
</div>
) : (
<div className='flex flex-wrap gap-2'>
{amounts.map((amount) => (
<StatusBadge
key={amount}
variant='neutral'
className='text-base'
copyable={false}
>
<span className='font-mono'>${amount}</span>
<Button
type='button'
variant='ghost'
size='icon-sm'
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
handleRemove(amount)
}}
className='hover:bg-muted-foreground/20 size-auto p-0.5'
aria-label={t('Remove ${{amount}}', { amount })}
>
<X className='h-3.5 w-3.5' />
</Button>
</StatusBadge>
))}
</div>
)}
</div>
<div className='flex flex-col gap-2 sm:flex-row sm:items-end'>
<div className='flex-1'>
<Label htmlFor='new-amount' className='mb-2 block'>
{t('Add new amount')}
</Label>
<Input
id='new-amount'
type='number'
step='0.01'
min='0'
placeholder={t('e.g., 100')}
value={newAmount}
onChange={(e) => setNewAmount(e.target.value)}
onKeyDown={handleKeyDown}
/>
</div>
<Button
type='button'
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
handleAdd()
}}
disabled={!newAmount || parseFloat(newAmount) <= 0}
className='w-full sm:w-auto'
>
<Plus className='h-4 w-4 sm:mr-2' />
<span className='sm:inline'>{t('Add')}</span>
</Button>
</div>
</div>
)
}