mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
added error handling on image load
This commit is contained in:
parent
dde02dc2e3
commit
28ac88ec64
3 changed files with 56 additions and 7 deletions
|
@ -6,6 +6,7 @@ import styles from './styles.module.scss';
|
||||||
import { IFile } from '~/redux/types';
|
import { IFile } from '~/redux/types';
|
||||||
import { LoaderCircleInner } from '~/components/input/LoaderCircleInner';
|
import { LoaderCircleInner } from '~/components/input/LoaderCircleInner';
|
||||||
import { LoaderCircle } from '~/components/input/LoaderCircle';
|
import { LoaderCircle } from '~/components/input/LoaderCircle';
|
||||||
|
import { Icon } from '~/components/input/Icon';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
file: IFile;
|
file: IFile;
|
||||||
|
@ -17,9 +18,11 @@ interface IProps {
|
||||||
const ImagePreloader: FC<IProps> = ({ file, onLoad, onClick, className }) => {
|
const ImagePreloader: FC<IProps> = ({ file, onLoad, onClick, className }) => {
|
||||||
const [maxHeight, setMaxHeight] = useState(window.innerHeight - 140);
|
const [maxHeight, setMaxHeight] = useState(window.innerHeight - 140);
|
||||||
const [loaded, setLoaded] = useState(false);
|
const [loaded, setLoaded] = useState(false);
|
||||||
|
const [hasError, setHasError] = useState(false);
|
||||||
|
|
||||||
const onImageLoad = useCallback(() => {
|
const onImageLoad = useCallback(() => {
|
||||||
setLoaded(true);
|
setLoaded(true);
|
||||||
|
setHasError(false);
|
||||||
|
|
||||||
if (onLoad) {
|
if (onLoad) {
|
||||||
onLoad();
|
onLoad();
|
||||||
|
@ -30,6 +33,10 @@ const ImagePreloader: FC<IProps> = ({ file, onLoad, onClick, className }) => {
|
||||||
setMaxHeight(window.innerHeight - 140);
|
setMaxHeight(window.innerHeight - 140);
|
||||||
}, [setMaxHeight]);
|
}, [setMaxHeight]);
|
||||||
|
|
||||||
|
const onError = useCallback(() => {
|
||||||
|
setHasError(true);
|
||||||
|
}, [setHasError]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
window.addEventListener('resize', onResize);
|
window.addEventListener('resize', onResize);
|
||||||
|
|
||||||
|
@ -64,12 +71,14 @@ const ImagePreloader: FC<IProps> = ({ file, onLoad, onClick, className }) => {
|
||||||
|
|
||||||
<rect fill="#242222" width="100%" height="100%" stroke="none" rx="8" ry="8" />
|
<rect fill="#242222" width="100%" height="100%" stroke="none" rx="8" ry="8" />
|
||||||
|
|
||||||
<image
|
{!hasError && (
|
||||||
xlinkHref={getURL(file, PRESETS['300'])}
|
<image
|
||||||
width="100%"
|
xlinkHref={getURL(file, PRESETS['300'])}
|
||||||
height="100%"
|
width="100%"
|
||||||
filter="url(#f1)"
|
height="100%"
|
||||||
/>
|
filter="url(#f1)"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
<img
|
<img
|
||||||
|
@ -80,9 +89,17 @@ const ImagePreloader: FC<IProps> = ({ file, onLoad, onClick, className }) => {
|
||||||
onLoad={onImageLoad}
|
onLoad={onImageLoad}
|
||||||
style={{ maxHeight }}
|
style={{ maxHeight }}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
|
onError={onError}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{!loaded && <LoaderCircle className={styles.icon} size={64} />}
|
{!loaded && !hasError && <LoaderCircle className={styles.icon} size={64} />}
|
||||||
|
|
||||||
|
{hasError && (
|
||||||
|
<div className={styles.error}>
|
||||||
|
<div className={styles.error__text}>Не удалось получить картинку</div>
|
||||||
|
<Icon icon="warn" size={64} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -28,3 +28,27 @@
|
||||||
fill: currentColor;
|
fill: currentColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
position: absolute;
|
||||||
|
right: 30px;
|
||||||
|
bottom: 40px;
|
||||||
|
opacity: 0.4;
|
||||||
|
color: $orange;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
|
||||||
|
svg {
|
||||||
|
fill: $orange;
|
||||||
|
}
|
||||||
|
|
||||||
|
&__text {
|
||||||
|
max-width: 200px;
|
||||||
|
text-align: right;
|
||||||
|
margin-right: $gap;
|
||||||
|
font: $font_16_semibold;
|
||||||
|
line-height: 24px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -302,6 +302,14 @@ const Sprites: FC = () => (
|
||||||
/>
|
/>
|
||||||
</g>
|
</g>
|
||||||
|
|
||||||
|
<g id="warn">
|
||||||
|
<path fill="none" d="M0 0h24v24H0V0z" stroke="none" />
|
||||||
|
<path
|
||||||
|
stroke="none"
|
||||||
|
d="M4.47 21h15.06c1.54 0 2.5-1.67 1.73-3L13.73 4.99c-.77-1.33-2.69-1.33-3.46 0L2.74 18c-.77 1.33.19 3 1.73 3zM12 14c-.55 0-1-.45-1-1v-2c0-.55.45-1 1-1s1 .45 1 1v2c0 .55-.45 1-1 1zm1 4h-2v-2h2v2z"
|
||||||
|
/>
|
||||||
|
</g>
|
||||||
|
|
||||||
<g id="hot">
|
<g id="hot">
|
||||||
<path fill="none" d="M0 0h24v24H0V0z" stroke="none" />
|
<path fill="none" d="M0 0h24v24H0V0z" stroke="none" />
|
||||||
<path
|
<path
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue