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';
|
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-02 18:23:40 -05:00
|
|
|
|
2024-09-09 15:56:51 -05:00
|
|
|
const NostrFeed = ({ searchQuery }) => {
|
2025-04-02 17:47:30 -05:00
|
|
|
const { communityNotes, isLoading, error } = useCommunityNotes();
|
|
|
|
const { ndk } = useNDKContext();
|
|
|
|
const { data: session } = useSession();
|
|
|
|
const [authorData, setAuthorData] = useState({});
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
const windowWidth = useWindowWidth();
|
2024-09-10 15:44:08 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
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,
|
|
|
|
}));
|
2024-09-02 18:23:40 -05:00
|
|
|
}
|
2025-04-02 17:47:30 -05:00
|
|
|
}
|
2024-09-03 17:40:22 -05:00
|
|
|
};
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
if (communityNotes && communityNotes.length > 0) {
|
|
|
|
fetchAuthors();
|
2024-09-02 18:23:40 -05:00
|
|
|
}
|
2025-04-02 17:47:30 -05:00
|
|
|
}, [communityNotes, authorData]);
|
|
|
|
|
|
|
|
const fetchAuthor = async pubkey => {
|
|
|
|
try {
|
|
|
|
const filter = {
|
|
|
|
kinds: [0],
|
|
|
|
authors: [pubkey],
|
|
|
|
};
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
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);
|
2024-09-02 18:23:40 -05:00
|
|
|
}
|
2025-04-02 17:47:30 -05:00
|
|
|
};
|
2024-09-02 18:23:40 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
if (isLoading) {
|
|
|
|
return (
|
|
|
|
<div className="h-[100vh] min-bottom-bar:w-[86vw] max-sidebar:w-[100vw]">
|
|
|
|
<ProgressSpinner className="w-full mt-24 mx-auto" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-09-09 15:56:51 -05:00
|
|
|
|
2025-04-02 17:47:30 -05:00
|
|
|
if (error) {
|
2024-09-02 18:23:40 -05:00
|
|
|
return (
|
2025-04-02 17:47:30 -05:00
|
|
|
<div className="text-red-500 text-center p-4">
|
|
|
|
Failed to load messages. Please try again later.
|
|
|
|
</div>
|
2024-09-02 18:23:40 -05:00
|
|
|
);
|
2025-04-02 17:47:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const filteredNotes = communityNotes
|
|
|
|
.filter(message =>
|
|
|
|
searchQuery ? message.content.toLowerCase().includes(searchQuery.toLowerCase()) : true
|
|
|
|
)
|
|
|
|
.sort((a, b) => b.created_at - a.created_at);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="h-full w-full">
|
|
|
|
<div className="mx-0 mt-4">
|
|
|
|
{filteredNotes.length > 0 ? (
|
|
|
|
filteredNotes.map(message => (
|
|
|
|
<CommunityMessage
|
|
|
|
key={message.id}
|
|
|
|
message={{
|
|
|
|
id: message.id,
|
|
|
|
author:
|
|
|
|
authorData[message.pubkey]?.username || message.pubkey.substring(0, 12) + '...',
|
|
|
|
avatar: authorData[message.pubkey]?.avatar,
|
|
|
|
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)}`}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<div className="text-gray-400 text-center p-4">No messages available.</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2024-09-02 18:23:40 -05:00
|
|
|
};
|
|
|
|
|
2024-10-23 14:48:11 -05:00
|
|
|
export default NostrFeed;
|