mirror of
https://github.com/streamyfin/streamyfin.git
synced 2025-08-20 18:37:18 +02:00
Co-authored-by: lostb1t <coding-mosses0z@icloud.com> Co-authored-by: Fredrik Burmester <fredrik.burmester@gmail.com> Co-authored-by: Gauvain <68083474+Gauvino@users.noreply.github.com> Co-authored-by: Gauvino <uruknarb20@gmail.com> Co-authored-by: storm1er <le.storm1er@gmail.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Chris <182387676+whoopsi-daisy@users.noreply.github.com> Co-authored-by: arch-fan <55891793+arch-fan@users.noreply.github.com> Co-authored-by: Alex Kim <alexkim@Alexs-MacBook-Pro.local>
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { useCallback, useEffect, useState } from "react";
|
|
import { useSegments } from "@/utils/segments";
|
|
import { msToSeconds, secondsToMs } from "@/utils/time";
|
|
import { useHaptic } from "./useHaptic";
|
|
|
|
export const useCreditSkipper = (
|
|
itemId: string,
|
|
currentTime: number,
|
|
seek: (time: number) => void,
|
|
play: () => void,
|
|
isVlc = false,
|
|
isOffline = false,
|
|
) => {
|
|
const [showSkipCreditButton, setShowSkipCreditButton] = useState(false);
|
|
const lightHapticFeedback = useHaptic("light");
|
|
|
|
if (isVlc) {
|
|
currentTime = msToSeconds(currentTime);
|
|
}
|
|
|
|
const wrappedSeek = (seconds: number) => {
|
|
if (isVlc) {
|
|
seek(secondsToMs(seconds));
|
|
return;
|
|
}
|
|
seek(seconds);
|
|
};
|
|
|
|
const { data: segments } = useSegments(itemId, isOffline);
|
|
const creditTimestamps = segments?.creditSegments?.[0];
|
|
|
|
useEffect(() => {
|
|
if (creditTimestamps) {
|
|
setShowSkipCreditButton(
|
|
currentTime > creditTimestamps.startTime &&
|
|
currentTime < creditTimestamps.endTime,
|
|
);
|
|
}
|
|
}, [creditTimestamps, currentTime]);
|
|
|
|
const skipCredit = useCallback(() => {
|
|
if (!creditTimestamps) return;
|
|
try {
|
|
lightHapticFeedback();
|
|
wrappedSeek(creditTimestamps.endTime);
|
|
setTimeout(() => {
|
|
play();
|
|
}, 200);
|
|
} catch (error) {
|
|
console.error("Error skipping credit", error);
|
|
}
|
|
}, [creditTimestamps, lightHapticFeedback, wrappedSeek, play]);
|
|
|
|
return { showSkipCreditButton, skipCredit };
|
|
};
|