Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 42 additions & 64 deletions src/components/groups/PostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,22 +342,6 @@ export function PostList({ communityId, showOnlyApproved = true, pendingOnly = f
if (isNestedReply) return post;
}

// For legacy kind 11 posts, they are always considered top-level
// Auto-approve for approved members and moderators
const isApprovedMember = approvedMembers.includes(post.pubkey);
const isModerator = moderators.includes(post.pubkey);
if (isApprovedMember || isModerator) {
return {
...post,
approval: {
id: `auto-approved-${post.id}`,
pubkey: post.pubkey,
created_at: post.created_at,
autoApproved: true,
kind: post.kind
}
};
}
return post;
}).filter(Boolean);

Expand All @@ -383,62 +367,56 @@ export function PostList({ communityId, showOnlyApproved = true, pendingOnly = f

// Memoize the sorted posts to avoid unnecessary re-renders
const sortedPosts = useMemo(() => {
// Process pinned posts through the same approval logic
const pinnedPostsProcessed = (pinnedPosts || [])
.filter(post =>
!removedPostIds.includes(post.id) &&
!bannedUsers.includes(post.pubkey)
)
.map(post => {
// Check if this pinned post is already in the approved posts
const existingApproval = (approvedPosts || []).find(ap => ap.id === post.id);
if (existingApproval) {
return existingApproval;
}

// Auto-approve for approved members and moderators
const isApprovedMember = approvedMembers.includes(post.pubkey);
const isModerator = moderators.includes(post.pubkey);
if (isApprovedMember || isModerator) {
return {
...post,
approval: {
id: `auto-approved-${post.id}`,
pubkey: post.pubkey,
created_at: post.created_at,
autoApproved: true,
kind: post.kind
}
};
}
return post;
});

// Filter pinned posts based on approval status
let filteredPinnedPosts = pinnedPostsProcessed;
if (showOnlyApproved) {
filteredPinnedPosts = pinnedPostsProcessed.filter(post => 'approval' in post);
} else if (pendingOnly) {
filteredPinnedPosts = pinnedPostsProcessed.filter(post => !('approval' in post));
}

// Separate regular posts (excluding pinned ones)
const regularPosts = filteredPosts.filter(post =>
post && !pinnedPostIds.includes(post.id)
);
// Combine all posts and apply approval logic once
const allPosts = [
...filteredPosts,
...pinnedPostsProcessed
];

// Apply approval logic to all posts
const postsWithApproval = allPosts.map(post => {
if (!post) return post;

// If post already has approval info, return it as is
if (!!post?.approval) return post;

// Auto-approve for approved members, moderators, and the community owner
const isApprovedMember = approvedMembers.includes(post.pubkey);
const isModerator = moderators.includes(post.pubkey);
const isOwner = communityEvent && post.pubkey === communityEvent.pubkey;
const isApproved = isApprovedMember || isModerator || isOwner;

return {
...post,
...(isApproved ? { approval: {
id: `auto-approved-${post.id}`,
pubkey: post.pubkey,
created_at: post.created_at,
autoApproved: true,
kind: post.kind
}} : {})
};
}).filter(Boolean);

// Filter posts based on approval status
const filteredPostsWithApproval = (() => {
if (showOnlyApproved) {
return postsWithApproval.filter(post => !!post?.approval);
} else if (pendingOnly) {
return postsWithApproval.filter(post => !post?.approval);
}
return postsWithApproval;
})();

// Sort regular posts by creation time
const sortedRegularPosts = regularPosts.sort((a, b) =>
// Sort all posts by creation time
return filteredPostsWithApproval.sort((a, b) =>
(b?.created_at || 0) - (a?.created_at || 0)
);

// Sort pinned posts by creation time (most recent pins first)
const sortedPinnedPosts = filteredPinnedPosts.sort((a, b) =>
(b?.created_at || 0) - (a?.created_at || 0)
);

// Combine pinned posts first, then regular posts
return [...sortedPinnedPosts, ...sortedRegularPosts];
}, [
pinnedPosts,
removedPostIds,
Expand Down