Ott 1701 part 2 (#553)
* refactor(1725): video width 100% * refactor(1701): building playlists and chapterskeep-around/fdb88b04b32b9392e76795099e2ec47c9856b38b
parent
95d0194c5b
commit
950a02c7ae
@ -0,0 +1,66 @@ |
||||
import last from 'lodash/last' |
||||
import reduce from 'lodash/reduce' |
||||
import concat from 'lodash/concat' |
||||
|
||||
import type { Episodes } from 'requests/getMatchPlaylists' |
||||
|
||||
import type { Chapters } from 'features/StreamPlayer/types' |
||||
|
||||
import type { MatchPlaylistOption, PlaylistOption } from '../../types' |
||||
import { FULL_GAME_KEY } from '../../helpers/buildPlaylists' |
||||
|
||||
const getFullMatchChapters = (url: string, playlist: MatchPlaylistOption) => { |
||||
const duration = (playlist.duration ?? 0) * 1000 |
||||
return [ |
||||
{ |
||||
duration, |
||||
endMs: duration, |
||||
endOffsetMs: duration, |
||||
index: 0, |
||||
startMs: 0, |
||||
startOffsetMs: 0, |
||||
url, |
||||
}, |
||||
] |
||||
} |
||||
|
||||
const getPlaylistChapters = (url: string, episodes: Episodes) => reduce( |
||||
episodes, |
||||
( |
||||
acc: Chapters, |
||||
episode, |
||||
index, |
||||
) => { |
||||
if (episode.s >= episode.e) return acc |
||||
|
||||
const episodeDuration = (episode.e - episode.s) * 1000 |
||||
const prevVideoEndMs = last(acc)?.endMs || 0 |
||||
const nextChapter = { |
||||
duration: episodeDuration, |
||||
endMs: prevVideoEndMs + episodeDuration, |
||||
endOffsetMs: episode.e * 1000, |
||||
index, |
||||
startMs: prevVideoEndMs, |
||||
startOffsetMs: episode.s * 1000, |
||||
url, |
||||
} |
||||
return concat(acc, nextChapter) |
||||
}, |
||||
[], |
||||
) |
||||
|
||||
type Args = { |
||||
selectedPlaylist?: PlaylistOption, |
||||
url: string, |
||||
} |
||||
|
||||
export const buildChapters = ({ |
||||
selectedPlaylist, |
||||
url, |
||||
}: Args): Chapters => { |
||||
if (!selectedPlaylist) return [] |
||||
if (selectedPlaylist.id === FULL_GAME_KEY) { |
||||
return getFullMatchChapters(url, selectedPlaylist) |
||||
} |
||||
return getPlaylistChapters(url, selectedPlaylist.episodes) |
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
import { useMemo } from 'react' |
||||
|
||||
import type { PlaylistOption } from 'features/MatchPage/types' |
||||
|
||||
import { buildChapters } from '../helpers' |
||||
|
||||
type Args = { |
||||
selectedPlaylist?: PlaylistOption, |
||||
url: string, |
||||
} |
||||
|
||||
export const useChapters = ({ |
||||
selectedPlaylist, |
||||
url, |
||||
}: Args) => { |
||||
const chapters = useMemo( |
||||
() => buildChapters({ |
||||
selectedPlaylist, |
||||
url, |
||||
}), |
||||
[ |
||||
selectedPlaylist, |
||||
url, |
||||
], |
||||
) |
||||
|
||||
return { chapters } |
||||
} |
||||
@ -0,0 +1,23 @@ |
||||
import { useMemo } from 'react' |
||||
import { useLocation } from 'react-router' |
||||
|
||||
import isNumber from 'lodash/isNumber' |
||||
|
||||
export const RESUME_KEY = 'resume' |
||||
|
||||
const readResumeParam = (search: string) => { |
||||
const params = new URLSearchParams(search) |
||||
const rawValue = params.get(RESUME_KEY) |
||||
if (!rawValue) return undefined |
||||
|
||||
const value = JSON.parse(rawValue) |
||||
return isNumber(value) ? value : 0 |
||||
} |
||||
|
||||
export const useResumeUrlParam = () => { |
||||
const { search } = useLocation() |
||||
|
||||
const resume = useMemo(() => readResumeParam(search), [search]) |
||||
|
||||
return resume |
||||
} |
||||
Loading…
Reference in new issue