Skip to content

Commit b0b6e32

Browse files
authored
Merge pull request #316 from prgrms-web-devcourse-final-project/feature/spotify-image-optimization
[feat] Spotify 이미지 로딩 로직 최적화 및 ImageKit 적용
2 parents dfa1ad5 + 5071db3 commit b0b6e32

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

src/components/ImageKitImg.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import defaultImage from '@assets/images/default.png';
2+
interface ImageKitImgProps {
3+
src: string; // Spotify 이미지 URL
4+
width: number;
5+
height: number;
6+
alt?: string;
7+
className?: string;
8+
}
9+
10+
export default function ImageKitImg({
11+
src,
12+
width,
13+
height,
14+
alt = '앨범 이미지',
15+
className = '',
16+
}: ImageKitImgProps) {
17+
const imagePath = src?.split('/image/')[1];
18+
19+
return (
20+
<img
21+
className={className}
22+
src={`https://ik.imagekit.io/${import.meta.env.VITE_IMAGEKIT_ID}/${imagePath}?tr=w-${width},h-${height}`}
23+
onError={(e) => {
24+
const target = e.target as HTMLImageElement;
25+
target.onerror = null; // 무한 루프 방지
26+
target.src = defaultImage; // 고정 기본 이미지
27+
}}
28+
alt={alt}
29+
/>
30+
);
31+
}

src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import EmotionRecordCard from './user/EmotionRecordCard';
55
import UserProfileCard from './user/UserProfileCard';
66
import EmotionRecordSection from './user/EmotionRecordSection';
77
import EmotionRecordCardList from './user/EmotionRecordCardList';
8+
import ImageKitImg from './ImageKitImg';
89

910
export {
1011
InfoMessage,
@@ -14,4 +15,5 @@ export {
1415
MusicCard,
1516
EmotionRecordSection,
1617
EmotionRecordCardList,
18+
ImageKitImg,
1719
};

src/components/user/EmotionRecordCard.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { EmotionBadge } from '@/components';
1+
import { EmotionBadge, ImageKitImg } from '@/components';
22
import { formatDate } from '@/utils/formatDate';
3-
import defaultImage from '@assets/images/default.png';
43

54
interface EmotionRecordCardProps {
65
record: EmotionRecord;
@@ -22,14 +21,10 @@ function EmotionRecordCard({ record, onClick }: EmotionRecordCardProps) {
2221
{/* 감정 뱃지 */}
2322
<div className="flex flex-col gap-1.5">
2423
<EmotionBadge size="small" emotion={emotion} />
25-
<img
24+
<ImageKitImg
2625
src={albumImage}
27-
alt="앨범 이미지"
28-
onError={(e) => {
29-
const target = e.target as HTMLImageElement;
30-
target.onerror = null; // 무한 루프 방지
31-
target.src = defaultImage; // 기본 이미지로 변경
32-
}}
26+
height={232}
27+
width={232}
3328
className="object-cover w-full rounded-lg aspect-square"
3429
/>
3530
</div>

src/pages/home/components/MainCard.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { ImageKitImg } from '@/components';
12
import EmotionBadge from '@/components/EmotionBadge';
23
import { formatDate } from '@/utils/formatDate';
3-
import defaultImage from '@assets/images/default.png';
44
import { Link } from 'react-router';
55

66
interface MainCardProps {
@@ -25,15 +25,11 @@ export default function MainCard({ record }: MainCardProps) {
2525
<div className="flex gap-2">
2626
{/* 앨범커버 */}
2727
<div className="flex-shrink-0 w-16 h-16 overflow-hidden rounded-lg">
28-
<img
28+
<ImageKitImg
29+
src={record.spotifyMusic.albumImage}
30+
width={160}
31+
height={160}
2932
className="object-cover w-full h-full"
30-
src={record.spotifyMusic.albumImage || defaultImage}
31-
onError={(e) => {
32-
const target = e.target as HTMLImageElement;
33-
target.onerror = null; // 무한 루프 방지
34-
target.src = defaultImage; // 기본 이미지로 변경
35-
}}
36-
alt="앨범이미지"
3733
/>
3834
</div>
3935
{/* 정보 */}

0 commit comments

Comments
 (0)