mirror of
https://github.com/Radarr/Radarr
synced 2025-12-06 08:28:50 +01:00
Add React Query
(cherry picked from commit 4491df3ae7530f2167beebc3548dd01fd2cc1a12)
This commit is contained in:
parent
ee8aaadb29
commit
b91517afd5
5 changed files with 83 additions and 8 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
import { ConnectedRouter, ConnectedRouterProps } from 'connected-react-router';
|
import { ConnectedRouter, ConnectedRouterProps } from 'connected-react-router';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import DocumentTitle from 'react-document-title';
|
import DocumentTitle from 'react-document-title';
|
||||||
|
|
@ -12,9 +13,12 @@ interface AppProps {
|
||||||
history: ConnectedRouterProps['history'];
|
history: ConnectedRouterProps['history'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
function App({ store, history }: AppProps) {
|
function App({ store, history }: AppProps) {
|
||||||
return (
|
return (
|
||||||
<DocumentTitle title={window.Radarr.instanceName}>
|
<DocumentTitle title={window.Radarr.instanceName}>
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<ConnectedRouter history={history}>
|
<ConnectedRouter history={history}>
|
||||||
<ApplyTheme />
|
<ApplyTheme />
|
||||||
|
|
@ -23,6 +27,7 @@ function App({ store, history }: AppProps) {
|
||||||
</PageConnector>
|
</PageConnector>
|
||||||
</ConnectedRouter>
|
</ConnectedRouter>
|
||||||
</Provider>
|
</Provider>
|
||||||
|
</QueryClientProvider>
|
||||||
</DocumentTitle>
|
</DocumentTitle>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
56
frontend/src/Helpers/Hooks/useApiQuery.ts
Normal file
56
frontend/src/Helpers/Hooks/useApiQuery.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
|
interface QueryOptions {
|
||||||
|
url: string;
|
||||||
|
headers?: HeadersInit;
|
||||||
|
}
|
||||||
|
|
||||||
|
const absUrlRegex = /^(https?:)?\/\//i;
|
||||||
|
const apiRoot = window.Radarr.apiRoot;
|
||||||
|
|
||||||
|
function isAbsolute(url: string) {
|
||||||
|
return absUrlRegex.test(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUrl(url: string) {
|
||||||
|
return apiRoot + url;
|
||||||
|
}
|
||||||
|
|
||||||
|
function useApiQuery<T>(options: QueryOptions) {
|
||||||
|
const { url, headers } = options;
|
||||||
|
|
||||||
|
const final = useMemo(() => {
|
||||||
|
if (isAbsolute(url)) {
|
||||||
|
return {
|
||||||
|
url,
|
||||||
|
headers,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
url: getUrl(url),
|
||||||
|
headers: {
|
||||||
|
...headers,
|
||||||
|
'X-Api-Key': window.Radarr.apiKey,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}, [url, headers]);
|
||||||
|
|
||||||
|
return useQuery({
|
||||||
|
queryKey: [final.url],
|
||||||
|
queryFn: async () => {
|
||||||
|
const result = await fetch(final.url, {
|
||||||
|
headers: final.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result.ok) {
|
||||||
|
throw new Error('Failed to fetch');
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.json() as T;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useApiQuery;
|
||||||
1
frontend/typings/Globals.d.ts
vendored
1
frontend/typings/Globals.d.ts
vendored
|
|
@ -3,6 +3,7 @@ declare module '*.module.css';
|
||||||
interface Window {
|
interface Window {
|
||||||
Radarr: {
|
Radarr: {
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
|
apiRoot: string;
|
||||||
instanceName: string;
|
instanceName: string;
|
||||||
theme: string;
|
theme: string;
|
||||||
urlBase: string;
|
urlBase: string;
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@
|
||||||
"@microsoft/signalr": "6.0.25",
|
"@microsoft/signalr": "6.0.25",
|
||||||
"@sentry/browser": "7.119.1",
|
"@sentry/browser": "7.119.1",
|
||||||
"@sentry/integrations": "7.119.1",
|
"@sentry/integrations": "7.119.1",
|
||||||
|
"@tanstack/react-query": "5.74.3",
|
||||||
"@types/node": "20.16.11",
|
"@types/node": "20.16.11",
|
||||||
"@types/react": "18.3.12",
|
"@types/react": "18.3.12",
|
||||||
"@types/react-dom": "18.3.1",
|
"@types/react-dom": "18.3.1",
|
||||||
|
|
|
||||||
12
yarn.lock
12
yarn.lock
|
|
@ -1295,6 +1295,18 @@
|
||||||
dependencies:
|
dependencies:
|
||||||
"@sentry/types" "7.119.1"
|
"@sentry/types" "7.119.1"
|
||||||
|
|
||||||
|
"@tanstack/query-core@5.74.3":
|
||||||
|
version "5.74.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.74.3.tgz#1fc97bd9a47f2acdf9f49737b1e6969e7bbcb7d7"
|
||||||
|
integrity sha512-Mqk+5o3qTuAiZML248XpNH8r2cOzl15+LTbUsZQEwvSvn1GU4VQhvqzAbil36p+MBxpr/58oBSnRzhrBevDhfg==
|
||||||
|
|
||||||
|
"@tanstack/react-query@5.74.3":
|
||||||
|
version "5.74.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.74.3.tgz#f7acd825abaea091f009d1c3f115212e45c4ee74"
|
||||||
|
integrity sha512-QrycUn0wxjVPzITvQvOxFRdhlAwIoOQSuav7qWD4SWCoKCdLbyRZ2vji2GuBq/glaxbF4wBx3fqcYRDOt8KDTA==
|
||||||
|
dependencies:
|
||||||
|
"@tanstack/query-core" "5.74.3"
|
||||||
|
|
||||||
"@types/archiver@^5.3.1":
|
"@types/archiver@^5.3.1":
|
||||||
version "5.3.4"
|
version "5.3.4"
|
||||||
resolved "https://registry.yarnpkg.com/@types/archiver/-/archiver-5.3.4.tgz#32172d5a56f165b5b4ac902e366248bf03d3ae84"
|
resolved "https://registry.yarnpkg.com/@types/archiver/-/archiver-5.3.4.tgz#32172d5a56f165b5b4ac902e366248bf03d3ae84"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue