mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
fix(console): fetch settings with swr on app init
This commit is contained in:
parent
3b17324d18
commit
c7344c2175
2 changed files with 14 additions and 5 deletions
|
@ -35,7 +35,9 @@ const Main = () => {
|
|||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
const fetcher = useSwrFetcher();
|
||||
const { data } = useSWR<Setting, RequestError>('/api/settings');
|
||||
|
||||
const settingsFetcher = useSwrFetcher<Setting>();
|
||||
const { data } = useSWR<Setting, RequestError>('/api/settings', settingsFetcher);
|
||||
|
||||
useEffect(() => {
|
||||
const theme = data?.adminConsole.appearanceMode ?? defaultTheme;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { ArbitraryObject, RequestErrorBody } from '@logto/schemas';
|
||||
import { RequestErrorBody } from '@logto/schemas';
|
||||
import { HTTPError } from 'ky';
|
||||
import { useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
@ -6,14 +6,21 @@ import { BareFetcher } from 'swr';
|
|||
|
||||
import useApi, { RequestError } from './use-api';
|
||||
|
||||
const useSwrFetcher = () => {
|
||||
type withTotalNumber<T> = Array<Awaited<T> | number>;
|
||||
|
||||
type useSwrFetcherHook = {
|
||||
<T>(): BareFetcher<T>;
|
||||
<T extends unknown[]>(): BareFetcher<withTotalNumber<T>>;
|
||||
};
|
||||
|
||||
const useSwrFetcher: useSwrFetcherHook = <T>() => {
|
||||
const api = useApi({ hideErrorToast: true });
|
||||
const { t } = useTranslation();
|
||||
const fetcher = useCallback<BareFetcher>(
|
||||
const fetcher = useCallback<BareFetcher<T | withTotalNumber<T>>>(
|
||||
async (resource, init) => {
|
||||
try {
|
||||
const response = await api.get(resource, init);
|
||||
const data = await response.json<ArbitraryObject>();
|
||||
const data = await response.json<T>();
|
||||
|
||||
if (typeof resource === 'string' && resource.includes('?')) {
|
||||
const parameters = new URLSearchParams(resource.split('?')[1]);
|
||||
|
|
Loading…
Reference in a new issue