mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-10 23:55:05 +01:00
progress restoring logic
This commit is contained in:
parent
f656f80996
commit
f37bec7a7a
@ -7,6 +7,25 @@ interface Props {
|
||||
onProgress?: (time: number, duration: number) => void;
|
||||
}
|
||||
|
||||
const FIVETEEN_MINUTES = 15 * 60;
|
||||
const FIVE_MINUTES = 5 * 60;
|
||||
|
||||
function shouldRestoreTime(time: number, duration: number): boolean {
|
||||
const timeFromEnd = Math.max(0, duration - time);
|
||||
|
||||
// short movie
|
||||
if (duration < FIVETEEN_MINUTES) {
|
||||
if (time < 5) return false;
|
||||
if (timeFromEnd < 60) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// long movie
|
||||
if (time < 30) return false;
|
||||
if (timeFromEnd < FIVE_MINUTES) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function ProgressListenerControl(props: Props) {
|
||||
const { videoState } = useVideoPlayerState();
|
||||
const didInitialize = useRef<true | null>(null);
|
||||
@ -31,7 +50,12 @@ export function ProgressListenerControl(props: Props) {
|
||||
useEffect(() => {
|
||||
if (didInitialize.current) return;
|
||||
if (!videoState.hasInitialized || Number.isNaN(videoState.duration)) return;
|
||||
if (props.startAt !== undefined) videoState.setTime(props.startAt);
|
||||
if (
|
||||
props.startAt !== undefined &&
|
||||
shouldRestoreTime(props.startAt, videoState.duration)
|
||||
) {
|
||||
videoState.setTime(props.startAt);
|
||||
}
|
||||
didInitialize.current = true;
|
||||
}, [didInitialize, videoState, props]);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user