This commit is contained in:
Fredrik Burmester
2024-10-06 13:03:16 +02:00
parent cc242a971f
commit 0233862fc1
13 changed files with 941 additions and 391 deletions

View File

@@ -7,69 +7,59 @@ import { useAtom } from "jotai";
import { apiAtom } from "@/providers/JellyfinProvider";
interface AdjacentEpisodesProps {
currentlyPlaying?: CurrentlyPlayingState | null;
item?: BaseItemDto | null;
}
export const useAdjacentEpisodes = ({
currentlyPlaying,
}: AdjacentEpisodesProps) => {
export const useAdjacentEpisodes = ({ item }: AdjacentEpisodesProps) => {
const [api] = useAtom(apiAtom);
const { data: previousItem } = useQuery({
queryKey: [
"previousItem",
currentlyPlaying?.item.ParentId,
currentlyPlaying?.item.IndexNumber,
],
queryKey: ["previousItem", item?.ParentId, item?.IndexNumber],
queryFn: async (): Promise<BaseItemDto | null> => {
if (
!api ||
!currentlyPlaying?.item.ParentId ||
currentlyPlaying?.item.IndexNumber === undefined ||
currentlyPlaying?.item.IndexNumber === null ||
currentlyPlaying.item.IndexNumber - 2 < 0
!item?.ParentId ||
item?.IndexNumber === undefined ||
item?.IndexNumber === null ||
item?.IndexNumber - 2 < 0
) {
console.log("No previous item");
return null;
}
const res = await getItemsApi(api).getItems({
parentId: currentlyPlaying.item.ParentId!,
startIndex: currentlyPlaying.item.IndexNumber! - 2,
parentId: item.ParentId!,
startIndex: item.IndexNumber! - 2,
limit: 1,
});
return res.data.Items?.[0] || null;
},
enabled: currentlyPlaying?.item.Type === "Episode",
enabled: item?.Type === "Episode",
});
const { data: nextItem } = useQuery({
queryKey: [
"nextItem",
currentlyPlaying?.item.ParentId,
currentlyPlaying?.item.IndexNumber,
],
queryKey: ["nextItem", item?.ParentId, item?.IndexNumber],
queryFn: async (): Promise<BaseItemDto | null> => {
if (
!api ||
!currentlyPlaying?.item.ParentId ||
currentlyPlaying?.item.IndexNumber === undefined ||
currentlyPlaying?.item.IndexNumber === null
!item?.ParentId ||
item?.IndexNumber === undefined ||
item?.IndexNumber === null
) {
console.log("No next item");
return null;
}
const res = await getItemsApi(api).getItems({
parentId: currentlyPlaying.item.ParentId!,
startIndex: currentlyPlaying.item.IndexNumber!,
parentId: item.ParentId!,
startIndex: item.IndexNumber!,
limit: 1,
});
return res.data.Items?.[0] || null;
},
enabled: currentlyPlaying?.item.Type === "Episode",
enabled: item?.Type === "Episode",
});
return { previousItem, nextItem };

View File

@@ -1,11 +1,9 @@
// hooks/useTrickplay.ts
import { useState, useCallback, useMemo, useRef } from "react";
import { Api } from "@jellyfin/sdk";
import { SharedValue } from "react-native-reanimated";
import { CurrentlyPlayingState } from "@/providers/PlaybackProvider";
import { useAtom } from "jotai";
import { apiAtom } from "@/providers/JellyfinProvider";
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import { useAtom } from "jotai";
import { useCallback, useMemo, useRef, useState } from "react";
interface TrickplayData {
Interval?: number;
@@ -28,21 +26,19 @@ interface TrickplayUrl {
url: string;
}
export const useTrickplay = (
currentlyPlaying?: CurrentlyPlayingState | null
) => {
export const useTrickplay = (item: BaseItemDto) => {
const [api] = useAtom(apiAtom);
const [trickPlayUrl, setTrickPlayUrl] = useState<TrickplayUrl | null>(null);
const lastCalculationTime = useRef(0);
const throttleDelay = 200; // 200ms throttle
const trickplayInfo = useMemo(() => {
if (!currentlyPlaying?.item.Id || !currentlyPlaying?.item.Trickplay) {
if (!item.Id || !item.Trickplay) {
return null;
}
const mediaSourceId = currentlyPlaying.item.Id;
const trickplayData = currentlyPlaying.item.Trickplay[mediaSourceId];
const mediaSourceId = item.Id;
const trickplayData = item.Trickplay[mediaSourceId];
if (!trickplayData) {
return null;
@@ -59,7 +55,7 @@ export const useTrickplay = (
data: trickplayData[firstResolution],
}
: null;
}, [currentlyPlaying]);
}, [item]);
const calculateTrickplayUrl = useCallback(
(progress: number) => {
@@ -69,7 +65,7 @@ export const useTrickplay = (
}
lastCalculationTime.current = now;
if (!trickplayInfo || !api || !currentlyPlaying?.item.Id) {
if (!trickplayInfo || !api || !item.Id) {
return null;
}
@@ -95,13 +91,13 @@ export const useTrickplay = (
const newTrickPlayUrl = {
x: rowInTile,
y: colInTile,
url: `${api.basePath}/Videos/${currentlyPlaying.item.Id}/Trickplay/${resolution}/${tileIndex}.jpg?api_key=${api.accessToken}`,
url: `${api.basePath}/Videos/${item.Id}/Trickplay/${resolution}/${tileIndex}.jpg?api_key=${api.accessToken}`,
};
setTrickPlayUrl(newTrickPlayUrl);
return newTrickPlayUrl;
},
[trickplayInfo, currentlyPlaying, api]
[trickplayInfo, item, api]
);
return { trickPlayUrl, calculateTrickplayUrl, trickplayInfo };