All files / src/hooks useLockScreenSkip.ts

100% Statements 11/11
100% Branches 2/2
80% Functions 4/5
100% Lines 9/9

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                          2x 42x 13x 2x 3x 3x   2x 2x 2x      
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { useEffect } from "react";
import { mpvSetSkipIntervals } from "@/lib/tauri";
import { isMobilePlatform } from "@/lib/platform";
import { readSkipIntervals, SKIP_INTERVALS_EVENT } from "@/lib/player-prefs";
 
/**
 * Keeps the lock screen's skip buttons jumping the lengths set in Settings.
 * Lives at the top of the app, since audio keeps playing with the player
 * closed and the lengths can change in Settings meanwhile.
 */
export const useLockScreenSkip = () => {
	useEffect(() => {
		if (!isMobilePlatform()) return;
		const send = () => {
			const { back, forward } = readSkipIntervals();
			mpvSetSkipIntervals(back, forward).catch(() => {});
		};
		send();
		window.addEventListener(SKIP_INTERVALS_EVENT, send);
		return () => window.removeEventListener(SKIP_INTERVALS_EVENT, send);
	}, []);
};