fix: guard against undefined metadata in AccountSwitcher

user.metadata can be undefined when the profile hasn't loaded yet.
Changed all .metadata.x accesses to .metadata?.x to prevent crash.

Co-authored-by: shakespeare.diy <assistant@shakespeare.diy>
This commit is contained in:
shakespeare.diy 2026-06-18 11:39:32 +07:00
parent bd3e4bee4e
commit 0e5abcb2db

View file

@ -24,11 +24,11 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
if (!currentUser) return null;
const getDisplayName = (account: Account): string => {
return account.metadata.name ?? genUserName(account.pubkey);
return account.metadata?.name ?? genUserName(account.pubkey);
}
const getNip05 = (account: Account): string | undefined => {
return account.metadata.nip05;
return account.metadata?.nip05;
}
return (
@ -37,7 +37,7 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
<button className='flex items-center gap-3 p-3 rounded-full hover:bg-accent transition-all w-full text-foreground'>
<div className="relative">
<Avatar className='w-10 h-10'>
<AvatarImage src={currentUser.metadata.picture} alt={getDisplayName(currentUser)} />
<AvatarImage src={currentUser.metadata?.picture} alt={getDisplayName(currentUser)} />
<AvatarFallback>{getDisplayName(currentUser).charAt(0)}</AvatarFallback>
</Avatar>
{/* Green online indicator dot */}
@ -63,7 +63,7 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
>
<Avatar className='w-8 h-8'>
<AvatarImage src={user.metadata.picture} alt={getDisplayName(user)} />
<AvatarImage src={user.metadata?.picture} alt={getDisplayName(user)} />
<AvatarFallback>{getDisplayName(user)?.charAt(0) || <UserIcon />}</AvatarFallback>
</Avatar>
<div className='flex-1 truncate'>