forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo-fa-card.tsx
More file actions
187 lines (169 loc) 路 6.37 KB
/
Copy pathtwo-fa-card.tsx
File metadata and controls
187 lines (169 loc) 路 6.37 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
/*
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 { Shield, AlertTriangle, RefreshCw } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { StatusBadge } from '@/components/status-badge'
import { Button } from '@/components/ui/button'
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card'
import { IconBadge } from '@/components/ui/icon-badge'
import { Skeleton } from '@/components/ui/skeleton'
import { useDialogs } from '@/hooks/use-dialog'
import { useTwoFA } from '../hooks'
import { TwoFABackupDialog } from './dialogs/two-fa-backup-dialog'
import { TwoFADisableDialog } from './dialogs/two-fa-disable-dialog'
import { TwoFASetupDialog } from './dialogs/two-fa-setup-dialog'
// ============================================================================
// Two-Factor Authentication Card Component
// ============================================================================
interface TwoFACardProps {
loading: boolean
}
type DialogKey = 'setup' | 'disable' | 'backup'
export function TwoFACard({ loading: pageLoading }: TwoFACardProps) {
const { t } = useTranslation()
const { status, loading, refetch } = useTwoFA(!pageLoading)
const dialogs = useDialogs<DialogKey>()
if (pageLoading || loading) {
return (
<Card data-card-hover='false' className='gap-0 overflow-hidden py-0'>
<CardHeader className='p-3 sm:p-5'>
<Skeleton className='h-6 w-48' />
<Skeleton className='mt-2 h-4 w-64' />
</CardHeader>
<CardContent className='p-3 sm:p-5'>
<Skeleton className='h-20 w-full' />
</CardContent>
</Card>
)
}
return (
<>
<Card data-card-hover='false' className='gap-0 overflow-hidden py-0'>
<CardHeader className='p-3 sm:p-5'>
<CardTitle className='text-lg tracking-tight sm:text-xl'>
{t('Two-Factor Authentication')}
</CardTitle>
<CardDescription className='text-xs sm:text-sm'>
{t('Add an extra layer of security to your account')}
</CardDescription>
</CardHeader>
<CardContent className='p-3 sm:p-5'>
<div className='space-y-6'>
{/* Status Section */}
<div className='flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between xl:flex-col 2xl:flex-row'>
<div className='flex items-start gap-4'>
<IconBadge tone='success' size='sm'>
<Shield />
</IconBadge>
<div className='space-y-1'>
<div className='flex items-center gap-2'>
<p className='font-medium'>{t('Two-Step Verification')}</p>
{status.enabled ? (
<StatusBadge
label={t('Enabled')}
variant='success'
showDot
copyable={false}
/>
) : (
<StatusBadge
label={t('Disabled')}
variant='neutral'
showDot
copyable={false}
/>
)}
{status.locked && (
<StatusBadge
label={t('Locked')}
variant='danger'
showDot
copyable={false}
/>
)}
</div>
<p className='text-muted-foreground text-sm'>
{status.enabled
? t('Backup codes remaining: {{count}}', {
count: status.backup_codes_remaining,
})
: t('Add an extra layer of security to your account')}
</p>
</div>
</div>
{!status.enabled && (
<Button
className='w-full sm:w-auto xl:w-full 2xl:w-auto'
onClick={() => dialogs.open('setup')}
>
{t('Enable')}
</Button>
)}
</div>
{/* Actions Section - Only show when enabled */}
{status.enabled && (
<div className='flex flex-col gap-3 border-t pt-6 sm:flex-row xl:flex-col 2xl:flex-row'>
<Button
variant='outline'
className='flex-1'
onClick={() => dialogs.open('backup')}
>
<RefreshCw className='mr-2 h-4 w-4' />
{t('Regenerate Backup Codes')}
</Button>
<Button
variant='destructive'
className='flex-1'
onClick={() => dialogs.open('disable')}
>
<AlertTriangle className='mr-2 h-4 w-4' />
{t('Disable 2FA')}
</Button>
</div>
)}
</div>
</CardContent>
</Card>
{/* Dialogs */}
<TwoFASetupDialog
open={dialogs.isOpen('setup')}
onOpenChange={(open) =>
open ? dialogs.open('setup') : dialogs.close('setup')
}
onSuccess={refetch}
/>
<TwoFADisableDialog
open={dialogs.isOpen('disable')}
onOpenChange={(open) =>
open ? dialogs.open('disable') : dialogs.close('disable')
}
onSuccess={refetch}
/>
<TwoFABackupDialog
open={dialogs.isOpen('backup')}
onOpenChange={(open) =>
open ? dialogs.open('backup') : dialogs.close('backup')
}
onSuccess={refetch}
/>
</>
)
}