import { useState } from "react"; import { useLocation } from "wouter"; import { useTranslation } from "react-i18next"; import { useAuth } from "@/hooks/use-auth"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { PasswordInput } from "@/components/password-input"; import { GuideHelp } from "@/components/guide-help"; import { useToast } from "@/hooks/use-toast"; import { useChangeMyPassword, useGetPasswordRedirect, getGetPasswordRedirectQueryKey } from "@workspace/api-client-react"; import { useQueryClient } from "@tanstack/react-query"; import { Bookmark, LogIn, LogOut, Trash2, KeyRound } from "lucide-react"; import { cn } from "@/lib/utils"; function initials(name?: string | null): string { if (!name) return "?"; const parts = name.trim().split(/\s+/).filter(Boolean); if (parts.length === 0) return "?"; return parts .slice(0, 2) .map((p) => p[0]?.toUpperCase() ?? "") .join(""); } export function UserMenu() { const [location, navigate] = useLocation(); const { t } = useTranslation(); const { user, isLoading, isAuthenticated, isAdmin, tier, hasFeature, login, logout } = useAuth(); const [open, setOpen] = useState(false); const [pwOpen, setPwOpen] = useState(false); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const { toast } = useToast(); const queryClient = useQueryClient(); const changePassword = useChangeMyPassword(); const { data: passwordRedirect } = useGetPasswordRedirect({ query: { queryKey: getGetPasswordRedirectQueryKey(), enabled: isAuthenticated && !!user && !user.isLocal }, }); const showWatchlist = hasFeature("watchlist"); const showTrash = hasFeature("trash"); const displayName = user?.name || user?.preferredUsername || "User"; if (isLoading) { return (
); } if (!isAuthenticated || !user) { return ( ); } function go(path: string) { setOpen(false); navigate(path); } return ( <> {displayName} {user.email && {user.email}} {isAdmin ? "admin" : tier} {(showWatchlist || showTrash) && ( <> {showWatchlist && ( go("/watchlist")}> {t("nav.watchlist")} )} {showTrash && ( go("/trash")}> {t("nav.trash")} )} )} { e.preventDefault(); setPwOpen(true); }} data-testid="button-change-password" > {t("auth.changePassword")} {t("auth.signOut")} {t("auth.changePassword")} {!user?.isLocal ? (

{t("auth.oidcPasswordHint")}

{passwordRedirect?.url && ( )}
) : (
setCurrentPassword(e.target.value)} data-testid="input-current-password" />
setNewPassword(e.target.value)} placeholder={t("auth.passwordMinLength")} data-testid="input-new-password" />
setConfirmPassword(e.target.value)} data-testid="input-confirm-password" />
)} {user?.isLocal && ( )}
); }