1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Refactor query keys to enum

This commit is contained in:
viown 2025-02-23 17:38:31 +03:00
parent dd539b89ca
commit a7621d242d
7 changed files with 18 additions and 15 deletions

View file

@ -0,0 +1,4 @@
export enum QueryKey {
Task = 'Task',
Tasks = 'Tasks'
};

View file

@ -3,7 +3,7 @@ import { getScheduledTasksApi } from '@jellyfin/sdk/lib/utils/api/scheduled-task
import { useMutation } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import { queryClient } from 'utils/query/queryClient'; import { queryClient } from 'utils/query/queryClient';
import { QUERY_KEY } from './useTasks'; import { QueryKey } from './queryKey';
export const useStartTask = () => { export const useStartTask = () => {
const { api } = useApi(); const { api } = useApi();
@ -15,7 +15,7 @@ export const useStartTask = () => {
), ),
onSuccess: () => { onSuccess: () => {
void queryClient.invalidateQueries({ void queryClient.invalidateQueries({
queryKey: [ QUERY_KEY ] queryKey: [ QueryKey.Tasks ]
}); });
} }
}); });

View file

@ -3,7 +3,7 @@ import { getScheduledTasksApi } from '@jellyfin/sdk/lib/utils/api/scheduled-task
import { useMutation } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import { queryClient } from 'utils/query/queryClient'; import { queryClient } from 'utils/query/queryClient';
import { QUERY_KEY } from './useTasks'; import { QueryKey } from './queryKey';
export const useStopTask = () => { export const useStopTask = () => {
const { api } = useApi(); const { api } = useApi();
@ -15,7 +15,7 @@ export const useStopTask = () => {
), ),
onSuccess: () => { onSuccess: () => {
void queryClient.invalidateQueries({ void queryClient.invalidateQueries({
queryKey: [ QUERY_KEY ] queryKey: [ QueryKey.Tasks ]
}); });
} }
}); });

View file

@ -5,8 +5,7 @@ import { getScheduledTasksApi } from '@jellyfin/sdk/lib/utils/api/scheduled-task
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import { QueryKey } from './queryKey';
export const QUERY_KEY = 'Task';
const fetchTask = async ( const fetchTask = async (
api: Api, api: Api,
@ -22,7 +21,7 @@ export const useTask = (params: ScheduledTasksApiGetTaskRequest) => {
const { api } = useApi(); const { api } = useApi();
return useQuery({ return useQuery({
queryKey: [ QUERY_KEY, params.taskId ], queryKey: [ QueryKey.Task, params.taskId ],
queryFn: ({ signal }) => queryFn: ({ signal }) =>
fetchTask(api!, params, { signal }), fetchTask(api!, params, { signal }),
enabled: !!api enabled: !!api

View file

@ -5,8 +5,7 @@ import { getScheduledTasksApi } from '@jellyfin/sdk/lib/utils/api/scheduled-task
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import { QueryKey } from './queryKey';
export const QUERY_KEY = 'Tasks';
const fetchTasks = async ( const fetchTasks = async (
api: Api, api: Api,
@ -22,7 +21,7 @@ export const useTasks = (params?: ScheduledTasksApiGetTasksRequest) => {
const { api } = useApi(); const { api } = useApi();
return useQuery({ return useQuery({
queryKey: [ QUERY_KEY ], queryKey: [ QueryKey.Tasks ],
queryFn: ({ signal }) => queryFn: ({ signal }) =>
fetchTasks(api!, params, { signal }), fetchTasks(api!, params, { signal }),
enabled: !!api enabled: !!api

View file

@ -3,7 +3,7 @@ import { getScheduledTasksApi } from '@jellyfin/sdk/lib/utils/api/scheduled-task
import { useMutation } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import { queryClient } from 'utils/query/queryClient'; import { queryClient } from 'utils/query/queryClient';
import { QUERY_KEY } from './useTask'; import { QueryKey } from './queryKey';
export const useUpdateTask = () => { export const useUpdateTask = () => {
const { api } = useApi(); const { api } = useApi();
@ -15,7 +15,7 @@ export const useUpdateTask = () => {
), ),
onSuccess: (_data, params) => { onSuccess: (_data, params) => {
void queryClient.invalidateQueries({ void queryClient.invalidateQueries({
queryKey: [ QUERY_KEY, params.taskId ] queryKey: [ QueryKey.Task, params.taskId ]
}); });
} }
}); });

View file

@ -3,7 +3,7 @@ import Page from 'components/Page';
import globalize from 'lib/globalize'; import globalize from 'lib/globalize';
import Box from '@mui/material/Box'; import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack'; import Stack from '@mui/material/Stack';
import { QUERY_KEY, useTasks } from '../../features/tasks/api/useTasks'; import { useTasks } from '../../features/tasks/api/useTasks';
import { getCategories, getTasksByCategory } from '../../features/tasks/utils/tasks'; import { getCategories, getTasksByCategory } from '../../features/tasks/utils/tasks';
import Loading from 'components/loading/LoadingComponent'; import Loading from 'components/loading/LoadingComponent';
import Tasks from '../../features/tasks/components/Tasks'; import Tasks from '../../features/tasks/components/Tasks';
@ -14,6 +14,7 @@ import Events, { Event } from 'utils/events';
import { ApiClient } from 'jellyfin-apiclient'; import { ApiClient } from 'jellyfin-apiclient';
import { useApi } from 'hooks/useApi'; import { useApi } from 'hooks/useApi';
import { queryClient } from 'utils/query/queryClient'; import { queryClient } from 'utils/query/queryClient';
import { QueryKey } from 'apps/dashboard/features/tasks/api/queryKey';
export const Component = () => { export const Component = () => {
const { __legacyApiClient__ } = useApi(); const { __legacyApiClient__ } = useApi();
@ -22,13 +23,13 @@ export const Component = () => {
// TODO: Replace usage of the legacy apiclient when websocket support is added to the TS SDK. // TODO: Replace usage of the legacy apiclient when websocket support is added to the TS SDK.
useEffect(() => { useEffect(() => {
const onScheduledTasksUpdate = (_e: Event, _apiClient: ApiClient, info: TaskInfo[]) => { const onScheduledTasksUpdate = (_e: Event, _apiClient: ApiClient, info: TaskInfo[]) => {
queryClient.setQueryData([ QUERY_KEY ], info); queryClient.setQueryData([ QueryKey.Tasks ], info);
}; };
const fallbackInterval = setInterval(() => { const fallbackInterval = setInterval(() => {
if (!__legacyApiClient__?.isMessageChannelOpen()) { if (!__legacyApiClient__?.isMessageChannelOpen()) {
void queryClient.invalidateQueries({ void queryClient.invalidateQueries({
queryKey: [ QUERY_KEY ] queryKey: [ QueryKey.Tasks ]
}); });
} }
}, 1e4); }, 1e4);