1- import { useRecoilState } from 'recoil' ;
2- import { isImageBright , previewImage } from '../atom' ;
1+ import { useRecoilState , useRecoilValue } from 'recoil' ;
2+ import { isImageBright , previewImage , ratioAtom } from '../atom' ;
33import { Icon } from '@iconify/react' ;
44import { useEffect , useRef , useState } from 'react' ;
55
6+ const FIXED_HEIGHT = 280 ;
7+
68const UnsplashUploader = ( ) => {
79 let timer : NodeJS . Timer | null = null ;
810 const [ imageSrc , setImageSrc ] = useRecoilState ( previewImage ) ;
911 const [ isBright , setIsBright ] = useRecoilState ( isImageBright ) ;
12+ const previewRatio = useRecoilValue ( ratioAtom ) ;
1013 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
1114 const mounted = useRef ( false ) ;
15+
1216 const getRandomImage = async ( ) => {
1317 if ( ! timer ) {
14- const response = await fetch ( `https://picsum.photos/${ 280 } /${ 280 } ?random=${ Date . now ( ) } ` ) ;
18+ const response = await fetch ( `https://picsum.photos/${ FIXED_HEIGHT * previewRatio } /${ FIXED_HEIGHT } ?random=${ Date . now ( ) } ` ) ;
1519 setIsLoading ( true ) ;
1620 timer = setTimeout ( function ( ) {
1721 timer = null ;
@@ -25,7 +29,7 @@ const UnsplashUploader = () => {
2529 } ;
2630 useEffect ( ( ) => {
2731 if ( imageSrc === '' ) {
28- fetch ( `https://picsum.photos/${ 280 } /${ 280 } ?random=${ Date . now ( ) } ` ) . then ( ( response ) => {
32+ fetch ( `https://picsum.photos/${ FIXED_HEIGHT * previewRatio } /${ FIXED_HEIGHT } ?random=${ Date . now ( ) } ` ) . then ( ( response ) => {
2933 setImageSrc ( response . url ) ;
3034 } ) ;
3135 }
0 commit comments