11import { Icon } from '@iconify/react' ;
2- import { useEffect , useMemo , useRef , useState } from 'react' ;
3- import { useRecoilState , useRecoilValue } from 'recoil' ;
4- import { isImageBright , previewImage , ratioAtom } from '../atom' ;
2+ import { useEffect , useRef , useState } from 'react' ;
3+ import { useRecoilState } from 'recoil' ;
4+ import { isImageBright , previewImage } from '../atom' ;
55
6- const HEIGHT = 280 ;
6+ const FIXED_RATIO = 16 / 9 ;
7+ const FIXED_HEIGHT = 450 ;
8+ const FIXED_WIDTH = FIXED_HEIGHT * FIXED_RATIO ; // 고정 값으로 이미지 랜더링
9+ const RANDOM_URL = `https://picsum.photos/${ FIXED_WIDTH } /${ FIXED_HEIGHT } ` ; //
710
811const UnsplashUploader = ( ) => {
912 let timer : NodeJS . Timer | null = null ;
1013 const [ imageSrc , setImageSrc ] = useRecoilState ( previewImage ) ;
1114 const [ isBright , setIsBright ] = useRecoilState ( isImageBright ) ;
12- const previewRatio = useRecoilValue ( ratioAtom ) ;
1315 const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
1416 const mounted = useRef ( false ) ;
15- const width = useMemo ( ( ) => Math . round ( HEIGHT * previewRatio ) , [ previewRatio ] ) ;
1617
1718 const getRandomImage = async ( ) => {
1819 if ( ! timer ) {
19- const response = await fetch ( `https://picsum.photos/ ${ width } / ${ HEIGHT } ?random=${ Date . now ( ) } ` ) ;
20+ const response = await fetch ( `${ RANDOM_URL } ?random=${ Date . now ( ) } ` ) ;
2021 setIsLoading ( true ) ;
2122 timer = setTimeout ( function ( ) {
2223 timer = null ;
@@ -25,13 +26,17 @@ const UnsplashUploader = () => {
2526 } , 1100 ) ;
2627 }
2728 } ;
28- const brigthControl = ( ) => {
29+
30+ const brightControl = ( ) => {
2931 setIsBright ( ( prev ) => ! prev ) ;
3032 } ;
33+
3134 useEffect ( ( ) => {
3235 if ( imageSrc === '' ) {
33- fetch ( `https://picsum.photos/${ width } /${ HEIGHT } ?random=${ Date . now ( ) } ` ) . then ( ( response ) => {
36+ setIsLoading ( true ) ;
37+ fetch ( `${ RANDOM_URL } ?random=${ Date . now ( ) } ` ) . then ( ( response ) => {
3438 setImageSrc ( response . url ) ;
39+ setIsLoading ( false ) ;
3540 } ) ;
3641 }
3742 mounted . current = true ;
@@ -50,7 +55,7 @@ const UnsplashUploader = () => {
5055 </ div >
5156 </ button >
5257 < label className = { `${ isBright ? 'text-darken' : 'text-muted' } flex justify-start items-center mt-3` } >
53- < input defaultChecked = { ! isBright } className = "h-4 w-4" type = "checkbox" onClick = { brigthControl } />
58+ < input defaultChecked = { ! isBright } className = "h-4 w-4" type = "checkbox" onClick = { brightControl } />
5459 < span className = "ml-[10px]" > 배경 어둡게</ span >
5560 </ label >
5661 </ div >
0 commit comments