2024-09-02 18:23:40 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2024-09-01 20:29:33 -05:00
|
|
|
import { ProgressSpinner } from 'primereact/progressspinner';
|
2024-09-02 12:09:27 -05:00
|
|
|
import { useDiscordQuery } from '@/hooks/communityQueries/useDiscordQuery';
|
2024-09-02 16:24:18 -05:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
import axios from 'axios';
|
2024-09-02 12:09:27 -05:00
|
|
|
import { useRouter } from 'next/router';
|
2024-09-02 18:23:40 -05:00
|
|
|
import { useCommunityNotes } from '@/hooks/nostr/useCommunityNotes';
|
|
|
|
import { useNDKContext } from '@/context/NDKContext';
|
|
|
|
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';
|
|
|
|
import { nip19 } from 'nostr-tools';
|
2024-09-10 15:44:08 -05:00
|
|
|
import CommunityMessage from '@/components/feeds/messages/CommunityMessage';
|
|
|
|
import useWindowWidth from '@/hooks/useWindowWidth';
|
2024-09-01 22:16:44 -05:00
|
|
|
|
2024-09-02 16:24:18 -05:00
|
|
|
const StackerNewsIconComponent = () => (
|
|
|
|
<svg width="16" height="16" className='mr-2' viewBox="0 0 256 256" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
|
|
<path fill="#facc15" fillRule="evenodd" d="m41.7 91.4 41.644 59.22-78.966 69.228L129.25 155.94l-44.083-58.14 54.353-65.441Z"/>
|
|
|
|
<path fill="#facc15" fillRule="evenodd" d="m208.355 136.74-54.358-64.36-38.4 128.449 48.675-74.094 64.36 65.175L251.54 42.497Z"/>
|
|
|
|
</svg>
|
|
|
|
);
|
|
|
|
|
|
|
|
const fetchStackerNews = async () => {
|
|
|
|
const response = await axios.get('/api/stackernews');
|
|
|
|
return response.data.data.items.items;
|
|
|
|
};
|
|
|
|
|
2024-09-09 15:56:51 -05:00
|
|
|
const GlobalFeed = ({searchQuery}) => {
|
2024-09-02 12:09:27 -05:00
|
|
|
const router = useRouter();
|
2024-09-02 16:24:18 -05:00
|
|
|
const { data: discordData, error: discordError, isLoading: discordLoading } = useDiscordQuery({page: router.query.page});
|
|
|
|
const { data: stackerNewsData, error: stackerNewsError, isLoading: stackerNewsLoading } = useQuery({queryKey: ['stackerNews'], queryFn: fetchStackerNews});
|
2024-09-02 18:23:40 -05:00
|
|
|
const { communityNotes: nostrData, error: nostrError, isLoading: nostrLoading } = useCommunityNotes();
|
|
|
|
const { ndk } = useNDKContext();
|
|
|
|
const { returnImageProxy } = useImageProxy();
|
2024-09-10 15:44:08 -05:00
|
|
|
const windowWidth = useWindowWidth();
|
2024-09-02 12:09:27 -05:00
|
|
|
|
2024-09-02 18:23:40 -05:00
|
|
|
const [authorData, setAuthorData] = useState({});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchAuthors = async () => {
|
|
|
|
const authorDataMap = {};
|
|
|
|
for (const message of nostrData) {
|
|
|
|
const author = await fetchAuthor(message.pubkey);
|
|
|
|
authorDataMap[message.pubkey] = author;
|
|
|
|
}
|
|
|
|
setAuthorData(authorDataMap);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (nostrData && nostrData.length > 0) {
|
|
|
|
fetchAuthors();
|
|
|
|
}
|
|
|
|
}, [nostrData]);
|
|
|
|
|
|
|
|
const fetchAuthor = async (pubkey) => {
|
2024-09-02 18:50:50 -05:00
|
|
|
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);
|
|
|
|
}
|
2024-09-02 18:23:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (discordLoading || stackerNewsLoading || nostrLoading) {
|
2024-09-02 12:09:27 -05:00
|
|
|
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 12:09:27 -05:00
|
|
|
<ProgressSpinner className='w-full mt-24 mx-auto' />
|
|
|
|
</div>
|
|
|
|
);
|
2024-09-01 20:29:33 -05:00
|
|
|
}
|
|
|
|
|
2024-09-02 18:23:40 -05:00
|
|
|
if (discordError || stackerNewsError || nostrError) {
|
2024-09-02 16:24:18 -05:00
|
|
|
return <div className="text-red-500 text-center p-4">Failed to load feed. Please try again later.</div>;
|
2024-09-01 20:29:33 -05:00
|
|
|
}
|
|
|
|
|
2024-09-02 16:24:18 -05:00
|
|
|
const combinedFeed = [
|
|
|
|
...(discordData || []).map(item => ({ ...item, type: 'discord' })),
|
2024-09-02 18:23:40 -05:00
|
|
|
...(stackerNewsData || []).map(item => ({ ...item, type: 'stackernews' })),
|
|
|
|
...(nostrData || []).map(item => ({ ...item, type: 'nostr' }))
|
|
|
|
].sort((a, b) => {
|
|
|
|
const dateA = a.type === 'nostr' ? a.created_at * 1000 : new Date(a.timestamp || a.createdAt);
|
|
|
|
const dateB = b.type === 'nostr' ? b.created_at * 1000 : new Date(b.timestamp || b.createdAt);
|
|
|
|
return dateB - dateA;
|
2024-09-09 15:56:51 -05:00
|
|
|
}).filter(item => {
|
|
|
|
const searchLower = searchQuery.toLowerCase();
|
|
|
|
if (item.type === 'discord' || item.type === 'nostr') {
|
|
|
|
return item.content.toLowerCase().includes(searchLower);
|
|
|
|
} else if (item.type === 'stackernews') {
|
|
|
|
return item.title.toLowerCase().includes(searchLower);
|
|
|
|
}
|
|
|
|
return false;
|
2024-09-02 18:23:40 -05:00
|
|
|
});
|
2024-09-02 16:24:18 -05:00
|
|
|
|
2024-09-01 20:29:33 -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 09:49:17 -05:00
|
|
|
<div className="mx-4 mt-4">
|
2024-09-02 16:24:18 -05:00
|
|
|
{combinedFeed.length > 0 ? (
|
|
|
|
combinedFeed.map(item => (
|
2024-09-10 15:44:08 -05:00
|
|
|
<CommunityMessage
|
|
|
|
key={item.id}
|
|
|
|
message={{
|
|
|
|
id: item.id,
|
|
|
|
author: item.type === 'discord' ? item.author :
|
|
|
|
item.type === 'stackernews' ? item.user.name :
|
|
|
|
authorData[item.pubkey]?.username || item.pubkey.substring(0, 12) + '...',
|
|
|
|
avatar: item.type === 'discord' ? item.avatar :
|
2024-09-25 17:19:02 -05:00
|
|
|
item.type === 'stackernews' ? "https://pbs.twimg.com/profile_images/1403162883941359619/oca7LMQ2_400x400.png" :
|
2024-09-10 15:44:08 -05:00
|
|
|
authorData[item.pubkey]?.avatar,
|
|
|
|
content: item.type === 'stackernews' ? item.title : item.content,
|
|
|
|
timestamp: item.type === 'nostr' ? item.created_at * 1000 : new Date(item.timestamp || item.createdAt),
|
|
|
|
channel: item.type === 'discord' ? item.channel :
|
|
|
|
item.type === 'stackernews' ? "~devs" :
|
2024-09-25 17:19:02 -05:00
|
|
|
"#plebdevs"
|
2024-09-10 15:44:08 -05:00
|
|
|
}}
|
|
|
|
searchQuery={searchQuery}
|
|
|
|
windowWidth={windowWidth}
|
|
|
|
platform={item.type}
|
|
|
|
platformIcon={item.type === 'stackernews' ? <StackerNewsIconComponent /> :
|
|
|
|
item.type === 'nostr' ? <Image src={NostrIcon} alt="Nostr" width={14} height={14} className='mr-[1px]' /> :
|
|
|
|
null}
|
|
|
|
platformLink={item.type === 'discord' ? `https://discord.com/channels/${item.channelId}/${item.id}` :
|
|
|
|
item.type === 'stackernews' ? `https://stacker.news/items/${item.id}` :
|
|
|
|
`https://nostr.band/${nip19.noteEncode(item.id)}`}
|
|
|
|
additionalContent={item.type === 'stackernews' ? `Comments: ${item.comments.length} | Sats: ${item.sats}` : null}
|
|
|
|
/>
|
2024-09-02 10:51:46 -05:00
|
|
|
))
|
|
|
|
) : (
|
2024-09-09 15:56:51 -05:00
|
|
|
<div className="text-gray-400 text-center p-4">
|
|
|
|
{searchQuery ? "No matching items found." : "No items available."}
|
|
|
|
</div>
|
2024-09-02 10:51:46 -05:00
|
|
|
)}
|
2024-09-02 09:49:17 -05:00
|
|
|
</div>
|
2024-09-01 20:29:33 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-09-02 12:09:27 -05:00
|
|
|
export default GlobalFeed;
|