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 } 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, loading } = useForum();
const { currentUser, isAuthenticated } = useAuth();
const [newComment, setNewComment] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
if (!postId || loading) {
return (
{[...Array(2)].map((_, i) => (
))}
);
}
const post = posts.find(p => p.id === postId);
if (!post) {
return (
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;
setIsSubmitting(true);
try {
const result = await createComment(postId, newComment);
if (result) {
setNewComment('');
}
} finally {
setIsSubmitting(false);
}
};
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.includes(currentUser.address);
const isPostDownvoted = currentUser && post.downvotes.includes(currentUser.address);
const isCommentVoted = (comment: Comment, isUpvote: boolean) => {
if (!currentUser) return false;
return isUpvote
? comment.upvotes.includes(currentUser.address)
: comment.downvotes.includes(currentUser.address);
};
return (
{cell ? `Back to ${cell.name}` : 'Back to Cells'}
{post.upvotes.length - post.downvotes.length}
{post.content}
{formatDistanceToNow(post.timestamp, { addSuffix: true })}
{postComments.length} {postComments.length === 1 ? 'comment' : 'comments'}
{post.authorAddress.slice(0, 6)}...{post.authorAddress.slice(-4)}
{isAuthenticated ? (
) : (
Connect wallet and verify Ordinal ownership to comment
)}
{postComments.length === 0 ? (
) : (
postComments.map(comment => (
{comment.upvotes.length - comment.downvotes.length}
{comment.content}
{formatDistanceToNow(comment.timestamp, { addSuffix: true })}
{comment.authorAddress.slice(0, 6)}...{comment.authorAddress.slice(-4)}
))
)}
);
};
export default PostDetail;