2024-09-02 18:23:40 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2024-09-01 20:29:33 -05:00
|
|
|
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';
|
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';
|
2024-09-09 15:56:51 -05:00
|
|
|
import { highlightText } from '@/utils/text';
|
2024-09-02 18:23:40 -05:00
|
|
|
import { nip19 } from 'nostr-tools';
|
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-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 18:50:50 -05:00
|
|
|
const getAvatarImage = (item) => {
|
|
|
|
if (item.type === 'discord') {
|
|
|
|
return item.avatar ? returnImageProxy(item.avatar) : null;
|
|
|
|
} else if (item.type === 'nostr') {
|
|
|
|
return authorData[item.pubkey]?.avatar ? returnImageProxy(authorData[item.pubkey]?.avatar) : null;
|
|
|
|
} else if (item.type === 'stackernews') {
|
|
|
|
return item.user.image ? returnImageProxy(item.user.image) : null;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
const header = (item) => (
|
2024-09-01 20:29:33 -05:00
|
|
|
<div className="flex flex-row w-full items-center justify-between p-4 bg-gray-800 rounded-t-lg">
|
|
|
|
<div className="flex flex-row items-center">
|
2024-09-02 18:23:40 -05:00
|
|
|
<Avatar
|
2024-09-02 18:50:50 -05:00
|
|
|
image={getAvatarImage(item)}
|
2024-09-06 13:11:00 -05:00
|
|
|
icon={getAvatarImage(item) ? null : 'pi pi-user'}
|
2024-09-02 18:23:40 -05:00
|
|
|
shape="circle"
|
|
|
|
size="large"
|
|
|
|
className="border-2 border-blue-400"
|
|
|
|
/>
|
|
|
|
<p className="pl-4 font-bold text-xl text-white">
|
|
|
|
{item.type === 'discord' ? item.author :
|
|
|
|
item.type === 'stackernews' ? item.user.name :
|
|
|
|
authorData[item.pubkey]?.username || item.pubkey.substring(0, 12) + '...'}
|
|
|
|
</p>
|
2024-09-01 20:29:33 -05:00
|
|
|
</div>
|
|
|
|
<div className="flex flex-col items-start justify-between">
|
2024-09-02 12:41:26 -05:00
|
|
|
<div className="flex flex-row w-full justify-between items-center my-1 max-sidebar:flex-col max-sidebar:items-start">
|
2024-09-02 18:23:40 -05:00
|
|
|
{item.type === 'discord' && (
|
2024-09-02 16:24:18 -05:00
|
|
|
<>
|
|
|
|
<Tag value={item.channel} severity="primary" className="w-fit text-[#f8f8ff] bg-gray-600 mr-2 max-sidebar:mr-0" />
|
|
|
|
<Tag icon="pi pi-discord" value="discord" className="w-fit text-[#f8f8ff] bg-blue-400 max-sidebar:mt-1" />
|
|
|
|
</>
|
2024-09-02 18:23:40 -05:00
|
|
|
)}
|
|
|
|
{item.type === 'stackernews' && (
|
2024-09-02 16:24:18 -05:00
|
|
|
<>
|
|
|
|
<Tag value="~devs" severity="contrast" className="w-fit text-[#f8f8ff] mr-2 max-sidebar:mr-0" />
|
|
|
|
<Tag icon={<StackerNewsIconComponent />} value="stackernews" className="w-fit bg-gray-600 text-[#f8f8ff] max-sidebar:mt-1" />
|
|
|
|
</>
|
|
|
|
)}
|
2024-09-02 18:23:40 -05:00
|
|
|
{item.type === 'nostr' && (
|
|
|
|
<>
|
|
|
|
<Tag icon="pi pi-hashtag" value="plebdevs" severity="primary" className="w-fit text-[#f8f8ff] bg-gray-600 mr-2 max-sidebar:mr-0" />
|
|
|
|
<Tag icon={<Image src={NostrIcon} alt="Nostr" width={14} height={14} className='mr-[1px]' />} value="nostr" className="w-fit text-[#f8f8ff] bg-blue-400 max-sidebar:mt-1" />
|
|
|
|
</>
|
|
|
|
)}
|
2024-09-01 20:29:33 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2024-09-02 16:24:18 -05:00
|
|
|
const footer = (item) => (
|
2024-09-01 20:29:33 -05:00
|
|
|
<div className="w-full flex justify-between items-center">
|
|
|
|
<span className="bg-gray-800 rounded-lg p-2 text-sm text-gray-300">
|
2024-09-02 18:23:40 -05:00
|
|
|
{item.type === 'nostr'
|
|
|
|
? new Date(item.created_at * 1000).toLocaleString()
|
|
|
|
: new Date(item.timestamp || item.createdAt).toLocaleString()}
|
2024-09-01 20:29:33 -05:00
|
|
|
</span>
|
|
|
|
<Button
|
2024-09-02 18:23:40 -05:00
|
|
|
label={item.type === 'discord' ? "View in Discord" :
|
|
|
|
item.type === 'stackernews' ? "View on StackerNews" :
|
|
|
|
"View on Nostr"}
|
2024-09-01 22:16:44 -05:00
|
|
|
icon="pi pi-external-link"
|
|
|
|
outlined
|
2024-09-02 18:23:40 -05:00
|
|
|
severity={item.type === 'discord' ? "info" :
|
|
|
|
item.type === 'stackernews' ? "warning" :
|
|
|
|
"success"}
|
2024-09-01 22:16:44 -05:00
|
|
|
size="small"
|
|
|
|
className='my-2'
|
2024-09-02 18:23:40 -05:00
|
|
|
onClick={() => window.open(
|
|
|
|
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)}`,
|
|
|
|
'_blank'
|
|
|
|
)}
|
2024-09-01 22:16:44 -05:00
|
|
|
/>
|
2024-09-01 20:29:33 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
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-02 10:51:46 -05:00
|
|
|
<Card
|
2024-09-02 16:24:18 -05:00
|
|
|
key={item.id}
|
|
|
|
header={() => header(item)}
|
|
|
|
footer={() => footer(item)}
|
2024-09-02 10:51:46 -05:00
|
|
|
className="w-full bg-gray-700 shadow-lg hover:shadow-xl transition-shadow duration-300 mb-4"
|
|
|
|
>
|
2024-09-02 18:23:40 -05:00
|
|
|
{item.type === 'discord' || item.type === 'nostr' ? (
|
2024-09-09 15:56:51 -05:00
|
|
|
<p className="m-0 text-lg text-gray-200 overflow-hidden break-words">
|
|
|
|
{highlightText(item.content, searchQuery)}
|
|
|
|
</p>
|
2024-09-02 16:24:18 -05:00
|
|
|
) : (
|
|
|
|
<>
|
2024-09-09 15:56:51 -05:00
|
|
|
<h3 className="m-0 text-lg text-gray-200">
|
|
|
|
{highlightText(item.title, searchQuery)}
|
|
|
|
</h3>
|
2024-09-02 16:24:18 -05:00
|
|
|
<p className="text-sm text-gray-400">
|
|
|
|
Comments: {item.comments.length} | Sats: {item.sats}
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)}
|
2024-09-02 10:51:46 -05:00
|
|
|
</Card>
|
|
|
|
))
|
|
|
|
) : (
|
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;
|