import React, { useState, useEffect } from "react"; 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 { Separator } from "@/components/ui/separator.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 { PasswordReset } from "@/ui/Desktop/User/PasswordReset.tsx"; import { useTranslation } from "react-i18next"; import { LanguageSwitcher } from "@/ui/Desktop/User/LanguageSwitcher.tsx"; import { useSidebar } from "@/components/ui/sidebar.tsx"; interface UserProfileProps { isTopbarOpen?: boolean; } export function UserProfile({ isTopbarOpen = true }: UserProfileProps) { const { t } = useTranslation(); const { state: sidebarState } = useSidebar(); 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) { const { toast } = await import("sonner"); toast.error(t("user.failedToLoadVersionInfo")); } }; 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 }); } }; const topMarginPx = isTopbarOpen ? 74 : 26; const leftMarginPx = sidebarState === "collapsed" ? 26 : 8; const bottomMarginPx = 8; const wrapperStyle: React.CSSProperties = { marginLeft: leftMarginPx, marginRight: 17, marginTop: topMarginPx, marginBottom: bottomMarginPx, height: `calc(100vh - ${topMarginPx + bottomMarginPx}px)`, }; if (loading) { return (

{t("nav.userProfile")}

{t("common.loading")}
); } if (error || !userInfo) { return (

{t("nav.userProfile")}

{t("common.error")} {error || t("errors.loadFailed")}
); } return (

{t("nav.userProfile")}

{t("nav.userProfile")} {!userInfo.is_oidc && ( {t("profile.security")} )}

{t("profile.accountInfo")}

{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 && }
); }