1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-26 22:56:23 -04:00

Convert Log Events to React Query

This commit is contained in:
Mark McDowall
2025-02-27 20:20:56 -08:00
parent 5d7c94f8e9
commit 5342416659
20 changed files with 585 additions and 261 deletions
+8 -6
View File
@@ -2,23 +2,25 @@ import { UndefinedInitialDataOptions, useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import fetchJson, {
ApiError,
apiRoot,
FetchJsonOptions,
} from 'Utilities/Fetch/fetchJson';
import getQueryPath from 'Utilities/Fetch/getQueryPath';
import getQueryString, { QueryParams } from 'Utilities/Fetch/getQueryString';
interface QueryOptions<T> extends FetchJsonOptions<unknown> {
export interface QueryOptions<T> extends FetchJsonOptions<unknown> {
queryParams?: QueryParams;
queryOptions?:
| Omit<UndefinedInitialDataOptions<T, ApiError>, 'queryKey' | 'queryFn'>
| undefined;
}
function useApiQuery<T>(options: QueryOptions<T>) {
const useApiQuery = <T>(options: QueryOptions<T>) => {
const requestOptions = useMemo(() => {
const { queryOptions, ...otherOptions } = options;
const { path: path, queryOptions, queryParams, ...otherOptions } = options;
return {
...otherOptions,
path: apiRoot + options.path,
path: getQueryPath(path) + getQueryString(queryParams),
headers: {
...options.headers,
'X-Api-Key': window.Sonarr.apiKey,
@@ -32,6 +34,6 @@ function useApiQuery<T>(options: QueryOptions<T>) {
queryFn: async ({ signal }) =>
fetchJson<T, unknown>({ ...requestOptions, signal }),
});
}
};
export default useApiQuery;