You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
spa_instat_tv/src/features/MatchPage/components/LiveMatch/hooks/index.tsx

77 lines
2.1 KiB

import { useMemo } from 'react'
import { API_ROOT } from 'config'
import { usePageParams } from 'hooks/usePageParams'
import { useMatchPageStore } from 'features/MatchPage/store'
import { usePlayerProgressReporter } from './usePlayerProgressReporter'
import { useResumeUrlParam } from './useResumeUrlParam'
import { useChapters } from './useChapters'
import { usePlaylistLogger } from './usePlaylistLogger'
export const useLiveMatch = () => {
const {
handlePlaylistClick,
profile,
selectedPlaylist,
setFullMatchPlaylistDuration,
} = useMatchPageStore()
const { profileId: matchId, sportType } = usePageParams()
const resume = useResumeUrlParam()
const fromStartIfStreamPaused = useMemo(
() => (profile && !profile.live ? 0 : undefined),
// deps намеренно оставляем пустым,
// не нужно реагировать на изменение live когда пользователь смотрит матч
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
)
const { chapters } = useChapters({
selectedPlaylist,
url: `${API_ROOT}/video/stream/${sportType}/${matchId}.m3u8`,
})
const {
logPlaylistChange,
onPlayingChange: notifyPlaylistLogger,
} = usePlaylistLogger()
const {
onPlayerProgressChange,
onPlayingChange: notifyProgressLogger,
} = usePlayerProgressReporter()
const onDurationChange = (duration: number) => {
if (profile?.live) return
setFullMatchPlaylistDuration(duration)
}
const onPlayingChange = (playing: boolean) => {
notifyPlaylistLogger(playing)
notifyProgressLogger(playing)
}
const onPlaylistSelect: typeof handlePlaylistClick = (playlist, e) => {
if (selectedPlaylist) {
logPlaylistChange(selectedPlaylist)
}
handlePlaylistClick(playlist, e)
}
return {
chapters,
onDurationChange,
onPlayerProgressChange,
onPlayingChange,
onPlaylistSelect,
resume: resume ?? fromStartIfStreamPaused,
selectedPlaylist,
streamUrl: (
profile?.playbackUrl
|| `${API_ROOT}/video/stream/${sportType}/${matchId}.m3u8`
),
}
}