import React, { useState } from 'react'; import { Link, useParams, useNavigate } from 'react-router-dom'; import { useForum } from '@/contexts/ForumContext'; import { useAuth } from '@/contexts/AuthContext'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Skeleton } from '@/components/ui/skeleton'; import { ArrowLeft, ArrowUp, ArrowDown, Clock, MessageCircle, Send, RefreshCw } from 'lucide-react'; import { formatDistanceToNow } from 'date-fns'; import { Comment } from '@/types'; const PostDetail = () => { const { postId } = useParams<{ postId: string }>(); const navigate = useNavigate(); const { posts, comments, getCommentsByPost, createComment, votePost, voteComment, getCellById, isInitialLoading, isPostingComment, isVoting, isRefreshing, refreshData } = useForum(); const { currentUser, isAuthenticated } = useAuth(); const [newComment, setNewComment] = useState(''); if (!postId || isInitialLoading) { return (
Loading...
{[...Array(2)].map((_, i) => (
))}
); } const post = posts.find(p => p.id === postId); if (!post) { return (
Back to Cells

Post Not Found

The post you're looking for doesn't exist or may have been pruned.

); } const cell = getCellById(post.cellId); const postComments = getCommentsByPost(postId); const handleCreateComment = async (e: React.FormEvent) => { e.preventDefault(); if (!newComment.trim()) return; try { const result = await createComment(postId, newComment); if (result) { setNewComment(''); } } catch (error) { console.error("Error creating comment:", error); } }; const handleVotePost = async (isUpvote: boolean) => { if (!isAuthenticated) return; await votePost(post.id, isUpvote); }; const handleVoteComment = async (commentId: string, isUpvote: boolean) => { if (!isAuthenticated) return; await voteComment(commentId, isUpvote); }; const isPostUpvoted = currentUser && post.upvotes.some(vote => vote.author === currentUser.address); const isPostDownvoted = currentUser && post.downvotes.some(vote => vote.author === currentUser.address); const isCommentVoted = (comment: Comment, isUpvote: boolean) => { if (!currentUser) return false; const votes = isUpvote ? comment.upvotes : comment.downvotes; return votes.some(vote => vote.author === currentUser.address); }; return (
{cell ? `Back to ${cell.name}` : 'Back to Cells'}
{post.upvotes.length - post.downvotes.length}

{post.title}

{post.content}

{formatDistanceToNow(post.timestamp, { addSuffix: true })} {postComments.length} {postComments.length === 1 ? 'comment' : 'comments'} {post.authorAddress.slice(0, 6)}...{post.authorAddress.slice(-4)}
{isAuthenticated ? (