import { ArchiveIcon, BellIcon, ChevronRightIcon, GlobeIcon, InfoIcon, LogOutIcon, type LucideIcon, PaletteIcon, SettingsIcon, } from "lucide-react"; import { useState } from "react"; import { matchPath, useLocation } from "react-router-dom"; import UserAvatar from "@/components/UserAvatar"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { useAppSidebar } from "@/contexts/AppSidebarContext"; import { useAuth } from "@/contexts/AuthContext"; import useCurrentUser from "@/hooks/useCurrentUser"; import { useSSEConnectionStatus } from "@/hooks/useLiveMemoRefresh"; import useNavigateTo from "@/hooks/useNavigateTo"; import { useNotifications, useUpdateUserGeneralSetting } from "@/hooks/useUserQueries"; import { locales } from "@/i18n"; import { cn } from "@/lib/utils"; import { Routes } from "@/router"; import { UserNotification_Status } from "@/types/proto/api/user_service_pb"; import { buildAuthRoute } from "@/utils/auth-redirect"; import { getLocaleDisplayName, getLocaleWithFallback, loadLocale, useTranslate } from "@/utils/i18n"; import { getThemeWithFallback, loadTheme, THEME_OPTIONS } from "@/utils/theme"; // One row grammar for the whole scope panel: 28px rows, 13px text, and a 20px leading slot // (a 14px glyph with 3px each side) so icons, the avatar and Space marks share one column. const rowClass = "h-7 w-full justify-start gap-1.5 rounded-sm px-2 text-ui font-normal shadow-none hover:bg-accent"; const iconClass = "mx-[3px] size-3.5 shrink-0 text-muted-foreground"; const chevronClass = "size-3.5 shrink-0 text-muted-foreground"; interface PreferenceSubmenuProps { icon: LucideIcon; label: string; value: string; options: Array<{ value: string; label: string }>; onChange: (value: string) => void; /** Long lists scroll; typing jumps to a match. */ className?: string; } /** A row that opens its choices beside the panel, like a submenu, and applies one on select. */ const PreferenceSubmenu = ({ icon: Icon, label, value, options, onChange, className }: PreferenceSubmenuProps) => ( }> {label} {options.find((option) => option.value === value)?.label ?? value} {/* Like a nested submenu: when neither side has room (narrow screens), drop below the row instead of overflowing. */} {options.map((option) => ( {option.label} ))} ); const LOCALE_OPTIONS = locales.map((locale) => ({ value: locale, label: getLocaleDisplayName(locale) })); /** Theme and language apply at once: saved to the account when signed in, otherwise kept in this browser. */ const UserPreferenceMenu = () => { const t = useTranslate(); const currentUser = useCurrentUser(); const { userGeneralSetting, refetchSettings } = useAuth(); const { mutate: updateUserGeneralSetting } = useUpdateUserGeneralSetting(currentUser?.name); // The chosen value shows immediately, before a signed-in save round-trips. const [theme, setTheme] = useState(); const [locale, setLocale] = useState(); const currentTheme = theme ?? getThemeWithFallback(userGeneralSetting?.theme); const currentLocale = locale ?? getLocaleWithFallback(userGeneralSetting?.locale); const handleThemeChange = (value: string) => { setTheme(value); loadTheme(value); if (currentUser) { updateUserGeneralSetting({ generalSetting: { theme: value }, updateMask: ["theme"] }, { onSuccess: () => refetchSettings() }); } }; const handleLocaleChange = (value: string) => { setLocale(loadLocale(value)); if (currentUser) { updateUserGeneralSetting({ generalSetting: { locale: value }, updateMask: ["locale"] }, { onSuccess: () => refetchSettings() }); } }; return (
); }; /** The account half of the scope menu: preferences for everyone, then the user's own pages. */ const UserMenu = ({ onClose }: { onClose: () => void }) => { const t = useTranslate(); const location = useLocation(); const navigateTo = useNavigateTo(); const { setMobileOpen } = useAppSidebar(); const currentUser = useCurrentUser(); const sseStatus = useSSEConnectionStatus(); const { logout } = useAuth(); const { data: notifications = [] } = useNotifications(); const unreadCount = notifications.filter((notification) => notification.status === UserNotification_Status.UNREAD).length; const inboxLabel = unreadCount > 0 ? `${t("common.inbox")}, ${unreadCount} ${t("inbox.unread")}` : t("common.inbox"); const accountLabel = currentUser?.displayName || currentUser?.username || t("common.profile"); const inboxActive = Boolean(matchPath(Routes.INBOX, location.pathname)); const archivedActive = Boolean(matchPath(Routes.ARCHIVED, location.pathname)); const navigateFromMenu = (path: string) => { onClose(); setMobileOpen(false); navigateTo(path); }; const handleSignOut = async () => { await logout(); try { const keysToPreserve = ["memos-theme", "memos-locale", "memos-view-setting", "tag-view-as-tree"]; const keysToRemove: string[] = []; for (let i = 0; i < localStorage.length; i++) { const key = localStorage.key(i); if (key && !keysToPreserve.includes(key)) keysToRemove.push(key); } keysToRemove.forEach((key) => localStorage.removeItem(key)); } catch { // Ignore errors from localStorage operations. } window.location.replace(buildAuthRoute({ autoSignIn: false })); }; return ( <>
{currentUser && ( <>
{sseStatus !== "connected" && ( [0])} className={cn( "absolute -bottom-0.5 -end-0.5 size-2.5 rounded-full border-2 border-popover", sseStatus === "connecting" ? "animate-pulse bg-muted-foreground" : "bg-destructive", )} /> )} {accountLabel}
)} {/* Guests sign in from the sidebar footer, which is always in view. */} {currentUser && ( )}
); }; export default UserMenu;