-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fe-develop' of https://github.com/Kernel360/F2-BAGUNI i…
…nto front/feat/#876
- Loading branch information
Showing
8 changed files
with
135 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
frontend/techpick/src/components/DragOverlay/PickCarouselCardOverlay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use client'; | ||
|
||
import Image from 'next/image'; | ||
import { postRecommendPickViewEventLog } from '@/apis/eventLog'; | ||
import { useOpenUrlInNewTab, useImageLoader } from '@/hooks'; | ||
import { | ||
pickCarouselItemStyle, | ||
pickTitleStyle, | ||
pickImageStyle, | ||
defaultImageStyle, | ||
defaultImageLayoutStyle, | ||
} from '../RecommendedPickCarousel/pickCarouselCard.css'; | ||
import { RecommendPickType } from '@/types'; | ||
|
||
export function PickCarouselCardOverlay({ | ||
recommendPick, | ||
}: PickCarouselCardOverlayProps) { | ||
const { openUrlInNewTab } = useOpenUrlInNewTab(recommendPick.url); | ||
const { imageStatus } = useImageLoader(recommendPick.imageUrl); | ||
|
||
const onOpenLink = async () => { | ||
try { | ||
openUrlInNewTab(); | ||
await postRecommendPickViewEventLog({ url: recommendPick.url }); | ||
} catch { | ||
/*empty */ | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={pickCarouselItemStyle} onClick={onOpenLink}> | ||
{imageStatus === 'loaded' ? ( | ||
<img | ||
src={recommendPick.imageUrl} | ||
alt="" | ||
className={pickImageStyle} | ||
width="250" | ||
height="131" | ||
/> | ||
) : ( | ||
<div className={defaultImageLayoutStyle}> | ||
<Image | ||
src={'/image/default_image.svg'} | ||
alt="" | ||
className={`${defaultImageStyle}`} | ||
width="80" | ||
height="65" | ||
/> | ||
</div> | ||
)} | ||
|
||
<p className={pickTitleStyle}>{recommendPick.title}</p> | ||
</div> | ||
); | ||
} | ||
|
||
interface PickCarouselCardOverlayProps { | ||
recommendPick: RecommendPickType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useState, useLayoutEffect } from 'react'; | ||
|
||
type ImageStatus = 'loading' | 'loaded' | 'error'; | ||
|
||
/** | ||
* | ||
* @param imageUrl | ||
* @description imageUrl의 상태를 나타냅니다. 'loading' | 'loaded' | 'error' | ||
* @returns imageStatus | ||
*/ | ||
|
||
export function useImageLoader(imageUrl: string | undefined) { | ||
const [imageStatus, setImageStatus] = useState<ImageStatus>('loading'); | ||
|
||
useLayoutEffect(() => { | ||
if (!imageUrl) { | ||
setImageStatus('error'); | ||
return; | ||
} | ||
|
||
const img = new window.Image(); | ||
img.onload = () => setImageStatus('loaded'); | ||
img.onerror = () => setImageStatus('error'); | ||
img.src = imageUrl; | ||
|
||
return () => { | ||
img.onload = null; | ||
img.onerror = null; | ||
}; | ||
}, [imageUrl]); | ||
|
||
return { imageStatus }; | ||
} |