import React, { useState } from 'react';
import { Link, useParams } from 'react-router-dom';
import {
useCell,
useCellPosts,
useForumActions,
usePermissions,
useUserVotes,
useAuth,
} from '@/hooks';
import { EVerificationStatus } from '@/types/identity';
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 }>();
// ✅ Use reactive hooks for data and actions
const cell = useCell(cellId);
const cellPosts = useCellPosts(cellId, { sortBy: 'relevance' });
const {
createPost,
votePost,
moderatePost,
moderateUser,
refreshData,
isCreatingPost,
isVoting,
} = useForumActions();
const { canPost, canVote, canModerate } = usePermissions();
const userVotes = useUserVotes();
const { currentUser, verificationStatus } = useAuth();
const [newPostTitle, setNewPostTitle] = useState('');
const [newPostContent, setNewPostContent] = useState('');
if (!cellId || cellPosts.isLoading) {
return (
{[...Array(3)].map((_, i) => (
))}
);
}
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;
// ✅ All validation handled in hook
const post = await createPost(cellId, newPostTitle, newPostContent);
if (post) {
setNewPostTitle('');
setNewPostContent('');
}
};
const handleVotePost = async (postId: string, isUpvote: boolean) => {
// ✅ Permission checking handled in hook
await votePost(postId, isUpvote);
};
const getPostVoteType = (postId: string) => {
return userVotes.getPostVoteType(postId);
};
// ✅ Posts already filtered by hook based on user permissions
const visiblePosts = cellPosts.posts;
const handleModerate = async (postId: string) => {
const reason =
window.prompt('Enter a reason for moderation (optional):') || undefined;
if (!cell) return;
// ✅ All validation handled in hook
await moderatePost(cell.id, postId, reason);
};
const handleModerateUser = async (userAddress: string) => {
const reason =
window.prompt('Reason for moderating this user? (optional)') || undefined;
if (!cell) return;
// ✅ All validation handled in hook
await moderateUser(cell.id, userAddress, reason);
};
return (
{cell.name}
{cell.description}
{canPost && (
)}
{!canPost &&
verificationStatus === EVerificationStatus.WALLET_CONNECTED && (
Read-Only Mode
Your wallet does not contain any Ordinal Operators. You can browse
threads but cannot post or interact.
No Ordinals Found
)}
{!canPost && !currentUser && (
Connect wallet and verify Ordinal ownership to post
)}
{visiblePosts.length === 0 ? (
No Threads Yet
{canPost
? '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
{canModerate(cell.id) && !post.moderated && (
)}
{canModerate(cell.id) && post.author !== cell.signature && (
)}
{post.moderated && (
[Moderated]
)}
))
)}
);
};
export default PostList;