Added story index management via query on homepage

Related to #27
This commit is contained in:
Dmitriy Shishkov 2023-07-20 13:12:48 +03:00
parent bc154f8b6b
commit 58d1996ce3
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
3 changed files with 66 additions and 10 deletions

View File

@ -1,3 +1,4 @@
export { default as useStoryDimensions } from './useStoryDimensions' export { default as useStoryDimensions } from './useStoryDimensions'
export { default as useSend } from './useSend' export { default as useSend } from './useSend'
export { default as useFetch } from './useFetch' export { default as useFetch } from './useFetch'
export { default as UseStoryIndex } from './useStoryIndex'

View File

@ -0,0 +1,47 @@
import { useEffect, useState } from 'react'
import { useSearchParams } from 'react-router-dom'
import { SetState } from '../utils/types'
function useStoryIndex(annLength: number | undefined) {
const [index, setIndex] = useState(0)
const [searchParams, setSearchParams] = useSearchParams()
const withReset = <T>(f: SetState<T>) => (...args: Parameters<SetState<T>>) => {
console.log('resetting index')
setIndex(0)
setSearchParams(prev => ({ ...prev, storyIndex: '0' }), { replace: true })
f(...args)
}
useEffect(() => {
setIndex(annLength ?
Number.parseInt(searchParams.get('storyIndex') || '0') :
0)
// searchParams have actual query string at first render
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [annLength])
const increment = () => setIndex(prev => {
const newIndex = (prev + 1) % (annLength || 1)
setSearchParams(prev => ({ ...prev, storyIndex: newIndex.toString() }), { replace: true })
return newIndex
})
const decrement = () => setIndex(prev => {
const newIndex = prev > 0 ? (prev - 1) : 0
setSearchParams(prev => ({ ...prev, storyIndex: newIndex.toString() }), { replace: true })
return newIndex
})
return {
n: index,
withReset,
increment,
decrement
}
}
export default useStoryIndex

View File

@ -1,4 +1,4 @@
import { CSSProperties, useEffect, useState } from 'react' import { CSSProperties, useEffect, useMemo, useState } from 'react'
import Stories from 'react-insta-stories' import Stories from 'react-insta-stories'
import { Story } from 'react-insta-stories/dist/interfaces' import { Story } from 'react-insta-stories/dist/interfaces'
@ -10,7 +10,8 @@ import { Announcement } from '../api/announcement/types'
import { categoryGraphics } from '../assets/category' import { categoryGraphics } from '../assets/category'
import puffSpinner from '../assets/puff.svg' import puffSpinner from '../assets/puff.svg'
import { gotError } from '../hooks/useFetch' import { UseFetchReturn, gotError } from '../hooks/useFetch'
import useStoryIndex from '../hooks/useStoryIndex'
function generateStories(announcements: Announcement[]): Story[] { function generateStories(announcements: Announcement[]): Story[] {
return announcements.map(announcement => { return announcements.map(announcement => {
@ -23,14 +24,14 @@ function generateStories(announcements: Announcement[]): Story[] {
}) })
} }
function fallbackGenerateStories(announcementsFetch: ReturnType<typeof useAnnouncements>) { function fallbackGenerateStories(announcements: UseFetchReturn<Announcement[]>) {
if (announcementsFetch.loading) if (announcements.loading)
return fallbackStory() return fallbackStory()
if (gotError(announcementsFetch)) if (gotError(announcements))
return fallbackStory(announcementsFetch.error, true) return fallbackStory(announcements.error, true)
const stories = generateStories(announcementsFetch.data) const stories = generateStories(announcements.data)
if (stories.length === 0) if (stories.length === 0)
return fallbackStory('Здесь пока пусто') return fallbackStory('Здесь пока пусто')
@ -68,14 +69,21 @@ function HomePage() {
const [filterShown, setFilterShown] = useState(false) const [filterShown, setFilterShown] = useState(false)
const [filter, setFilter] = useState(defaultFilters) const [filter, setFilter] = useState(defaultFilters)
const announcementsFetch = useAnnouncements(filter)
const stories = fallbackGenerateStories(announcementsFetch) const announcements = useAnnouncements(filter)
const stories = useMemo(() => fallbackGenerateStories(announcements), [announcements])
const index = useStoryIndex(announcements.data?.length)
return (<> return (<>
<Filters filter={filter} setFilter={setFilter} filterShown={filterShown} setFilterShown={setFilterShown} /> <Filters filter={filter} setFilter={index.withReset(setFilter)} filterShown={filterShown} setFilterShown={setFilterShown} />
<div style={styles.container}> <div style={styles.container}>
<Stories <Stories
currentIndex={index.n}
onStoryEnd={index.increment}
onNext={index.increment}
onPrevious={index.decrement}
stories={stories} stories={stories}
defaultInterval={11000} defaultInterval={11000}
height={height} height={height}