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 { useCommunityNotes } from '@/hooks/nostr/useCommunityNotes'; import { useRouter } from 'next/router'; import { useNDKContext } from '@/context/NDKContext'; import { findKind0Fields } from '@/utils/nostr'; import NostrIcon from '../../../public/nostr.png'; import Image from 'next/image'; import { useImageProxy } from '@/hooks/useImageProxy'; import { nip19 } from 'nostr-tools'; const NostrFeed = () => { const router = useRouter(); const { communityNotes, error, isLoading } = useCommunityNotes(); const { ndk, addSigner } = useNDKContext(); const { returnImageProxy } = useImageProxy(); const [authorData, setAuthorData] = useState({}); useEffect(() => { const fetchAuthors = async () => { const authorDataMap = {}; for (const message of communityNotes) { const author = await fetchAuthor(message.pubkey); authorDataMap[message.pubkey] = author; } setAuthorData(authorDataMap); }; if (communityNotes && communityNotes.length > 0) { fetchAuthors(); } }, [communityNotes]); const fetchAuthor = async (pubkey) => { try { await ndk.connect(); 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); } } else { return null; } } 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 && 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;