From 91830a539ad1e5984ae79f505e48f5819d9ce4c9 Mon Sep 17 00:00:00 2001 From: enes Date: Wed, 13 Nov 2024 10:55:51 +0100 Subject: [PATCH] fix(home): latest blogs published_at sort --- src/pages/home.tsx | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/pages/home.tsx b/src/pages/home.tsx index 64ec771..a8394e2 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -361,15 +361,12 @@ const DisplayLatestBlogs = () => { const latestFilter: NDKFilter = { authors: blogHexkeys, kinds: [kinds.LongFormArticle], - limit: 4 + limit: PROFILE_BLOG_FILTER_LIMIT } // Filter by NSFW tag if (filterOptions.nsfw === NSFWFilter.Only_NSFW) { latestFilter['#L'] = ['content-warning'] - } else if (filterOptions.nsfw === NSFWFilter.Hide_NSFW) { - // Up the limit in case we fetch multiple NSFW blogs - latestFilter.limit = PROFILE_BLOG_FILTER_LIMIT } const results = await Promise.allSettled([ @@ -377,28 +374,38 @@ const DisplayLatestBlogs = () => { fetchEvents(latestFilter) ]) - const events: NDKEvent[] = [] + const events: Partial[] = [] // Get featured blogs posts result results.forEach((r) => { // Add events from both promises to the array if (r.status === 'fulfilled' && r.value) { - events.push(...r.value) + events.push( + ...r.value + .map(extractBlogCardDetails) // Extract the blog card details + .sort( + // Sort each result by published_at in descending order + // We can't sort everything at once we'd lose prefered + (a, b) => + a.published_at && b.published_at + ? b.published_at - a.published_at + : 0 + ) + ) } }) // Remove duplicates const unique = Array.from( events + .filter((b) => b.id) .reduce((map, obj) => { - map.set(obj.id, obj) + map.set(obj.id!, obj) return map - }, new Map()) + }, new Map>()) .values() + ).filter( + (b) => !(b.nsfw && filterOptions.nsfw === NSFWFilter.Hide_NSFW) ) - .map(extractBlogCardDetails) - .filter( - (b) => !(b.nsfw && filterOptions.nsfw === NSFWFilter.Hide_NSFW) - ) const latest = unique.slice(0, 4) setBlogs(latest)