forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels-table.tsx
More file actions
229 lines (207 loc) · 6.63 KB
/
Copy pathmodels-table.tsx
File metadata and controls
229 lines (207 loc) · 6.63 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
/*
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 { useQuery } from '@tanstack/react-query'
import { getRouteApi } from '@tanstack/react-router'
import { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { DataTablePage, useDataTable } from '@/components/data-table'
import { useMediaQuery } from '@/hooks'
import { useTableUrlState } from '@/hooks/use-table-url-state'
import { getModels, searchModels, getVendors } from '../api'
import {
DEFAULT_PAGE_SIZE,
getModelStatusOptions,
getSyncStatusOptions,
} from '../constants'
import { modelsQueryKeys, vendorsQueryKeys } from '../lib'
import { DataTableBulkActions } from './data-table-bulk-actions'
import { useModelsColumns } from './models-columns'
import { useModels } from './models-provider'
const route = getRouteApi('/_authenticated/models/$section')
export function ModelsTable() {
const { t } = useTranslation()
const { selectedVendor } = useModels()
const isMobile = useMediaQuery('(max-width: 640px)')
// URL state management
const {
globalFilter,
onGlobalFilterChange,
columnFilters,
onColumnFiltersChange,
pagination,
onPaginationChange,
ensurePageInRange,
} = useTableUrlState({
search: route.useSearch(),
navigate: route.useNavigate(),
pagination: {
defaultPage: 1,
defaultPageSize: isMobile ? 10 : DEFAULT_PAGE_SIZE,
},
globalFilter: { enabled: true, key: 'filter' },
columnFilters: [
{ columnId: 'status', searchKey: 'status', type: 'array' },
{ columnId: 'vendor_id', searchKey: 'vendor', type: 'array' },
{ columnId: 'sync_official', searchKey: 'sync', type: 'array' },
],
})
// Extract filters from column filters
const statusFilter =
(columnFilters.find((f) => f.id === 'status')?.value as string[]) || []
const vendorFilter =
(columnFilters.find((f) => f.id === 'vendor_id')?.value as string[]) || []
const syncFilter =
(columnFilters.find((f) => f.id === 'sync_official')?.value as string[]) ||
[]
// Fetch vendors for filter
const { data: vendorsData } = useQuery({
queryKey: vendorsQueryKeys.list(),
queryFn: () => getVendors({ page_size: 1000 }),
})
const vendors = useMemo(
() => vendorsData?.data?.items || [],
[vendorsData?.data?.items]
)
const vendorOptions = useMemo(() => {
return vendors.map((v) => ({
label: v.name,
value: String(v.id),
}))
}, [vendors])
// Apply selected vendor from context or filter
const activeVendorFilter =
selectedVendor ||
(vendorFilter.length > 0 && !vendorFilter.includes('all')
? vendorFilter[0]
: undefined)
const statusFilterValue =
statusFilter.length > 0 && !statusFilter.includes('all')
? statusFilter[0]
: undefined
const syncFilterValue =
syncFilter.length > 0 && !syncFilter.includes('all')
? syncFilter[0]
: undefined
// Use search API whenever any filter is active so status/sync are applied server-side
const shouldSearch = Boolean(
globalFilter?.trim() ||
activeVendorFilter ||
statusFilterValue ||
syncFilterValue
)
// Fetch models data
// eslint-disable-next-line @tanstack/query/exhaustive-deps
const { data, isLoading, isFetching } = useQuery({
queryKey: modelsQueryKeys.list({
keyword: globalFilter,
vendor: activeVendorFilter,
status: statusFilterValue,
sync_official: syncFilterValue,
p: pagination.pageIndex + 1,
page_size: pagination.pageSize,
}),
queryFn: async () => {
if (shouldSearch) {
return searchModels({
keyword: globalFilter,
vendor: activeVendorFilter,
status: statusFilterValue,
sync_official: syncFilterValue,
p: pagination.pageIndex + 1,
page_size: pagination.pageSize,
})
}
return getModels({
p: pagination.pageIndex + 1,
page_size: pagination.pageSize,
})
},
})
const models = data?.data?.items || []
const totalCount = data?.data?.total || 0
const vendorCounts = data?.data?.vendor_counts
// Columns configuration
const columns = useModelsColumns(vendors)
// React Table instance
const { table } = useDataTable({
data: models,
columns,
totalCount,
initialColumnVisibility: {
description: false,
bound_channels: false,
quota_types: false,
},
columnFilters,
pagination,
globalFilter,
enableRowSelection: true,
onColumnFiltersChange,
onPaginationChange,
onGlobalFilterChange,
manualPagination: true,
manualFiltering: true,
ensurePageInRange,
})
// Prepare filter options
const vendorFilterOptions = [
{
label: `${t('All Vendors')}${vendorCounts?.all ? ` (${vendorCounts.all})` : ''}`,
value: 'all',
},
...vendorOptions.map((option) => ({
label: `${option.label}${vendorCounts?.[option.value] ? ` (${vendorCounts[option.value]})` : ''}`,
value: option.value,
})),
]
return (
<DataTablePage
table={table}
columns={columns}
isLoading={isLoading}
isFetching={isFetching}
emptyTitle={t('No Models Found')}
emptyDescription={t(
'No models available. Create your first model to get started.'
)}
skeletonKeyPrefix='model-skeleton'
applyHeaderSize
toolbarProps={{
searchPlaceholder: t('Filter by model name...'),
filters: [
{
columnId: 'status',
title: t('Status'),
options: [...getModelStatusOptions(t)],
singleSelect: true,
},
{
columnId: 'vendor_id',
title: t('Vendor'),
options: vendorFilterOptions,
singleSelect: true,
},
{
columnId: 'sync_official',
title: t('Official Sync'),
options: [...getSyncStatusOptions(t)],
singleSelect: true,
},
],
}}
bulkActions={<DataTableBulkActions table={table} />}
/>
)
}