import React, { useState } from 'react';
import { Link, useParams } from 'react-router-dom';
import { useForum } from '@/contexts/useForum';
import { useAuth } from '@/contexts/useAuth';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Textarea } from '@/components/ui/textarea';
import { Skeleton } from '@/components/ui/skeleton';
import {
ArrowLeft,
MessageSquare,
MessageCircle,
ArrowUp,
ArrowDown,
RefreshCw,
Eye,
} from 'lucide-react';
import { formatDistanceToNow } from 'date-fns';
import { CypherImage } from './ui/CypherImage';
import { Badge } from '@/components/ui/badge';
import { AuthorDisplay } from './ui/author-display';
const PostList = () => {
const { cellId } = useParams<{ cellId: string }>();
const {
getCellById,
getPostsByCell,
createPost,
isInitialLoading,
isPostingPost,
isRefreshing,
refreshData,
votePost,
isVoting,
posts,
moderatePost,
moderateUser,
userVerificationStatus,
} = useForum();
const { isAuthenticated, currentUser, verificationStatus } = useAuth();
const [newPostTitle, setNewPostTitle] = useState('');
const [newPostContent, setNewPostContent] = useState('');
if (!cellId || isInitialLoading) {
return (
{[...Array(3)].map((_, i) => (
))}
);
}
const cell = getCellById(cellId);
const cellPosts = getPostsByCell(cellId);
if (!cell) {
return (
Cell Not Found
The cell you're looking for doesn't exist.
);
}
const handleCreatePost = async (e: React.FormEvent) => {
e.preventDefault();
if (!newPostContent.trim()) return;
try {
const post = await createPost(cellId, newPostTitle, newPostContent);
if (post) {
setNewPostTitle('');
setNewPostContent('');
}
} catch (error) {
console.error('Error creating post:', error);
}
};
const handleVotePost = async (postId: string, isUpvote: boolean) => {
if (!isAuthenticated) return;
await votePost(postId, isUpvote);
};
const isPostVoted = (postId: string, isUpvote: boolean) => {
if (!currentUser) return false;
const post = posts.find(p => p.id === postId);
if (!post) return false;
const votes = isUpvote ? post.upvotes : post.downvotes;
return votes.some(vote => vote.author === currentUser.address);
};
// Only show unmoderated posts, or all if admin
const isCellAdmin =
currentUser && cell && currentUser.address === cell.signature;
const visiblePosts = isCellAdmin
? cellPosts
: cellPosts.filter(post => !post.moderated);
const handleModerate = async (postId: string) => {
const reason =
window.prompt('Enter a reason for moderation (optional):') || undefined;
if (!cell) return;
await moderatePost(cell.id, postId, reason, cell.signature);
};
const handleModerateUser = async (userAddress: string) => {
const reason =
window.prompt('Reason for moderating this user? (optional)') || undefined;
if (!cell) return;
await moderateUser(cell.id, userAddress, reason, cell.signature);
};
return (
{cell.name}
{cell.description}
{verificationStatus === 'verified-owner' && (
)}
{verificationStatus === 'verified-none' && (
Read-Only Mode
Your wallet does not contain any Ordinal Operators. You can browse
threads but cannot post or interact.
No Ordinals Found
)}
{!currentUser && (
Connect wallet and verify Ordinal ownership to post
)}
{cellPosts.length === 0 ? (
No Threads Yet
{isAuthenticated
? 'Be the first to post in this cell!'
: 'Connect your wallet and verify Ordinal ownership to start a thread.'}
) : (
visiblePosts.map(post => (
{post.upvotes.length - post.downvotes.length}
{post.title}
{post.content}
{formatDistanceToNow(post.timestamp, {
addSuffix: true,
})}
by
{isCellAdmin && !post.moderated && (
)}
{isCellAdmin && post.authorAddress !== cell.signature && (
)}
{post.moderated && (
[Moderated]
)}
))
)}
);
};
export default PostList;