mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
Skeletonize account UI while metadata is loading
While the kind-0 metadata query for logged-in accounts is in flight, we
were rendering the genUserName-derived animal name (and its first
letter as the avatar fallback) before falling back to the real display
name once the profile arrived. That made the AccountSwitcher trigger
flash a meaningless 'Brave Otter' / 'B' before settling on the real
identity.
Expose isLoading from useLoggedInAccounts and, while it is still true
and we have no metadata.name yet, render Skeletons in place of:
- the avatar fallback letter (in the AccountSwitcher trigger and the
other-accounts dropdown rows),
- the displayed name in the dropdown rows.
Once the query settles, behavior is unchanged: a real name renders if
present, otherwise the genUserName fallback fills in.
This commit is contained in:
2 files changed
+51
-20
No files matched your search
@@ -7,6 +7,7 @@ import {
|
|||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from '@/components/ui/dropdown-menu.tsx';
|
} from '@/components/ui/dropdown-menu.tsx';
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar.tsx';
|
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar.tsx';
|
||||||
|
import { Skeleton } from '@/components/ui/skeleton.tsx';
|
||||||
import { useLoggedInAccounts, type Account } from '@/hooks/useLoggedInAccounts';
|
import { useLoggedInAccounts, type Account } from '@/hooks/useLoggedInAccounts';
|
||||||
import { genUserName } from '@/lib/genUserName';
|
import { genUserName } from '@/lib/genUserName';
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ interface AccountSwitcherProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
||||||
const { currentUser, otherUsers, setLogin, removeLogin } = useLoggedInAccounts();
|
const { currentUser, otherUsers, isLoading, setLogin, removeLogin } = useLoggedInAccounts();
|
||||||
|
|
||||||
if (!currentUser) return null;
|
if (!currentUser) return null;
|
||||||
|
|
||||||
@@ -23,35 +24,64 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
|||||||
return account.metadata.name ?? genUserName(account.pubkey);
|
return account.metadata.name ?? genUserName(account.pubkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// While the metadata query is in-flight and we don't yet have a name,
|
||||||
|
// we don't want to flash a generated animal name / its first letter.
|
||||||
|
const isCurrentUserPending = isLoading && !currentUser.metadata.name;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DropdownMenu modal={false}>
|
<DropdownMenu modal={false}>
|
||||||
<DropdownMenuTrigger asChild>
|
<DropdownMenuTrigger asChild>
|
||||||
<button className='flex items-center gap-2 h-10 p-1 pr-2.5 rounded-full hover:bg-accent transition-all text-foreground'>
|
<button className='flex items-center gap-2 h-10 p-1 pr-2.5 rounded-full hover:bg-accent transition-all text-foreground'>
|
||||||
<Avatar className='w-8 h-8'>
|
<Avatar className='w-8 h-8'>
|
||||||
<AvatarImage src={currentUser.metadata.picture} alt={getDisplayName(currentUser)} />
|
<AvatarImage
|
||||||
<AvatarFallback>{getDisplayName(currentUser).charAt(0)}</AvatarFallback>
|
src={currentUser.metadata.picture}
|
||||||
|
alt={isCurrentUserPending ? '' : getDisplayName(currentUser)}
|
||||||
|
/>
|
||||||
|
<AvatarFallback>
|
||||||
|
{isCurrentUserPending ? (
|
||||||
|
<Skeleton className='size-full rounded-full' />
|
||||||
|
) : (
|
||||||
|
getDisplayName(currentUser).charAt(0)
|
||||||
|
)}
|
||||||
|
</AvatarFallback>
|
||||||
</Avatar>
|
</Avatar>
|
||||||
<ChevronDown className='w-4 h-4 text-muted-foreground' />
|
<ChevronDown className='w-4 h-4 text-muted-foreground' />
|
||||||
</button>
|
</button>
|
||||||
</DropdownMenuTrigger>
|
</DropdownMenuTrigger>
|
||||||
<DropdownMenuContent className='w-56 p-2 animate-scale-in'>
|
<DropdownMenuContent className='w-56 p-2 animate-scale-in'>
|
||||||
<div className='font-medium text-sm px-2 py-1.5'>Switch Account</div>
|
<div className='font-medium text-sm px-2 py-1.5'>Switch Account</div>
|
||||||
{otherUsers.map((user) => (
|
{otherUsers.map((user) => {
|
||||||
<DropdownMenuItem
|
const isPending = isLoading && !user.metadata.name;
|
||||||
key={user.id}
|
return (
|
||||||
onClick={() => setLogin(user.id)}
|
<DropdownMenuItem
|
||||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
key={user.id}
|
||||||
>
|
onClick={() => setLogin(user.id)}
|
||||||
<Avatar className='w-8 h-8'>
|
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||||
<AvatarImage src={user.metadata.picture} alt={getDisplayName(user)} />
|
>
|
||||||
<AvatarFallback>{getDisplayName(user)?.charAt(0) || <UserIcon />}</AvatarFallback>
|
<Avatar className='w-8 h-8'>
|
||||||
</Avatar>
|
<AvatarImage
|
||||||
<div className='flex-1 truncate'>
|
src={user.metadata.picture}
|
||||||
<p className='text-sm font-medium'>{getDisplayName(user)}</p>
|
alt={isPending ? '' : getDisplayName(user)}
|
||||||
</div>
|
/>
|
||||||
{user.id === currentUser.id && <div className='w-2 h-2 rounded-full bg-primary'></div>}
|
<AvatarFallback>
|
||||||
</DropdownMenuItem>
|
{isPending ? (
|
||||||
))}
|
<Skeleton className='size-full rounded-full' />
|
||||||
|
) : (
|
||||||
|
getDisplayName(user)?.charAt(0) || <UserIcon />
|
||||||
|
)}
|
||||||
|
</AvatarFallback>
|
||||||
|
</Avatar>
|
||||||
|
<div className='flex-1 truncate'>
|
||||||
|
{isPending ? (
|
||||||
|
<Skeleton className='h-4 w-24' />
|
||||||
|
) : (
|
||||||
|
<p className='text-sm font-medium'>{getDisplayName(user)}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{user.id === currentUser.id && <div className='w-2 h-2 rounded-full bg-primary'></div>}
|
||||||
|
</DropdownMenuItem>
|
||||||
|
);
|
||||||
|
})}
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
onClick={onAddAccountClick}
|
onClick={onAddAccountClick}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export function useLoggedInAccounts() {
|
|||||||
const { nostr } = useNostr();
|
const { nostr } = useNostr();
|
||||||
const { logins, setLogin, removeLogin } = useNostrLogin();
|
const { logins, setLogin, removeLogin } = useNostrLogin();
|
||||||
|
|
||||||
const { data: authors = [] } = useQuery({
|
const { data: authors = [], isLoading } = useQuery({
|
||||||
queryKey: ['nostr', 'logins', logins.map((l) => l.id).join(';')],
|
queryKey: ['nostr', 'logins', logins.map((l) => l.id).join(';')],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const events = await nostr.query(
|
const events = await nostr.query(
|
||||||
@@ -50,6 +50,7 @@ export function useLoggedInAccounts() {
|
|||||||
authors,
|
authors,
|
||||||
currentUser,
|
currentUser,
|
||||||
otherUsers,
|
otherUsers,
|
||||||
|
isLoading,
|
||||||
setLogin,
|
setLogin,
|
||||||
removeLogin,
|
removeLogin,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user