"use client"; import { createApiClient } from "@app/lib/api"; import { Avatar, AvatarFallback } from "@app/components/ui/avatar"; import { Button } from "@app/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { toast } from "@app/hooks/useToast"; import { formatAxiosError } from "@app/lib/api"; import { Laptop, LogOut, Moon, Sun } from "lucide-react"; import { useTheme } from "next-themes"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { useUserContext } from "@app/hooks/useUserContext"; import Disable2FaForm from "./Disable2FaForm"; import SecurityKeyForm from "./SecurityKeyForm"; import Enable2FaDialog from "./Enable2FaDialog"; import SupporterStatus from "./SupporterStatus"; import { UserType } from "@server/types/UserTypes"; import LocaleSwitcher from '@app/components/LocaleSwitcher'; import { useTranslations } from "next-intl"; export default function ProfileIcon() { const { setTheme, theme } = useTheme(); const { env } = useEnvContext(); const api = createApiClient({ env }); const { user, updateUser } = useUserContext(); const router = useRouter(); const [userTheme, setUserTheme] = useState<"light" | "dark" | "system">( theme as "light" | "dark" | "system" ); const [openEnable2fa, setOpenEnable2fa] = useState(false); const [openDisable2fa, setOpenDisable2fa] = useState(false); const [openSecurityKey, setOpenSecurityKey] = useState(false); const t = useTranslations(); function getInitials() { return (user.name || user.email || user.username) .substring(0, 1) .toUpperCase(); } function handleThemeChange(theme: "light" | "dark" | "system") { setUserTheme(theme); setTheme(theme); } function logout() { api.post("/auth/logout") .catch((e) => { console.error(t('logoutError'), e); toast({ title: t('logoutError'), description: formatAxiosError(e, t('logoutError')) }); }) .then(() => { router.push("/auth/login"); router.refresh(); }); } return ( <>
{user.name || user.email || user.username}

{t('signingAs')}

{user.name || user.email || user.username}

{user.serverAdmin ? (

{t('serverAdmin')}

) : (

{user.idpName || t('idpNameInternal')}

)}
{user?.type === UserType.Internal && ( <> {!user.twoFactorEnabled && ( setOpenEnable2fa(true)} > {t('otpEnable')} )} {user.twoFactorEnabled && ( setOpenDisable2fa(true)} > {t('otpDisable')} )} setOpenSecurityKey(true)} > {t('securityKeyManage')} )} {t("theme")} {(["light", "dark", "system"] as const).map( (themeOption) => ( handleThemeChange(themeOption) } > {themeOption === "light" && ( )} {themeOption === "dark" && ( )} {themeOption === "system" && ( )} {t(themeOption)} {userTheme === themeOption && ( )} ) )} logout()}> {/* */} {t('logout')}
); }