diff --git a/.changeset/sixty-scissors-tell.md b/.changeset/sixty-scissors-tell.md new file mode 100644 index 00000000000..f954f885dfc --- /dev/null +++ b/.changeset/sixty-scissors-tell.md @@ -0,0 +1,8 @@ +--- +"@logto/console": patch +--- + +improve error handling on audit logs + +- No longer toasts error messages if the audit log related user entity has been removed. +- Display a fallback `user-id (deleted)` information instead. diff --git a/packages/console/src/components/UserName/index.tsx b/packages/console/src/components/UserName/index.tsx index 7e54f63afd4..0dda7496b6f 100644 --- a/packages/console/src/components/UserName/index.tsx +++ b/packages/console/src/components/UserName/index.tsx @@ -1,11 +1,15 @@ import type { User } from '@logto/schemas'; -import { conditionalString } from '@silverhand/essentials'; import classNames from 'classnames'; +import { useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; import { Link } from 'react-router-dom'; import useSWR from 'swr'; import type { RequestError } from '@/hooks/use-api'; +import useApi from '@/hooks/use-api'; +import useSwrFetcher from '@/hooks/use-swr-fetcher'; import useTenantPathname from '@/hooks/use-tenant-pathname'; +import { shouldRetryOnError } from '@/utils/request'; import { getUserTitle } from '@/utils/user'; import UserAvatar from '../UserAvatar'; @@ -18,18 +22,28 @@ type Props = { }; function UserName({ userId, isLink = false }: Props) { - const { data, error } = useSWR(`api/users/${userId}`); - const isLoading = !data && !error; - const name = conditionalString(data && getUserTitle(data)); + const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); + const fetchApi = useApi({ hideErrorToast: true }); + const fetcher = useSwrFetcher(fetchApi); + const { data, error } = useSWR(`api/users/${userId}`, { + fetcher, + shouldRetryOnError: shouldRetryOnError({ ignore: [404] }), + }); const { getTo } = useTenantPathname(); - if (isLoading) { - return null; - } + const name = useMemo(() => { + if (data) { + return getUserTitle(data); + } + if (error?.status === 404) { + return `${userId} (${t('general.deleted')})`; + } + return '-'; + }, [userId, data, error?.status, t]); return (
- {isLink ? ( + {isLink && data ? ( {name}