Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/components/elements/ScreenBlock.tsx
7461 views
1
import React from 'react';
2
import PageContentBlock from '@/components/elements/PageContentBlock';
3
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4
import { faArrowLeft, faSyncAlt } from '@fortawesome/free-solid-svg-icons';
5
import styled, { keyframes } from 'styled-components/macro';
6
import tw from 'twin.macro';
7
import Button from '@/components/elements/Button';
8
import NotFoundSvg from '@/assets/images/not_found.svg';
9
import ServerErrorSvg from '@/assets/images/server_error.svg';
10
11
interface BaseProps {
12
title: string;
13
image: string;
14
message: string;
15
onRetry?: () => void;
16
onBack?: () => void;
17
}
18
19
interface PropsWithRetry extends BaseProps {
20
onRetry?: () => void;
21
onBack?: never;
22
}
23
24
interface PropsWithBack extends BaseProps {
25
onBack?: () => void;
26
onRetry?: never;
27
}
28
29
export type ScreenBlockProps = PropsWithBack | PropsWithRetry;
30
31
const spin = keyframes`
32
to { transform: rotate(360deg) }
33
`;
34
35
const ActionButton = styled(Button)`
36
${tw`rounded-full w-8 h-8 flex items-center justify-center p-0`};
37
38
&.hover\\:spin:hover {
39
animation: ${spin} 2s linear infinite;
40
}
41
`;
42
43
const ScreenBlock = ({ title, image, message, onBack, onRetry }: ScreenBlockProps) => (
44
<PageContentBlock>
45
<div css={tw`flex justify-center`}>
46
<div
47
css={tw`w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center relative`}
48
>
49
{(typeof onBack === 'function' || typeof onRetry === 'function') && (
50
<div css={tw`absolute left-0 top-0 ml-4 mt-4`}>
51
<ActionButton
52
onClick={() => (onRetry ? onRetry() : onBack ? onBack() : null)}
53
className={onRetry ? 'hover:spin' : undefined}
54
>
55
<FontAwesomeIcon icon={onRetry ? faSyncAlt : faArrowLeft} />
56
</ActionButton>
57
</div>
58
)}
59
<img src={image} css={tw`w-2/3 h-auto select-none mx-auto`} />
60
<h2 css={tw`mt-10 text-neutral-900 font-bold text-4xl`}>{title}</h2>
61
<p css={tw`text-sm text-neutral-700 mt-2`}>{message}</p>
62
</div>
63
</div>
64
</PageContentBlock>
65
);
66
67
type ServerErrorProps = (Omit<PropsWithBack, 'image' | 'title'> | Omit<PropsWithRetry, 'image' | 'title'>) & {
68
title?: string;
69
};
70
71
const ServerError = ({ title, ...props }: ServerErrorProps) => (
72
<ScreenBlock title={title || 'Something went wrong'} image={ServerErrorSvg} {...props} />
73
);
74
75
const NotFound = ({ title, message, onBack }: Partial<Pick<ScreenBlockProps, 'title' | 'message' | 'onBack'>>) => (
76
<ScreenBlock
77
title={title || '404'}
78
image={NotFoundSvg}
79
message={message || 'The requested resource was not found.'}
80
onBack={onBack}
81
/>
82
);
83
84
export { ServerError, NotFound };
85
export default ScreenBlock;
86
87