'use client' import { useState, useEffect } from 'react' import { useAuth } from '@/lib/AuthContext' import { Plus, ChevronRight, MessageCircle, X, ArrowLeft } from 'lucide-react' interface Category { id: string; name: string; description: string; icon: string } interface Thread { id: string; title: string; content: string author: { name: string }; category: { name: string } _count: { posts: number }; createdAt: string } export default function ForumPage() { const { token } = useAuth() const [categories, setCategories] = useState([]) const [threads, setThreads] = useState([]) const [selectedCat, setSelectedCat] = useState(null) const [view, setView] = useState<'categories' | 'threads'>('categories') const [showCreate, setShowCreate] = useState(false) const [loading, setLoading] = useState(true) const [toast, setToast] = useState('') useEffect(() => { fetch('/api/forum/categories') .then(r => r.json()) .then(d => setCategories(d.categories || [])) .finally(() => setLoading(false)) }, []) const loadThreads = async (catId: string) => { setLoading(true) const res = await fetch(`/api/forum/threads?categoryId=${catId}`) const data = await res.json() setThreads(data.threads || []) setLoading(false) } const showToast = (msg: string) => { setToast(msg); setTimeout(() => setToast(''), 3000) } return (
{/* Header */}
{view === 'threads' && ( )}

{view === 'threads' && selectedCat ? selectedCat.name : 'Forum'}

{view === 'categories' ? 'Community discussions' : `${threads.length} threads`}

{token && ( )}
{/* Loading */} {loading && (
{[...Array(4)].map((_, i) => (
))}
)} {/* Categories view */} {!loading && view === 'categories' && (
{categories.length === 0 ? (
🕌

No categories yet

) : categories.map(c => ( ))}
)} {/* Threads view */} {!loading && view === 'threads' && (
{threads.length === 0 ? (
💬

No threads yet

Start the first discussion

) : threads.map(t => (

{t.title}

{t.content}

{t.author.name[0]}
{t.author.name}
{t._count.posts}
))}
)} {/* Toast */} {toast && (
{toast}
)} {/* Create thread bottom sheet */} {showCreate && token && ( setShowCreate(false)} onCreated={() => { if (selectedCat) loadThreads(selectedCat.id); showToast('Thread posted!') }} showToast={showToast} /> )}
) } function CreateThreadSheet({ token, categories, onClose, onCreated, showToast }: { token: string; categories: Category[]; onClose: () => void; onCreated: () => void; showToast: (m: string) => void }) { const [title, setTitle] = useState('') const [content, setContent] = useState('') const [categoryId, setCategoryId] = useState(categories[0]?.id || '') const [loading, setLoading] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!categoryId) return setLoading(true) try { const res = await fetch('/api/forum/threads', { method: 'POST', headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ title, content, categoryId }), }) const data = await res.json() if (res.ok) { onCreated(); onClose() } else showToast(data.error || 'Failed to post') } catch { showToast('Something went wrong') } setLoading(false) } return (
e.stopPropagation()} >

New Thread

setTitle(e.target.value)} required className="w-full bg-bg-deep border border-border rounded-xl px-4 py-4 text-sm text-white placeholder-text-muted focus:border-gold focus:outline-none" />