mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +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
+52
-21
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu.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 { genUserName } from '@/lib/genUserName';
|
||||
|
||||
@@ -15,7 +16,7 @@ interface AccountSwitcherProps {
|
||||
}
|
||||
|
||||
export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
||||
const { currentUser, otherUsers, setLogin, removeLogin } = useLoggedInAccounts();
|
||||
const { currentUser, otherUsers, isLoading, setLogin, removeLogin } = useLoggedInAccounts();
|
||||
|
||||
if (!currentUser) return null;
|
||||
|
||||
@@ -23,35 +24,64 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
||||
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 (
|
||||
<DropdownMenu modal={false}>
|
||||
<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'>
|
||||
<Avatar className='w-8 h-8'>
|
||||
<AvatarImage src={currentUser.metadata.picture} alt={getDisplayName(currentUser)} />
|
||||
<AvatarFallback>{getDisplayName(currentUser).charAt(0)}</AvatarFallback>
|
||||
<AvatarImage
|
||||
src={currentUser.metadata.picture}
|
||||
alt={isCurrentUserPending ? '' : getDisplayName(currentUser)}
|
||||
/>
|
||||
<AvatarFallback>
|
||||
{isCurrentUserPending ? (
|
||||
<Skeleton className='size-full rounded-full' />
|
||||
) : (
|
||||
getDisplayName(currentUser).charAt(0)
|
||||
)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<ChevronDown className='w-4 h-4 text-muted-foreground' />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent className='w-56 p-2 animate-scale-in'>
|
||||
<div className='font-medium text-sm px-2 py-1.5'>Switch Account</div>
|
||||
{otherUsers.map((user) => (
|
||||
<DropdownMenuItem
|
||||
key={user.id}
|
||||
onClick={() => setLogin(user.id)}
|
||||
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)} />
|
||||
<AvatarFallback>{getDisplayName(user)?.charAt(0) || <UserIcon />}</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className='flex-1 truncate'>
|
||||
<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>
|
||||
))}
|
||||
{otherUsers.map((user) => {
|
||||
const isPending = isLoading && !user.metadata.name;
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={user.id}
|
||||
onClick={() => setLogin(user.id)}
|
||||
className='flex items-center gap-2 cursor-pointer p-2 rounded-md'
|
||||
>
|
||||
<Avatar className='w-8 h-8'>
|
||||
<AvatarImage
|
||||
src={user.metadata.picture}
|
||||
alt={isPending ? '' : getDisplayName(user)}
|
||||
/>
|
||||
<AvatarFallback>
|
||||
{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 />
|
||||
<DropdownMenuItem
|
||||
onClick={onAddAccountClick}
|
||||
@@ -70,4 +100,4 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) {
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ export function useLoggedInAccounts() {
|
||||
const { nostr } = useNostr();
|
||||
const { logins, setLogin, removeLogin } = useNostrLogin();
|
||||
|
||||
const { data: authors = [] } = useQuery({
|
||||
const { data: authors = [], isLoading } = useQuery({
|
||||
queryKey: ['nostr', 'logins', logins.map((l) => l.id).join(';')],
|
||||
queryFn: async () => {
|
||||
const events = await nostr.query(
|
||||
@@ -50,6 +50,7 @@ export function useLoggedInAccounts() {
|
||||
authors,
|
||||
currentUser,
|
||||
otherUsers,
|
||||
isLoading,
|
||||
setLogin,
|
||||
removeLogin,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user