forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-wizard.tsx
More file actions
402 lines (361 loc) · 12.1 KB
/
Copy pathsetup-wizard.tsx
File metadata and controls
402 lines (361 loc) · 12.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
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
/*
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 { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useNavigate } from '@tanstack/react-router'
import { useEffect, useMemo, useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { ErrorState } from '@/components/error-state'
import { LanguageSwitcher } from '@/components/language-switcher'
import { LoadingState } from '@/components/loading-state'
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { Form } from '@/components/ui/form'
import { Skeleton } from '@/components/ui/skeleton'
import { useSystemConfig } from '@/hooks/use-system-config'
import { cn } from '@/lib/utils'
import { buildSetupPayload, getSetupStatus, submitSetup } from './api'
import { AdminStep } from './components/admin-step'
import { CompleteStep } from './components/complete-step'
import { DatabaseStep } from './components/database-step'
import { StepNavigation } from './components/step-navigation'
import { UsageModeStep } from './components/usage-mode-step'
import type { SetupFormValues, SetupStatus } from './types'
const STEPS = [
{
titleKey: 'Database check',
descriptionKey: 'Verify your database connection',
},
{
titleKey: 'Administrator account',
descriptionKey: 'Create credentials for the root user',
},
{
titleKey: 'Usage mode',
descriptionKey: 'Choose how the platform will operate',
},
{
titleKey: 'Review & initialize',
descriptionKey: 'Confirm settings and finish setup',
},
]
const DEFAULT_FORM_VALUES: SetupFormValues = {
username: '',
password: '',
confirmPassword: '',
usageMode: 'external',
}
export function SetupWizard() {
const { t } = useTranslation()
const navigate = useNavigate()
const queryClient = useQueryClient()
const { systemName, logo, loading: systemConfigLoading } = useSystemConfig()
const [currentStep, setCurrentStep] = useState(0)
const [setupStatus, setSetupStatus] = useState<SetupStatus | undefined>()
const form = useForm<SetupFormValues>({
defaultValues: DEFAULT_FORM_VALUES,
mode: 'onBlur',
})
const watchedValues = form.watch()
const {
data: statusResponse,
isLoading,
isError,
refetch,
} = useQuery({
queryKey: ['setup-status'],
queryFn: getSetupStatus,
retry: false,
})
const mutation = useMutation({
mutationKey: ['setup-submit'],
mutationFn: submitSetup,
onSuccess: async (response) => {
if (response.success) {
toast.success(t('System initialized successfully! Redirecting…'))
await queryClient.invalidateQueries({ queryKey: ['setup-status'] })
setTimeout(() => {
navigate({ to: '/' })
}, 1200)
} else {
toast.error(
response.message || t('Initialization failed, please try again.')
)
}
},
onError: () => {
toast.error(t('Failed to initialize system'))
},
})
useEffect(() => {
if (!statusResponse) return
if (!statusResponse.success) {
toast.error(statusResponse.message || t('Failed to load setup status'))
return
}
const status = statusResponse.data
if (!status) return
if (status.status) {
navigate({ to: '/' })
return
}
setSetupStatus(status)
setCurrentStep(0)
// Pre-fill usage mode if backend echoes it
if (status.SelfUseModeEnabled) {
form.setValue('usageMode', 'self', {
shouldDirty: false,
shouldTouch: false,
shouldValidate: false,
})
} else if (status.DemoSiteEnabled) {
form.setValue('usageMode', 'demo', {
shouldDirty: false,
shouldTouch: false,
shouldValidate: false,
})
} else {
form.setValue('usageMode', 'external', {
shouldDirty: false,
shouldTouch: false,
shouldValidate: false,
})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [statusResponse, navigate, form])
useEffect(() => {
if (!setupStatus) return
// Reset admin fields when backend reports they are already initialized
if (setupStatus.root_init) {
form.setValue('username', '', {
shouldDirty: false,
shouldTouch: false,
shouldValidate: false,
})
form.setValue('password', '', {
shouldDirty: false,
shouldTouch: false,
shouldValidate: false,
})
form.setValue('confirmPassword', '', {
shouldDirty: false,
shouldTouch: false,
shouldValidate: false,
})
}
}, [setupStatus, form])
const currentStepComponent = useMemo(() => {
if (currentStep === 0) {
return <DatabaseStep status={setupStatus} />
}
if (currentStep === 1) {
return (
<AdminStep
form={form}
rootInitialized={Boolean(setupStatus?.root_init)}
/>
)
}
if (currentStep === 2) {
return <UsageModeStep form={form} />
}
return <CompleteStep status={setupStatus} values={watchedValues} />
}, [currentStep, setupStatus, form, watchedValues])
const validateAdminStep = () => {
if (setupStatus?.root_init) return true
const username = form.getValues('username')?.trim()
const password = form.getValues('password')?.trim()
const confirmPassword = form.getValues('confirmPassword')?.trim()
if (!username) {
form.setError('username', {
type: 'manual',
message: t('Please enter an administrator username'),
})
toast.error(t('Please enter an administrator username'))
return false
}
if (!password || password.length < 8) {
form.setError('password', {
type: 'manual',
message: t('Password must be at least 8 characters'),
})
toast.error(t('Password must be at least 8 characters'))
return false
}
if (password !== confirmPassword) {
form.setError('confirmPassword', {
type: 'manual',
message: t('Passwords do not match'),
})
toast.error(t('Passwords do not match'))
return false
}
return true
}
const validateUsageModeStep = () => {
const usageMode = form.getValues('usageMode')
if (!usageMode) {
form.setError('usageMode', {
type: 'manual',
message: t('Select a usage mode to continue'),
})
toast.error(t('Select a usage mode to continue'))
return false
}
return true
}
const handleNextStep = () => {
if (currentStep === 1 && !validateAdminStep()) return
if (currentStep === 2 && !validateUsageModeStep()) return
setCurrentStep((step) => Math.min(step + 1, STEPS.length - 1))
}
const handlePreviousStep = () => {
setCurrentStep((step) => Math.max(step - 1, 0))
}
const handleSubmit = async () => {
const adminValid = validateAdminStep()
const usageValid = validateUsageModeStep()
if (!adminValid || !usageValid) return
const payload = buildSetupPayload(
form.getValues(),
Boolean(setupStatus?.root_init)
)
mutation.mutate(payload)
}
return (
<div className='bg-muted/40 relative min-h-svh py-10'>
<div className='absolute top-4 right-4 sm:top-6 sm:right-6'>
<LanguageSwitcher />
</div>
<div className='container mx-auto flex max-w-5xl flex-col gap-8 px-4 sm:px-6'>
<div className='flex flex-col items-center gap-3'>
<div className='relative h-12 w-12'>
{systemConfigLoading ? (
<Skeleton className='absolute inset-0 rounded-full' />
) : (
<img
src={logo}
alt={t('System logo')}
className='h-12 w-12 rounded-full object-cover shadow-sm'
/>
)}
</div>
{systemConfigLoading ? (
<Skeleton className='h-7 w-40' />
) : (
<h1 className='text-2xl font-semibold tracking-tight'>
{t('Initialize')} {systemName}
</h1>
)}
<p className='text-muted-foreground text-center text-sm sm:text-base'>
{t(
'Follow the guided steps to prepare your workspace before the first login.'
)}
</p>
</div>
<Card className='shadow-lg'>
<CardHeader className='space-y-2'>
<CardTitle className='text-xl font-semibold'>
{t('System setup wizard')}
</CardTitle>
<CardDescription>
{t('Complete these steps to finish the initial installation.')}
</CardDescription>
</CardHeader>
<CardContent className='space-y-6'>
<ol className='grid gap-3 sm:grid-cols-4'>
{STEPS.map((step, index) => {
const isActive = currentStep === index
const isCompleted = currentStep > index
return (
<li
key={step.titleKey}
className={cn(
'rounded-xl border p-3',
isActive
? 'border-primary ring-primary/20 ring-2'
: isCompleted
? 'border-primary/40 bg-primary/5'
: 'border-muted bg-card'
)}
>
<div className='flex items-start gap-3'>
<span
className={cn(
'flex size-6 items-center justify-center rounded-md border text-xs font-semibold',
isActive
? 'border-primary bg-primary text-primary-foreground'
: isCompleted
? 'border-primary bg-primary text-primary-foreground'
: 'border-muted-foreground/40 text-muted-foreground'
)}
>
{index + 1}
</span>
<div className='space-y-1'>
<p className='text-sm font-semibold'>
{t(step.titleKey)}
</p>
<p className='text-muted-foreground text-xs'>
{t(step.descriptionKey)}
</p>
</div>
</div>
</li>
)
})}
</ol>
{isLoading ? (
<LoadingState message={t('Loading setup status…')} />
) : isError ? (
<ErrorState
title={t('We could not load the setup status.')}
onRetry={() => refetch()}
/>
) : (
<Form {...form}>
<form
className='space-y-6'
onSubmit={(event) => event.preventDefault()}
>
{currentStepComponent}
</form>
</Form>
)}
</CardContent>
{!isLoading && !isError && (
<CardFooter className='w-full justify-end border-t'>
<StepNavigation
currentStep={currentStep}
totalSteps={STEPS.length}
onBack={handlePreviousStep}
onNext={handleNextStep}
onSubmit={handleSubmit}
isSubmitting={mutation.isPending}
/>
</CardFooter>
)}
</Card>
</div>
</div>
)
}