import React, { useState, useCallback } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import CircularProgress from '@material-ui/core/CircularProgress'; import ErrorIcon from '@material-ui/icons/BrokenImage'; interface PropTypes { src?: string; alt?: string; className?: string; } const useStyles = makeStyles(theme => ({ container: { width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' } })); type Status = 'success' |'loading' | 'error'; const Image: React.FC = ({ src, alt, className }) => { const classes = useStyles(); const [status, setStatus] = useState('loading'); const handleLoad = useCallback((): void => { setStatus('success'); }, [setStatus]); const handleError = useCallback((): void => { setStatus('error'); }, [setStatus]); const image = ( {alt} ); return ( <> {src && image} { status !== 'success' && (
{ (status === 'error' || !src) ? : }
)} ); }; export default Image;