|
|
|
|
@ -9,7 +9,7 @@ import { useQueryClient } from 'react-query' |
|
|
|
|
|
|
|
|
|
import throttle from 'lodash/throttle' |
|
|
|
|
import isEmpty from 'lodash/isEmpty' |
|
|
|
|
import every from 'lodash/every' |
|
|
|
|
import includes from 'lodash/includes' |
|
|
|
|
import find from 'lodash/find' |
|
|
|
|
import isUndefined from 'lodash/isUndefined' |
|
|
|
|
import flatMapDepth from 'lodash/flatMapDepth' |
|
|
|
|
@ -19,7 +19,11 @@ import size from 'lodash/size' |
|
|
|
|
|
|
|
|
|
import { querieKeys } from 'config' |
|
|
|
|
|
|
|
|
|
import type { MatchScore, PlayerParam } from 'requests' |
|
|
|
|
import type { |
|
|
|
|
DataItem, |
|
|
|
|
MatchScore, |
|
|
|
|
PlayerParam, |
|
|
|
|
} from 'requests' |
|
|
|
|
import { |
|
|
|
|
MatchInfo, |
|
|
|
|
PlayersStats, |
|
|
|
|
@ -33,21 +37,24 @@ import { getLocalStorageItem } from 'helpers/getLocalStorage' |
|
|
|
|
import { useObjectState, usePageParams } from 'hooks' |
|
|
|
|
|
|
|
|
|
import type{ PlaylistOption } from 'features/MatchPage/types' |
|
|
|
|
import { StatsType } from 'features/MatchSidePlaylists/components/TabStats/config' |
|
|
|
|
import { StatsType, Tabs as StatsTabs } from 'features/MatchSidePlaylists/components/TabStats/config' |
|
|
|
|
import { FULL_GAME_KEY } from 'features/MatchPage/helpers/buildPlaylists' |
|
|
|
|
import { getHalfTime } from 'features/MatchPage/helpers/getHalfTime' |
|
|
|
|
import { TOUR_COMPLETED_STORAGE_KEY } from 'features/MatchTour' |
|
|
|
|
import { Tabs } from 'features/MatchSidePlaylists/config' |
|
|
|
|
|
|
|
|
|
import { DISPLAYED_PARAMS_COLUMNS } from 'features/MatchSidePlaylists/components/PlayersTable/config' |
|
|
|
|
import { useFakeData } from './useFakeData' |
|
|
|
|
|
|
|
|
|
const REQUEST_DELAY = 3000 |
|
|
|
|
const REQUEST_DELAY = 5000 |
|
|
|
|
const STATS_POLL_INTERVAL = 30000 |
|
|
|
|
|
|
|
|
|
type UsePlayersStatsArgs = { |
|
|
|
|
matchProfile: MatchInfo, |
|
|
|
|
playingProgress: number, |
|
|
|
|
selectedPlaylist?: PlaylistOption, |
|
|
|
|
selectedStatsTable: StatsTabs, |
|
|
|
|
selectedTab: Tabs, |
|
|
|
|
setIsPlayersStatsFetching: Dispatch<SetStateAction<boolean>>, |
|
|
|
|
statsType: StatsType, |
|
|
|
|
} |
|
|
|
|
@ -61,6 +68,8 @@ export const usePlayersStats = ({ |
|
|
|
|
matchProfile, |
|
|
|
|
playingProgress, |
|
|
|
|
selectedPlaylist, |
|
|
|
|
selectedStatsTable, |
|
|
|
|
selectedTab, |
|
|
|
|
setIsPlayersStatsFetching, |
|
|
|
|
statsType, |
|
|
|
|
}: UsePlayersStatsArgs) => { |
|
|
|
|
@ -81,12 +90,6 @@ export const usePlayersStats = ({ |
|
|
|
|
|
|
|
|
|
const isCurrentStats = statsType === StatsType.CURRENT_STATS |
|
|
|
|
|
|
|
|
|
const isEmptyPlayersStats = (teamId: number) => ( |
|
|
|
|
isEmpty(playersStats[teamId]) |
|
|
|
|
|| every(playersStats[teamId], isEmpty) |
|
|
|
|
|| isEmpty(playersData[matchProfile?.team1.id === teamId ? 'team1' : 'team2']) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const getParams = useCallback((stats: PlayersStats) => ( |
|
|
|
|
uniqBy(flatMapDepth(stats, values), 'id') as Array<PlayerParam> |
|
|
|
|
), []) |
|
|
|
|
@ -141,16 +144,25 @@ export const usePlayersStats = ({ |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
const fetchData = useMemo(() => throttle(async (second?: number) => { |
|
|
|
|
const isTeam1Selected = selectedStatsTable === StatsTabs.TEAM1 |
|
|
|
|
|
|
|
|
|
if ( |
|
|
|
|
selectedPlaylist?.id !== FULL_GAME_KEY |
|
|
|
|
|| (matchProfile?.live && Number(matchProfile.c_match_calc_status) <= 1) |
|
|
|
|
|| selectedTab !== Tabs.STATS |
|
|
|
|
|| !includes([StatsTabs.TEAM1, StatsTabs.TEAM2], selectedStatsTable) |
|
|
|
|
|| !matchProfile?.team1.id |
|
|
|
|
|| !matchProfile?.team2.id |
|
|
|
|
|| (!matchProfile?.live && !isCurrentStats && !isEmpty(playersStats[isTeam1Selected |
|
|
|
|
? matchProfile.team1.id |
|
|
|
|
: matchProfile.team2.id])) |
|
|
|
|
) return |
|
|
|
|
|
|
|
|
|
const [res1, res2, res3] = await Promise.all([ |
|
|
|
|
const [res1, res2] = await Promise.all([ |
|
|
|
|
fetchPlayers(second), |
|
|
|
|
fetchPlayersStats('team1', second), |
|
|
|
|
fetchPlayersStats('team2', second), |
|
|
|
|
]) |
|
|
|
|
isTeam1Selected |
|
|
|
|
? fetchPlayersStats('team1', second) |
|
|
|
|
: fetchPlayersStats('team2', second), |
|
|
|
|
]) as [Array<DataItem> | null, PlayersStats | null] |
|
|
|
|
|
|
|
|
|
const team1Players = find(res1, { team_id: matchProfile?.team1.id })?.players || [] |
|
|
|
|
const team2Players = find(res1, { team_id: matchProfile?.team2.id })?.players || [] |
|
|
|
|
@ -168,19 +180,20 @@ export const usePlayersStats = ({ |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
setPlayersStats({ |
|
|
|
|
...(matchProfile?.team1.id && res2 && { |
|
|
|
|
...(isTeam1Selected && res2 && { |
|
|
|
|
[matchProfile.team1.id]: needUseFakeData |
|
|
|
|
? fakeData.playersStats as unknown as PlayersStats |
|
|
|
|
: res2, |
|
|
|
|
}), |
|
|
|
|
...(matchProfile?.team2.id && res3 && { |
|
|
|
|
...(!isTeam1Selected && res2 && { |
|
|
|
|
[matchProfile.team2.id]: needUseFakeData |
|
|
|
|
? fakeData.playersStats as unknown as PlayersStats |
|
|
|
|
: res3, |
|
|
|
|
: res2, |
|
|
|
|
}), |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
setIsPlayersStatsFetching(false) |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, REQUEST_DELAY), [ |
|
|
|
|
selectedPlaylist?.id, |
|
|
|
|
fetchPlayers, |
|
|
|
|
@ -189,10 +202,12 @@ export const usePlayersStats = ({ |
|
|
|
|
matchProfile?.team1.id, |
|
|
|
|
matchProfile?.team2.id, |
|
|
|
|
matchProfile?.live, |
|
|
|
|
matchProfile?.c_match_calc_status, |
|
|
|
|
setIsPlayersStatsFetching, |
|
|
|
|
getParams, |
|
|
|
|
fakeData, |
|
|
|
|
selectedTab, |
|
|
|
|
selectedStatsTable, |
|
|
|
|
isCurrentStats, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
const beforeCloseTourCallback = () => { |
|
|
|
|
@ -233,7 +248,6 @@ export const usePlayersStats = ({ |
|
|
|
|
return { |
|
|
|
|
beforeCloseTourCallback, |
|
|
|
|
getParams, |
|
|
|
|
isEmptyPlayersStats, |
|
|
|
|
playersData, |
|
|
|
|
playersStats, |
|
|
|
|
} |
|
|
|
|
|