forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels-columns.tsx
More file actions
478 lines (441 loc) · 13.2 KB
/
Copy pathmodels-columns.tsx
File metadata and controls
478 lines (441 loc) · 13.2 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
475
476
477
478
/*
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 type { ColumnDef } from '@tanstack/react-table'
import { useTranslation } from 'react-i18next'
import { BadgeCell, BadgeListCell } from '@/components/data-table'
import { GroupBadge } from '@/components/group-badge'
import { ProviderBadge } from '@/components/provider-badge'
import { StatusBadge } from '@/components/status-badge'
import { TableId } from '@/components/table-id'
import { Checkbox } from '@/components/ui/checkbox'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip'
import { formatTimestampToDate } from '@/lib/format'
import { getLobeIcon } from '@/lib/lobe-icon'
import {
getModelStatusConfig,
getNameRuleConfig,
getQuotaTypeConfig,
} from '../constants'
import { parseModelTags, formatEndpointsDisplay } from '../lib'
import type { Model, Vendor } from '../types'
import { DataTableRowActions } from './data-table-row-actions'
import { DescriptionCell } from './description-cell'
function getCompactModelIcon(iconKey: string) {
const baseIconKey = iconKey.split('.')[0]
return getLobeIcon(`${baseIconKey}.Avatar.type={'platform'}`, 20)
}
/**
* Generate models columns configuration
*/
export function useModelsColumns(vendors: Vendor[] = []): ColumnDef<Model>[] {
const { t } = useTranslation()
// Get translated configs
const NAME_RULE_CONFIG = getNameRuleConfig(t)
const MODEL_STATUS_CONFIG = getModelStatusConfig(t)
const QUOTA_TYPE_CONFIG = getQuotaTypeConfig(t)
const vendorMap: Record<number, Vendor> = {}
vendors.forEach((v) => {
vendorMap[v.id] = v
})
return [
// Checkbox column
{
id: 'select',
header: ({ table }) => (
<Checkbox
checked={table.getIsAllPageRowsSelected()}
indeterminate={table.getIsSomePageRowsSelected()}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label='Select all'
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
aria-label='Select row'
/>
),
enableSorting: false,
enableHiding: false,
size: 40,
},
// ID column
{
accessorKey: 'id',
header: t('ID'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const id = row.getValue('id') as number
return <TableId value={id} />
},
size: 64,
},
// Model Name column (with model icon)
{
accessorKey: 'model_name',
header: t('Model Name'),
meta: { mobileTitle: true },
cell: ({ row }) => {
const model = row.original
const name = row.getValue('model_name') as string
const iconKey =
model.icon ||
vendorMap[model.vendor_id || 0]?.icon ||
model.model_name?.[0] ||
'N'
const icon = getCompactModelIcon(iconKey)
return (
<div className='flex max-w-full min-w-0 items-center gap-2'>
<div className='flex size-5 shrink-0 items-center justify-center overflow-hidden'>
{icon}
</div>
<StatusBadge
label={name}
variant='neutral'
copyText={name}
size='sm'
className='-ml-1.5 font-mono'
/>
</div>
)
},
size: 260,
minSize: 200,
},
// Name Rule column
{
accessorKey: 'name_rule',
header: t('Match Type'),
cell: ({ row }) => {
const rule = row.getValue('name_rule') as 0 | 1 | 2 | 3
const model = row.original
const config = NAME_RULE_CONFIG[rule]
let label = config.label
if (rule !== 0 && model.matched_count) {
label = `${config.label} (${model.matched_count})`
}
const badge = (
<StatusBadge
variant={
(config.color === 'error' ? 'danger' : config.color) as
| 'neutral'
| 'success'
| 'warning'
| 'danger'
| 'info'
}
size='sm'
className='-ml-1.5 max-w-none shrink-0'
>
{label}
</StatusBadge>
)
// Show tooltip with matched models for non-exact rules
if (
rule !== 0 &&
model.matched_models &&
model.matched_models.length > 0
) {
const matchedBadges = model.matched_models.map((m) => (
<StatusBadge key={m} label={m} autoColor={m} size='sm' />
))
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger
render={<div className='inline-flex max-w-full min-w-0' />}
>
{badge}
</TooltipTrigger>
<TooltipContent
side='top'
className='border-border bg-popover max-h-48 max-w-[320px] overflow-y-auto p-2'
>
<div className='flex flex-wrap gap-1'>{matchedBadges}</div>
</TooltipContent>
</Tooltip>
</TooltipProvider>
)
}
return badge
},
size: 100,
enableSorting: false,
},
// Status column
{
accessorKey: 'status',
header: t('Status'),
meta: { mobileBadge: true },
cell: ({ row }) => {
const status = row.getValue('status') as number
const config =
MODEL_STATUS_CONFIG[status as 0 | 1] || MODEL_STATUS_CONFIG[0]
return (
<StatusBadge
variant={config.variant}
size='sm'
copyable={false}
className='-ml-1.5 max-w-none shrink-0'
>
{config.label}
</StatusBadge>
)
},
filterFn: (row, id, value) => {
if (!value || value.length === 0 || value.includes('all')) return true
const status = row.getValue(id) as number
if (value.includes('enabled')) return status === 1
if (value.includes('disabled')) return status !== 1
return false
},
size: 110,
minSize: 110,
enableSorting: false,
},
// Vendor column
{
accessorKey: 'vendor_id',
header: t('Vendor'),
cell: ({ row }) => {
const vendorId = row.getValue('vendor_id') as number
const vendor = vendorMap[vendorId]
if (!vendor) {
return <span className='text-muted-foreground text-xs'>-</span>
}
return (
<BadgeCell>
<ProviderBadge iconKey={vendor.icon} label={vendor.name} />
</BadgeCell>
)
},
filterFn: (row, id, value) => {
if (!value || value.length === 0 || value.includes('all')) return true
return value.includes(String(row.getValue(id)))
},
size: 130,
enableSorting: false,
},
// Description column
{
accessorKey: 'description',
header: t('Description'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const description = row.getValue('description') as string
const modelName = row.getValue('model_name') as string
return (
<DescriptionCell modelName={modelName} description={description} />
)
},
size: 150,
enableSorting: false,
},
// Tags column
{
accessorKey: 'tags',
header: t('Tags'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const tags = row.getValue('tags') as string
const tagArray = parseModelTags(tags)
return (
<BadgeListCell
items={tagArray.map((tag) => (
<StatusBadge key={tag} label={tag} autoColor={tag} size='sm' />
))}
/>
)
},
size: 100,
enableSorting: false,
},
// Endpoints column
{
accessorKey: 'endpoints',
header: t('Endpoints'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const endpoints = row.getValue('endpoints') as string
const endpointArray = formatEndpointsDisplay(endpoints)
return (
<BadgeListCell
max={3}
items={endpointArray.map((ep) => (
<StatusBadge key={ep} label={ep} autoColor={ep} size='sm' />
))}
/>
)
},
size: 200,
enableSorting: false,
},
// Bound Channels column
{
accessorKey: 'bound_channels',
header: t('Bound Channels'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const channels = row.getValue('bound_channels') as Array<{
id: number
name: string
type?: number
status?: number
}>
return (
<BadgeListCell
items={(channels ?? []).map((c) => (
<StatusBadge
key={c.id}
label={`${c.name} (${c.type})`}
autoColor={c.name}
size='sm'
/>
))}
/>
)
},
size: 150,
enableSorting: false,
},
// Enable Groups column
{
accessorKey: 'enable_groups',
header: t('Enable Groups'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const groups = row.getValue('enable_groups') as string[]
return (
<BadgeListCell
max={3}
items={(groups ?? []).map((g) => (
<GroupBadge key={g} group={g} size='sm' />
))}
/>
)
},
size: 200,
enableSorting: false,
},
// Quota Types column
{
accessorKey: 'quota_types',
header: t('Quota Types'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const quotaTypes = row.getValue('quota_types') as number[]
return (
<BadgeListCell
items={(quotaTypes ?? []).map((qt) => {
const config = QUOTA_TYPE_CONFIG[qt]
return (
<StatusBadge
key={qt}
label={config?.label || String(qt)}
variant={
(config?.color === 'error' ? 'danger' : config?.color) as
| 'neutral'
| 'success'
| 'warning'
| 'danger'
| 'info'
}
size='sm'
/>
)
})}
/>
)
},
size: 150,
enableSorting: false,
},
// Sync Official column
{
accessorKey: 'sync_official',
header: t('Official Sync'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const syncOfficial = row.getValue('sync_official') as number
return (
<StatusBadge
variant={syncOfficial === 1 ? 'success' : 'warning'}
size='sm'
copyable={false}
className='-ml-1.5 max-w-none shrink-0'
>
{syncOfficial === 1 ? t('Official Sync') : t('No Sync')}
</StatusBadge>
)
},
filterFn: (row, id, value) => {
if (!value || value.length === 0 || value.includes('all')) return true
const syncOfficial = row.getValue(id) as number
if (value.includes('yes')) return syncOfficial === 1
if (value.includes('no')) return syncOfficial !== 1
return false
},
size: 100,
enableSorting: false,
},
// Created Time column
{
accessorKey: 'created_time',
header: t('Created'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const timestamp = row.getValue('created_time') as number
return (
<div className='font-mono text-sm whitespace-nowrap'>
{formatTimestampToDate(timestamp)}
</div>
)
},
size: 140,
},
// Updated Time column
{
accessorKey: 'updated_time',
header: t('Updated'),
meta: { mobileHidden: true },
cell: ({ row }) => {
const timestamp = row.getValue('updated_time') as number
return (
<div className='font-mono text-sm whitespace-nowrap'>
{formatTimestampToDate(timestamp)}
</div>
)
},
size: 140,
},
// Actions column
{
id: 'actions',
header: () => t('Actions'),
cell: ({ row }) => {
return <DataTableRowActions row={row} />
},
enableSorting: false,
enableHiding: false,
meta: { pinned: 'right' as const },
},
]
}