import * as React from 'react'; import { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { useTranslation } from 'react-i18next'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Item, ItemMedia, ItemContent, ItemTitle, ItemDescription, ItemActions, } from '@/components/ui/item'; import { httpClient } from '@/app/infra/http/HttpClient'; import { systemInfo } from '@/app/infra/http'; import { Loader2, ExternalLink, KeyRound } from 'lucide-react'; import PasswordChangeDialog from '../password-change-dialog/PasswordChangeDialog'; interface AccountSettingsDialogProps { open: boolean; onOpenChange: (open: boolean) => void; } export default function AccountSettingsDialog({ open, onOpenChange, }: AccountSettingsDialogProps) { const { t } = useTranslation(); const [accountType, setAccountType] = useState<'local' | 'space'>('local'); const [hasPassword, setHasPassword] = useState(false); const [userEmail, setUserEmail] = useState(''); const [loading, setLoading] = useState(true); const [spaceBindLoading, setSpaceBindLoading] = useState(false); const [passwordDialogOpen, setPasswordDialogOpen] = useState(false); useEffect(() => { if (open) { loadUserInfo(); } }, [open]); async function loadUserInfo() { setLoading(true); try { const info = await httpClient.getUserInfo(); setAccountType(info.account_type); setHasPassword(info.has_password); setUserEmail(info.user); } catch { toast.error(t('common.error')); } finally { setLoading(false); } } const handleBindSpace = async () => { setSpaceBindLoading(true); try { const token = localStorage.getItem('token'); if (!token) { toast.error(t('common.error')); setSpaceBindLoading(false); return; } const currentOrigin = window.location.origin; const redirectUri = `${currentOrigin}/auth/space/callback?mode=bind`; // Pass token as state for security verification const response = await httpClient.getSpaceAuthorizeUrl( redirectUri, token, ); window.location.href = response.authorize_url; } catch { toast.error(t('common.spaceLoginFailed')); setSpaceBindLoading(false); } }; const handlePasswordDialogClose = (dialogOpen: boolean) => { setPasswordDialogOpen(dialogOpen); if (!dialogOpen) { // Reload user info to update password status loadUserInfo(); } }; return ( <> {t('account.settings')} {userEmail} {loading ? (
) : (
{/* Password Item */} {t('account.passwordStatus')} {hasPassword ? t('account.passwordSetDescription') : t('account.setPasswordHint')} {/* Space Account Item */} {t('account.spaceStatus')} {accountType === 'space' ? t('account.spaceBoundDescription') : t('account.bindSpaceDescription')} {accountType === 'local' && ( )}
)}
); }