import React, { useState, useEffect } from 'react'; import { Card } from 'primereact/card'; import { Avatar } from 'primereact/avatar'; import { Tag } from 'primereact/tag'; import { Button } from 'primereact/button'; import { ProgressSpinner } from 'primereact/progressspinner'; import { useNDKContext } from '@/context/NDKContext'; import { useSession } from 'next-auth/react'; import { findKind0Fields } from '@/utils/nostr'; import NostrIcon from '../../../public/images/nostr.png'; import Image from 'next/image'; import { useImageProxy } from '@/hooks/useImageProxy'; import { nip19 } from 'nostr-tools'; import { useCommunityNotes } from '@/hooks/nostr/useCommunityNotes'; import { highlightText } from '@/utils/text'; import ZapThreadsWrapper from '@/components/ZapThreadsWrapper'; const NostrFeed = ({ searchQuery }) => { const { communityNotes, isLoading, error } = useCommunityNotes(); const { ndk } = useNDKContext(); const { returnImageProxy } = useImageProxy(); const { data: session } = useSession(); const [authorData, setAuthorData] = useState({}); const [npub, setNpub] = useState(null); useEffect(() => { const fetchAuthors = async () => { const authorDataMap = {}; for (const message of communityNotes) { if (!authorDataMap[message.pubkey]) { const author = await fetchAuthor(message.pubkey); authorDataMap[message.pubkey] = author; } } setAuthorData(authorDataMap); }; if (communityNotes && communityNotes.length > 0) { fetchAuthors(); } }, [communityNotes]); useEffect(() => { if (session?.user?.pubkey) { setNpub(nip19.npubEncode(session.user.pubkey)); } }, [session]); const fetchAuthor = async (pubkey) => { try { const filter = { kinds: [0], authors: [pubkey] }; const author = await ndk.fetchEvent(filter); if (author) { try { const fields = await findKind0Fields(JSON.parse(author.content)); return fields; } catch (error) { console.error('Error fetching author:', error); } } return null; } catch (error) { console.error('Error fetching author:', error); } }; const getAvatarImage = (message) => { return authorData[message.pubkey]?.avatar ? returnImageProxy(authorData[message.pubkey]?.avatar) : null; }; const renderHeader = (message) => (

{authorData[message.pubkey]?.username || message.pubkey.substring(0, 12) + '...'}

} value="nostr" className="w-fit text-[#f8f8ff] bg-blue-400 max-sidebar:mt-1" />
); const footer = (message) => ( <>
{new Date(message.created_at * 1000).toLocaleString()}
{session?.user?.pubkey && ( )}
); if (isLoading) { return (
); } if (error) { return
Failed to load messages. Please try again later.
; } const filteredNotes = communityNotes.filter(message => message.content.toLowerCase().includes(searchQuery.toLowerCase()) ); return (
{filteredNotes.length > 0 ? ( filteredNotes.map(message => ( footer(message)} className="w-full bg-gray-700 shadow-lg hover:shadow-xl transition-shadow duration-300 mb-4" >

{highlightText(message.content, searchQuery)}

)) ) : (
No messages available.
)}
); }; export default NostrFeed;