source: frontend/src/components/Sidebar.tsx@ f5bc95e

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

fix music cover ui bug

  • Property mode set to 100644
File size: 6.5 KB
Line 
1import { useEffect, useState } from "react";
2import { useNavigate } from "react-router-dom";
3import axiosInstance, { baseURL } from "../api/axiosInstance";
4import { useAuth } from "../context/authContext";
5import { usePlayer } from "../context/playerContext";
6import type { BasicPlaylist, BasicSong, SidebarProps } from "../utils/types";
7import { toEmbedUrl } from "../utils/utils";
8
9const Sidebar = ({ isOpen, onClose }: SidebarProps) => {
10 const { user } = useAuth();
11 const navigate = useNavigate();
12 const { play, currentSong } = usePlayer();
13 const [recentlyListened, setRecentlyListened] = useState<BasicSong[]>([]);
14 const [playlists, setPlaylists] = useState<BasicPlaylist[]>([]);
15
16 useEffect(() => {
17 const fetchData = async () => {
18 try {
19 const data = await axiosInstance.get<BasicSong[]>("/songs/recent");
20 setRecentlyListened(data.data);
21 } catch (error) {
22 console.error("Error fetching recently listened songs:", error);
23 // todo: show toast
24 }
25 try {
26 const data =
27 await axiosInstance.get<BasicPlaylist[]>("/playlists/user");
28 setPlaylists(data.data);
29 } catch (error) {
30 console.error("Error fetching playlists:", error);
31 // todo: show toast
32 }
33 };
34 if (user) {
35 fetchData();
36 } else {
37 setRecentlyListened([]);
38 setPlaylists([]);
39 }
40 }, [user]);
41 return (
42 <div
43 className={`fixed left-0 top-0 h-full bg-[#121212] border-r border-white/10 transition-transform duration-300 ease-in-out z-40 ${
44 isOpen ? "translate-x-0" : "-translate-x-full"
45 } w-64 overflow-y-auto`}
46 >
47 <div className="p-6">
48 {/* Sidebar Header */}
49 <div className="flex justify-between items-center mb-6 pt-2">
50 <h2 className="text-xl font-bold text-white">Library</h2>
51 <button
52 onClick={onClose}
53 className="text-gray-400 hover:text-white transition-colors"
54 aria-label="Close sidebar"
55 >
56 <svg
57 className="w-6 h-6"
58 fill="none"
59 stroke="currentColor"
60 viewBox="0 0 24 24"
61 >
62 <path
63 strokeLinecap="round"
64 strokeLinejoin="round"
65 strokeWidth={2}
66 d="M6 18L18 6M6 6l12 12"
67 />
68 </svg>
69 </button>
70 </div>
71
72 {/* Recently Listened */}
73 <div className="mb-8">
74 <h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-4">
75 Recently Played
76 </h3>
77 <div className="space-y-3">
78 {recentlyListened.map((song) => (
79 <div
80 key={song.id}
81 className="flex items-center gap-3 p-2 rounded-lg hover:bg-white/5 cursor-pointer transition-colors group relative"
82 >
83 <img
84 src={song.cover ? `${baseURL}/${song.cover}` : "/favicon.png"}
85 alt={song.title}
86 className="w-10 h-10 rounded object-cover"
87 onError={(e) => {
88 (e.target as HTMLImageElement).src = "/favicon.png";
89 }}
90 />
91 <div className="flex-1 min-w-0">
92 <p
93 onClick={() => navigate(`/songs/${song.id}`)}
94 className="text-sm font-medium text-white truncate hover:underline cursor-pointer"
95 >
96 {song.title}
97 </p>
98 <div className="flex items-center gap-1 text-xs text-gray-400">
99 <span
100 onClick={(e) => {
101 e.stopPropagation();
102 if (song.artistUsername) {
103 navigate(`/users/${song.artistUsername}`);
104 }
105 }}
106 className={`truncate ${
107 song.artistUsername
108 ? "hover:underline cursor-pointer hover:text-white"
109 : ""
110 }`}
111 >
112 {song.artist}
113 </span>
114 {song.album && (
115 <>
116 <span>•</span>
117 <span
118 onClick={(e) => {
119 e.stopPropagation();
120 if (song.albumId) {
121 navigate(`/collection/album/${song.albumId}`);
122 }
123 }}
124 className={`truncate ${
125 song.albumId
126 ? "hover:underline cursor-pointer hover:text-white"
127 : ""
128 }`}
129 >
130 {song.album}
131 </span>
132 </>
133 )}
134 </div>
135 </div>
136 {song.link && (
137 <button
138 onClick={(e) => {
139 e.stopPropagation();
140 play({
141 id: song.id,
142 title: song.title,
143 artist: song.artist,
144 cover: song.cover,
145 embedUrl: toEmbedUrl(song.link!),
146 });
147 }}
148 className={`p-1.5 rounded-full transition-all opacity-0 group-hover:opacity-100 ${
149 currentSong?.id === song.id
150 ? "bg-white text-[#1db954]"
151 : "bg-[#1db954] text-black hover:scale-110"
152 }`}
153 aria-label={
154 currentSong?.id === song.id ? "Now playing" : "Play song"
155 }
156 >
157 {currentSong?.id === song.id ? (
158 <svg
159 className="w-4 h-4"
160 fill="currentColor"
161 viewBox="0 0 24 24"
162 >
163 <path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z" />
164 </svg>
165 ) : (
166 <svg
167 className="w-4 h-4"
168 fill="currentColor"
169 viewBox="0 0 24 24"
170 >
171 <path d="M8 5v14l11-7z" />
172 </svg>
173 )}
174 </button>
175 )}
176 </div>
177 ))}
178 </div>
179 </div>
180
181 {/* Playlists */}
182 <div>
183 <h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-4">
184 Your Playlists
185 </h3>
186 <div className="space-y-2">
187 {playlists.map((playlist) => (
188 <div
189 key={playlist.id}
190 className="flex items-center justify-between p-3 rounded-lg hover:bg-white/5 cursor-pointer transition-colors"
191 >
192 <div className="flex items-center gap-3">
193 <div className="w-10 h-10 bg-linear-to-br from-purple-500 to-pink-500 rounded flex items-center justify-center">
194 <svg
195 className="w-5 h-5 text-white"
196 fill="currentColor"
197 viewBox="0 0 20 20"
198 >
199 <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" />
200 </svg>
201 </div>
202 <div>
203 <p className="text-sm font-medium text-white">
204 {playlist.name}
205 </p>
206 <p className="text-xs text-gray-400">
207 {playlist.songCount} songs
208 </p>
209 </div>
210 </div>
211 </div>
212 ))}
213 </div>
214 </div>
215 </div>
216 </div>
217 );
218};
219
220export default Sidebar;
Note: See TracBrowser for help on using the repository browser.