1- import { useState , useEffect } from ' react' ;
2- import { Music , Loader } from ' lucide-react' ;
1+ import { useState , useEffect } from " react" ;
2+ import { Music , Loader } from " lucide-react" ;
33
4- const playlists = [
5- "58ggvvTcs95yhcSeSxLGks" , // Playlist 1
6- "6e2q3GgjEDxMWJBSln2Py5" // Playlist 2
4+ interface Track {
5+ name : string ;
6+ artist : string ;
7+ playlistId ?: string ;
8+ }
9+
10+ const PLAYLISTS = [
11+ "58ggvvTcs95yhcSeSxLGks" ,
12+ "6e2q3GgjEDxMWJBSln2Py5"
713] ;
814
915const MusicDisplay = ( ) => {
10- const [ currentTrack , setCurrentTrack ] = useState ( null ) ;
16+ const [ currentTrack , setCurrentTrack ] = useState < Track | null > ( null ) ;
1117 const [ loading , setLoading ] = useState ( true ) ;
1218
1319 useEffect ( ( ) => {
14- const getRandomPlaylist = ( ) => playlists [ Math . floor ( Math . random ( ) * playlists . length ) ] ;
20+ const getRandomPlaylist = ( ) => PLAYLISTS [ Math . floor ( Math . random ( ) * PLAYLISTS . length ) ] ;
1521
1622 const updateTrack = async ( ) => {
17- setLoading ( true ) ;
18- const playlist = getRandomPlaylist ( ) ;
19- setCurrentTrack ( {
20- name : "Music from Playlist" ,
21- artist : "Current Mix"
22- } ) ;
23- setLoading ( false ) ;
23+ try {
24+ setLoading ( true ) ;
25+ const playlistId = getRandomPlaylist ( ) ;
26+
27+ const track : Track = {
28+ name : "Music from Playlist" ,
29+ artist : "Current Mix" ,
30+ playlistId
31+ } ;
32+
33+ setCurrentTrack ( track ) ;
34+ } catch ( error ) {
35+ console . error ( "Failed to update track:" , error ) ;
36+ setCurrentTrack ( null ) ;
37+ } finally {
38+ setLoading ( false ) ;
39+ }
2440 } ;
2541
2642 updateTrack ( ) ;
@@ -30,31 +46,40 @@ const MusicDisplay = () => {
3046
3147 if ( loading ) {
3248 return (
33- < div className = "flex items-center gap-2 " >
49+ < div className = "flex items-center gap-3 p-4 " >
3450 < Loader size = { 20 } className = "animate-spin" />
3551 < span > Loading music...</ span >
3652 </ div >
3753 ) ;
3854 }
3955
56+ if ( ! currentTrack ) {
57+ return (
58+ < div className = "flex items-center gap-3 p-4" >
59+ < Music size = { 20 } className = "text-accent-primary" />
60+ < span > No track available</ span >
61+ </ div >
62+ ) ;
63+ }
64+
4065 return (
41- < div className = "flex items-center gap-4 p-4 bg-background-secondary/50 rounded-lg" >
42- < div className = "relative w-8 h-8 " >
66+ < div className = "flex items-center gap-6 p-6 bg-background-secondary/50 rounded-lg" >
67+ < div className = "relative w-10 h-10 " >
4368 { [ ...Array ( 3 ) ] . map ( ( _ , i ) => (
4469 < div
4570 key = { i }
4671 className = "absolute bottom-0 w-2 bg-accent-neon animate-float"
4772 style = { {
48- left : `${ i * 12 } px` ,
73+ left : `${ i * 14 } px` ,
4974 height : `${ Math . random ( ) * 100 } %` ,
5075 animationDelay : `${ i * 0.2 } s`
5176 } }
5277 />
5378 ) ) }
5479 </ div >
55- < div >
56- < p className = "font-medium" > { currentTrack ? .name } </ p >
57- < p className = "text-sm opacity-80" > { currentTrack ? .artist } </ p >
80+ < div className = "space-y-2" >
81+ < p className = "font-medium text-lg " > { currentTrack . name } </ p >
82+ < p className = "text-sm opacity-80" > { currentTrack . artist } </ p >
5883 </ div >
5984 </ div >
6085 ) ;
0 commit comments