2023-05-15 09:54:50 +02:00
|
|
|
import { Image, Loader, createStyles } from '@mantine/core';
|
2022-12-11 00:17:34 +01:00
|
|
|
import { useDebouncedValue } from '@mantine/hooks';
|
2023-05-15 09:54:50 +02:00
|
|
|
import { IconPhotoOff } from '@tabler/icons';
|
2022-12-11 00:17:34 +01:00
|
|
|
|
2023-05-15 09:54:50 +02:00
|
|
|
interface DebouncedImageProps {
|
2022-12-11 00:17:34 +01:00
|
|
|
width: number;
|
|
|
|
|
height: number;
|
2023-05-15 09:54:50 +02:00
|
|
|
src: string;
|
2022-12-11 00:17:34 +01:00
|
|
|
debouncedWaitPeriod?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 09:54:50 +02:00
|
|
|
export const DebouncedImage = ({
|
|
|
|
|
src,
|
2022-12-11 00:17:34 +01:00
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
debouncedWaitPeriod = 1000,
|
2023-05-15 09:54:50 +02:00
|
|
|
}: DebouncedImageProps) => {
|
2022-12-11 00:17:34 +01:00
|
|
|
const { classes } = useStyles();
|
2023-05-15 09:54:50 +02:00
|
|
|
const [debouncedIconImageUrl] = useDebouncedValue(src, debouncedWaitPeriod);
|
2022-12-11 00:17:34 +01:00
|
|
|
|
2023-05-15 09:54:50 +02:00
|
|
|
if (debouncedIconImageUrl !== src) {
|
2022-12-11 00:17:34 +01:00
|
|
|
return <Loader width={width} height={height} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (debouncedIconImageUrl.length > 0) {
|
|
|
|
|
return (
|
2023-05-15 09:54:50 +02:00
|
|
|
<Image
|
|
|
|
|
placeholder={<IconPhotoOff />}
|
2022-12-11 00:17:34 +01:00
|
|
|
className={classes.iconImage}
|
|
|
|
|
src={debouncedIconImageUrl}
|
|
|
|
|
width={width}
|
|
|
|
|
height={height}
|
2023-05-15 09:54:50 +02:00
|
|
|
fit="contain"
|
2022-12-11 00:17:34 +01:00
|
|
|
alt=""
|
2023-05-15 09:54:50 +02:00
|
|
|
withPlaceholder
|
2022-12-11 00:17:34 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Image
|
|
|
|
|
className={classes.iconImage}
|
|
|
|
|
src="/imgs/logo/logo.png"
|
|
|
|
|
width={width}
|
|
|
|
|
height={height}
|
2023-05-15 09:54:50 +02:00
|
|
|
fit="contain"
|
2022-12-11 00:17:34 +01:00
|
|
|
alt=""
|
2023-05-15 09:54:50 +02:00
|
|
|
withPlaceholder
|
2022-12-11 00:17:34 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const useStyles = createStyles(() => ({
|
|
|
|
|
iconImage: {
|
|
|
|
|
objectFit: 'contain',
|
|
|
|
|
},
|
|
|
|
|
}));
|