Ott 1701 part 1 (#552)
parent
c565ff1b55
commit
95d0194c5b
@ -0,0 +1,83 @@ |
||||
import { |
||||
useEffect, |
||||
useState, |
||||
useMemo, |
||||
} from 'react' |
||||
|
||||
import type { MatchInfo } from 'requests/getMatchInfo' |
||||
import { getMatchInfo } from 'requests/getMatchInfo' |
||||
|
||||
import { usePageParams } from 'hooks/usePageParams' |
||||
|
||||
import { parseDate } from 'helpers/parseDate' |
||||
|
||||
import { useMatchData } from './useMatchData' |
||||
|
||||
import type { Playlists } from '../../types' |
||||
|
||||
const addScoresFromPlaylists = ( |
||||
profile: MatchInfo, |
||||
playlists: Playlists, |
||||
): MatchInfo => ( |
||||
profile |
||||
? { |
||||
...profile, |
||||
team1: { |
||||
...profile?.team1, |
||||
score: playlists.score1, |
||||
}, |
||||
team2: { |
||||
...profile?.team2, |
||||
score: playlists.score2, |
||||
}, |
||||
} |
||||
: null |
||||
) |
||||
|
||||
export const useMatchPage = () => { |
||||
const [matchProfile, setMatchProfile] = useState<MatchInfo>(null) |
||||
const { profileId: matchId, sportType } = usePageParams() |
||||
|
||||
useEffect(() => { |
||||
getMatchInfo(sportType, matchId).then(setMatchProfile) |
||||
}, [sportType, matchId]) |
||||
|
||||
useEffect(() => { |
||||
let getIntervalMatch: ReturnType<typeof setInterval> |
||||
if (matchProfile?.live && !matchProfile.youtube_link) { |
||||
getIntervalMatch = setInterval( |
||||
() => getMatchInfo(sportType, matchId).then(setMatchProfile), 1000 * 60 * 3, |
||||
) |
||||
} |
||||
return () => clearInterval(getIntervalMatch) |
||||
}, [matchProfile, sportType, matchId]) |
||||
|
||||
const { |
||||
events, |
||||
handlePlaylistClick, |
||||
matchPlaylists, |
||||
selectedPlaylist, |
||||
setFullMatchPlaylistDuration, |
||||
} = useMatchData(matchProfile?.live) |
||||
|
||||
const profile = useMemo( |
||||
() => addScoresFromPlaylists(matchProfile, matchPlaylists), |
||||
[matchProfile, matchPlaylists], |
||||
) |
||||
|
||||
const isStarted = useMemo(() => ( |
||||
profile?.date |
||||
? parseDate(profile.date) < new Date() |
||||
: true |
||||
), [profile?.date]) |
||||
|
||||
return { |
||||
events, |
||||
handlePlaylistClick, |
||||
isStarted, |
||||
matchPlaylists, |
||||
profile, |
||||
selectedPlaylist, |
||||
setFullMatchPlaylistDuration, |
||||
} |
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
import { useCallback, useState } from 'react' |
||||
|
||||
import type { Events } from 'requests' |
||||
import { getMatchEvents } from 'requests' |
||||
|
||||
import { usePageParams } from 'hooks/usePageParams' |
||||
|
||||
import { useEventsLexics } from './useEventsLexics' |
||||
|
||||
export const useEvents = () => { |
||||
const [events, setEvents] = useState<Events>([]) |
||||
const { fetchLexics } = useEventsLexics() |
||||
const { profileId: matchId, sportType } = usePageParams() |
||||
|
||||
const fetchMatchEvents = useCallback(() => { |
||||
getMatchEvents({ |
||||
matchId, |
||||
sportType, |
||||
}).then(fetchLexics) |
||||
.then(setEvents) |
||||
}, [ |
||||
fetchLexics, |
||||
matchId, |
||||
sportType, |
||||
]) |
||||
|
||||
return { events, fetchMatchEvents } |
||||
} |
||||
@ -0,0 +1,25 @@ |
||||
import { useCallback } from 'react' |
||||
|
||||
import isEmpty from 'lodash/isEmpty' |
||||
import map from 'lodash/map' |
||||
import uniq from 'lodash/uniq' |
||||
|
||||
import type { Events } from 'requests' |
||||
|
||||
import { useLexicsStore } from 'features/LexicsStore' |
||||
|
||||
export const useEventsLexics = () => { |
||||
const { addLexicsConfig } = useLexicsStore() |
||||
|
||||
const fetchLexics = useCallback((events: Events) => { |
||||
const lexics = uniq(map(events, ({ l }) => l)) |
||||
|
||||
if (!isEmpty(lexics)) { |
||||
addLexicsConfig(lexics) |
||||
} |
||||
|
||||
return events |
||||
}, [addLexicsConfig]) |
||||
|
||||
return { fetchLexics } |
||||
} |
||||
@ -0,0 +1,72 @@ |
||||
import { useEffect, useMemo } from 'react' |
||||
|
||||
import debounce from 'lodash/debounce' |
||||
|
||||
import { usePageParams } from 'hooks/usePageParams' |
||||
import { useInterval } from 'hooks/useInterval' |
||||
|
||||
import { useMatchPlaylists } from './useMatchPlaylists' |
||||
import { useEvents } from './useEvents' |
||||
|
||||
const MATCH_DATA_POLL_INTERVAL = 60000 |
||||
const MATCH_PLAYLISTS_DELAY = 5000 |
||||
|
||||
export const useMatchData = (live: boolean = false) => { |
||||
const { profileId: matchId, sportType } = usePageParams() |
||||
const { |
||||
fetchMatchPlaylists, |
||||
handlePlaylistClick, |
||||
matchPlaylists, |
||||
selectedPlaylist, |
||||
setFullMatchPlaylistDuration, |
||||
} = useMatchPlaylists() |
||||
const { events, fetchMatchEvents } = useEvents() |
||||
|
||||
const fetchPlaylistsDebounced = useMemo( |
||||
() => debounce(fetchMatchPlaylists, MATCH_PLAYLISTS_DELAY), |
||||
[fetchMatchPlaylists], |
||||
) |
||||
|
||||
useEffect(() => { |
||||
fetchMatchPlaylists({ |
||||
id: matchId, |
||||
sportType, |
||||
}) |
||||
fetchMatchEvents() |
||||
}, [ |
||||
matchId, |
||||
sportType, |
||||
fetchMatchPlaylists, |
||||
fetchMatchEvents, |
||||
]) |
||||
|
||||
const intervalCallback = () => { |
||||
fetchPlaylistsDebounced({ |
||||
id: matchId, |
||||
sportType, |
||||
}) |
||||
fetchMatchEvents() |
||||
} |
||||
|
||||
const { start, stop } = useInterval({ |
||||
callback: intervalCallback, |
||||
intervalDuration: MATCH_DATA_POLL_INTERVAL, |
||||
startImmediate: false, |
||||
}) |
||||
|
||||
useEffect(() => { |
||||
if (live) { |
||||
start() |
||||
} else { |
||||
stop() |
||||
} |
||||
}, [live, start, stop]) |
||||
|
||||
return { |
||||
events, |
||||
handlePlaylistClick, |
||||
matchPlaylists, |
||||
selectedPlaylist, |
||||
setFullMatchPlaylistDuration, |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@ |
||||
import { |
||||
useState, |
||||
useCallback, |
||||
} from 'react' |
||||
|
||||
import isEmpty from 'lodash/isEmpty' |
||||
|
||||
import type { SportTypes } from 'config/sportTypes' |
||||
|
||||
import { getMatchPlaylists } from 'requests/getMatchPlaylists' |
||||
|
||||
import type { Playlists } from 'features/MatchPage/types' |
||||
import { buildPlaylists } from 'features/MatchPage/helpers/buildPlaylists' |
||||
|
||||
import { usePlaylistLexics } from './usePlaylistLexics' |
||||
import { useSelectedPlaylist } from './useSelectedPlaylist' |
||||
|
||||
type ArgsFetchMatchPlaylists = { |
||||
id: number, |
||||
sportType: SportTypes, |
||||
} |
||||
|
||||
const initialPlaylists = buildPlaylists(null) |
||||
|
||||
export const useMatchPlaylists = () => { |
||||
const [matchPlaylists, setMatchPlaylists] = useState<Playlists>(initialPlaylists) |
||||
|
||||
const { fetchLexics } = usePlaylistLexics() |
||||
const { |
||||
handlePlaylistClick, |
||||
selectedPlaylist, |
||||
setSelectedPlaylist, |
||||
} = useSelectedPlaylist() |
||||
|
||||
const setInitialSeletedPlaylist = useCallback((playlists: Playlists) => { |
||||
setSelectedPlaylist((playlist) => { |
||||
if (!playlist && !isEmpty(playlists.match)) { |
||||
return playlists.match[0] |
||||
} |
||||
return playlist |
||||
}) |
||||
return playlists |
||||
}, [setSelectedPlaylist]) |
||||
|
||||
const fetchMatchPlaylists = useCallback(({ |
||||
id, |
||||
sportType, |
||||
}: ArgsFetchMatchPlaylists) => { |
||||
getMatchPlaylists({ |
||||
matchId: id, |
||||
selectedActions: [], |
||||
sportType, |
||||
}).then(fetchLexics) |
||||
.then(buildPlaylists) |
||||
.then(setInitialSeletedPlaylist) |
||||
.then(setMatchPlaylists) |
||||
}, [fetchLexics, setInitialSeletedPlaylist]) |
||||
|
||||
const setFullMatchPlaylistDuration = (duration: number) => { |
||||
const playlists = [...matchPlaylists.match] |
||||
if (!playlists[0]) return |
||||
|
||||
playlists[0].duration = duration |
||||
setMatchPlaylists({ |
||||
...matchPlaylists, |
||||
match: playlists, |
||||
}) |
||||
|
||||
if (selectedPlaylist) { |
||||
setSelectedPlaylist({ ...selectedPlaylist }) |
||||
} |
||||
} |
||||
|
||||
return { |
||||
fetchMatchPlaylists, |
||||
handlePlaylistClick, |
||||
matchPlaylists, |
||||
selectedPlaylist, |
||||
setFullMatchPlaylistDuration, |
||||
} |
||||
} |
||||
@ -0,0 +1,21 @@ |
||||
import { useCallback } from 'react' |
||||
|
||||
import isEmpty from 'lodash/isEmpty' |
||||
import values from 'lodash/values' |
||||
|
||||
import type { MatchPlaylists } from 'requests' |
||||
|
||||
import { useLexicsStore } from 'features/LexicsStore' |
||||
|
||||
export const usePlaylistLexics = () => { |
||||
const { addLexicsConfig } = useLexicsStore() |
||||
const fetchLexics = useCallback((playlist: MatchPlaylists | null) => { |
||||
const lexics = values(playlist?.lexics) |
||||
if (!isEmpty(lexics)) { |
||||
addLexicsConfig(lexics) |
||||
} |
||||
return playlist |
||||
}, [addLexicsConfig]) |
||||
|
||||
return { fetchLexics } |
||||
} |
||||
@ -0,0 +1,49 @@ |
||||
import type { MouseEvent } from 'react' |
||||
import { useState, useCallback } from 'react' |
||||
|
||||
import { getPlayerPlaylists } from 'requests/getPlayerPlaylists' |
||||
|
||||
import { usePageParams } from 'hooks/usePageParams' |
||||
|
||||
import { |
||||
PlayerPlaylistOption, |
||||
PlaylistOption, |
||||
PlaylistTypes, |
||||
} from 'features/MatchPage/types' |
||||
import { defaultSettings } from 'features/MatchPopup/types' |
||||
|
||||
export const useSelectedPlaylist = () => { |
||||
const { profileId: matchId, sportType } = usePageParams() |
||||
const [selectedPlaylist, setSelectedPlaylist] = useState<PlaylistOption>() |
||||
|
||||
const fetchPlayerEpisodes = useCallback((playlistOption: PlayerPlaylistOption) => ( |
||||
getPlayerPlaylists({ |
||||
matchId, |
||||
playerId: playlistOption.id, |
||||
settings: defaultSettings, |
||||
sportType, |
||||
}) |
||||
), [matchId, sportType]) |
||||
|
||||
const handlePlaylistClick = useCallback((playlist: PlaylistOption, e?: MouseEvent) => { |
||||
e?.stopPropagation() |
||||
if (playlist === selectedPlaylist) return |
||||
|
||||
if (playlist.type === PlaylistTypes.PLAYER) { |
||||
fetchPlayerEpisodes(playlist).then((episodes) => { |
||||
setSelectedPlaylist({ |
||||
...playlist, |
||||
episodes, |
||||
}) |
||||
}) |
||||
} else { |
||||
setSelectedPlaylist(playlist) |
||||
} |
||||
}, [fetchPlayerEpisodes, selectedPlaylist]) |
||||
|
||||
return { |
||||
handlePlaylistClick, |
||||
selectedPlaylist, |
||||
setSelectedPlaylist, |
||||
} |
||||
} |
||||
Loading…
Reference in new issue