1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/hooks/api/liveTvHooks/useGetChannel.ts
2024-09-08 20:12:37 +03:00

40 lines
1.3 KiB
TypeScript

import type { AxiosRequestConfig } from 'axios';
import type { LiveTvApiGetChannelRequest } from '@jellyfin/sdk/lib/generated-client';
import { getLiveTvApi } from '@jellyfin/sdk/lib/utils/api/live-tv-api';
import { queryOptions, useQuery } from '@tanstack/react-query';
import { type JellyfinApiContext, useApi } from 'hooks/useApi';
const getChannel = async (
apiContext: JellyfinApiContext,
params: LiveTvApiGetChannelRequest,
options?: AxiosRequestConfig
) => {
const { api, user } = apiContext;
if (!api) throw new Error('No API instance available');
if (!user?.Id) throw new Error('No User ID provided');
const response = await getLiveTvApi(api).getChannel(
{
userId: user.Id,
...params
},
options
);
return response.data;
};
export const getChannelQuery = (
apiContext: JellyfinApiContext,
params: LiveTvApiGetChannelRequest
) => queryOptions({
queryKey: ['Channel', params.channelId],
queryFn: ({ signal }) => getChannel(apiContext, params, { signal }),
enabled: !!apiContext.api && !!apiContext.user?.Id && !!params.channelId
});
export const useGetChannel = (
params: LiveTvApiGetChannelRequest
) => {
const apiContext = useApi();
return useQuery(getChannelQuery(apiContext, params));
};