Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | 3x 1716x 3x 1694x 3x 2975x 2975x 2975x 2975x 2975x 2975x 2975x 2975x 2975x 2975x 2975x 12x 2963x 3x 2975x 2975x 2975x 2975x 2975x 2975x 4x 2975x 2975x 2975x 2975x 353x 53x 53x 53x 53x 53x 53x 2975x 1446x 1112x 1112x 1112x 1112x 16x 16x 11x 16x 1112x 1112x 327x 327x 1112x 235x 235x 53x 53x 34x 34x 34x 28x 28x 28x 28x 28x 19x 13x 13x 2975x 1357x 7x 2975x 330x 28x 28x 2975x 22x 22x 22x 4x 4x 4x 4x 4x 18x 11x 11x 11x 11x 1x 10x 10x 11x 2975x 4x 4x 4x 4x 4x 4x 4x 2975x 2x 2x 2x 2x 2x 2x 2975x | // SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
import { useCallback, useEffect, useRef, useState } from "react";
import type { SkipSegmentModes } from "@/lib/player-prefs";
import {
extendRun,
heldInAutoSkip,
MAX_PLAY_STEP,
MIN_AUTO_SKIP,
MIN_SKIP_AFTER_HOLD,
NOTICE_MS,
OFFER_SECONDS,
reachedAutoSkip,
segmentAt,
segmentGroup,
segmentKey,
SKIP_SETTLE_MS,
skipActionFor,
skipLabel,
skippedLabel,
type PlayRun,
} from "@/lib/skip-segments";
import type { SkipSegment, SkipSegmentKind } from "@/lib/types";
import { useLatestRef } from "@/hooks/useLatestRef";
interface SegmentSkippingOptions {
/** Tells videos apart, from `useSkipSegments`. A different one starts over. */
video: string | null;
/** The video's segments, from `useSkipSegments`. */
segments: readonly SkipSegment[];
position: number;
duration: number;
/** Whether the stream is reconnecting, when mpv's position isn't where the viewer is. */
reconnecting: boolean;
/**
* Whether the video is playing, rather than paused or loading, with nothing
* open over the prompt. The player doesn't skip on its own while it's false.
*/
playing: boolean;
modes: SkipSegmentModes;
/** Whether the player goes on to the next episode at the end. */
autoplay: boolean;
hasNext: boolean;
seek: (to: number) => void;
onNextEpisode: () => void;
}
/** A segment the player offers to skip with a button. */
export interface SkipOffer {
label: string;
/** Whether the button goes to the next episode rather than further into this one. */
next: boolean;
/**
* True for the first few seconds of the segment, counted from where
* playback last started in it. After that the button only shows with the
* controls.
*/
fresh: boolean;
/** The video's length is off from the one the times were made for, so they may be too. */
unsure: boolean;
}
/**
* Whether the Skip button is on screen. After its first few seconds it only
* shows with the controls.
*/
export const offerShows = (offer: SkipOffer | null, controlsVisible: boolean): boolean =>
offer !== null && (offer.fresh || controlsVisible);
/**
* Whether the prompt shows a button that skips: the countdown's, or the Skip
* button. The "Skipped intro" notice covers the Skip button of a segment
* right after the skipped one, as the prompt shows one thing at a time.
*/
export const skipButtonShows = (
{ offer, notice, countdown }: Pick<SegmentSkipping, "offer" | "notice" | "countdown">,
controlsVisible: boolean
): boolean => countdown !== null || (notice === null && offerShows(offer, controlsVisible));
/** Shown for a moment after the player skips a segment on its own. */
export interface SkipNotice {
label: string;
}
/** A countdown to the next episode, started at the credits. */
export interface NextCountdown {
/** The label of the button that stops it, such as "Watch credits". */
cancelLabel: string;
}
export interface SegmentSkipping {
offer: SkipOffer | null;
notice: SkipNotice | null;
countdown: NextCountdown | null;
/**
* Does what the countdown's or the offer's button says, even when the
* offer's button is hidden. Returns false when neither is up, so there's
* nothing to skip.
*/
skip: () => boolean;
/** Goes back to where the player skipped from. */
undo: () => void;
/** Stops the countdown and stays on the credits. */
cancelCountdown: () => void;
}
/**
* Skips intros, recaps, credits and previews as Settings says. With "button"
* a Skip button shows while the segment plays. With "auto" the player skips
* on its own once playback runs into the segment, and offers to undo it. At
* credits that run to the end, with autoplay on, it counts down to the next
* episode instead. "auto" falls back to the button when the times aren't
* sure enough, or when the segment was reached by seeking into it.
*
* Each segment is skipped on its own at most once per video, so going back
* to watch it works.
*/
export const useSegmentSkipping = ({
video,
segments,
position,
duration,
reconnecting,
playing,
modes,
autoplay,
hasNext,
seek,
onNextEpisode,
}: SegmentSkippingOptions): SegmentSkipping => {
const videoRef = useRef(video);
const runRef = useRef<PlayRun | null>(null);
// When the current run began, to tell a seek made after a skip.
const runStartedAtRef = useRef(0);
// Where the current run began in the video, for `fresh`.
const [runStart, setRunStart] = useState<number | null>(null);
// Where the player's own skip lands, until playback gets there.
const landingRef = useRef<number | null>(null);
// Segments the player has skipped on its own, or started a countdown at.
const doneRef = useRef(new Set<string>());
const [skipped, setSkipped] = useState<{ key: string; at: number } | null>(null);
const [notice, setNotice] = useState<{
key: string;
kind: SkipSegmentKind;
from: number;
} | null>(null);
const [countdown, setCountdown] = useState<{ key: string; kind: SkipSegmentKind } | null>(null);
// The position polled while the stream last reconnected. The reconnect can
// end before the next poll, so until a poll moves off it, it's still unknown.
const [reconnectPosition, setReconnectPosition] = useState<number | null>(null);
if (reconnecting) {
if (reconnectPosition !== position) setReconnectPosition(position);
} else if (reconnectPosition !== null && reconnectPosition !== position) {
setReconnectPosition(null);
}
// While a stream opens, mpv reports no length, and while it reconnects its
// position can be 0. Neither is where the viewer is, so those polls are
// left out.
const located = duration > 0 && !reconnecting && position !== reconnectPosition;
const current = located ? segmentAt(segments, position) : null;
const currentKey = current && segmentKey(current);
const mode = current ? modes[segmentGroup(current.kind)] : "off";
const action = current && mode !== "off" ? skipActionFor(current, duration, hasNext) : null;
// The countdown stops when playback leaves the credits, or when autoplay,
// the next episode or "auto" goes away. A reconnect doesn't stop it.
if (
countdown &&
located &&
(currentKey !== countdown.key || !autoplay || action?.type !== "next" || mode !== "auto")
) {
setCountdown(null);
}
const hidden =
currentKey !== null &&
(currentKey === skipped?.key ||
currentKey === countdown?.key ||
currentKey === notice?.key);
const offer: SkipOffer | null =
current && action && !hidden
? {
label: skipLabel(current.kind, action),
next: action.type === "next",
fresh: position - Math.max(current.start, runStart ?? 0) < OFFER_SECONDS,
unsure: current.fit === "mismatch",
}
: null;
// The latest values, for the callbacks below, which stay the same between renders.
const latestRef = useLatestRef({
currentKey,
action,
offer,
countdown,
notice,
seek,
onNextEpisode,
});
// A different video starts over. Losing the video for a moment, such as
// while the player reloads its stream, doesn't.
useEffect(() => {
if (video === null || video === videoRef.current) return;
videoRef.current = video;
doneRef.current = new Set();
landingRef.current = null;
setSkipped(null);
setNotice(null);
setCountdown(null);
}, [video]);
// Follow the stretch being played, and skip on its own when playback runs
// into a segment set to "auto".
useEffect(() => {
if (!located) return;
const last = runRef.current;
let run = extendRun(last, position);
// The player's own skip isn't a seek by the viewer, so playback goes on
// from where it began before the skip. That way a segment that starts
// where the skipped one ends can be skipped on its own too, and the
// landing doesn't count as a seek back into the skipped segment.
const landing = landingRef.current;
if (landing !== null) {
const jumped = last !== null && run.start !== last.start;
if (jumped && Math.abs(run.start - landing) <= MAX_PLAY_STEP) {
run = { start: last.start, last: position };
}
if (jumped || position >= landing) landingRef.current = null;
}
runRef.current = run;
if (run.start !== last?.start) {
runStartedAtRef.current = Date.now();
setRunStart(run.start);
}
if (!playing || !current || mode !== "auto") return;
const key = segmentKey(current);
if (doneRef.current.has(key) || !reachedAutoSkip(current, run, position)) return;
const action = skipActionFor(current, duration, hasNext);
if (action?.type === "seek") {
const to = current.autoTo ?? action.to;
const least = heldInAutoSkip(current, last, run) ? MIN_SKIP_AFTER_HOLD : MIN_AUTO_SKIP;
if (to - position < least) return;
doneRef.current.add(key);
landingRef.current = to;
latestRef.current.seek(to);
setSkipped({ key, at: Date.now() });
setNotice({ key, kind: current.kind, from: position });
} else if (action?.type === "next" && autoplay) {
doneRef.current.add(key);
setCountdown({ key, kind: current.kind });
}
}, [located, position, playing, current, mode, duration, hasNext, autoplay, latestRef]);
// A skipped segment gets its button back once playback has left it, or
// once a seek after the skip went back into it.
useEffect(() => {
if (!located || !skipped || Date.now() - skipped.at < SKIP_SETTLE_MS) return;
if (currentKey !== skipped.key || runStartedAtRef.current > skipped.at) setSkipped(null);
}, [located, skipped, currentKey, position]);
useEffect(() => {
if (!notice) return;
const timer = setTimeout(() => setNotice(null), NOTICE_MS);
return () => clearTimeout(timer);
}, [notice]);
// Each callback clears what it acted on in `latestRef` until the next render,
// so a second call in the same moment, such as Enter as the countdown
// ends, does nothing.
const skip = useCallback((): boolean => {
const now = latestRef.current;
const { currentKey, action, offer, countdown, seek, onNextEpisode } = now;
if (countdown) {
now.countdown = null;
setCountdown(null);
setSkipped({ key: countdown.key, at: Date.now() });
onNextEpisode();
return true;
}
if (!offer || currentKey === null || !action) return false;
now.offer = null;
doneRef.current.add(currentKey);
setSkipped({ key: currentKey, at: Date.now() });
if (action.type === "next") {
onNextEpisode();
} else {
landingRef.current = action.to;
seek(action.to);
}
return true;
}, [latestRef]);
const undo = useCallback(() => {
const now = latestRef.current;
const { notice, seek } = now;
Iif (!notice) return;
now.notice = null;
seek(notice.from);
setNotice(null);
setSkipped(null);
}, [latestRef]);
const cancelCountdown = useCallback(() => {
const now = latestRef.current;
const { countdown } = now;
Iif (!countdown) return;
now.countdown = null;
setCountdown(null);
setSkipped({ key: countdown.key, at: Date.now() });
}, [latestRef]);
return {
offer,
notice: notice && { label: skippedLabel(notice.kind) },
countdown: countdown && { cancelLabel: `Watch ${countdown.kind}` },
skip,
undo,
cancelCountdown,
};
};
|