import { useEffect, useRef, useState, } from 'react' import { createPortal } from 'react-dom' import { useTour } from '@reactour/tour' import type { PlaylistOption } from 'features/MatchPage/types' import { useMatchPageStore } from 'features/MatchPage/store' import { Spotlight, Steps, TOUR_COMPLETED_STORAGE_KEY, } from 'features/MatchTour' import { Overlay } from 'components/Overlay' import { useEventListener, useModalRoot } from 'hooks' import { isIOS, isMobileDevice } from 'config/userAgent' import { getLocalStorageItem } from 'helpers/getLocalStorage' import { MATCH_ADS } from 'components/Ads/types' import { Tabs } from './config' import { TabEvents } from './components/TabEvents' import { TabWatch } from './components/TabWatch' import { TabPlayers } from './components/TabPlayers' import { TabStats } from './components/TabStats' import { useMatchSidePlaylists } from './hooks' import { Wrapper, TabsWrapper, TabsGroup, Tab, TabIcon, TabTitle, Container, TabButton, EventsAdsWrapper, } from './styled' import { HeaderAds } from '../../components/Ads' const tabPanes = { [Tabs.WATCH]: TabWatch, [Tabs.EVENTS]: TabEvents, [Tabs.STATS]: TabStats, [Tabs.PLAYERS]: TabPlayers, } type Props = { onSelect: (option: PlaylistOption) => void, selectedPlaylist?: PlaylistOption, } export const MatchSidePlaylists = ({ onSelect, selectedPlaylist, }: Props) => { const { ads, hideProfileCard, matchPlaylists: playlists, profile, profileCardShown, selectedTab, showProfileCard, tournamentData, } = useMatchPageStore() const { onTabClick, showTabs, } = useMatchSidePlaylists() const { currentStep, isOpen, setIsOpen, } = useTour() const modalRoot = useModalRoot() const TabPane = tabPanes[selectedTab] const containerRef = useRef(null) const tabPaneContainerRef = useRef(null) const [hasTabPaneScroll, setTabPaneScroll] = useState(false) const { PLAYS_TOP, PLAYS_TOP_MOBILE } = MATCH_ADS const adsPositionId = isMobileDevice ? PLAYS_TOP_MOBILE : PLAYS_TOP useEffect(() => { const { clientHeight = 0, scrollHeight = 0, } = tabPaneContainerRef.current || {} const hasScroll = scrollHeight > clientHeight setTabPaneScroll(hasScroll) }, [ playlists, selectedTab, tabPaneContainerRef.current?.clientHeight, ]) useEffect(() => { if ( getLocalStorageItem(TOUR_COMPLETED_STORAGE_KEY) === 'true' || isOpen || !showTabs || Number(profile?.c_match_calc_status) < 2 ) return undefined const timer = setTimeout(() => setIsOpen(true), 1500) return () => clearTimeout(timer) }, [showTabs, setIsOpen, profile?.c_match_calc_status, isOpen]) useEventListener({ callback: () => { const screenLandscape = isIOS ? window.orientation : window.screen.orientation.type const yOffset = containerRef.current?.scrollTop const isScreenLandscape = isIOS ? (screenLandscape === 90 || screenLandscape === -90) : (screenLandscape === 'landscape-primary' || screenLandscape === 'landscape-secondary') if (Number(yOffset) < 10) showProfileCard() if (Number(yOffset) > 10 && !isScreenLandscape) hideProfileCard() }, event: 'scroll', target: containerRef, }) return ( {showTabs && ( {selectedTab === Tabs.EVENTS && ads && ( position.id === adsPositionId)} /> )} onTabClick(Tabs.WATCH)} id='match_watch' > onTabClick(Tabs.EVENTS)} id='match_plays' > onTabClick(Tabs.PLAYERS)} id='match_players' > onTabClick(Tabs.STATS)} data-step={Steps.Start} id='match_stats' > {Boolean(currentStep === Steps.Start && isOpen) && ( )} )} {modalRoot.current && isOpen && createPortal( , modalRoot.current, )} ) }