1- import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
1+ import { useCallback , useEffect , useMemo , useState } from 'react'
22import { useParams } from 'react-router-dom'
3- import type { YouTubeEvent } from 'react-youtube'
43
54import styled from 'styled-components'
65
@@ -20,18 +19,19 @@ import { PlaylistLayout, YoutubePlayer } from '@/widgets/playlist'
2019const DiscoverPage = ( ) => {
2120 const { id : playlistId } = useParams ( )
2221 const {
23- setPlaylist,
24- isPlaying,
2522 currentPlaylist,
2623 currentTrackIndex,
27- nextTrack,
28- prevTrack,
24+ currentTime,
25+ isPlaying,
26+ playerRef,
27+ setPlaylist,
2928 play,
3029 pause,
31- currentTime,
32- updateCurrentTime,
30+ nextTrack,
31+ prevTrack,
32+ handlePlayerStateChange,
3333 } = usePlaylist ( )
34- const playerRef = useRef < YT . Player | null > ( null )
34+
3535 const [ showCoachmark , setShowCoachmark ] = useState ( false )
3636 const [ isMuted , setIsMuted ] = useState < boolean | null > ( null )
3737
@@ -96,8 +96,6 @@ const DiscoverPage = () => {
9696 return [ playlistAsInfo , ...shufflePlaylists ]
9797 } , [ playlistAsInfo , shufflePlaylists ] )
9898
99- console . log ( playlistsData )
100-
10199 const videoId = currentPlaylist
102100 ? getVideoId ( currentPlaylist . songs [ currentTrackIndex ] ?. youtubeUrl )
103101 : null
@@ -109,11 +107,11 @@ const DiscoverPage = () => {
109107 if ( ! currentPlaylist && playlistsData . length > 0 && isReady ) {
110108 const initialPlaylist =
111109 playlistsData . find ( ( p ) => p . playlistId === Number ( playlistId ) ) || playlistsData [ 0 ]
112-
113110 setPlaylist ( initialPlaylist , 0 , 0 )
114111 }
115- } , [ playlistsData , currentPlaylist , playlistId , setPlaylist ] )
112+ } , [ playlistsData , currentPlaylist , playlistId , setPlaylist , isReady ] )
116113
114+ // 재생, 확인, 조회수 refetch
117115 useEffect ( ( ) => {
118116 if ( ! currentPlaylist || ! isPlaying ) return
119117 startPlaylist ( currentPlaylist . playlistId )
@@ -135,21 +133,6 @@ const DiscoverPage = () => {
135133 }
136134 } , [ currentPlaylist , isPlaying , startPlaylist , confirmPlaylist , refetchViewCounts ] )
137135
138- // 현재 재생 시간 업데이트
139- useEffect ( ( ) => {
140- const intervalId = setInterval ( ( ) => {
141- if ( playerRef . current ) updateCurrentTime ( playerRef . current . getCurrentTime ( ) )
142- } , 1000 )
143- return ( ) => clearInterval ( intervalId )
144- } , [ updateCurrentTime ] )
145-
146- // 재생, 일시정지
147- useEffect ( ( ) => {
148- if ( ! playerRef . current ) return
149- if ( isPlaying ) playerRef . current . playVideo ( )
150- else playerRef . current . pauseVideo ( )
151- } , [ isPlaying ] )
152-
153136 // 캐러셀 스와이프 시 현재 플레이리스트 업데이트
154137 const handleSelectPlaylist = useCallback (
155138 ( index : number ) => {
@@ -162,48 +145,26 @@ const DiscoverPage = () => {
162145 fetchNextPage ( )
163146 }
164147 } ,
165- [ setPlaylist , currentPlaylist , playlistsData , fetchNextPage , hasNextPage , isFetchingNextPage ]
148+ [ playlistsData , currentPlaylist , setPlaylist , hasNextPage , isFetchingNextPage , fetchNextPage ]
166149 )
167150
168- const handlePlayerStateChange = useCallback (
169- ( event : YouTubeEvent ) => {
170- if ( event . data === window . YT . PlayerState . ENDED ) nextTrack ( )
171- } ,
172- [ nextTrack ]
173- )
174-
175- const handlePlayPause = ( ) => {
176- if ( isPlaying ) pause ( )
177- else play ( )
178- }
179-
180- const isCurrentlyPlaying = ( ( ) => {
181- if ( ! window . YT || ! playerRef . current ) return false
182- return isPlaying && playerRef . current . getPlayerState ( ) === window . YT . PlayerState . PLAYING
183- } ) ( )
184-
185151 return (
186152 < Page >
187153 { showCoachmark && < DiscoverCoachMark onClick = { handleCloseCoachmark } /> }
188154 < SwipeCarousel data = { playlistsData } onSelectIndexChange = { handleSelectPlaylist } >
189155 { playlistsData . map ( ( data ) => (
190156 < Slide key = { data . playlistId } >
191157 < PlaylistLayout
192- key = { data . playlistId }
193158 data = { data }
194159 currentPlaylist = { currentPlaylist }
195160 currentTrackIndex = { currentTrackIndex }
196161 currentTime = { currentTime }
197- isPlaying = { isCurrentlyPlaying }
198- onPlayPause = { handlePlayPause }
162+ isPlaying = { isPlaying }
163+ onPlayPause = { ( ) => ( isPlaying ? pause ( ) : play ( ) ) }
199164 onNext = { nextTrack }
200165 onPrev = { prevTrack }
201166 onSelectTrack = { ( trackIndex , time ) => {
202- if ( currentPlaylist ) {
203- setPlaylist ( currentPlaylist , trackIndex )
204- if ( time !== undefined ) playerRef . current ?. seekTo ( time , true )
205- if ( ! isPlaying ) play ( )
206- }
167+ if ( currentPlaylist ) setPlaylist ( currentPlaylist , trackIndex , time )
207168 } }
208169 playerRef = { playerRef }
209170 isMuted = { isMuted }
@@ -212,22 +173,16 @@ const DiscoverPage = () => {
212173 </ Slide >
213174 ) ) }
214175 </ SwipeCarousel >
215- { ! showCoachmark && videoId && (
176+
177+ { videoId && (
216178 < YoutubePlayer
217179 videoId = { videoId }
218180 onReady = { ( event ) => {
219181 playerRef . current = event . target
220-
221- // 현 상태 참조해서 동기화
182+ playerRef . current ?. seekTo ( currentTime , true )
183+ if ( isPlaying ) playerRef . current ?. playVideo ( )
184+ else playerRef . current ?. pauseVideo ( )
222185 setIsMuted ( playerRef . current ?. isMuted ( ) ?? null )
223-
224- if ( isPlaying ) {
225- playerRef . current ?. seekTo ( currentTime , true )
226- playerRef . current ?. playVideo ( )
227- } else {
228- playerRef . current ?. seekTo ( currentTime , true )
229- playerRef . current ?. pauseVideo ( )
230- }
231186 } }
232187 onStateChange = { handlePlayerStateChange }
233188 />
@@ -241,7 +196,6 @@ export default DiscoverPage
241196const Slide = styled . div `
242197 flex: 0 0 100%;
243198`
244-
245199const Page = styled . div `
246200 position: relative;
247201`
0 commit comments