import { useNavigate, Link } from '@tanstack/react-router'; import { LogOut, UserCircle, Building, Sparkles, ChevronUp, FileText } from 'lucide-react'; import { useProfile } from '../../profile/hooks/useProfile'; import { tokenStorage } from '@/infrastructure/storage/LocalTokenStorage'; import { authApi } from '@/infrastructure/http/api/auth'; import { useState, useRef, useEffect } from 'react'; import { useTranslation } from '@/i18n/TranslationProvider'; import { useAuthenticatedUser } from '@/features/auth/hooks/useAuthenticatedUser'; import { AuthOrigin } from '@archer/domain'; /** * DashboardUserProfile Component * * Displays authenticated user's profile in the sidebar bottom. * Click opens a popover menu above with Profile, Company, and Logout. */ export function DashboardUserProfile() { const { t } = useTranslation(); const navigate = useNavigate(); const { data: profile, isLoading } = useProfile(); // Shopify-authenticated users are billed by Shopify, not by TWWIM, so profile // + company edit screens don't apply. Hide the edit surface for them; every // other origin (TWWIM / WP / future channels) retains the full menu. const authed = useAuthenticatedUser(); const canEditAccount = authed?.authOrigin !== AuthOrigin.SHOPIFY; const [open, setOpen] = useState(false); const containerRef = useRef(null); const handleLogout = async (): Promise => { try { await authApi.logout(); } catch (error) { console.error('Logout API call failed:', error); } finally { tokenStorage.removeTokens(); navigate({ to: '/login' }); } }; useEffect(() => { if (!open) return; const handler = (e: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(e.target as Node)) { setOpen(false); } }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, [open]); if (isLoading || !profile) { return (
); } return (
{open && (
{canEditAccount && ( <> setOpen(false)} className="flex items-center gap-3 px-4 py-3 text-slate-50 hover:bg-primary/20 hover:text-primary transition-colors" activeProps={{ className: 'bg-primary/20 text-primary font-medium' }} > {t('nav.profile')} setOpen(false)} data-testid="sidebar-link-company" className="flex items-center gap-3 px-4 py-3 text-slate-50 hover:bg-primary/20 hover:text-primary transition-colors" activeProps={{ className: 'bg-primary/20 text-primary font-medium' }} > {profile.isPersonal ? : } {profile.isPersonal ? t('nav.convertToBusiness') : t('nav.company')} setOpen(false)} className="flex items-center gap-3 px-4 py-3 text-slate-50 hover:bg-primary/20 hover:text-primary transition-colors" activeProps={{ className: 'bg-primary/20 text-primary font-medium' }} > {t('nav.documents')}
)}
)}
); }