Update to SWR version 2.x (#296)

* Update SWR to version 2.1.5

* Improve generic type names

* Use "isLoading" property of "useSWR" response

* Correctly load library without selected tab

With the latest SWR version the library was empty on the first load.
Not sure what exactly is the reason, but due to "isPaused" the request wasn't sent even after "activeTab" wasn't undefined anymore.
Instead of using "isPaused", passing "null" as the key will also prevent SWR from doing the request.
This commit is contained in:
schroda
2023-05-15 11:56:55 +02:00
committed by GitHub
parent 4aff22079a
commit 8dae72604f
11 changed files with 34 additions and 35 deletions

View File

@@ -46,13 +46,7 @@ export async function fetcher<T = any>(path: string) {
return res.data as T;
}
export const useQuery = <D extends any = any, E extends any = any>(
key: string,
config?: SWRConfiguration<D, E>,
): SWRResponse<D, E> & { loading: boolean } => {
const res = useSWR(key, config);
return {
...res,
loading: res.data == null && res.error == null,
};
};
export const useQuery = <Data extends any = any, Error extends any = any>(
key: string | null,
config?: SWRConfiguration<Data, Error>,
): SWRResponse<Data, Error> => useSWR(key, config);

View File

@@ -45,10 +45,10 @@ export const useDefaultReaderSettings = (): {
settings: IReaderSettings;
loading: boolean;
} => {
const { data: meta, loading } = useQuery<Metadata>('/api/v1/meta');
const { data: meta, isLoading } = useQuery<Metadata>('/api/v1/meta');
const settings = getReaderSettingsWithDefaultValueFallback<IReaderSettings>(meta);
return { metadata: meta, settings, loading };
return { metadata: meta, settings, loading: isLoading };
};
/**

View File

@@ -16,8 +16,8 @@ export const useSearchSettings = (): {
settings: ISearchSettings;
loading: boolean;
} => {
const { data: meta, loading } = useQuery<Metadata>('/api/v1/meta');
const { data: meta, isLoading } = useQuery<Metadata>('/api/v1/meta');
const settings = getSearchSettingsWithDefaultValueFallback(meta);
return { metadata: meta, settings, loading };
return { metadata: meta, settings, loading: isLoading };
};