From 130300f1c16ff7f4c57d75a152e0299d4905fcbf Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 1 Jun 2025 11:11:13 -0500 Subject: [PATCH] Use generated username fallback in other parts of the UI --- CONTEXT.md | 5 ++-- src/components/NoteContent.tsx | 33 ++----------------------- src/components/auth/AccountSwitcher.tsx | 17 +++++++------ 3 files changed, 15 insertions(+), 40 deletions(-) diff --git a/CONTEXT.md b/CONTEXT.md index 5861318..45bc9c0 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -122,14 +122,15 @@ The data may be transformed into a more appropriate format if needed, and multip To display profile data for a user by their Nostr pubkey (such as an event author), use the `useAuthor` hook. ```tsx -import { NostrEvent, NostrMetadata } from '@nostrify/nostrify'; +import type { NostrEvent, NostrMetadata } from '@nostrify/nostrify'; import { useAuthor } from '@/hooks/useAuthor'; +import { genUserName } from '@/lib/genUserName'; function Post({ event }: { event: NostrEvent }) { const author = useAuthor(event.pubkey); const metadata: NostrMetadata | undefined = author.data?.metadata; - const displayName = metadata?.name || event.pubkey.slice(0, 8); + const displayName = metadata?.name ?? genUserName(event.pubkey); const profileImage = metadata?.picture; // ...render elements with this data diff --git a/src/components/NoteContent.tsx b/src/components/NoteContent.tsx index 2572463..2e7257e 100644 --- a/src/components/NoteContent.tsx +++ b/src/components/NoteContent.tsx @@ -3,6 +3,7 @@ import { type NostrEvent } from '@nostrify/nostrify'; import { Link } from 'react-router-dom'; import { nip19 } from 'nostr-tools'; import { useAuthor } from '@/hooks/useAuthor'; +import { genUserName } from '@/lib/genUserName'; import { cn } from '@/lib/utils'; interface NoteContentProps { @@ -118,7 +119,7 @@ function NostrMention({ pubkey }: { pubkey: string }) { const author = useAuthor(pubkey); const npub = nip19.npubEncode(pubkey); const hasRealName = !!author.data?.metadata?.name; - const displayName = author.data?.metadata?.name ?? generateDeterministicName(pubkey); + const displayName = author.data?.metadata?.name ?? genUserName(pubkey); return ( ); -} - -// Generate a deterministic name based on pubkey -function generateDeterministicName(pubkey: string): string { - // Use a simple hash of the pubkey to generate consistent adjective + noun combinations - const adjectives = [ - 'Swift', 'Bright', 'Calm', 'Bold', 'Wise', 'Kind', 'Quick', 'Brave', - 'Cool', 'Sharp', 'Clear', 'Strong', 'Smart', 'Fast', 'Keen', 'Pure', - 'Noble', 'Gentle', 'Fierce', 'Steady', 'Clever', 'Proud', 'Silent', 'Wild' - ]; - - const nouns = [ - 'Fox', 'Eagle', 'Wolf', 'Bear', 'Lion', 'Tiger', 'Hawk', 'Owl', - 'Deer', 'Raven', 'Falcon', 'Lynx', 'Otter', 'Whale', 'Shark', 'Dolphin', - 'Phoenix', 'Dragon', 'Panther', 'Jaguar', 'Cheetah', 'Leopard', 'Puma', 'Cobra' - ]; - - // Create a simple hash from the pubkey - let hash = 0; - for (let i = 0; i < pubkey.length; i++) { - const char = pubkey.charCodeAt(i); - hash = ((hash << 5) - hash) + char; - hash = hash & hash; // Convert to 32-bit integer - } - - // Use absolute value to ensure positive index - const adjIndex = Math.abs(hash) % adjectives.length; - const nounIndex = Math.abs(hash >> 8) % nouns.length; - - return `${adjectives[adjIndex]}${nouns[nounIndex]}`; } \ No newline at end of file diff --git a/src/components/auth/AccountSwitcher.tsx b/src/components/auth/AccountSwitcher.tsx index e78a004..81d2fdf 100644 --- a/src/components/auth/AccountSwitcher.tsx +++ b/src/components/auth/AccountSwitcher.tsx @@ -10,7 +10,8 @@ import { DropdownMenuTrigger, } from '@/components/ui/dropdown-menu.tsx'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar.tsx'; -import { useLoggedInAccounts } from '@/hooks/useLoggedInAccounts'; +import { useLoggedInAccounts, type Account } from '@/hooks/useLoggedInAccounts'; +import { genUserName } from '@/lib/genUserName'; interface AccountSwitcherProps { onAddAccountClick: () => void; @@ -21,16 +22,18 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) { if (!currentUser) return null; + const getDisplayName = (account: Account): string => account.metadata.name ?? genUserName(account.pubkey); + return ( @@ -44,11 +47,11 @@ export function AccountSwitcher({ onAddAccountClick }: AccountSwitcherProps) { className='flex items-center gap-2 cursor-pointer p-2 rounded-md' > - - {user.metadata.name?.charAt(0) || } + + {getDisplayName(user)?.charAt(0) || }
-

{user.metadata.name || user.pubkey}

+

{getDisplayName(user)}

{user.id === currentUser.id &&
}