refactor: use suspense queries to fetch page data

This commit is contained in:
Prayag Prajapati committed 2025-05-21 23:12:17 +05:30
1 parent dfb8eeee12
commit 18d55a4af6
27 files changed
+2140 -1493

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
import { type MotionValue, useTransform } from "framer-motion";
import { type MotionValue, useTransform } from "motion/react";
export default function useParallax(
value: MotionValue<number>,
+68
View File
@@ -0,0 +1,68 @@
import type { Api } from "@jellyfin/sdk";
import { BaseItemKind, ItemFields } from "@jellyfin/sdk/lib/generated-client";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
import { queryOptions } from "@tanstack/react-query";
/**
* @param id itemId
* @param api api instance
* @param userId user's id
* @returns query options for the item
* @description This function returns the query options for the item. It uses the getUserLibraryApi to get the item data.
*/
export const getItemQueryOptions = (
id: string,
api: Api | null | undefined,
userId: string | null | undefined,
) =>
queryOptions({
queryKey: ["item", id],
queryFn: async () => {
if (!api || !userId) {
throw Error("API or User ID is not defined");
}
const result = await getUserLibraryApi(api).getItem({
userId: userId,
itemId: id,
});
return result.data;
},
});
/**
* @param api api instance
* @param userId user's id
* @returns query options for the latest items
* @description This function returns the query options for the latest items. It uses the getUserLibraryApi to get the latest items data.
*/
export const getLatestItemsQueryOptions = (
api: Api | null | undefined,
userId: string | null | undefined,
) =>
queryOptions({
queryKey: ["home", "latestMedia"],
queryFn: async () => {
if (!api || !userId) {
throw Error("API or User ID is not defined");
}
const media = await getUserLibraryApi(api).getLatestMedia({
userId: userId,
fields: [
ItemFields.Overview,
ItemFields.ParentId,
ItemFields.SeasonUserData,
ItemFields.IsHd,
ItemFields.MediaStreams,
ItemFields.MediaSources,
],
includeItemTypes: [
BaseItemKind.Movie,
BaseItemKind.Series,
BaseItemKind.MusicAlbum,
],
enableUserData: true,
enableImages: true,
});
return media.data;
},
});
+26
View File
@@ -0,0 +1,26 @@
import type { Api } from "@jellyfin/sdk";
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
import { queryOptions } from "@tanstack/react-query";
/**
* @param api Api instance
* @param userId user's id
* @param id library id
* @returns query options for the current library
*/
export const getLibraryQueryOptions = (
api: Api | null | undefined,
userId: string | null | undefined,
id: string,
) =>
queryOptions({
queryKey: ["library", "currentLib", id],
queryFn: async () => {
if (!api || !userId) throw Error("API or User ID is not defined");
const result = await getUserLibraryApi(api).getItem({
userId: userId,
itemId: id,
});
return result.data;
},
});