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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-05-28 19:37:26 +04:30
|
|
|
|
2024-03-01 18:27:46 +01:00
|
|
|
import { useState, CSSProperties, useEffect, forwardRef, ForwardedRef } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import CircularProgress from '@mui/material/CircularProgress';
|
2023-06-04 21:08:39 +02:00
|
|
|
import Box from '@mui/material/Box';
|
2024-01-15 00:32:39 +01:00
|
|
|
import { Theme, SxProps, Stack, Button } from '@mui/material';
|
|
|
|
|
|
|
|
|
|
import BrokenImageIcon from '@mui/icons-material/BrokenImage';
|
|
|
|
|
import RefreshIcon from '@mui/icons-material/Refresh';
|
2024-01-15 00:55:39 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-03-01 01:17:23 +01:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
2021-05-28 19:37:26 +04:30
|
|
|
|
|
|
|
|
interface IProps {
|
2023-02-06 10:06:33 +01:00
|
|
|
src: string;
|
|
|
|
|
alt: string;
|
2021-05-28 19:37:26 +04:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
spinnerStyle?: SxProps<Theme>;
|
|
|
|
|
imgStyle?: CSSProperties;
|
2021-05-28 19:37:26 +04:30
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
onImageLoad?: () => void;
|
2021-05-28 19:37:26 +04:30
|
|
|
}
|
|
|
|
|
|
2024-03-01 18:27:46 +01:00
|
|
|
export const SpinnerImage = forwardRef((props: IProps, imgRef: ForwardedRef<HTMLImageElement | null>) => {
|
|
|
|
|
const { src, alt, onImageLoad, spinnerStyle, imgStyle } = props;
|
2021-05-28 19:37:26 +04:30
|
|
|
|
2024-01-15 00:55:39 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-03-01 01:17:23 +01:00
|
|
|
const [imageSourceUrl, setImageSourceUrl] = useState('');
|
2024-01-15 00:32:39 +01:00
|
|
|
const [imgLoadRetryKey, setImgLoadRetryKey] = useState(0);
|
|
|
|
|
const [isLoading, setIsLoading] = useState<boolean | undefined>(undefined);
|
|
|
|
|
const [hasError, setHasError] = useState(false);
|
2021-05-28 19:37:26 +04:30
|
|
|
|
2024-01-15 00:32:39 +01:00
|
|
|
const updateImageState = (loading: boolean, error: boolean = false) => {
|
|
|
|
|
setIsLoading(loading);
|
|
|
|
|
setHasError(error);
|
2021-09-18 00:04:09 +04:30
|
|
|
|
2024-01-15 00:32:39 +01:00
|
|
|
if (!loading && !error) {
|
|
|
|
|
onImageLoad?.();
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-05-28 19:37:26 +04:30
|
|
|
|
2024-01-15 00:32:39 +01:00
|
|
|
useEffect(() => {
|
2024-03-03 23:07:12 +01:00
|
|
|
let tmpImageSourceUrl: string;
|
2024-03-01 01:17:23 +01:00
|
|
|
const imageRequest = requestManager.requestImage(src);
|
|
|
|
|
|
|
|
|
|
const fetchImage = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const updateImage = async () => {
|
|
|
|
|
const image = await imageRequest.response;
|
|
|
|
|
|
|
|
|
|
updateImageState(false);
|
|
|
|
|
setImageSourceUrl(image);
|
2024-03-03 23:07:12 +01:00
|
|
|
tmpImageSourceUrl = image;
|
2024-03-01 01:17:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const checkCache = await Promise.race([imageRequest.response, Promise.resolve(false)]);
|
|
|
|
|
const isImageCached = !!checkCache;
|
|
|
|
|
|
|
|
|
|
if (isImageCached) {
|
|
|
|
|
await updateImage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateImageState(true);
|
|
|
|
|
await updateImage();
|
|
|
|
|
} catch (e) {
|
2024-03-05 02:07:05 +01:00
|
|
|
const wasAborted = e instanceof Error && e.name === 'AbortError';
|
2024-03-01 01:17:23 +01:00
|
|
|
updateImageState(false, !wasAborted);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchImage().catch(defaultPromiseErrorHandler);
|
|
|
|
|
|
|
|
|
|
return () => {
|
2024-03-03 23:07:12 +01:00
|
|
|
if (tmpImageSourceUrl) {
|
|
|
|
|
URL.revokeObjectURL(tmpImageSourceUrl);
|
|
|
|
|
}
|
2024-03-01 01:17:23 +01:00
|
|
|
imageRequest.abortRequest(new Error('Component was unmounted'));
|
|
|
|
|
};
|
|
|
|
|
}, [imgLoadRetryKey]);
|
2021-09-18 00:04:09 +04:30
|
|
|
|
2024-01-15 00:32:39 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{(isLoading || hasError) && (
|
|
|
|
|
<Box sx={spinnerStyle}>
|
|
|
|
|
<Stack height="100%" alignItems="center" justifyContent="center">
|
|
|
|
|
{isLoading && <CircularProgress thickness={5} />}
|
2024-03-01 01:17:23 +01:00
|
|
|
{hasError && isLoading === false && (
|
2024-01-15 00:32:39 +01:00
|
|
|
<>
|
|
|
|
|
<BrokenImageIcon />
|
|
|
|
|
<Button
|
|
|
|
|
startIcon={<RefreshIcon />}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setImgLoadRetryKey((prevState) => (prevState + 1) % 100);
|
|
|
|
|
}}
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
2024-01-15 00:55:39 +01:00
|
|
|
{t('global.button.retry')}
|
2024-01-15 00:32:39 +01:00
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
2024-03-01 01:17:23 +01:00
|
|
|
|
2024-01-15 00:32:39 +01:00
|
|
|
<img
|
|
|
|
|
key={`${src}_${imgLoadRetryKey}`}
|
2024-03-01 01:17:23 +01:00
|
|
|
style={{
|
|
|
|
|
...imgStyle,
|
|
|
|
|
display: !imageSourceUrl || isLoading || hasError ? 'none' : imgStyle?.display,
|
|
|
|
|
}}
|
2024-01-15 00:32:39 +01:00
|
|
|
ref={imgRef}
|
2024-03-01 01:17:23 +01:00
|
|
|
src={imageSourceUrl}
|
2024-01-15 00:32:39 +01:00
|
|
|
alt={alt}
|
|
|
|
|
draggable={false}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2024-03-01 18:27:46 +01:00
|
|
|
});
|