import React, {useState, useEffect} from "react"; import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card.tsx"; import {Button} from "@/components/ui/button.tsx"; import {Input} from "@/components/ui/input.tsx"; import {Label} from "@/components/ui/label.tsx"; import {Alert, AlertDescription, AlertTitle} from "@/components/ui/alert.tsx"; import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs.tsx"; import {User, Shield, Key, AlertCircle} from "lucide-react"; import {TOTPSetup} from "@/ui/Desktop/User/TOTPSetup.tsx"; import {getUserInfo} from "@/ui/main-axios.ts"; import {getVersionInfo} from "@/ui/main-axios.ts"; import {toast} from "sonner"; import {PasswordReset} from "@/ui/Desktop/User/PasswordReset.tsx"; import {useTranslation} from "react-i18next"; import {LanguageSwitcher} from "@/components/LanguageSwitcher.tsx"; interface UserProfileProps { isTopbarOpen?: boolean; } export function UserProfile({isTopbarOpen = true}: UserProfileProps) { const {t} = useTranslation(); const [userInfo, setUserInfo] = useState<{ username: string; is_admin: boolean; is_oidc: boolean; totp_enabled: boolean; } | null>(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [versionInfo, setVersionInfo] = useState<{ version: string } | null>(null); useEffect(() => { fetchUserInfo(); fetchVersion(); }, []); const fetchVersion = async () => { try { const info = await getVersionInfo(); setVersionInfo({version: info.localVersion}); } catch (err) { console.error("Failed to load version info", err); } }; const fetchUserInfo = async () => { setLoading(true); setError(null); try { const info = await getUserInfo(); setUserInfo({ username: info.username, is_admin: info.is_admin, is_oidc: info.is_oidc, totp_enabled: info.totp_enabled || false }); } catch (err: any) { setError(err?.response?.data?.error || t('errors.loadFailed')); } finally { setLoading(false); } }; const handleTOTPStatusChange = (enabled: boolean) => { if (userInfo) { setUserInfo({...userInfo, totp_enabled: enabled}); } }; if (loading) { return (
{t('common.loading')}
); } if (error || !userInfo) { return (
{t('common.error')} {error || t('errors.loadFailed')}
); } return (

{t('common.profile')}

{t('profile.description')}

{t('common.profile')} {!userInfo.is_oidc && ( {t('profile.security')} )} {t('profile.accountInfo')} {t('profile.description')}

{userInfo.username}

{userInfo.is_admin ? t('interface.administrator') : t('interface.user')}

{userInfo.is_oidc ? t('profile.external') : t('profile.local')}

{userInfo.is_oidc ? ( {t('auth.lockedOidcAuth')} ) : ( userInfo.totp_enabled ? ( {t('common.enabled')} ) : ( {t('common.disabled')} ) )}

{versionInfo?.version || t('common.loading')}

{t('profile.selectPreferredLanguage')}

{!userInfo.is_oidc && ( )}
); }