import * as Avatar from '@radix-ui/react-avatar'; import { useQuery } from '@tanstack/react-query'; import { minidenticon } from 'minidenticons'; import { useMemo } from 'react'; import { twMerge } from 'tailwind-merge'; import { useArk } from '@libs/ark'; import { RepostIcon } from '@shared/icons'; import { displayNpub, formatCreatedAt } from '@utils/formater'; export function NoteUser({ pubkey, time, variant = 'text', className, }: { pubkey: string; time: number; variant?: 'text' | 'repost'; className?: string; }) { const ark = useArk(); const createdAt = useMemo(() => formatCreatedAt(time), [time]); const fallbackName = useMemo(() => displayNpub(pubkey, 16), [pubkey]); const fallbackAvatar = useMemo( () => 'data:image/svg+xml;utf8,' + encodeURIComponent(minidenticon(pubkey, 90, 50)), [pubkey] ); const { isLoading, data: user } = useQuery({ queryKey: ['user', pubkey], queryFn: async () => { try { const profile = await ark.getUserProfile({ pubkey }); if (!profile) throw new Error( `Cannot get metadata for ${pubkey}, will be retry after 10 seconds` ); return profile; } catch (e) { throw new Error(e); } }, refetchOnMount: false, refetchOnWindowFocus: false, refetchOnReconnect: false, retry: 2, }); if (variant === 'repost') { if (isLoading) { return (