mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Merge pull request #5437 from thornbill/fix-item-state
This commit is contained in:
commit
c2a5f1e05d
6 changed files with 144 additions and 93 deletions
|
@ -28,36 +28,6 @@ import { LibraryViewSettings, ParentId } from 'types/library';
|
|||
import { LibraryTab } from 'types/libraryTab';
|
||||
import { Section, SectionApiMethod, SectionType } from 'types/sections';
|
||||
|
||||
const fetchGetItem = async (
|
||||
currentApi: JellyfinApiContext,
|
||||
parentId: ParentId,
|
||||
options?: AxiosRequestConfig
|
||||
) => {
|
||||
const { api, user } = currentApi;
|
||||
if (api && user?.Id && parentId) {
|
||||
const response = await getUserLibraryApi(api).getItem(
|
||||
{
|
||||
userId: user.Id,
|
||||
itemId: parentId
|
||||
},
|
||||
{
|
||||
signal: options?.signal
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
}
|
||||
};
|
||||
|
||||
export const useGetItem = (parentId: ParentId) => {
|
||||
const currentApi = useApi();
|
||||
const isLivetv = parentId === 'livetv';
|
||||
return useQuery({
|
||||
queryKey: ['Item', parentId],
|
||||
queryFn: ({ signal }) => fetchGetItem(currentApi, parentId, { signal }),
|
||||
enabled: !!parentId && !isLivetv
|
||||
});
|
||||
};
|
||||
|
||||
const fetchGetItems = async (
|
||||
currentApi: JellyfinApiContext,
|
||||
parametersOptions: ItemsApiGetItemsRequest,
|
||||
|
|
41
src/hooks/useItem.ts
Normal file
41
src/hooks/useItem.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import type { Api } from '@jellyfin/sdk/lib/api';
|
||||
import { getUserLibraryApi } from '@jellyfin/sdk/lib/utils/api/user-library-api';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import type { AxiosRequestConfig } from 'axios';
|
||||
|
||||
import { queryOptions } from 'utils/query/queryOptions';
|
||||
|
||||
import { useApi } from './useApi';
|
||||
|
||||
const fetchItem = async (
|
||||
api?: Api,
|
||||
userId?: string,
|
||||
itemId?: string,
|
||||
options?: AxiosRequestConfig
|
||||
) => {
|
||||
if (!api) throw new Error('No API instance available');
|
||||
if (!itemId) throw new Error('No item ID provided');
|
||||
|
||||
const response = await getUserLibraryApi(api)
|
||||
.getItem({ userId, itemId }, options);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const getItemQuery = (
|
||||
api?: Api,
|
||||
userId?: string,
|
||||
itemId?: string
|
||||
) => queryOptions({
|
||||
queryKey: [ 'User', userId, 'Items', itemId ],
|
||||
queryFn: ({ signal }) => fetchItem(api, userId, itemId, { signal }),
|
||||
staleTime: 1000, // 1 second
|
||||
enabled: !!api && !!userId && !!itemId
|
||||
});
|
||||
|
||||
export const useItem = (
|
||||
itemId?: string
|
||||
) => {
|
||||
const apiContext = useApi();
|
||||
const { api, user } = apiContext;
|
||||
return useQuery(getItemQuery(api, user?.Id, itemId));
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue