import { Button } from '@/components/ui/button' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { DEFAULT_FAVORITE_RELAYS } from '@/constants' import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider' import { useNostr } from '@/providers/NostrProvider' import { forwardRef, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import PrimaryPageLayout from '@/layouts/PrimaryPageLayout' import { MessageSquarePlus, Hash } from 'lucide-react' import ThreadCard from '@/pages/primary/DiscussionsPage/ThreadCard' import TopicFilter from '@/pages/primary/DiscussionsPage/TopicFilter' import { NostrEvent } from 'nostr-tools' import client from '@/services/client.service' const DISCUSSION_TOPICS = [ { id: 'general', label: 'General', icon: Hash }, { id: 'meetups', label: 'Meetups', icon: Hash }, { id: 'devs', label: 'Developers', icon: Hash }, { id: 'finance', label: 'Finance & Economics', icon: Hash }, { id: 'politics', label: 'Politics & Breaking News', icon: Hash }, { id: 'literature', label: 'Literature & Art', icon: Hash }, { id: 'philosophy', label: 'Philosophy & Theology', icon: Hash }, { id: 'tech', label: 'Technology & Science', icon: Hash }, { id: 'sports', label: 'Sports', icon: Hash } ] const DiscussionsPage = forwardRef((_, ref) => { const { t } = useTranslation() const { favoriteRelays } = useFavoriteRelays() const { pubkey } = useNostr() const [selectedTopic, setSelectedTopic] = useState('general') const [selectedRelay, setSelectedRelay] = useState(null) const [threads, setThreads] = useState([]) const [loading, setLoading] = useState(false) const [showCreateThread, setShowCreateThread] = useState(false) // Use DEFAULT_FAVORITE_RELAYS for logged-out users, or user's favorite relays for logged-in users const availableRelays = pubkey && favoriteRelays.length > 0 ? favoriteRelays : DEFAULT_FAVORITE_RELAYS useEffect(() => { fetchThreads() }, [selectedTopic, selectedRelay]) const fetchThreads = async () => { setLoading(true) try { // Filter by relay if selected, otherwise use all available relays const relayUrls = selectedRelay ? [selectedRelay] : availableRelays const events = await client.fetchEvents(relayUrls, [ { kinds: [11], // Thread events '#t': [selectedTopic], '#-': ['-'], // Must have the "-" tag for relay privacy limit: 50 } ]) // Filter and sort threads const filteredThreads = events .filter(event => { // Ensure it has a title tag const titleTag = event.tags.find(tag => tag[0] === 'title' && tag[1]) return titleTag && event.content.trim().length > 0 }) .sort((a, b) => b.created_at - a.created_at) setThreads(filteredThreads) } catch (error) { console.error('Error fetching threads:', error) setThreads([]) } finally { setLoading(false) } } const handleCreateThread = () => { setShowCreateThread(true) } const handleThreadCreated = () => { setShowCreateThread(false) fetchThreads() // Refresh the list } return (
{availableRelays.length > 1 && ( )}
} displayScrollToTopButton >

{t('Discussions')} - {DISCUSSION_TOPICS.find(t => t.id === selectedTopic)?.label}

{loading ? (
{t('Loading threads...')}
) : threads.length === 0 ? (

{t('No threads yet')}

{t('Be the first to start a discussion in this topic!')}

) : (
{threads.map(thread => ( { // TODO: Navigate to thread detail view console.log('Open thread:', thread.id) }} /> ))}
)}
{showCreateThread && ( setShowCreateThread(false)} onThreadCreated={handleThreadCreated} /> )}
) }) DiscussionsPage.displayName = 'DiscussionsPage' export default DiscussionsPage // Placeholder components - to be implemented function CreateThreadDialog({ onClose, onThreadCreated }: { topic: string availableRelays: string[] onClose: () => void onThreadCreated: () => void }) { // TODO: Implement thread creation dialog return (
Create New Thread

Thread creation will be implemented here...

) }