fix(home): latest blogs published_at sort
Some checks failed
Release to Staging / build_and_release (push) Failing after 23s
Some checks failed
Release to Staging / build_and_release (push) Failing after 23s
This commit is contained in:
parent
834701aa2c
commit
91830a539a
@ -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<BlogCardDetails>[] = []
|
||||
// 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<string, Partial<BlogCardDetails>>())
|
||||
.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)
|
||||
|
Loading…
Reference in New Issue
Block a user