From 98c52e65daf8feba38a4c82b1f14fac62c5fc02f Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Mon, 3 Jun 2024 17:26:47 +0200 Subject: [PATCH] feat(home): in popular threads box, show more posts per sub depending on available subs --- src/hooks/use-popular-posts.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/hooks/use-popular-posts.ts b/src/hooks/use-popular-posts.ts index b857bb0b..65360890 100644 --- a/src/hooks/use-popular-posts.ts +++ b/src/hooks/use-popular-posts.ts @@ -6,12 +6,20 @@ const usePopularPosts = (subplebbits: Subplebbit[]) => { const [popularPosts, setPopularPosts] = useState([]); useEffect(() => { - const subplebbitToPost: { [key: string]: any } = {}; const uniqueLinks: Set = new Set(); + const allPosts: Comment[] = []; + + let postsPerSub = 1; + if (subplebbits.length == 2) { + postsPerSub = 4; + } else if (subplebbits.length == 3) { + postsPerSub = 3; + } else if (subplebbits.length >= 4 && subplebbits.length < 8) { + postsPerSub = 2; + } subplebbits.forEach((subplebbit: any) => { - let maxTimestamp = -Infinity; - let mostRecentPost = null; + let subplebbitPosts: Comment[] = []; if (subplebbit?.posts?.pages?.hot?.comments) { for (const post of Object.values(subplebbit.posts.pages.hot.comments as Comment)) { @@ -21,32 +29,27 @@ const usePopularPosts = (subplebbits: Subplebbit[]) => { if ( isMediaShowed && - replyCount > 0 && + (replyCount > 0 || postsPerSub > 1) && !deleted && !removed && !locked && !pinned && timestamp > Date.now() / 1000 - 60 * 60 * 24 * 30 && - timestamp > maxTimestamp && !uniqueLinks.has(link) ) { - maxTimestamp = post.timestamp; - mostRecentPost = post; + subplebbitPosts.push(post); uniqueLinks.add(link); } } - if (mostRecentPost) { - subplebbitToPost[subplebbit.address] = { post: mostRecentPost, timestamp: maxTimestamp }; - } + subplebbitPosts.sort((a: any, b: any) => b.timestamp - a.timestamp); + allPosts.push(...subplebbitPosts.slice(0, postsPerSub)); } }); - const sortedPosts = Object.values(subplebbitToPost) - .sort((a, b) => b.timestamp - a.timestamp) - .slice(0, 8); + const sortedPosts = allPosts.sort((a: any, b: any) => b.timestamp - a.timestamp).slice(0, 8); - setPopularPosts(sortedPosts.map((item) => item.post)); + setPopularPosts(sortedPosts); }, [subplebbits]); return popularPosts;