2021-05-28 19:37:26 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2021-10-31 21:02:45 +03:30
|
|
|
import React, { useEffect, useState, CSSProperties } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import CircularProgress from '@mui/material/CircularProgress';
|
2021-10-31 21:02:45 +03:30
|
|
|
import Box from '@mui/system/Box';
|
|
|
|
|
import { Theme } from '@mui/system/createTheme';
|
|
|
|
|
import { SxProps } from '@mui/system/styleFunctionSx';
|
2021-05-28 19:37:26 +04:30
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
src: string
|
|
|
|
|
alt: string
|
|
|
|
|
|
|
|
|
|
imgRef?: React.RefObject<HTMLImageElement>
|
|
|
|
|
|
2021-11-15 17:21:31 +03:30
|
|
|
spinnerClassName?: string
|
2021-10-31 21:02:45 +03:30
|
|
|
spinnerStyle?: SxProps<Theme>
|
2021-11-15 17:21:31 +03:30
|
|
|
imgClassName?: string
|
2021-10-31 21:02:45 +03:30
|
|
|
imgStyle?: CSSProperties
|
2021-05-28 19:37:26 +04:30
|
|
|
|
|
|
|
|
onImageLoad?: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SpinnerImage(props: IProps) {
|
|
|
|
|
const {
|
2021-11-15 17:21:31 +03:30
|
|
|
src, alt, onImageLoad, imgRef, spinnerClassName, imgClassName, spinnerStyle, imgStyle,
|
2021-05-28 19:37:26 +04:30
|
|
|
} = props;
|
|
|
|
|
const [imageSrc, setImagsrc] = useState<string>('');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const img = new Image();
|
|
|
|
|
img.src = src;
|
|
|
|
|
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
setImagsrc(src);
|
|
|
|
|
onImageLoad?.();
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-18 00:04:09 +04:30
|
|
|
img.onerror = () => {
|
2021-10-26 22:41:28 +02:00
|
|
|
// Setting to an actual image so CSS styling works consistently
|
|
|
|
|
setImagsrc('/notFound.svg');
|
2021-09-18 00:04:09 +04:30
|
|
|
};
|
|
|
|
|
|
2021-05-28 19:37:26 +04:30
|
|
|
return () => {
|
|
|
|
|
img.onload = null;
|
2021-09-18 00:04:09 +04:30
|
|
|
img.onerror = null;
|
2021-05-28 19:37:26 +04:30
|
|
|
};
|
|
|
|
|
}, [src]);
|
|
|
|
|
|
|
|
|
|
if (imageSrc.length === 0) {
|
|
|
|
|
return (
|
2021-11-15 17:21:31 +03:30
|
|
|
<Box className={spinnerClassName} sx={spinnerStyle}>
|
2021-05-28 19:37:26 +04:30
|
|
|
<CircularProgress thickness={5} />
|
2021-10-31 21:02:45 +03:30
|
|
|
</Box>
|
2021-05-28 19:37:26 +04:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 00:04:09 +04:30
|
|
|
if (imageSrc === 'Not Found') {
|
2021-11-15 17:21:31 +03:30
|
|
|
return <Box className={spinnerClassName} sx={spinnerStyle} />;
|
2021-09-18 00:04:09 +04:30
|
|
|
}
|
|
|
|
|
|
2021-05-28 19:37:26 +04:30
|
|
|
return (
|
|
|
|
|
<img
|
2021-11-15 17:21:31 +03:30
|
|
|
className={imgClassName}
|
2021-10-31 21:02:45 +03:30
|
|
|
style={imgStyle}
|
2021-05-28 19:37:26 +04:30
|
|
|
ref={imgRef}
|
|
|
|
|
src={imageSrc}
|
|
|
|
|
alt={alt}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SpinnerImage.defaultProps = {
|
2021-11-15 17:21:31 +03:30
|
|
|
spinnerClassName: '',
|
|
|
|
|
imgClassName: '',
|
2021-10-31 21:02:45 +03:30
|
|
|
spinnerStyle: {},
|
|
|
|
|
imgStyle: {},
|
2021-05-28 19:37:26 +04:30
|
|
|
onImageLoad: () => {},
|
|
|
|
|
imgRef: undefined,
|
|
|
|
|
};
|