Files
suwayomi-material-you-webui/src/components/SpinnerImage.tsx

76 lines
1.7 KiB
TypeScript
Raw Normal View History

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/. */
import React, { useEffect, useState } from 'react';
2021-09-09 17:51:22 +04:30
import CircularProgress from '@mui/material/CircularProgress';
2021-05-28 19:37:26 +04:30
interface IProps {
src: string
alt: string
imgRef?: React.RefObject<HTMLImageElement>
spinnerClassName?: string
imgClassName?: string
onImageLoad?: () => void
}
export default function SpinnerImage(props: IProps) {
const {
src, alt, onImageLoad, imgRef, spinnerClassName, imgClassName,
} = props;
const [imageSrc, setImagsrc] = useState<string>('');
useEffect(() => {
const img = new Image();
img.src = src;
img.onload = () => {
setImagsrc(src);
onImageLoad?.();
};
img.onerror = () => {
setImagsrc('Not Found');
};
2021-05-28 19:37:26 +04:30
return () => {
img.onload = null;
img.onerror = null;
2021-05-28 19:37:26 +04:30
};
}, [src]);
if (imageSrc.length === 0) {
return (
<div className={spinnerClassName}>
<CircularProgress thickness={5} />
</div>
);
}
if (imageSrc === 'Not Found') {
return <div className={spinnerClassName} />;
}
2021-05-28 19:37:26 +04:30
return (
<img
className={imgClassName}
ref={imgRef}
src={imageSrc}
alt={alt}
/>
);
}
SpinnerImage.defaultProps = {
spinnerClassName: '',
imgClassName: '',
onImageLoad: () => {},
imgRef: undefined,
};