refactor(home): fetch latest blogs in parallel w/ nsfw filter
This commit is contained in:
parent
dae94733fa
commit
c81b2c0a1d
@ -10,13 +10,15 @@ import { LANDING_PAGE_DATA } from '../constants'
|
|||||||
import {
|
import {
|
||||||
useDidMount,
|
useDidMount,
|
||||||
useGames,
|
useGames,
|
||||||
|
useLocalStorage,
|
||||||
useMuteLists,
|
useMuteLists,
|
||||||
useNDKContext,
|
useNDKContext,
|
||||||
useNSFWList
|
useNSFWList
|
||||||
} from '../hooks'
|
} from '../hooks'
|
||||||
import { appRoutes, getModPageRoute } from '../routes'
|
import { appRoutes, getModPageRoute } from '../routes'
|
||||||
import { BlogCardDetails, ModDetails } from '../types'
|
import { BlogCardDetails, ModDetails, NSFWFilter, SortBy } from '../types'
|
||||||
import {
|
import {
|
||||||
|
extractBlogCardDetails,
|
||||||
extractModData,
|
extractModData,
|
||||||
handleModImageError,
|
handleModImageError,
|
||||||
log,
|
log,
|
||||||
@ -29,11 +31,14 @@ import '../styles/SimpleSlider.css'
|
|||||||
import '../styles/styles.css'
|
import '../styles/styles.css'
|
||||||
|
|
||||||
// Import Swiper styles
|
// Import Swiper styles
|
||||||
import { filterForEventsTaggingId, NDKFilter } from '@nostr-dev-kit/ndk'
|
import {
|
||||||
|
filterForEventsTaggingId,
|
||||||
|
NDKEvent,
|
||||||
|
NDKFilter
|
||||||
|
} from '@nostr-dev-kit/ndk'
|
||||||
import 'swiper/css'
|
import 'swiper/css'
|
||||||
import 'swiper/css/navigation'
|
import 'swiper/css/navigation'
|
||||||
import 'swiper/css/pagination'
|
import 'swiper/css/pagination'
|
||||||
import { extractBlogCardDetails } from 'utils/blog'
|
|
||||||
|
|
||||||
export const HomePage = () => {
|
export const HomePage = () => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
@ -317,13 +322,15 @@ const Spinner = () => {
|
|||||||
const DisplayLatestBlogs = () => {
|
const DisplayLatestBlogs = () => {
|
||||||
const [blogs, setBlogs] = useState<Partial<BlogCardDetails>[]>()
|
const [blogs, setBlogs] = useState<Partial<BlogCardDetails>[]>()
|
||||||
const { fetchEvents } = useNDKContext()
|
const { fetchEvents } = useNDKContext()
|
||||||
|
const [filterOptions] = useLocalStorage('filter-blog-curated', {
|
||||||
|
sort: SortBy.Latest,
|
||||||
|
nsfw: NSFWFilter.Hide_NSFW
|
||||||
|
})
|
||||||
useDidMount(() => {
|
useDidMount(() => {
|
||||||
const fetchBlogs = async () => {
|
const fetchBlogs = async () => {
|
||||||
try {
|
try {
|
||||||
// Show maximum of 4 blog posts
|
// Show maximum of 4 blog posts
|
||||||
// 2 should be featured and the most recent 2 from blog npubs
|
// 2 should be featured and the most recent 2 from blog npubs
|
||||||
|
|
||||||
// Populate the filter from known naddr (constants.ts)
|
// Populate the filter from known naddr (constants.ts)
|
||||||
const filters: NDKFilter[] = []
|
const filters: NDKFilter[] = []
|
||||||
for (let i = 0; i < LANDING_PAGE_DATA.featuredBlogPosts.length; i++) {
|
for (let i = 0; i < LANDING_PAGE_DATA.featuredBlogPosts.length; i++) {
|
||||||
@ -350,25 +357,46 @@ const DisplayLatestBlogs = () => {
|
|||||||
'#a': []
|
'#a': []
|
||||||
} as NDKFilter
|
} as NDKFilter
|
||||||
)
|
)
|
||||||
// Fetch featured blogs posts
|
// Prepare filter for the latest
|
||||||
const featuredBlogPosts = await fetchEvents(filter)
|
|
||||||
|
|
||||||
// Fetch latest blog npubs posts
|
|
||||||
const blogNpubs = import.meta.env.VITE_BLOG_NPUBS.split(',')
|
const blogNpubs = import.meta.env.VITE_BLOG_NPUBS.split(',')
|
||||||
const blogHexkeys = blogNpubs
|
const blogHexkeys = blogNpubs
|
||||||
.map(npubToHex)
|
.map(npubToHex)
|
||||||
.filter((hexkey) => hexkey !== null)
|
.filter((hexkey) => hexkey !== null)
|
||||||
|
|
||||||
// We fetch 4 posts in case of duplicates (from featured)
|
// We fetch 4 posts in case of duplicates (from featured)
|
||||||
const latestBlogPosts = await fetchEvents({
|
const latestFilter: NDKFilter = {
|
||||||
authors: blogHexkeys,
|
authors: blogHexkeys,
|
||||||
kinds: [kinds.LongFormArticle],
|
kinds: [kinds.LongFormArticle],
|
||||||
limit: 4
|
limit: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by NSFW tag
|
||||||
|
// NSFWFilter.Show_NSFW -> filter not needed
|
||||||
|
// NSFWFilter.Only_NSFW -> true
|
||||||
|
// NSFWFilter.Hide_NSFW -> false
|
||||||
|
if (filterOptions.nsfw !== NSFWFilter.Show_NSFW) {
|
||||||
|
latestFilter['#nsfw'] = [
|
||||||
|
(filterOptions.nsfw === NSFWFilter.Only_NSFW).toString()
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
const results = await Promise.allSettled([
|
||||||
|
fetchEvents(filter),
|
||||||
|
fetchEvents(latestFilter)
|
||||||
|
])
|
||||||
|
|
||||||
|
const events: NDKEvent[] = []
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Remove duplicates
|
// Remove duplicates
|
||||||
const unique = Array.from(
|
const unique = Array.from(
|
||||||
[...featuredBlogPosts, ...latestBlogPosts]
|
events
|
||||||
.reduce((map, obj) => {
|
.reduce((map, obj) => {
|
||||||
map.set(obj.id, obj)
|
map.set(obj.id, obj)
|
||||||
return map
|
return map
|
||||||
|
Loading…
Reference in New Issue
Block a user