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

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

clickable items now take the user to the respective pages; added artist username and album id to dto-s

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