forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
176 lines (157 loc) · 5.63 KB
/
Copy pathindex.tsx
File metadata and controls
176 lines (157 loc) · 5.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
/*
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 { useQueryClient } from '@tanstack/react-query'
import { getRouteApi, useNavigate } from '@tanstack/react-router'
import { Plus } from 'lucide-react'
import { useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { SectionPageLayout } from '@/components/layout'
import { Button } from '@/components/ui/button'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { listDeployments } from './api'
import { DeploymentAccessGuard } from './components/deployment-access-guard'
import { DeploymentsTable } from './components/deployments-table'
import { CreateDeploymentDrawer } from './components/dialogs/create-deployment-drawer'
import { ModelsDialogs } from './components/models-dialogs'
import { ModelsPrimaryButtons } from './components/models-primary-buttons'
import { ModelsProvider, useModels } from './components/models-provider'
import { ModelsTable } from './components/models-table'
import { useModelDeploymentSettings } from './hooks/use-model-deployment-settings'
import { deploymentsQueryKeys } from './lib'
import {
type ModelsSectionId,
MODELS_DEFAULT_SECTION,
MODELS_SECTION_IDS,
} from './section-registry'
const route = getRouteApi('/_authenticated/models/$section')
const SECTION_META: Record<ModelsSectionId, { titleKey: string }> = {
metadata: {
titleKey: 'Metadata',
},
deployments: {
titleKey: 'Deployments',
},
}
function ModelsContent() {
const { t } = useTranslation()
const navigate = useNavigate()
const { tabCategory, setTabCategory } = useModels()
const params = route.useParams()
const activeSection = (params.section ??
MODELS_DEFAULT_SECTION) as ModelsSectionId
// Deployment create dialog state
const [createDeploymentOpen, setCreateDeploymentOpen] = useState(false)
// keep context state in sync (for components that rely on it)
useEffect(() => {
if (tabCategory !== activeSection) {
setTabCategory(activeSection)
}
}, [activeSection, setTabCategory, tabCategory])
const handleSectionChange = useCallback(
(section: string) => {
void navigate({
to: '/models/$section',
params: { section: section as ModelsSectionId },
})
},
[navigate]
)
const meta = SECTION_META[activeSection] ?? SECTION_META.metadata
return (
<>
<SectionPageLayout fixedContent>
<SectionPageLayout.Title>{t(meta.titleKey)}</SectionPageLayout.Title>
<SectionPageLayout.Actions>
{activeSection === 'metadata' ? (
<ModelsPrimaryButtons />
) : (
<Button onClick={() => setCreateDeploymentOpen(true)} size='sm'>
<Plus className='h-4 w-4' />
{t('Create deployment')}
</Button>
)}
</SectionPageLayout.Actions>
<SectionPageLayout.Content>
<div className='flex h-full min-h-0 flex-col gap-4'>
<Tabs value={activeSection} onValueChange={handleSectionChange}>
<TabsList className='max-w-full flex-wrap justify-start group-data-horizontal/tabs:h-auto'>
{MODELS_SECTION_IDS.map((section) => (
<TabsTrigger key={section} value={section}>
{t(SECTION_META[section].titleKey)}
</TabsTrigger>
))}
</TabsList>
</Tabs>
<div className='min-h-0 flex-1'>
{activeSection === 'metadata' ? (
<ModelsTable />
) : (
<DeploymentsSection />
)}
</div>
</div>
</SectionPageLayout.Content>
</SectionPageLayout>
<ModelsDialogs />
<CreateDeploymentDrawer
open={createDeploymentOpen}
onOpenChange={setCreateDeploymentOpen}
/>
</>
)
}
function DeploymentsSection() {
const queryClient = useQueryClient()
const {
loading: deploymentLoading,
loadingPhase,
isIoNetEnabled,
connectionLoading,
connectionOk,
connectionError,
testConnection,
} = useModelDeploymentSettings()
// Prefetch deployments list while connection check is in progress.
useEffect(() => {
if (isIoNetEnabled && loadingPhase === 'connection') {
const defaultParams = { p: 1, page_size: 10 }
queryClient.prefetchQuery({
queryKey: deploymentsQueryKeys.list(defaultParams),
queryFn: () => listDeployments(defaultParams),
staleTime: 30 * 1000,
})
}
}, [isIoNetEnabled, loadingPhase, queryClient])
return (
<DeploymentAccessGuard
loading={deploymentLoading}
loadingPhase={loadingPhase}
isEnabled={isIoNetEnabled}
connectionLoading={connectionLoading}
connectionOk={connectionOk}
connectionError={connectionError}
onRetry={testConnection}
>
<DeploymentsTable />
</DeploymentAccessGuard>
)
}
export function Models() {
return (
<ModelsProvider>
<ModelsContent />
</ModelsProvider>
)
}