ConceptPortal-public/rsconcept/frontend/src/components/info/InfoError.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

import axios, { type AxiosError } from 'axios';
2024-03-15 12:34:41 +03:00
import clsx from 'clsx';
2024-03-15 12:34:41 +03:00
import { urls } from '@/utils/constants';
import { isResponseHtml } from '@/utils/utils';
2023-07-25 20:27:29 +03:00
2024-03-20 15:27:32 +03:00
import PrettyJson from '../ui/PrettyJSON';
import TextURL from '../ui/TextURL';
import AnimateFade from '../wrap/AnimateFade';
2023-07-15 17:46:19 +03:00
export type ErrorData = string | Error | AxiosError | undefined;
2023-07-15 17:46:19 +03:00
interface InfoErrorProps {
2023-12-28 14:04:44 +03:00
error: ErrorData;
2023-07-15 17:46:19 +03:00
}
2023-12-28 14:04:44 +03:00
function DescribeError({ error }: { error: ErrorData }) {
2023-07-15 17:46:19 +03:00
if (!error) {
return <p>Ошибки отсутствуют</p>;
} else if (typeof error === 'string') {
return <p>{error}</p>;
} else if (!axios.isAxiosError(error)) {
return <PrettyJson data={error} />;
}
if (!error?.response) {
return <p>Нет ответа от сервера</p>;
}
if (error.response.status === 404) {
2023-07-15 17:46:19 +03:00
return (
2023-12-28 14:04:44 +03:00
<div>
<p>{'Обращение к несуществующему API'}</p>
<PrettyJson data={error} />
</div>
);
2023-07-15 17:46:19 +03:00
}
2023-07-25 20:27:29 +03:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const isHtml = isResponseHtml(error.response);
return (
2023-12-17 20:19:28 +03:00
<div>
<p className='underline'>Ошибка</p>
<p>{error.message}</p>
2023-12-28 14:04:44 +03:00
{error.response.data && (
<>
<p className='mt-2 underline'>Описание</p>
{isHtml ? <div dangerouslySetInnerHTML={{ __html: error.response.data as TrustedHTML }} /> : null}
{!isHtml ? <PrettyJson data={error.response.data as object} /> : null}
</>
)}
</div>
);
2023-07-15 17:46:19 +03:00
}
function InfoError({ error }: InfoErrorProps) {
2023-07-15 17:46:19 +03:00
return (
2024-03-15 12:34:41 +03:00
<AnimateFade
className={clsx(
'min-w-[25rem] w-full',
'px-3 py-2 flex flex-col',
'clr-text-red',
'text-sm font-semibold',
'select-text'
)}
>
<p className='clr-text-default font-normal'>
Пожалуйста сделайте скриншот и отправьте вместе с описанием ситуации на почту{' '}
<TextURL href={urls.mail_portal} text='portal@acconcept.ru' />
<br />
Для продолжения работы перезагрузите страницу
</p>
2023-12-28 14:04:44 +03:00
<DescribeError error={error} />
2024-01-07 03:29:16 +03:00
</AnimateFade>
2023-12-28 14:04:44 +03:00
);
2023-07-15 17:46:19 +03:00
}
2023-12-28 14:04:44 +03:00
export default InfoError;