0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

fix(console): useApi hook should not cause component re-render

This commit is contained in:
Charles Zhao 2022-10-27 16:56:13 +08:00
parent ee374d19cc
commit 4074d694ef
No known key found for this signature in database
GPG key ID: 4858774754C92DF2

View file

@ -2,7 +2,7 @@ import { useLogto } from '@logto/react';
import type { RequestErrorBody } from '@logto/schemas'; import type { RequestErrorBody } from '@logto/schemas';
import { managementResource } from '@logto/schemas/lib/seeds'; import { managementResource } from '@logto/schemas/lib/seeds';
import ky from 'ky'; import ky from 'ky';
import { useMemo } from 'react'; import { useCallback, useMemo } from 'react';
import { toast } from 'react-hot-toast'; import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@ -19,10 +19,16 @@ export class RequestError extends Error {
} }
} }
const useToastError = () => { type Props = {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); hideErrorToast?: boolean;
};
const toastError = async (response: Response) => { const useApi = ({ hideErrorToast }: Props = {}) => {
const { isAuthenticated, getAccessToken } = useLogto();
const { t, i18n } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const toastError = useCallback(
async (response: Response) => {
const fallbackErrorMessage = t('errors.unknown_server_error'); const fallbackErrorMessage = t('errors.unknown_server_error');
try { try {
@ -31,19 +37,9 @@ const useToastError = () => {
} catch { } catch {
toast.error(fallbackErrorMessage); toast.error(fallbackErrorMessage);
} }
}; },
[t]
return toastError; );
};
type Props = {
hideErrorToast?: boolean;
};
const useApi = ({ hideErrorToast }: Props = {}) => {
const { isAuthenticated, getAccessToken } = useLogto();
const { i18n } = useTranslation();
const toastError = useToastError();
const api = useMemo( const api = useMemo(
() => () =>