1
+ import { useState , useEffect , useRef } from "react" ;
1
2
import { useBoundStore } from "../../store/store" ;
3
+ import { ArtistInSong , TrackDetails } from "../../types/GlobalTypes" ;
2
4
import songfallback from "../../assets/icons8-song-fallback.png" ;
3
5
import speaker from "../../assets/speaker-svgrepo.svg" ;
4
6
import favorite from "../../assets/icons8-heart.svg" ;
@@ -14,12 +16,10 @@ import previous from "../../assets/previous.svg";
14
16
import next from "../../assets/next.svg" ;
15
17
import play from "../../assets/icons8-play.svg" ;
16
18
import pause from "../../assets/icons8-pause.svg" ;
17
- import { useState , useEffect , useRef } from "react" ;
18
19
import WaveSurfer from "wavesurfer.js" ;
19
20
import secondsToHMS from "../../utils/utils" ;
20
21
import tick from "../../assets/icons8-tick.svg" ;
21
22
import "../../App.css" ;
22
- import { ArtistInSong , TrackDetails } from "../../types/GlobalTypes" ;
23
23
24
24
export default function NowPlaying ( ) {
25
25
const {
@@ -38,7 +38,6 @@ export default function NowPlaying() {
38
38
setIsShuffling,
39
39
setHistory,
40
40
} = useBoundStore ( ) ;
41
- const [ volume , setVolume ] = useState < number > ( 0 ) ;
42
41
const [ currentTime , setCurrentTime ] = useState ( 0 ) ;
43
42
const wavesurfer = useRef < WaveSurfer | null > ( null ) ;
44
43
const songIndex = nowPlaying . queue . songs ?. findIndex (
@@ -84,7 +83,7 @@ export default function NowPlaying() {
84
83
function playOrder ( ) {
85
84
if ( isShuffling === false ) {
86
85
setIsPlaying ( true ) ;
87
- songIndex && setNowPlaying ( nowPlaying . queue . songs [ songIndex + 1 ] ) ;
86
+ songIndex && setNowPlaying ( nowPlaying . queue . songs [ songIndex ] ) ;
88
87
} else {
89
88
const randomIndex = Math . floor (
90
89
Math . random ( ) * nowPlaying . queue . songs . length ,
@@ -145,12 +144,16 @@ export default function NowPlaying() {
145
144
barWidth : 2 ,
146
145
minPxPerSec : 1 ,
147
146
height : 12 ,
147
+ duration : Number ( nowPlaying . track ?. duration ) ,
148
+ url : nowPlaying . track ?. downloadUrl [ 4 ] ?. url ,
148
149
} ) ;
149
150
nowPlaying . track &&
150
- wavesurfer . current ?. load ( nowPlaying . track . downloadUrl [ 4 ] . url ) ;
151
+ wavesurfer . current ?. load ( nowPlaying . track . downloadUrl [ 4 ] ? .url ) ;
151
152
wavesurfer . current ?. seekTo ( 0 ) ;
152
- wavesurfer . current ?. setVolume ( volume ) ;
153
153
}
154
+ wavesurfer . current ?. on ( "redrawcomplete" , ( ) => {
155
+ setIsPlaying ( true ) ;
156
+ } ) ;
154
157
wavesurfer . current ?. on ( "seeking" , ( ) => {
155
158
wavesurfer . current && setCurrentTime ( wavesurfer . current . getCurrentTime ( ) ) ;
156
159
} ) ;
@@ -165,9 +168,6 @@ export default function NowPlaying() {
165
168
playOrder ( ) ;
166
169
}
167
170
} ) ;
168
- wavesurfer . current ?. on ( "ready" , ( ) => {
169
- setIsPlaying ( true ) ;
170
- } ) ;
171
171
return ( ) => {
172
172
wavesurfer . current ?. destroy ( ) ;
173
173
wavesurfer . current ?. stop ( ) ;
@@ -180,7 +180,9 @@ export default function NowPlaying() {
180
180
< div className = "flex h-full w-[30%] max-w-[300px] items-center justify-center p-2.5" >
181
181
< img
182
182
src = {
183
- nowPlaying . track ? nowPlaying . track ?. image [ 2 ] ?. url : songfallback
183
+ nowPlaying . track && nowPlaying . track . id !== ""
184
+ ? nowPlaying . track ?. image [ 2 ] ?. url
185
+ : songfallback
184
186
}
185
187
id = "songImg"
186
188
alt = "song-img"
@@ -267,7 +269,7 @@ export default function NowPlaying() {
267
269
outline : "none" ,
268
270
} }
269
271
className = { `h-auto w-auto rounded-full border-none bg-neutral-100 p-1 outline-none disabled:cursor-not-allowed disabled:bg-neutral-600` }
270
- // disabled={isReady === false }
272
+ disabled = { ! nowPlaying . track ?. id }
271
273
>
272
274
< img
273
275
src = { pause }
@@ -286,7 +288,7 @@ export default function NowPlaying() {
286
288
outline : "none" ,
287
289
} }
288
290
className = { `h-auto w-auto rounded-full border-none bg-neutral-100 p-1 outline-none disabled:cursor-not-allowed disabled:bg-neutral-600` }
289
- // disabled={isReady === false }
291
+ disabled = { ! nowPlaying . track ?. id }
290
292
>
291
293
< img
292
294
src = { play }
@@ -337,7 +339,7 @@ export default function NowPlaying() {
337
339
< p className = "h-full w-[50px] text-center text-[12px] text-white" >
338
340
{ nowPlaying . track ?. duration
339
341
? secondsToHMS ( Number ( nowPlaying . track ?. duration ) )
340
- : "" }
342
+ : "--:-- " }
341
343
</ p >
342
344
</ div >
343
345
</ div >
@@ -428,18 +430,26 @@ export default function NowPlaying() {
428
430
min = { 0 }
429
431
className = "h-[3px] w-full appearance-none transition-all ease-linear disabled:cursor-not-allowed"
430
432
max = { 1 }
431
- step = { 0.05 }
432
- value = { volume }
433
- onChange = { ( e ) => setVolume ( Number ( e . target . value ) ) }
433
+ step = { 0.1 }
434
+ value = { wavesurfer . current ?. getVolume ( ) }
435
+ onChange = { ( e ) =>
436
+ wavesurfer . current ?. setVolume ( Number ( e . target . value ) )
437
+ }
434
438
disabled = { nowPlaying . track ?. id === "" }
435
439
/>
436
440
< img
437
- src = { volume > 0.1 ? ( volume < 0.7 ? vol : high ) : mute }
441
+ src = {
442
+ wavesurfer . current && wavesurfer . current ?. getVolume ( ) > 0.1
443
+ ? wavesurfer . current ?. getVolume ( ) < 0.7
444
+ ? vol
445
+ : high
446
+ : mute
447
+ }
438
448
alt = "volume"
439
449
className = { `-mr-1 ml-1 h-[28px] w-[28px] bg-transparent ${
440
450
nowPlaying . track ?. id === "" ? "invert-[0.4]" : ""
441
451
} ${
442
- volume === 0 && "invert-[0.5]"
452
+ wavesurfer . current ?. getVolume ( ) === 0 && "invert-[0.5]"
443
453
} disabled:cursor-not-allowed disabled:bg-neutral-900`}
444
454
/>
445
455
</ div >
0 commit comments