import { Component } from 'react'; import Box from '@mui/material/Box'; import { thumb } from '../../api'; import ThumbHashImage from './ThumbHashImage'; import { SCRIM_SOFT, } from '../../colors'; class ArticlePhoto extends Component { constructor(props) { super(props); this.state = { thumbLoaded: false }; this.handleThumbLoad = this.handleThumbLoad.bind(this); this.handleThumbError = this.handleThumbError.bind(this); } componentDidUpdate(prevProps) { if (prevProps.photo?.fullHash !== this.props.photo?.fullHash) { this.setState({ thumbLoaded: false }); } } handleThumbLoad() { this.setState({ thumbLoaded: true }); } handleThumbError() { this.setState({ thumbLoaded: false }); } render() { const { photo, size = 200, fill = false, objectFit = fill ? 'cover' : 'contain', sx, } = this.props; const { thumbLoaded } = this.state; const fullHash = photo?.fullHash; const hasPreview = photo?.thumbHash != null; const aspectRatio = photo?.thumbW != null && photo?.thumbH != null ? `${photo.thumbW} / ${photo.thumbH}` : '1 / 1'; return ( {hasPreview && !thumbLoaded ? ( = photo.thumbH ? '100%' : 'auto', height: photo.thumbH > photo.thumbW ? '100%' : 'auto', } } > ) : null} {fullHash ? ( ) : null} ); } } export default ArticlePhoto;