forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate-limit-visual-editor.tsx
More file actions
208 lines (183 loc) · 6.18 KB
/
Copy pathrate-limit-visual-editor.tsx
File metadata and controls
208 lines (183 loc) · 6.18 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
/*
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, Search } 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 { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { safeJsonParseWithValidation } from '../utils/json-parser'
import { isObjectRecord } from '../utils/json-validators'
import { RateLimitDialog, type RateLimitEntryData } from './rate-limit-dialog'
type RateLimitVisualEditorProps = {
value: string
onChange: (value: string) => void
}
type RateLimitEntry = RateLimitEntryData
export function RateLimitVisualEditor({
value,
onChange,
}: RateLimitVisualEditorProps) {
const { t } = useTranslation()
const [searchText, setSearchText] = useState('')
const [dialogOpen, setDialogOpen] = useState(false)
const [editData, setEditData] = useState<RateLimitEntry | null>(null)
const rateLimits = useMemo(() => {
if (!value || value.trim() === '') return []
const parsed = safeJsonParseWithValidation<Record<string, unknown>>(value, {
fallback: {},
validator: isObjectRecord,
validatorMessage: 'Rate limits must be a JSON object',
context: 'rate limits',
})
return Object.entries(parsed)
.map(([groupName, limits]) => {
if (
Array.isArray(limits) &&
limits.length === 2 &&
typeof limits[0] === 'number' &&
typeof limits[1] === 'number'
) {
return {
groupName,
maxRequests: limits[0],
maxSuccess: limits[1],
}
}
return null
})
.filter((item): item is RateLimitEntry => item !== null)
}, [value])
const filteredRateLimits = useMemo(() => {
if (!searchText) return rateLimits
const lowerSearch = searchText.toLowerCase()
return rateLimits.filter((limit) =>
limit.groupName.toLowerCase().includes(lowerSearch)
)
}, [rateLimits, searchText])
const handleSave = (data: RateLimitEntryData) => {
const parsed = safeJsonParseWithValidation<Record<string, unknown>>(value, {
fallback: {},
validator: isObjectRecord,
silent: true,
})
if (editData && editData.groupName !== data.groupName) {
delete parsed[editData.groupName]
}
parsed[data.groupName] = [data.maxRequests, data.maxSuccess]
onChange(JSON.stringify(parsed, null, 2))
}
const handleDelete = (groupName: string) => {
const parsed = safeJsonParseWithValidation<Record<string, unknown>>(value, {
fallback: {},
validator: isObjectRecord,
silent: true,
})
delete parsed[groupName]
onChange(JSON.stringify(parsed, null, 2))
}
const handleEdit = (limit: RateLimitEntry) => {
setEditData(limit)
setDialogOpen(true)
}
const handleAdd = () => {
setEditData(null)
setDialogOpen(true)
}
return (
<div className='space-y-4'>
<div className='flex items-center gap-4'>
<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 group names...')}
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
className='pl-9'
/>
</div>
<Button onClick={handleAdd}>
<Plus className='mr-2 h-4 w-4' />
{t('Add group')}
</Button>
</div>
<StaticDataTable
data={filteredRateLimits}
getRowKey={(limit) => limit.groupName}
emptyContent={
searchText
? t('No groups match your search')
: t(
'No group-based rate limits configured. Click "Add group" to get started.'
)
}
columns={[
{
id: 'group',
header: t('Group Name'),
cellClassName: 'font-medium',
cell: (limit) => limit.groupName,
},
{
id: 'max-requests',
header: t('Max Requests (incl. failures)'),
className: 'text-right',
cellClassName: 'text-right',
cell: (limit) => (
<span className='font-mono'>
{limit.maxRequests === 0
? t('Unlimited')
: limit.maxRequests.toLocaleString()}
</span>
),
},
{
id: 'max-success',
header: t('Max Success'),
className: 'text-right',
cellClassName: 'text-right',
cell: (limit) => (
<span className='font-mono'>
{limit.maxSuccess.toLocaleString()}
</span>
),
},
{
id: 'actions',
header: t('Actions'),
className: 'text-right',
cellClassName: 'text-right',
cell: (limit) => (
<StaticRowActions
editLabel={t('Edit')}
deleteLabel={t('Delete')}
menuLabel={t('Open menu')}
onEdit={() => handleEdit(limit)}
onDelete={() => handleDelete(limit.groupName)}
/>
),
},
]}
/>
<RateLimitDialog
open={dialogOpen}
onOpenChange={setDialogOpen}
onSave={handleSave}
editData={editData}
/>
</div>
)
}