1+ import { Icon } from '@iconify/react' ;
2+ import { useEffect , useMemo , useRef , useState } from 'react' ;
13import { useRecoilState , useRecoilValue } from 'recoil' ;
24import { isImageBright , previewImage , ratioAtom } from '../atom' ;
3- import { Icon } from '@iconify/react' ;
4- import { useEffect , useRef , useState } from 'react' ;
55
6- const FIXED_HEIGHT = 280 ;
6+ const HEIGHT = 280 ;
77
88const UnsplashUploader = ( ) => {
99 let timer : NodeJS . Timer | null = null ;
@@ -12,10 +12,11 @@ const UnsplashUploader = () => {
1212 const previewRatio = useRecoilValue ( ratioAtom ) ;
1313 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
1414 const mounted = useRef ( false ) ;
15-
15+ const width = useMemo ( ( ) => Math . round ( HEIGHT * previewRatio ) , [ previewRatio ] ) ;
16+
1617 const getRandomImage = async ( ) => {
1718 if ( ! timer ) {
18- const response = await fetch ( `https://picsum.photos/${ FIXED_HEIGHT * previewRatio } /${ FIXED_HEIGHT } ?random=${ Date . now ( ) } ` ) ;
19+ const response = await fetch ( `https://picsum.photos/${ width } /${ HEIGHT } ?random=${ Date . now ( ) } ` ) ;
1920 setIsLoading ( true ) ;
2021 timer = setTimeout ( function ( ) {
2122 timer = null ;
@@ -29,7 +30,7 @@ const UnsplashUploader = () => {
2930 } ;
3031 useEffect ( ( ) => {
3132 if ( imageSrc === '' ) {
32- fetch ( `https://picsum.photos/${ FIXED_HEIGHT * previewRatio } /${ FIXED_HEIGHT } ?random=${ Date . now ( ) } ` ) . then ( ( response ) => {
33+ fetch ( `https://picsum.photos/${ width } /${ HEIGHT } ?random=${ Date . now ( ) } ` ) . then ( ( response ) => {
3334 setImageSrc ( response . url ) ;
3435 } ) ;
3536 }
@@ -45,7 +46,7 @@ const UnsplashUploader = () => {
4546 }
4647 >
4748 < Icon className = { 'w-6 h-6 ' + `${ isLoading && `animate-spinner` } ` } icon = "tabler:refresh" vFlip = { true } />
48- < div className = "text-sm" > Unsplash 랜덤 이미지 삽입</ div >
49+ < div className = "text-sm" > PICSUM 랜덤 이미지 삽입</ div >
4950 </ div >
5051 </ button >
5152 < label className = { `${ isBright ? 'text-darken' : 'text-muted' } flex justify-start items-center mt-3` } >
0 commit comments