import { useState, useEffect, useRef } from 'react' import { ChevronDown, User, LogOut, Languages, Check, Monitor, Moon, Palette, Sun } from 'lucide-react' import toast from 'react-hot-toast' import { useAccess } from '@xenia/access' import { PALETTES, STYLES, THEMES, applyLook, getStoredLook } from '@xenia/ui' import { LANGUAGE_NAMES, setUserLanguage, useLanguage, useTranslation } from '@xenia/i18n' import client from '../api/client' /** * The profile menu (plan step 2.6a): edit profile, language, the look (6.15: light / dark / * system, the palette, the style; saved on this device, applied at once), sign out. * The language changes the screen at once, is saved to the user's profile * (PATCH /api/auth/me) and then the platform bootstrap is reloaded, so it also * applies on the next login and in the other apps (D-12). */ export default function UserMenuButton({ displayName, onEditProfile, onSignOut }) { const { t, i18n } = useTranslation('manager') const { language, languages } = useLanguage() const { refresh } = useAccess() const [open, setOpen] = useState(false) const [look, setLook] = useState(getStoredLook) const pickLook = change => setLook(applyLook({ ...look, ...change }, { save: true })) const ref = useRef(null) useEffect(() => { function handleOutside(e) { if (ref.current && !ref.current.contains(e.target)) setOpen(false) } function handleEscape(e) { if (e.key === 'Escape') setOpen(false) } if (open) { document.addEventListener('mousedown', handleOutside) document.addEventListener('keydown', handleEscape) } return () => { document.removeEventListener('mousedown', handleOutside) document.removeEventListener('keydown', handleEscape) } }, [open]) async function chooseLanguage(lng) { setOpen(false) if (lng === language) return try { await setUserLanguage(i18n, lng, { save: l => client.patch('/api/auth/me', { language: l }) }) await refresh() } catch { toast.error(t('common:language.notSaved')) } } return (
{open && (
{t('profile.language')}
{languages.map(lng => ( ))}
{t('profile.look')}
pickLook({ mode })} name={m => t(`profile.modes.${m}`)} icon={m => ({ light: Sun, dark: Moon, system: Monitor })[m]} testId="look-mode" /> pickLook({ palette })} name={p => t(`profile.palettes.${p}`)} testId="look-palette" /> pickLook({ style })} name={x => t(`profile.styles.${x}`)} testId="look-style" />
)}
) } /** One choice of the look: a small segmented row (the menu stays open, the page changes at once). */ function LookRow({ label, options, value, onPick, name, icon, testId }) { return (
{label} {options.map(o => { const Icon = icon?.(o) const on = o === value return ( ) })}
) }