-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIllustration.tsx
46 lines (37 loc) · 1.49 KB
/
Illustration.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react';
import {useThemeValue} from '@gravity-ui/uikit';
import {cn} from '../../utils/cn';
export interface IllustrationProps extends React.ImgHTMLAttributes<HTMLImageElement> {
name: string;
className?: string;
}
type IllustrationStore = Record<string, Record<string, () => Promise<{default: any}>>>;
const store: IllustrationStore = {
light: {
403: () => import('../../assets/illustrations/light/403.svg'),
thumbsUp: () => import('../../assets/illustrations/light/thumbsUp.svg'),
error: () => import('../../assets/illustrations/light/error.svg'),
},
dark: {
403: () => import('../../assets/illustrations/dark/403.svg'),
thumbsUp: () => import('../../assets/illustrations/dark/thumbsUp.svg'),
error: () => import('../../assets/illustrations/dark/error.svg'),
},
};
const b = cn('kv-illustration');
export const Illustration = ({name, className, ...props}: IllustrationProps) => {
const theme = useThemeValue();
const [src, setSrc] = React.useState('');
const srcGetter = store[theme] && store[theme][name];
React.useEffect(() => {
if (typeof srcGetter === 'function') {
srcGetter()
.then((svg) => setSrc(svg.default))
.catch((e) => {
console.error(e);
setSrc('');
});
}
}, [srcGetter]);
return src ? <img alt={name} src={src} className={b(null, className)} {...props} /> : null;
};