'use client' import { useState, useEffect } from 'react' import { useParams, useRouter } from 'next/navigation' import { useAuth } from '@/lib/AuthContext' import { ArrowLeft, ShieldCheck, ShieldAlert, Send, MessageCircle } from 'lucide-react' interface Thread { id: string title: string content: string author: { id: string; name: string } category: { id: string; name: string } shariahStatus: string shariahFlags: string | null pinned: boolean createdAt: string } interface Post { id: string content: string author: { id: string; name: string } shariahStatus: string shariahFlags: string | null createdAt: string } export default function ThreadDetailPage() { const { threadId } = useParams<{ threadId: string }>() const router = useRouter() const { token, user } = useAuth() const [thread, setThread] = useState(null) const [posts, setPosts] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [reply, setReply] = useState('') const [submitting, setSubmitting] = useState(false) const [replyError, setReplyError] = useState(null) useEffect(() => { if (!threadId) return setLoading(true) Promise.all([ fetch(`/api/forum/threads?id=${threadId}`).then(r => r.json()), fetch(`/api/forum/posts?threadId=${threadId}`).then(r => r.json()), ]) .then(([threadData, postsData]) => { const t = threadData.threads?.[0] || threadData.thread if (!t) { setError('Thread not found'); return } setThread(t) setPosts(postsData.posts || []) }) .catch(() => setError('Failed to load thread')) .finally(() => setLoading(false)) }, [threadId]) const handleReply = async (e: React.FormEvent) => { e.preventDefault() if (!token || !reply.trim()) return setSubmitting(true) setReplyError(null) const res = await fetch('/api/forum/posts', { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ threadId, content: reply.trim() }), }) const data = await res.json() setSubmitting(false) if (!res.ok) { setReplyError(data.error || 'Failed to post reply'); return } setPosts(prev => [...prev, data.post]) setReply('') } if (loading) { return (
) } if (error || !thread) { return (

{error || 'Thread not found'}

) } return (
{/* Thread card */}
{thread.category.name} {thread.shariahStatus === 'approved' ? ( Shariah Approved ) : ( {thread.shariahStatus === 'pending' ? 'Pending Review' : 'Flagged'} )}

{thread.title}

By {thread.author.name} · {new Date(thread.createdAt).toLocaleDateString()}

{thread.content}

{/* Replies section */}

Replies ({posts.length})

{posts.length === 0 ? (

No replies yet. Be the first to respond.

) : ( posts.map(post => (
{post.author.name[0]?.toUpperCase()}
{post.author.name}
{new Date(post.createdAt).toLocaleDateString()}

{post.content}

{post.shariahStatus !== 'approved' && (
Pending shariah review
)}
)) )}
{/* Reply form */} {token ? (