import React from 'react'; import { useForumData } from '@/hooks'; import { Link } from 'react-router-dom'; import { formatDistanceToNow } from 'date-fns'; import { Skeleton } from '@/components/ui/skeleton'; import { MessageSquareText, Newspaper } from 'lucide-react'; import { AuthorDisplay } from './ui/author-display'; import { LinkRenderer } from './ui/link-renderer'; interface FeedItemBase { id: string; type: 'post' | 'comment'; timestamp: number; ownerAddress: string; cellId?: string; postId?: string; } interface PostFeedItem extends FeedItemBase { type: 'post'; title: string; cellId: string; postId: string; commentCount: number; voteCount: number; } interface CommentFeedItem extends FeedItemBase { type: 'comment'; content: string; postId: string; voteCount: number; } type FeedItem = PostFeedItem | CommentFeedItem; const ActivityFeed: React.FC = () => { // ✅ Use reactive hooks for data const forumData = useForumData(); const { postsWithVoteStatus, commentsWithVoteStatus, cellsWithStats, isInitialLoading, } = forumData; // ✅ Use pre-computed data with vote scores const combinedFeed: FeedItem[] = [ ...postsWithVoteStatus.map( (post): PostFeedItem => ({ id: post.id, type: 'post', timestamp: post.timestamp, ownerAddress: post.author, title: post.title, cellId: post.cellId, postId: post.id, commentCount: forumData.commentsByPost[post.id]?.length || 0, voteCount: post.voteScore, }) ), ...commentsWithVoteStatus .map((comment): CommentFeedItem | null => { const parentPost = postsWithVoteStatus.find( p => p.id === comment.postId ); if (!parentPost) return null; return { id: comment.id, type: 'comment', timestamp: comment.timestamp, ownerAddress: comment.author, content: comment.content, postId: comment.postId, cellId: parentPost.cellId, voteCount: comment.voteScore, }; }) .filter((item): item is CommentFeedItem => item !== null), ].sort((a, b) => b.timestamp - a.timestamp); const renderFeedItem = (item: FeedItem) => { const cell = item.cellId ? cellsWithStats.find(c => c.id === item.cellId) : undefined; const timeAgo = formatDistanceToNow(new Date(item.timestamp), { addSuffix: true, }); const linkTarget = item.type === 'post' ? `/post/${item.id}` : `/post/${item.postId}`; return (
Be the first to create a post or comment!