mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-01-15 23:59:08 +00:00
refactor: optimize segment handling with useMemo and improve skip function fallback
This commit is contained in:
@@ -4,7 +4,14 @@ import type {
|
|||||||
MediaSourceInfo,
|
MediaSourceInfo,
|
||||||
} from "@jellyfin/sdk/lib/generated-client";
|
} from "@jellyfin/sdk/lib/generated-client";
|
||||||
import { useLocalSearchParams } from "expo-router";
|
import { useLocalSearchParams } from "expo-router";
|
||||||
import { type FC, useCallback, useEffect, useRef, useState } from "react";
|
import {
|
||||||
|
type FC,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { StyleSheet, useWindowDimensions, View } from "react-native";
|
import { StyleSheet, useWindowDimensions, View } from "react-native";
|
||||||
import Animated, {
|
import Animated, {
|
||||||
@@ -43,6 +50,9 @@ import { useControlsTimeout } from "./useControlsTimeout";
|
|||||||
import { PlaybackSpeedScope } from "./utils/playback-speed-settings";
|
import { PlaybackSpeedScope } from "./utils/playback-speed-settings";
|
||||||
import { type AspectRatio } from "./VideoScalingModeSelector";
|
import { type AspectRatio } from "./VideoScalingModeSelector";
|
||||||
|
|
||||||
|
// No-op function to avoid creating new references on every render
|
||||||
|
const noop = () => {};
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
item: BaseItemDto;
|
item: BaseItemDto;
|
||||||
isPlaying: boolean;
|
isPlaying: boolean;
|
||||||
@@ -315,7 +325,7 @@ export const Controls: FC<Props> = ({
|
|||||||
|
|
||||||
// Fetch all segments for the current item
|
// Fetch all segments for the current item
|
||||||
const { data: segments } = useSegments(
|
const { data: segments } = useSegments(
|
||||||
item.Id!,
|
item.Id ?? "",
|
||||||
offline,
|
offline,
|
||||||
downloadedFiles,
|
downloadedFiles,
|
||||||
api,
|
api,
|
||||||
@@ -388,7 +398,7 @@ export const Controls: FC<Props> = ({
|
|||||||
|
|
||||||
// Determine which segment button to show (priority order)
|
// Determine which segment button to show (priority order)
|
||||||
// Commercial > Recap > Intro > Preview > Outro
|
// Commercial > Recap > Intro > Preview > Outro
|
||||||
const activeSegment = (() => {
|
const activeSegment = useMemo(() => {
|
||||||
if (commercialSkipper.currentSegment)
|
if (commercialSkipper.currentSegment)
|
||||||
return { type: "Commercial", ...commercialSkipper };
|
return { type: "Commercial", ...commercialSkipper };
|
||||||
if (recapSkipper.currentSegment) return { type: "Recap", ...recapSkipper };
|
if (recapSkipper.currentSegment) return { type: "Recap", ...recapSkipper };
|
||||||
@@ -397,14 +407,25 @@ export const Controls: FC<Props> = ({
|
|||||||
return { type: "Preview", ...previewSkipper };
|
return { type: "Preview", ...previewSkipper };
|
||||||
if (outroSkipper.currentSegment) return { type: "Outro", ...outroSkipper };
|
if (outroSkipper.currentSegment) return { type: "Outro", ...outroSkipper };
|
||||||
return null;
|
return null;
|
||||||
})();
|
}, [
|
||||||
|
commercialSkipper.currentSegment,
|
||||||
|
recapSkipper.currentSegment,
|
||||||
|
introSkipper.currentSegment,
|
||||||
|
previewSkipper.currentSegment,
|
||||||
|
outroSkipper.currentSegment,
|
||||||
|
commercialSkipper,
|
||||||
|
recapSkipper,
|
||||||
|
introSkipper,
|
||||||
|
previewSkipper,
|
||||||
|
outroSkipper,
|
||||||
|
]);
|
||||||
|
|
||||||
// Legacy compatibility: map to old variable names
|
// Legacy compatibility: map to old variable names
|
||||||
const showSkipButton = !!(
|
const showSkipButton = !!(
|
||||||
activeSegment &&
|
activeSegment &&
|
||||||
["Intro", "Recap", "Commercial", "Preview"].includes(activeSegment.type)
|
["Intro", "Recap", "Commercial", "Preview"].includes(activeSegment.type)
|
||||||
);
|
);
|
||||||
const skipIntro = activeSegment?.skipSegment || (() => {});
|
const skipIntro = activeSegment?.skipSegment || noop;
|
||||||
const showSkipCreditButton = activeSegment?.type === "Outro";
|
const showSkipCreditButton = activeSegment?.type === "Outro";
|
||||||
const skipCredit = outroSkipper.skipSegment;
|
const skipCredit = outroSkipper.skipSegment;
|
||||||
const hasContentAfterCredits =
|
const hasContentAfterCredits =
|
||||||
|
|||||||
Reference in New Issue
Block a user