Skip to content

Commit 977a9b4

Browse files
authored
Merge pull request #95 from dnd-side-project/fix/#94/index
[fix] ๋‘˜๋Ÿฌ๋ณด๊ธฐ selectedIndex ์˜ค๋ฅ˜ ์ˆ˜์ •
2 parents ad06a47 + a49e9d1 commit 977a9b4

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

โ€Žsrc/features/swipe/ui/SwipeCarousel.tsxโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const SwipeCarousel = ({ children, data, onSelectIndexChange }: SwipeCarouselPro
3232
if (!emblaApi || data.length === 0) return
3333

3434
const selectedIndex = emblaApi.selectedScrollSnap()
35+
console.log('selectedIndex ', selectedIndex)
3536
onSelectIndexChange?.(selectedIndex) // ๋ถ€๋ชจ์— ์•Œ๋ฆผ
3637
const newId = data[selectedIndex]?.playlistId
3738

โ€Žsrc/pages/discover/index.tsxโ€Ž

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
2-
import { useParams } from 'react-router-dom'
2+
import { useNavigate, useParams } from 'react-router-dom'
33
import type { YouTubeEvent } from 'react-youtube'
44

55
import styled from 'styled-components'
@@ -34,6 +34,8 @@ const DiscoverPage = () => {
3434
const playerRef = useRef<YT.Player | null>(null)
3535
const [showCoachmark, setShowCoachmark] = useState(false)
3636
const [isMuted, setIsMuted] = useState<boolean | null>(null)
37+
const [, setCurrentIndex] = useState(0)
38+
const navigate = useNavigate()
3739

3840
// ์ฝ”์น˜๋งˆํฌ
3941
useEffect(() => {
@@ -81,15 +83,28 @@ const DiscoverPage = () => {
8183

8284
// ์ตœ์ข… ๋ฐฐ์—ด
8385
const playlistsData = useMemo(() => {
84-
if (playlistAsInfo) {
85-
return [
86-
playlistAsInfo,
87-
...shufflePlaylists.filter((p) => p.playlistId !== playlistAsInfo.playlistId),
88-
]
86+
if (!playlistAsInfo) {
87+
return shufflePlaylists
8988
}
90-
return shufflePlaylists
89+
// URL์—์„œ ๊ฐ€์ ธ์˜จ ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ๋ฅผ playlistsData ๋ฐฐ์—ด์— ์ถ”๊ฐ€
90+
// ๊ธฐ์กด์— shufflePlaylists์— ์žˆ๋Š” ๊ฒฝ์šฐ, ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ถ”๊ฐ€
91+
const existingPlaylist = shufflePlaylists.find(
92+
(p) => p.playlistId === playlistAsInfo.playlistId
93+
)
94+
if (existingPlaylist) {
95+
return shufflePlaylists
96+
}
97+
return [playlistAsInfo, ...shufflePlaylists]
9198
}, [playlistAsInfo, shufflePlaylists])
9299

100+
// ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ ID๋กœ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
101+
const getPlaylistIndexById = useCallback(
102+
(id: number) => {
103+
return playlistsData.findIndex((p) => p.playlistId === id)
104+
},
105+
[playlistsData]
106+
)
107+
93108
const videoId = currentPlaylist
94109
? getVideoId(currentPlaylist.songs[currentTrackIndex]?.youtubeUrl)
95110
: null
@@ -98,12 +113,14 @@ const DiscoverPage = () => {
98113

99114
// ์ตœ์ดˆ playlist ์ดˆ๊ธฐํ™”
100115
useEffect(() => {
101-
if (!currentPlaylist && playlistsData.length > 0 && isReady) {
102-
const initialPlaylist =
103-
playlistsData.find((p) => p.playlistId === Number(playlistId)) || playlistsData[0]
116+
if (playlistsData.length > 0 && isReady) {
117+
const initialIndex = getPlaylistIndexById(Number(playlistId))
118+
const initialPlaylist = playlistsData[initialIndex > -1 ? initialIndex : 0]
119+
120+
setCurrentIndex(initialIndex > -1 ? initialIndex : 0)
104121
setPlaylist(initialPlaylist, 0, 0)
105122
}
106-
}, [playlistsData, currentPlaylist, playlistId, setPlaylist])
123+
}, [playlistsData, playlistId, getPlaylistIndexById, setPlaylist])
107124

108125
// // URL์„ ํ˜„์žฌ ์„ ํƒ๋œ ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ๋กœ ๋™๊ธฐํ™”
109126
// useEffect(() => {
@@ -155,15 +172,34 @@ const DiscoverPage = () => {
155172
// ์บ๋Ÿฌ์…€ ์Šค์™€์ดํ”„ ์‹œ ํ˜„์žฌ ํ”Œ๋ ˆ์ด๋ฆฌ์ŠคํŠธ ์—…๋ฐ์ดํŠธ
156173
const handleSelectPlaylist = useCallback(
157174
(index: number) => {
175+
// ํ˜„์žฌ ์ธ๋ฑ์Šค ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธ
176+
setCurrentIndex(index)
177+
158178
const selectedPlaylist = playlistsData[index]
159179
if (selectedPlaylist && currentPlaylist?.playlistId !== selectedPlaylist.playlistId) {
160180
setPlaylist(selectedPlaylist, 0, 0)
181+
182+
// URL์„ ๋™๊ธฐํ™”
183+
const newId = selectedPlaylist.playlistId
184+
if (Number(playlistId) !== newId) {
185+
navigate(`/discover/${newId}`, { replace: true })
186+
}
161187
}
188+
162189
if (index === playlistsData.length - 1 && hasNextPage && !isFetchingNextPage) {
163190
fetchNextPage()
164191
}
165192
},
166-
[setPlaylist, currentPlaylist, playlistsData, fetchNextPage, hasNextPage, isFetchingNextPage]
193+
[
194+
setPlaylist,
195+
currentPlaylist,
196+
playlistsData,
197+
fetchNextPage,
198+
hasNextPage,
199+
isFetchingNextPage,
200+
navigate,
201+
playlistId,
202+
]
167203
)
168204

169205
const handlePlayerStateChange = useCallback(

โ€Žsrc/shared/ui/Cd.tsxโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const Cd = ({ variant, bgColor = 'default', stickers }: CdProps) => {
5959
key={`${cdItemId}-${xCoordinate}-${yCoordinate}`}
6060
src={src}
6161
alt="cd-sticker"
62+
// crossOrigin="anonymous"
6263
style={{
6364
position: 'absolute',
6465
left: xCoordinate * ratio,

โ€Žsrc/widgets/chat/ChatBottomSheet.tsxโ€Ž

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ const ChatBottomSheet = ({ isOpen, onClose, roomId, creatorId }: ChatBottomSheet
5555
}
5656
}
5757

58-
console.log(allMessages)
59-
6058
return (
6159
<BottomSheet isOpen={isOpen} onClose={onClose} height="fit-content">
6260
<Title>์‹ค์‹œ๊ฐ„ ์ฑ„ํŒ…</Title>

0 commit comments

Comments
ย (0)