top login button drop down menu, should show login user full name and em

This commit is contained in:
Indigo Tang 2025-07-06 13:03:15 +00:00
parent a3f2a71aad
commit 396f1a76a1
1 changed files with 21 additions and 26 deletions

View File

@ -15,44 +15,42 @@ import {
} from "@/components/ui/dropdown-menu"; } from "@/components/ui/dropdown-menu";
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { ThemeToggleButton } from '@/components/ui/theme-toggle'; import { ThemeToggleButton } from '@/components/ui/theme-toggle';
// import LanguageSwitcher from './LanguageSwitcher'; // Removed import { useI18n, useCurrentLocale } from '@/locales/client';
import { useI18n, useCurrentLocale, useChangeLocale } from '@/locales/client'; import { getUserByEmail } from '@/app/actions/user';
import type { User } from '@/types';
export default function Header() { export default function Header() {
const pathname = usePathname(); const pathname = usePathname();
const router = useRouter(); const router = useRouter();
const t = useI18n(); const t = useI18n();
const locale = useCurrentLocale(); const locale = useCurrentLocale();
const changeLocale = useChangeLocale();
const [isAuthenticated, setIsAuthenticated] = useState(false); const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isMounted, setIsMounted] = useState(false); const [isMounted, setIsMounted] = useState(false);
const [userEmail, setUserEmail] = useState<string | null>(null); const [user, setUser] = useState<User | null>(null);
useEffect(() => { useEffect(() => {
setIsMounted(true); setIsMounted(true);
const authStatus = localStorage.getItem('isToyShareAuthenticated') === 'true';
setIsAuthenticated(authStatus);
if (authStatus) {
setUserEmail(localStorage.getItem('userEmail'));
}
}, []);
useEffect(() => { const checkAuth = async () => {
// This effect handles re-authentication status if localStorage changes from another tab/window
const handleStorageChange = () => {
const authStatus = localStorage.getItem('isToyShareAuthenticated') === 'true'; const authStatus = localStorage.getItem('isToyShareAuthenticated') === 'true';
setIsAuthenticated(authStatus); setIsAuthenticated(authStatus);
if (authStatus) { if (authStatus) {
setUserEmail(localStorage.getItem('userEmail')); const email = localStorage.getItem('userEmail');
if (email) {
const fetchedUser = await getUserByEmail(email);
setUser(fetchedUser);
}
} else { } else {
setUserEmail(null); setUser(null);
} }
}; };
window.addEventListener('storage', handleStorageChange); checkAuth();
window.addEventListener('storage', checkAuth);
return () => { return () => {
window.removeEventListener('storage', handleStorageChange); window.removeEventListener('storage', checkAuth);
}; };
}, []); }, []);
@ -62,8 +60,6 @@ export default function Header() {
const preferredLang = localStorage.getItem('userPreferredLanguage') as 'en' | 'zh-TW' | null; const preferredLang = localStorage.getItem('userPreferredLanguage') as 'en' | 'zh-TW' | null;
if (preferredLang && preferredLang !== locale) { if (preferredLang && preferredLang !== locale) {
const currentPathWithoutLocale = pathname.startsWith(`/${locale}`) ? pathname.substring(`/${locale}`.length) : pathname; const currentPathWithoutLocale = pathname.startsWith(`/${locale}`) ? pathname.substring(`/${locale}`.length) : pathname;
// Ensure the path exists before redirecting. For simplicity, assume it does.
// A more robust solution would check against a list of valid routes for the preferredLang.
router.push(`/${preferredLang}${currentPathWithoutLocale}${window.location.search}`); router.push(`/${preferredLang}${currentPathWithoutLocale}${window.location.search}`);
} }
} }
@ -74,11 +70,11 @@ export default function Header() {
localStorage.removeItem('isToyShareAuthenticated'); localStorage.removeItem('isToyShareAuthenticated');
localStorage.removeItem('userEmail'); localStorage.removeItem('userEmail');
setIsAuthenticated(false); setIsAuthenticated(false);
setUserEmail(null); setUser(null);
router.push(`/${locale}/`); router.push(`/${locale}/`);
}; };
const isAdmin = userEmail === 'user@example.com' || userEmail === 'admin@example.com'; const isAdmin = user?.role === 'Admin';
const cleanPathname = pathname.startsWith(`/${locale}`) const cleanPathname = pathname.startsWith(`/${locale}`)
? pathname.substring(`/${locale}`.length) || '/' ? pathname.substring(`/${locale}`.length) || '/'
@ -99,7 +95,7 @@ export default function Header() {
{t('header.browse_toys')} {t('header.browse_toys')}
</Button> </Button>
</Link> </Link>
{isMounted && isAuthenticated ? ( {isMounted && isAuthenticated && user ? (
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon" className="h-9 w-9 md:h-10 md:w-10"> <Button variant="ghost" size="icon" className="h-9 w-9 md:h-10 md:w-10">
@ -110,11 +106,11 @@ export default function Header() {
<DropdownMenuContent align="end"> <DropdownMenuContent align="end">
<DropdownMenuLabel className="font-normal"> <DropdownMenuLabel className="font-normal">
<div className="flex flex-col space-y-1"> <div className="flex flex-col space-y-1">
<p className="text-sm font-medium leading-none capitalize"> <p className="text-sm font-medium leading-none">
{userEmail?.split('@')[0]} {user.name}
</p> </p>
<p className="text-xs leading-none text-muted-foreground"> <p className="text-xs leading-none text-muted-foreground">
{userEmail} {user.email}
</p> </p>
</div> </div>
</DropdownMenuLabel> </DropdownMenuLabel>
@ -168,7 +164,6 @@ export default function Header() {
</div> </div>
)} )}
</nav> </nav>
{/* <LanguageSwitcher /> Removed */}
<ThemeToggleButton /> <ThemeToggleButton />
</div> </div>
</div> </div>