2024-09-02 18:23:40 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { ProgressSpinner } from 'primereact/progressspinner';
|
|
|
|
import { useNDKContext } from '@/context/NDKContext';
|
2024-09-06 23:21:40 -05:00
|
|
|
import { useSession } from 'next-auth/react';
|
2024-09-02 18:23:40 -05:00
|
|
|
import { findKind0Fields } from '@/utils/nostr';
|
2024-09-02 18:50:50 -05:00
|
|
|
import NostrIcon from '../../../public/images/nostr.png';
|
2024-09-02 18:23:40 -05:00
|
|
|
import Image from 'next/image';
|
|
|
|
import { useImageProxy } from '@/hooks/useImageProxy';
|
2024-09-10 15:44:08 -05:00
|
|
|
import useWindowWidth from '@/hooks/useWindowWidth';
|
2024-09-02 18:23:40 -05:00
|
|
|
import { nip19 } from 'nostr-tools';
|
2024-09-03 17:40:22 -05:00
|
|
|
import { useCommunityNotes } from '@/hooks/nostr/useCommunityNotes';
|
2024-09-10 15:44:08 -05:00
|
|
|
import CommunityMessage from '@/components/feeds/messages/CommunityMessage';
|
2024-09-06 23:21:40 -05:00
|
|
|
import ZapThreadsWrapper from '@/components/ZapThreadsWrapper';
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2024-09-09 15:56:51 -05:00
|
|
|
const NostrFeed = ({ searchQuery }) => {
|
2024-09-03 17:40:22 -05:00
|
|
|
const { communityNotes, isLoading, error } = useCommunityNotes();
|
|
|
|
const { ndk } = useNDKContext();
|
2024-09-02 18:23:40 -05:00
|
|
|
const { returnImageProxy } = useImageProxy();
|
2024-09-06 23:21:40 -05:00
|
|
|
const { data: session } = useSession();
|
2024-09-02 18:23:40 -05:00
|
|
|
const [authorData, setAuthorData] = useState({});
|
2024-09-06 23:21:40 -05:00
|
|
|
const [npub, setNpub] = useState(null);
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2024-09-10 15:44:08 -05:00
|
|
|
const windowWidth = useWindowWidth();
|
|
|
|
|
2024-09-02 18:23:40 -05:00
|
|
|
useEffect(() => {
|
2024-09-06 13:11:00 -05:00
|
|
|
const fetchAuthors = async () => {
|
|
|
|
const authorDataMap = {};
|
|
|
|
for (const message of communityNotes) {
|
|
|
|
if (!authorDataMap[message.pubkey]) {
|
|
|
|
const author = await fetchAuthor(message.pubkey);
|
|
|
|
authorDataMap[message.pubkey] = author;
|
|
|
|
}
|
2024-09-02 18:23:40 -05:00
|
|
|
}
|
2024-09-06 13:11:00 -05:00
|
|
|
setAuthorData(authorDataMap);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (communityNotes && communityNotes.length > 0) {
|
|
|
|
fetchAuthors();
|
|
|
|
}
|
|
|
|
}, [communityNotes]);
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2024-09-06 23:21:40 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (session?.user?.pubkey) {
|
|
|
|
setNpub(nip19.npubEncode(session.user.pubkey));
|
|
|
|
}
|
|
|
|
}, [session]);
|
|
|
|
|
2024-09-02 18:23:40 -05:00
|
|
|
const fetchAuthor = async (pubkey) => {
|
|
|
|
try {
|
|
|
|
const filter = {
|
|
|
|
kinds: [0],
|
|
|
|
authors: [pubkey]
|
2024-09-03 17:40:22 -05:00
|
|
|
};
|
2024-09-02 18:23:40 -05:00
|
|
|
|
|
|
|
const author = await ndk.fetchEvent(filter);
|
|
|
|
if (author) {
|
|
|
|
try {
|
|
|
|
const fields = await findKind0Fields(JSON.parse(author.content));
|
2024-09-06 13:11:00 -05:00
|
|
|
return fields;
|
2024-09-02 18:23:40 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching author:', error);
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 13:11:00 -05:00
|
|
|
return null;
|
2024-09-02 18:23:40 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching author:', error);
|
|
|
|
}
|
2024-09-03 17:40:22 -05:00
|
|
|
};
|
2024-09-02 18:23:40 -05:00
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return (
|
2024-09-06 16:57:27 -05:00
|
|
|
<div className="h-[100vh] min-bottom-bar:w-[86vw] max-sidebar:w-[100vw]">
|
2024-09-02 18:23:40 -05:00
|
|
|
<ProgressSpinner className='w-full mt-24 mx-auto' />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return <div className="text-red-500 text-center p-4">Failed to load messages. Please try again later.</div>;
|
|
|
|
}
|
|
|
|
|
2024-09-09 15:56:51 -05:00
|
|
|
const filteredNotes = communityNotes.filter(message =>
|
|
|
|
message.content.toLowerCase().includes(searchQuery.toLowerCase())
|
|
|
|
);
|
|
|
|
|
2024-09-02 18:23:40 -05:00
|
|
|
return (
|
2024-09-06 16:57:27 -05:00
|
|
|
<div className="bg-gray-900 h-full w-full min-bottom-bar:w-[86vw]">
|
2024-09-02 18:23:40 -05:00
|
|
|
<div className="mx-4 mt-4">
|
2024-09-09 15:56:51 -05:00
|
|
|
{filteredNotes.length > 0 ? (
|
|
|
|
filteredNotes.map(message => (
|
2024-09-10 15:44:08 -05:00
|
|
|
<CommunityMessage
|
2024-09-02 18:23:40 -05:00
|
|
|
key={message.id}
|
2024-09-10 15:44:08 -05:00
|
|
|
message={{
|
|
|
|
id: message.id,
|
|
|
|
author: authorData[message.pubkey]?.username || message.pubkey.substring(0, 12) + '...',
|
|
|
|
avatar: authorData[message.pubkey]?.avatar ? returnImageProxy(authorData[message.pubkey]?.avatar) : null,
|
|
|
|
content: message.content,
|
|
|
|
timestamp: message.created_at * 1000,
|
|
|
|
channel: "plebdevs"
|
|
|
|
}}
|
|
|
|
searchQuery={searchQuery}
|
|
|
|
windowWidth={windowWidth}
|
|
|
|
platform="nostr"
|
|
|
|
platformIcon={<Image src={NostrIcon} alt="Nostr" width={14} height={14} className='mr-[1px]' />}
|
|
|
|
platformLink={`https://nostr.band/${nip19.noteEncode(message.id)}`}
|
|
|
|
/>
|
2024-09-02 18:23:40 -05:00
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<div className="text-gray-400 text-center p-4">No messages available.</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NostrFeed;
|