2024-10-14 19:20:43 +05:00
|
|
|
import {
|
|
|
|
NDKFilter,
|
|
|
|
NDKKind,
|
|
|
|
NDKSubscriptionCacheUsage
|
|
|
|
} from '@nostr-dev-kit/ndk'
|
2024-09-18 21:42:50 +05:00
|
|
|
import { ModCard } from 'components/ModCard'
|
2024-10-07 15:45:21 +05:00
|
|
|
import { ModFilter } from 'components/ModsFilter'
|
2024-09-18 21:42:50 +05:00
|
|
|
import { PaginationWithPageNumbers } from 'components/Pagination'
|
|
|
|
import { MAX_MODS_PER_PAGE, T_TAG_VALUE } from 'constants.ts'
|
2024-10-07 15:45:21 +05:00
|
|
|
import {
|
|
|
|
useAppSelector,
|
|
|
|
useFilteredMods,
|
|
|
|
useMuteLists,
|
2024-10-14 19:20:43 +05:00
|
|
|
useNDKContext,
|
2024-10-07 15:45:21 +05:00
|
|
|
useNSFWList
|
|
|
|
} from 'hooks'
|
2024-10-14 19:20:43 +05:00
|
|
|
import { useEffect, useState } from 'react'
|
2024-09-18 21:42:50 +05:00
|
|
|
import { useParams } from 'react-router-dom'
|
2024-10-07 15:45:21 +05:00
|
|
|
import {
|
|
|
|
FilterOptions,
|
|
|
|
ModDetails,
|
|
|
|
ModeratedFilter,
|
|
|
|
NSFWFilter,
|
|
|
|
SortBy
|
|
|
|
} from 'types'
|
2024-10-14 19:20:43 +05:00
|
|
|
import { extractModData, isModDataComplete } from 'utils'
|
2024-09-18 21:42:50 +05:00
|
|
|
|
|
|
|
export const GamePage = () => {
|
|
|
|
const params = useParams()
|
|
|
|
const { name: gameName } = params
|
2024-10-14 19:20:43 +05:00
|
|
|
const { ndk } = useNDKContext()
|
2024-09-18 21:42:50 +05:00
|
|
|
const muteLists = useMuteLists()
|
2024-10-07 15:45:21 +05:00
|
|
|
const nsfwList = useNSFWList()
|
2024-09-18 21:42:50 +05:00
|
|
|
|
|
|
|
const [filterOptions, setFilterOptions] = useState<FilterOptions>({
|
2024-10-07 15:45:21 +05:00
|
|
|
sort: SortBy.Latest,
|
|
|
|
nsfw: NSFWFilter.Hide_NSFW,
|
|
|
|
source: window.location.host,
|
|
|
|
moderated: ModeratedFilter.Moderated
|
2024-09-18 21:42:50 +05:00
|
|
|
})
|
|
|
|
const [mods, setMods] = useState<ModDetails[]>([])
|
|
|
|
|
|
|
|
const [currentPage, setCurrentPage] = useState(1)
|
|
|
|
|
|
|
|
const userState = useAppSelector((state) => state.user)
|
|
|
|
|
2024-10-07 15:45:21 +05:00
|
|
|
const filteredMods = useFilteredMods(
|
2024-09-18 21:42:50 +05:00
|
|
|
mods,
|
2024-10-07 15:45:21 +05:00
|
|
|
userState,
|
|
|
|
filterOptions,
|
|
|
|
nsfwList,
|
2024-09-18 21:42:50 +05:00
|
|
|
muteLists
|
2024-10-07 15:45:21 +05:00
|
|
|
)
|
2024-09-18 21:42:50 +05:00
|
|
|
|
|
|
|
// Pagination logic
|
|
|
|
const totalGames = filteredMods.length
|
|
|
|
const totalPages = Math.ceil(totalGames / MAX_MODS_PER_PAGE)
|
|
|
|
const startIndex = (currentPage - 1) * MAX_MODS_PER_PAGE
|
|
|
|
const endIndex = startIndex + MAX_MODS_PER_PAGE
|
|
|
|
const currentMods = filteredMods.slice(startIndex, endIndex)
|
|
|
|
|
|
|
|
const handlePageChange = (page: number) => {
|
|
|
|
if (page >= 1 && page <= totalPages) {
|
|
|
|
setCurrentPage(page)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-10-14 19:20:43 +05:00
|
|
|
const filter: NDKFilter = {
|
|
|
|
kinds: [NDKKind.Classified],
|
2024-09-18 21:42:50 +05:00
|
|
|
'#t': [T_TAG_VALUE]
|
|
|
|
}
|
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
const subscription = ndk.subscribe(filter, {
|
|
|
|
cacheUsage: NDKSubscriptionCacheUsage.PARALLEL,
|
|
|
|
closeOnEose: true
|
|
|
|
})
|
|
|
|
|
|
|
|
subscription.on('event', (ndkEvent) => {
|
|
|
|
if (isModDataComplete(ndkEvent)) {
|
|
|
|
const mod = extractModData(ndkEvent)
|
|
|
|
if (mod.game === gameName)
|
|
|
|
setMods((prev) => {
|
|
|
|
if (prev.find((e) => e.aTag === mod.aTag)) return [...prev]
|
2024-09-18 21:42:50 +05:00
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
return [...prev, mod]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2024-09-18 21:42:50 +05:00
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
subscription.start()
|
2024-09-18 21:42:50 +05:00
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
// Cleanup function to stop subscription
|
2024-09-18 21:42:50 +05:00
|
|
|
return () => {
|
2024-10-14 19:20:43 +05:00
|
|
|
subscription.stop()
|
2024-09-18 21:42:50 +05:00
|
|
|
}
|
2024-10-14 19:20:43 +05:00
|
|
|
}, [gameName, ndk])
|
2024-09-18 21:42:50 +05:00
|
|
|
|
|
|
|
if (!gameName) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className='InnerBodyMain'>
|
|
|
|
<div className='ContainerMain'>
|
|
|
|
<div className='IBMSecMainGroup IBMSecMainGroupAlt'>
|
|
|
|
<div className='IBMSecMain'>
|
|
|
|
<div className='SearchMainWrapper'>
|
|
|
|
<div className='IBMSMTitleMain'>
|
|
|
|
<h2 className='IBMSMTitleMainHeading'>
|
|
|
|
Game:
|
|
|
|
<span className='IBMSMTitleMainHeadingSpan'>
|
|
|
|
{gameName}
|
|
|
|
</span>
|
|
|
|
</h2>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-10-07 15:45:21 +05:00
|
|
|
<ModFilter
|
2024-09-18 21:42:50 +05:00
|
|
|
filterOptions={filterOptions}
|
|
|
|
setFilterOptions={setFilterOptions}
|
|
|
|
/>
|
|
|
|
<div className='IBMSecMain IBMSMListWrapper'>
|
|
|
|
<div className='IBMSMList'>
|
2024-09-23 20:58:50 +05:00
|
|
|
{currentMods.map((mod) => (
|
|
|
|
<ModCard key={mod.id} {...mod} />
|
|
|
|
))}
|
2024-09-18 21:42:50 +05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<PaginationWithPageNumbers
|
|
|
|
currentPage={currentPage}
|
|
|
|
totalPages={totalPages}
|
|
|
|
handlePageChange={handlePageChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|