forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment-access-guard.tsx
More file actions
203 lines (186 loc) · 6.1 KB
/
Copy pathdeployment-access-guard.tsx
File metadata and controls
203 lines (186 loc) · 6.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
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
/*
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 { useNavigate } from '@tanstack/react-router'
import {
AlertCircle,
CheckCircle2,
Circle,
Loader2,
Server,
Settings,
WifiOff,
} from 'lucide-react'
import type { ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
type LoadingPhase = 'idle' | 'settings' | 'connection' | 'done'
type StepStatus = 'pending' | 'loading' | 'done'
function getSettingsStatus(phase: LoadingPhase): StepStatus {
if (phase === 'settings') return 'loading'
if (phase === 'connection' || phase === 'done') return 'done'
return 'pending'
}
function getConnectionStatus(
phase: LoadingPhase,
connectionOk: boolean | null
): StepStatus {
if (phase === 'connection') return 'loading'
if (phase === 'done' && connectionOk) return 'done'
return 'pending'
}
interface DeploymentAccessGuardProps {
children: ReactNode
loading: boolean
loadingPhase?: LoadingPhase
isEnabled: boolean
connectionLoading: boolean
connectionOk: boolean | null
connectionError: string | null
onRetry: () => void
}
function LoadingStep({
label,
status,
}: {
label: string
status: 'pending' | 'loading' | 'done'
}) {
return (
<div className='flex items-center gap-3'>
{status === 'loading' && (
<Loader2 className='text-primary h-5 w-5 animate-spin' />
)}
{status === 'done' && <CheckCircle2 className='h-5 w-5 text-green-500' />}
{status === 'pending' && (
<Circle className='text-muted-foreground/40 h-5 w-5' />
)}
<span
className={cn(
'text-sm',
status === 'loading' && 'text-foreground font-medium',
status === 'done' && 'text-muted-foreground',
status === 'pending' && 'text-muted-foreground/60'
)}
>
{label}
</span>
</div>
)
}
export function DeploymentAccessGuard({
children,
loading,
loadingPhase = 'settings',
isEnabled,
connectionLoading,
connectionOk,
connectionError,
onRetry,
}: DeploymentAccessGuardProps) {
const { t } = useTranslation()
const navigate = useNavigate()
const handleGoToSettings = () => {
navigate({
to: '/system-settings/models/$section',
params: { section: 'model-deployment' },
})
}
// Combined loading state with step indicator
if (loading || connectionLoading) {
const settingsStatus = getSettingsStatus(loadingPhase)
const connectionStatus = getConnectionStatus(loadingPhase, connectionOk)
return (
<div className='mx-auto mt-8 max-w-md'>
<div className='flex flex-col items-center justify-center py-12'>
<Loader2 className='text-primary mb-6 h-10 w-10 animate-spin' />
<div className='space-y-3'>
<LoadingStep
label={t('Loading configuration')}
status={settingsStatus}
/>
<LoadingStep
label={t('Checking connection')}
status={connectionStatus}
/>
</div>
</div>
</div>
)
}
// Disabled state
if (!isEnabled) {
return (
<div className='mx-auto mt-8 max-w-md'>
<div className='text-center'>
<div className='mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-2xl bg-amber-100 dark:bg-amber-900/20'>
<Server className='h-8 w-8 text-amber-600 dark:text-amber-400' />
</div>
<h3 className='mb-6 text-xl font-semibold'>
{t('Model deployment service is disabled')}
</h3>
</div>
<div className='space-y-4'>
<Alert variant='default'>
<AlertCircle className='h-4 w-4' />
<AlertTitle>{t('Configuration required')}</AlertTitle>
<AlertDescription>
{t(
'Please enable io.net model deployment service and configure an API key in System Settings.'
)}
</AlertDescription>
</Alert>
<Button onClick={handleGoToSettings} className='w-full'>
<Settings className='mr-2 h-4 w-4' />
{t('Go to settings')}
</Button>
</div>
</div>
)
}
// Connection error state
if (connectionOk === false && connectionError) {
return (
<div className='mx-auto mt-8 max-w-md'>
<div className='text-center'>
<div className='mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-2xl bg-red-100 dark:bg-red-900/20'>
<WifiOff className='h-8 w-8 text-red-600 dark:text-red-400' />
</div>
<h3 className='mb-6 text-xl font-semibold'>
{t('Connection failed')}
</h3>
</div>
<div className='space-y-4'>
<Alert variant='destructive'>
<AlertCircle className='h-4 w-4' />
<AlertTitle>{t('Connection error')}</AlertTitle>
<AlertDescription>{t(connectionError)}</AlertDescription>
</Alert>
<div className='flex gap-2'>
<Button variant='outline' onClick={onRetry} className='flex-1'>
{t('Retry')}
</Button>
<Button onClick={handleGoToSettings} className='flex-1'>
<Settings className='mr-2 h-4 w-4' />
{t('Go to settings')}
</Button>
</div>
</div>
</div>
)
}
return <>{children}</>
}