Feature/swr for simple queries (#187)

* Refactor simple queries with useSWR hook

* Fix plus button in Extensions screen

* Add type to categories query

* Add useQuery abstraction which adds loading attribute

* Reload categories even if reorder fails
This commit is contained in:
Valter Martinek
2022-11-02 21:48:22 +01:00
committed by GitHub
parent 6f8755fa05
commit 14265164a4
11 changed files with 134 additions and 182 deletions

View File

@@ -6,6 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import axios from 'axios';
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
import storage from './localStorage';
const { hostname, port, protocol } = window.location;
@@ -40,3 +41,17 @@ 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,
};
};