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 { 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'; const NostrFeed = () => { const { communityNotes, isLoading, error } = useCommunityNotes(); const { ndk } = useNDKContext(); const { returnImageProxy } = useImageProxy(); const [authorData, setAuthorData] = useState({}); useEffect(() => { communityNotes.forEach(note => { if (!authorData[note.pubkey]) { fetchAuthor(note.pubkey); } }); }, [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)); setAuthorData(prevData => ({ ...prevData, [pubkey]: fields })); } catch (error) { console.error('Error fetching author:', error); } } } catch (error) { console.error('Error fetching author:', error); } }; const renderHeader = (message) => { const author = authorData[message.pubkey]; if (!author || Object.keys(author).length === 0 || !author.username || !author.avatar) { return null; } return (

{author?.username || author?.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()}
); if (isLoading) { return (
); } if (error) { return
Failed to load messages. Please try again later.
; } return (
{communityNotes.length > 0 ? ( communityNotes.map(message => ( footer(message)} className="w-full bg-gray-700 shadow-lg hover:shadow-xl transition-shadow duration-300 mb-4" >

{message.content}

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