import { useCallback, useEffect, useLayoutEffect, useRef, useState, } from 'react' import isEmpty from 'lodash/isEmpty' import { MatchesSlider } from 'features/MatchesSlider' import { TimelineTournamentList, useTimeline } from 'features/MatchesTimeline/hooks' import { InfiniteScroll } from 'features/InfiniteScroll' import { T9n } from 'features/T9n' import { TournamentSubtitle } from 'features/TournamentSubtitle' import { ProfileTypes } from 'config' import { Content, Tournament, RowWrapper, Wrapper, TournamentLogo, Loading, TournamentSubtitleWrapper, } from './styled' const TOURNAMENT_LIMIT = 10 const MATCH_COUNT = 6 const PADDING = 30 const GAP_COUNT = 5 const SLIDER_ITEM_GAP = 0.9 // rem export const MatchesTimeline = () => { const { isTimelineFetching, onlineUpcomingMatches, tournamentList, } = useTimeline() const [tournaments, setTournaments] = useState([]) const pageRef = useRef(0) const isLastPageRef = useRef(false) const wrapperRef = useRef(null) const [cardSize, setCardSize] = useState(0) // вычисляем размеры плитки useLayoutEffect(() => { const offsetWidth = wrapperRef.current?.offsetWidth const ulItemGapFromRem = SLIDER_ITEM_GAP * parseFloat( getComputedStyle(document.documentElement).fontSize, ) if (offsetWidth) { const size = Math.round( ((offsetWidth - (ulItemGapFromRem * GAP_COUNT) - PADDING) / MATCH_COUNT), ) setCardSize(size) } }, []) const getTournaments = useCallback(() => tournamentList.slice( pageRef.current * TOURNAMENT_LIMIT, pageRef.current * TOURNAMENT_LIMIT + TOURNAMENT_LIMIT, ), [tournamentList]) const getMoreTournaments = () => { if (isLastPageRef.current) return const res = getTournaments() if (res.length) { setTournaments((prev) => ([ ...prev, ...res, ])) pageRef.current++ } else { isLastPageRef.current = true } } useEffect(() => { if (tournamentList.length) { pageRef.current = 0 isLastPageRef.current = false tournamentList.length > 0 && setTournaments(getTournaments()) pageRef.current = 1 } }, [getTournaments, tournamentList]) return ( {isTimelineFetching && ...} {(!isEmpty(onlineUpcomingMatches)) && ( LIVE & UPCOMING )} {tournaments.map(({ matches, sport_id, tournament, tournament_id, }) => ( ))} ) }