source: frontend/src/components/Sidebar.tsx@ 5938bc9

main
Last change on this file since 5938bc9 was 5938bc9, checked in by Filip Gavrilovski <filipgavrilovski28@…>, 5 months ago

fix bug that prevented unauthenticated users to see top songs

  • Property mode set to 100644
File size: 4.0 KB
Line 
1import { useEffect, useState } from "react";
2import axiosInstance from "../api/axiosInstance";
3import { useAuth } from "../context/authContext";
4import type { BasicPlaylist, BasicSong, SidebarProps } from "../utils/types";
5
6const Sidebar = ({ isOpen, onClose }: SidebarProps) => {
7 const { user } = useAuth();
8 const [recentlyListened, setRecentlyListened] = useState<BasicSong[]>([]);
9 const [playlists, setPlaylists] = useState<BasicPlaylist[]>([]);
10
11 useEffect(() => {
12 const fetchData = async () => {
13 try {
14 const data = await axiosInstance.get<BasicSong[]>("/songs/recent");
15 setRecentlyListened(data.data);
16 } catch (error) {
17 console.error("Error fetching recently listened songs:", error);
18 // todo: show toast
19 }
20 try {
21 const data =
22 await axiosInstance.get<BasicPlaylist[]>("/playlists/user");
23 setPlaylists(data.data);
24 } catch (error) {
25 console.error("Error fetching playlists:", error);
26 // todo: show toast
27 }
28 };
29 if (user) {
30 fetchData();
31 } else {
32 setRecentlyListened([]);
33 setPlaylists([]);
34 }
35 }, [user]);
36 return (
37 <div
38 className={`fixed left-0 top-0 h-full bg-[#121212] border-r border-white/10 transition-transform duration-300 ease-in-out z-40 ${
39 isOpen ? "translate-x-0" : "-translate-x-full"
40 } w-64 overflow-y-auto`}
41 >
42 <div className="p-6">
43 {/* Sidebar Header */}
44 <div className="flex justify-between items-center mb-6 pt-2">
45 <h2 className="text-xl font-bold text-white">Library</h2>
46 <button
47 onClick={onClose}
48 className="text-gray-400 hover:text-white transition-colors"
49 aria-label="Close sidebar"
50 >
51 <svg
52 className="w-6 h-6"
53 fill="none"
54 stroke="currentColor"
55 viewBox="0 0 24 24"
56 >
57 <path
58 strokeLinecap="round"
59 strokeLinejoin="round"
60 strokeWidth={2}
61 d="M6 18L18 6M6 6l12 12"
62 />
63 </svg>
64 </button>
65 </div>
66
67 {/* Recently Listened */}
68 <div className="mb-8">
69 <h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-4">
70 Recently Played
71 </h3>
72 <div className="space-y-3">
73 {recentlyListened.map((song) => (
74 <div
75 key={song.id}
76 className="flex items-center gap-3 p-2 rounded-lg hover:bg-white/5 cursor-pointer transition-colors"
77 >
78 <img
79 src={song.cover || "/favicon.png"}
80 alt={song.title}
81 className="w-10 h-10 rounded object-cover"
82 />
83 <div className="flex-1 min-w-0">
84 <p className="text-sm font-medium text-white truncate">
85 {song.title}
86 </p>
87 <p className="text-xs text-gray-400 truncate">
88 {song.artist}
89 </p>
90 </div>
91 </div>
92 ))}
93 </div>
94 </div>
95
96 {/* Playlists */}
97 <div>
98 <h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-4">
99 Your Playlists
100 </h3>
101 <div className="space-y-2">
102 {playlists.map((playlist) => (
103 <div
104 key={playlist.id}
105 className="flex items-center justify-between p-3 rounded-lg hover:bg-white/5 cursor-pointer transition-colors"
106 >
107 <div className="flex items-center gap-3">
108 <div className="w-10 h-10 bg-linear-to-br from-purple-500 to-pink-500 rounded flex items-center justify-center">
109 <svg
110 className="w-5 h-5 text-white"
111 fill="currentColor"
112 viewBox="0 0 20 20"
113 >
114 <path d="M18 3a1 1 0 00-1.196-.98l-10 2A1 1 0 006 5v9.114A4.369 4.369 0 005 14c-1.657 0-3 .895-3 2s1.343 2 3 2 3-.895 3-2V7.82l8-1.6v5.894A4.37 4.37 0 0015 12c-1.657 0-3 .895-3 2s1.343 2 3 2 3-.895 3-2V3z" />
115 </svg>
116 </div>
117 <div>
118 <p className="text-sm font-medium text-white">
119 {playlist.name}
120 </p>
121 <p className="text-xs text-gray-400">
122 {playlist.songCount} songs
123 </p>
124 </div>
125 </div>
126 </div>
127 ))}
128 </div>
129 </div>
130 </div>
131 </div>
132 );
133};
134
135export default Sidebar;
Note: See TracBrowser for help on using the repository browser.