import React, { useState, useEffect } from 'react'; 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 useWindowWidth from '@/hooks/useWindowWidth'; import { nip19 } from 'nostr-tools'; import { useCommunityNotes } from '@/hooks/nostr/useCommunityNotes'; import CommunityMessage from '@/components/feeds/messages/CommunityMessage'; const NostrFeed = ({ searchQuery }) => { const { communityNotes, isLoading, error } = useCommunityNotes(); const { ndk } = useNDKContext(); const { data: session } = useSession(); const [authorData, setAuthorData] = useState({}); const windowWidth = useWindowWidth(); useEffect(() => { const fetchAuthors = async () => { for (const message of communityNotes) { if (!authorData[message.pubkey]) { const author = await fetchAuthor(message.pubkey); setAuthorData(prevData => ({ ...prevData, [message.pubkey]: author })); } } }; if (communityNotes && communityNotes.length > 0) { fetchAuthors(); } }, [communityNotes, authorData]); 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); } }; if (isLoading) { return (
); } if (error) { return
Failed to load messages. Please try again later.
; } const filteredNotes = communityNotes .filter(message => searchQuery ? message.content.toLowerCase().includes(searchQuery.toLowerCase()) : true ) .sort((a, b) => b.created_at - a.created_at); return (
{filteredNotes.length > 0 ? ( filteredNotes.map(message => ( } platformLink={`https://nostr.band/${nip19.noteEncode(message.id)}`} /> )) ) : (
No messages available.
)}
); }; export default NostrFeed;