1- import { useRecoilState } from 'recoil' ;
2- import { isImageBright , previewImage } from '../atom' ;
31import { Icon } from '@iconify/react' ;
4- import { useEffect , useRef , useState } from 'react' ;
2+ import { useEffect , useMemo , useRef , useState } from 'react' ;
3+ import { useRecoilState , useRecoilValue } from 'recoil' ;
4+ import { isImageBright , previewImage , ratioAtom } from '../atom' ;
5+
6+ const HEIGHT = 280 ;
57
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+ const width = useMemo ( ( ) => Math . round ( HEIGHT * previewRatio ) , [ previewRatio ] ) ;
16+
1217 const getRandomImage = async ( ) => {
1318 if ( ! timer ) {
14- const response = await fetch ( ' https://source.unsplash.com/ random' ) ;
19+ const response = await fetch ( ` https://picsum.photos/ ${ width } / ${ HEIGHT } ? random= ${ Date . now ( ) } ` ) ;
1520 setIsLoading ( true ) ;
1621 timer = setTimeout ( function ( ) {
1722 timer = null ;
@@ -25,7 +30,7 @@ const UnsplashUploader = () => {
2530 } ;
2631 useEffect ( ( ) => {
2732 if ( imageSrc === '' ) {
28- fetch ( ' https://source.unsplash.com/ random' ) . then ( ( response ) => {
33+ fetch ( ` https://picsum.photos/ ${ width } / ${ HEIGHT } ? random= ${ Date . now ( ) } ` ) . then ( ( response ) => {
2934 setImageSrc ( response . url ) ;
3035 } ) ;
3136 }
@@ -41,7 +46,7 @@ const UnsplashUploader = () => {
4146 }
4247 >
4348 < Icon className = { 'w-6 h-6 ' + `${ isLoading && `animate-spinner` } ` } icon = "tabler:refresh" vFlip = { true } />
44- < div className = "text-sm" > Unsplash 랜덤 이미지 삽입</ div >
49+ < div className = "text-sm" > PICSUM 랜덤 이미지 삽입</ div >
4550 </ div >
4651 </ button >
4752 < label className = { `${ isBright ? 'text-darken' : 'text-muted' } flex justify-start items-center mt-3` } >
0 commit comments