Skip to content

Commit

Permalink
Merge pull request #217 from softeerbootcamp4th/feat/216
Browse files Browse the repository at this point in the history
[Fix] 이미지 캐러셀 오류 수정
  • Loading branch information
moana16 authored Aug 25, 2024
2 parents 2ffd278 + 0232853 commit 29fca26
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 45 deletions.
42 changes: 6 additions & 36 deletions service/src/components/MainPage/CarInfoSection/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,24 @@ import CarouselBg from './CarouselBg';
import { motion } from 'framer-motion';
import { FLOATING_CAROUSEL } from '@/constants/animation';
import { CarInfoList } from '@/types/main/type';
import { getTransformClass } from '@/utils/main/getTransformClass';
import { getVisibleItems } from '@/utils/main/getVisibleItems';

interface CarouselProps {
carInfoList: CarInfoList[];
}

const Carousel = ({ carInfoList }: CarouselProps) => {
const { state, openCarDetail, selectCurrentIndex } = useCarInfoContext();

const handleSlideClick = (index: number) => {
if (index >= 0 && index < carInfoList.length) {
selectCurrentIndex({ index: index });
}
};

const getVisibleItems = () => {
switch (state.currentIndex) {
case 0:
return [carInfoList[0], carInfoList[1], carInfoList[2]];
case 1:
return [carInfoList[0], carInfoList[1], carInfoList[2], carInfoList[3]];
case 2:
return carInfoList;
case 3:
return [carInfoList[1], carInfoList[2], carInfoList[3], carInfoList[4]];
case 4:
return [carInfoList[2], carInfoList[3], carInfoList[4]];
default:
return carInfoList;
}
};

const visibleItems = getVisibleItems();
const visibleItems = getVisibleItems(state.currentIndex, carInfoList);

const getTransformClass = (id: number) => {
switch (id) {
case 1:
return 'translate-x-custom-1';
case 2:
return 'translate-x-custom-2';
case 3:
return '';
case 4:
return 'translate-x-custom-4';
case 5:
return 'translate-x-custom-5';
default:
return '';
}
};
return (
<div className='snap-center carousel-container'>
<CarouselBg currentIdx={state.currentIndex} />
Expand All @@ -79,7 +49,7 @@ const Carousel = ({ carInfoList }: CarouselProps) => {
<VideoPlayer videoUrl={item.imgUrl} />
) : (
<motion.div
className='h-[422px] relative transform duration-200 px-2'
className='h-[422px] relative transform duration-200'
style={{
width: '784px',
transform: `translateX(${-(item.id - 3) * 50}px)`,
Expand All @@ -91,7 +61,7 @@ const Carousel = ({ carInfoList }: CarouselProps) => {
alt={item.title}
className='w-full h-full object-cover transform duration-200'
/>
<div className='w-full h-full absolute top-0 bg-gradient-bottom-gray' />
<div className='w-full h-full absolute top-0 left-0 bg-gradient-bottom-gray' />
<motion.div
className={`absolute top-12 right-12 `}
{...FLOATING_CAROUSEL}
Expand Down
12 changes: 3 additions & 9 deletions service/src/components/MainPage/CarInfoSection/VideoPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useVideoMetadata from '@/hooks/MainPage/useVideoMetaData';
import { useCarInfoContext } from '@/store/context/useCarInfoContext';
import React, { useEffect, useRef, useState } from 'react';
import React, { useRef, useState } from 'react';

interface VideoPlayerProps {
videoUrl: string;
Expand All @@ -12,16 +13,9 @@ const VideoPlayer = ({ videoUrl }: VideoPlayerProps) => {
const [isPlaying, setIsPlaying] = useState(true);
const [isMuted, setIsMuted] = useState(true);
const [progress, setProgress] = useState(0);
const [duration, setDuration] = useState(0);
const [isHovered, setIsHovered] = useState(false);

useEffect(() => {
if (videoRef.current) {
videoRef.current.addEventListener('loadedmetadata', () => {
setDuration(videoRef.current!.duration);
});
}
});
const duration = useVideoMetadata(videoRef);

const handlePlayPause = () => {
if (videoRef.current) {
Expand Down
27 changes: 27 additions & 0 deletions service/src/hooks/MainPage/useVideoMetaData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect, RefObject } from 'react';

const useVideoMetadata = (videoRef: RefObject<HTMLVideoElement>) => {
const [duration, setDuration] = useState(0);

useEffect(() => {
if (videoRef.current) {
const handleLoadedMetadata = () => {
setDuration(videoRef.current!.duration);
};

const videoElement = videoRef.current;
videoElement.addEventListener('loadedmetadata', handleLoadedMetadata);

return () => {
videoElement.removeEventListener(
'loadedmetadata',
handleLoadedMetadata
);
};
}
}, [videoRef]);

return duration;
};

export default useVideoMetadata;
16 changes: 16 additions & 0 deletions service/src/utils/main/getTransformClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const getTransformClass = (id: number) => {
switch (id) {
case 1:
return 'translate-x-custom-1';
case 2:
return 'translate-x-custom-2';
case 3:
return '';
case 4:
return 'translate-x-custom-4';
case 5:
return 'translate-x-custom-5';
default:
return '';
}
};
21 changes: 21 additions & 0 deletions service/src/utils/main/getVisibleItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CarInfoList } from '@/types/main/type';

export const getVisibleItems = (
currentIndex: number,
carInfoList: CarInfoList[]
): CarInfoList[] => {
switch (currentIndex) {
case 0:
return [carInfoList[0], carInfoList[1], carInfoList[2]];
case 1:
return [carInfoList[0], carInfoList[1], carInfoList[2], carInfoList[3]];
case 2:
return carInfoList;
case 3:
return [carInfoList[1], carInfoList[2], carInfoList[3], carInfoList[4]];
case 4:
return [carInfoList[2], carInfoList[3], carInfoList[4]];
default:
return carInfoList;
}
};

0 comments on commit 29fca26

Please sign in to comment.