1- import React , { useCallback , useState } from 'react' ;
1+ import React , { useCallback , useState , useEffect , useRef } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { PlusOutlined } from '@ant-design/icons' ;
44
55import ImagesZoom from './ImagesZoom' ;
66
7+ function isImageValid ( src ) {
8+ return new Promise ( ( resolve ) => {
9+ const img = document . createElement ( 'img' ) ;
10+ img . onerror = ( ) => resolve ( false ) ;
11+ img . onload = ( ) => resolve ( true ) ;
12+ img . src = src ;
13+ } ) ;
14+ }
715const PostImages = ( { images } ) => {
816 const [ showImagesZoom , setShowImagesZoom ] = useState ( false ) ;
17+ const imgEl = useRef ( ) ;
18+
19+ useEffect (
20+ ( ) => {
21+ isImageValid ( images [ 0 ] . src ) . then ( ( isValid ) => {
22+ if ( ! isValid ) {
23+ imgEl . current . remove ( ) ;
24+ }
25+ } ) ;
26+ } ,
27+ [ images [ 0 ] . src ] ,
28+ ) ;
929
1030 const onZoom = useCallback ( ( ) => {
1131 setShowImagesZoom ( true ) ;
1232 } , [ ] ) ;
1333 const onClose = useCallback ( ( ) => {
1434 setShowImagesZoom ( false ) ;
1535 } , [ ] ) ;
36+
1637 const onError = useCallback ( ( e ) => {
17- e . target . src = e . target . src . replace ( '/thumb/' , '/original/' ) ;
38+ console . dir ( e . target ) ;
39+ e . target . previousSibling . remove ( ) ;
40+ // e.target.src = e.target.currentSrc.replace('/thumb/', '/original/');
1841 } , [ ] ) ;
1942
2043 if ( images . length === 1 ) {
2144 return (
2245 < >
23- < img role = "presentation" src = { `${ images [ 0 ] . src } ` } alt = { images [ 0 ] . src } onClick = { onZoom } onError = { onError } />
46+ < picture >
47+ < source ref = { imgEl } srcSet = { `${ images [ 0 ] . src } ` } />
48+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 0 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '100%' , display : 'inline-block' } } />
49+ </ picture >
2450 { showImagesZoom && < ImagesZoom images = { images } onClose = { onClose } /> }
2551 </ >
2652 ) ;
2753 }
2854 if ( images . length === 2 ) {
2955 return (
3056 < >
31- < img role = "presentation" style = { { width : '50%' , display : 'inline-block' } } src = { `${ images [ 0 ] . src } ` } alt = { images [ 0 ] . src } onClick = { onZoom } onError = { onError } />
32- < img role = "presentation" style = { { width : '50%' , display : 'inline-block' } } src = { `${ images [ 1 ] . src } ` } alt = { images [ 1 ] . src } onClick = { onZoom } onError = { onError } />
57+ < picture style = { { width : '50%' , display : 'inline-block' } } >
58+ < source ref = { imgEl } srcSet = { `${ images [ 0 ] . src } ` } />
59+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 0 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '100%' , display : 'inline-block' } } />
60+ </ picture >
61+ < picture style = { { width : '50%' , display : 'inline-block' } } >
62+ < source ref = { imgEl } srcSet = { `${ images [ 1 ] . src } ` } />
63+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 1 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '100%' , display : 'inline-block' } } />
64+ </ picture >
3365 { showImagesZoom && < ImagesZoom images = { images } onClose = { onClose } /> }
3466 </ >
3567 ) ;
3668 }
3769 return (
3870 < >
3971 < div >
40- < img role = "presentation" style = { { width : '50%' } } src = { `${ images [ 0 ] . src } ` } alt = { images [ 0 ] . src } onClick = { onZoom } onError = { onError } />
72+ < picture style = { { width : '50%' } } onError = { onError } >
73+ < source ref = { imgEl } srcSet = { `${ images [ 0 ] . src } ` } />
74+ < img role = "presentation" onClick = { onZoom } src = { `${ images [ 0 ] . src . replace ( '/thumb/' , '/original/' ) } ` } alt = { images [ 0 ] . src } style = { { maxWidth : '50%' } } />
75+ </ picture >
4176 < div
4277 role = "presentation"
4378 style = { { display : 'inline-block' , width : '50%' , textAlign : 'center' , verticalAlign : 'middle' } }
@@ -55,7 +90,7 @@ const PostImages = ({ images }) => {
5590} ;
5691
5792PostImages . propTypes = {
58- images : PropTypes . arrayOf ( PropTypes . object ) ,
93+ images : PropTypes . arrayOf ( PropTypes . object ) . isRequired ,
5994} ;
6095
6196export default PostImages ;
0 commit comments