8
8
playSong ,
9
9
endSong ,
10
10
stopSong ,
11
- youtubeAPIReady
11
+ youtubeAPIReady ,
12
12
} from 'slices/youtubeSlice' ;
13
13
import { ClientPositionContext } from 'contexts/clientposition' ;
14
14
import { Box } from '@chakra-ui/react' ;
@@ -88,53 +88,64 @@ function YoutubePlayer(props) {
88
88
const player = useRef ( null ) ;
89
89
const boundingBox = useRef ( baseBoundingBox ) ;
90
90
const song = useSelector ( state => state . currentRoom . data . currentSong ) ;
91
- const isYouTubeAPIReady = useSelector ( state => state . youtube . isYouTubeAPIReady )
92
-
93
- const onError = useCallback ( event => {
94
- const { data } = event ;
95
-
96
- dispatch ( reportError ( data ) ) ;
97
- } , [ dispatch ] )
98
-
99
- const onPlayerStateChange = useCallback ( event => {
100
- const { data } = event ;
101
-
102
- switch ( data ) {
103
- case - 1 :
104
- dispatch ( clearError ( ) ) ;
105
- break ;
106
- case 0 :
107
- dispatch ( endSong ( ) ) ;
108
- dispatch ( clearError ( ) ) ;
109
- break ;
110
- case 1 :
111
- dispatch ( playSong ( ) ) ;
112
- break ;
113
- case 2 :
114
- dispatch ( clearError ( ) ) ;
115
- break ;
116
- case 3 :
117
- dispatch ( clearError ( ) ) ;
118
- break ;
119
- case 5 :
120
- dispatch ( clearError ( ) ) ;
121
- break ;
122
- default :
123
- break ;
124
- }
125
- } , [ dispatch ] )
91
+ const isYouTubeAPIReady = useSelector (
92
+ state => state . youtube . isYouTubeAPIReady
93
+ ) ;
126
94
127
- const onPlayerReady = useCallback ( event => {
128
- event . target . playVideo ( ) ;
95
+ const onError = useCallback (
96
+ event => {
97
+ const { data } = event ;
129
98
130
- if ( ! isAuth ) {
131
- // Set player volume for non authed users,
132
- // because they can't control volume by moving bubble
133
- dispatch ( changeVolume ( 50 ) ) ;
134
- }
99
+ dispatch ( reportError ( data ) ) ;
100
+ } ,
101
+ [ dispatch ]
102
+ ) ;
103
+
104
+ const onPlayerStateChange = useCallback (
105
+ event => {
106
+ const { data } = event ;
107
+
108
+ switch ( data ) {
109
+ case - 1 :
110
+ dispatch ( clearError ( ) ) ;
111
+ break ;
112
+ case 0 :
113
+ dispatch ( endSong ( ) ) ;
114
+ dispatch ( clearError ( ) ) ;
115
+ break ;
116
+ case 1 :
117
+ dispatch ( playSong ( ) ) ;
118
+ break ;
119
+ case 2 :
120
+ dispatch ( clearError ( ) ) ;
121
+ break ;
122
+ case 3 :
123
+ dispatch ( clearError ( ) ) ;
124
+ break ;
125
+ case 5 :
126
+ dispatch ( clearError ( ) ) ;
127
+ break ;
128
+ default :
129
+ break ;
130
+ }
131
+ } ,
132
+ [ dispatch ]
133
+ ) ;
135
134
136
- boundingBox . current = event . target . getIframe ( ) . getBoundingClientRect ( ) ;
137
- } , [ dispatch , isAuth ] )
135
+ const onPlayerReady = useCallback (
136
+ event => {
137
+ event . target . playVideo ( ) ;
138
+
139
+ if ( ! isAuth ) {
140
+ // Set player volume for non authed users,
141
+ // because they can't control volume by moving bubble
142
+ dispatch ( changeVolume ( 50 ) ) ;
143
+ }
144
+
145
+ boundingBox . current = event . target . getIframe ( ) . getBoundingClientRect ( ) ;
146
+ } ,
147
+ [ dispatch , isAuth ]
148
+ ) ;
138
149
139
150
const createPlayer = useCallback ( ( ) => {
140
151
player . current = new window . YT . Player ( 'youtube-player' , {
@@ -156,7 +167,7 @@ function YoutubePlayer(props) {
156
167
onError : onError ,
157
168
} ,
158
169
} ) ;
159
- } , [ height , width , song , onPlayerReady , onPlayerStateChange , onError ] )
170
+ } , [ height , width , song , onPlayerReady , onPlayerStateChange , onError ] ) ;
160
171
161
172
function updateSong ( song ) {
162
173
if ( song ?. videoId == null ) {
@@ -176,13 +187,13 @@ function YoutubePlayer(props) {
176
187
useEffect ( ( ) => {
177
188
// callback for youtube iframe api
178
189
window . onYouTubeIframeAPIReady = ( ) => {
179
- dispatch ( youtubeAPIReady ( ) )
180
- }
190
+ dispatch ( youtubeAPIReady ( ) ) ;
191
+ } ;
181
192
182
193
// if youtube iframe api was loaded before
183
194
if ( isYouTubeAPIReady ) {
184
195
if ( ! song ) {
185
- return
196
+ return ;
186
197
}
187
198
188
199
// create a youtube player if there isn't already one
0 commit comments