import React, { useState, useEffect, useMemo } from 'react'; import { useProfile } from "nostr-react"; import { nip19, } from "nostr-tools"; import { Card, CardContent, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip" import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious, } from "@/components/ui/carousel" import ReactionButton from '@/components/ReactionButton'; import { Avatar, AvatarImage } from '@/components/ui/avatar'; import ViewNoteButton from './ViewNoteButton'; import Link from 'next/link'; import { Event as NostrEvent } from "nostr-tools"; import ZapButton from './ZapButton'; import CardOptionsDropdown from './CardOptionsDropdown'; import { renderTextWithLinkedTags } from '@/utils/textUtils'; interface NoteCardProps { pubkey: string; text: string; eventId: string; tags: string[][]; event: NostrEvent; showViewNoteCardButton: boolean; } const NoteCard: React.FC = ({ pubkey, text, eventId, tags, event, showViewNoteCardButton }) => { const [retryCount, setRetryCount] = useState(0); const { data: userData, isLoading: profileLoading } = useProfile({ pubkey, }); // Add retry mechanism for profile loading useEffect(() => { // If userData is not loaded and we haven't exceeded max retries if (!userData && !profileLoading && retryCount < 3) { const timer = setTimeout(() => { setRetryCount(prev => prev + 1); }, 2000); // Retry after 2 seconds return () => clearTimeout(timer); } }, [userData, profileLoading, retryCount]); // Create a shortened npub that can be used as a fallback when userData is not available const npubShortened = useMemo(() => { const encoded = nip19.npubEncode(pubkey); const parts = encoded.split('npub'); return 'npub' + parts[1].slice(0, 4) + ':' + parts[1].slice(-3); }, [pubkey]); const title = userData?.username || userData?.display_name || userData?.name || userData?.npub || npubShortened; // text = text.replaceAll('\n', '
'); text = text.replaceAll('\n', ' '); const imageSrc = text.match(/https?:\/\/[^ ]*\.(png|jpg|gif|jpeg)/g); const videoSrc = text.match(/https?:\/\/[^ ]*\.(mp4|webm|mov)/g); const textWithoutImage = text.replace(/https?:\/\/.*\.(?:png|jpg|gif|mp4|webm|mov|jpeg)/g, ''); const createdAt = new Date(event.created_at * 1000); const hrefProfile = `/profile/${nip19.npubEncode(pubkey)}`; const profileImageSrc = userData?.picture || "https://robohash.org/" + pubkey; return ( <>
{title}

{title}

{
{imageSrc && imageSrc.length > 1 ? ( {imageSrc.map((src, index) => ( {textWithoutImage ))} ) : ( imageSrc ? {textWithoutImage : "" )}
{videoSrc && videoSrc.length > 1 ? ( {videoSrc.map((src, index) => ( ))} ) : ( videoSrc ?
}
{renderTextWithLinkedTags(textWithoutImage, tags)}

{showViewNoteCardButton && }
{createdAt.toLocaleString()}
); } export default NoteCard;