source: frontend/src/components/Sidebar.tsx@ 9c1dcc7

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

add experimental video miniplayer

  • Property mode set to 100644
File size: 5.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 onClick={() => navigate(`/songs/${song.id}`)}
100 className="flex items-center gap-3 p-2 rounded-lg hover:bg-white/5 cursor-pointer transition-colors group relative"
101 >
102 <img
103 src={song.cover || "/favicon.png"}
104 alt={song.title}
105 className="w-10 h-10 rounded object-cover"
106 onError={(e) => {
107 (e.target as HTMLImageElement).src = "/favicon.png";
108 }}
109 />
110 <div className="flex-1 min-w-0">
111 <p className="text-sm font-medium text-white truncate">
112 {song.title}
113 </p>
114 <p className="text-xs text-gray-400 truncate">
115 {song.artist}
116 </p>
117 </div>
118 {song.link && (
119 <button
120 onClick={(e) => {
121 e.stopPropagation();
122 play({
123 id: song.id,
124 title: song.title,
125 artist: song.artist,
126 cover: song.cover,
127 embedUrl: toEmbedUrl(song.link!),
128 });
129 }}
130 className={`p-1.5 rounded-full transition-all opacity-0 group-hover:opacity-100 ${
131 currentSong?.id === song.id
132 ? "bg-white text-[#1db954]"
133 : "bg-[#1db954] text-black hover:scale-110"
134 }`}
135 aria-label={
136 currentSong?.id === song.id ? "Now playing" : "Play song"
137 }
138 >
139 {currentSong?.id === song.id ? (
140 <svg
141 className="w-4 h-4"
142 fill="currentColor"
143 viewBox="0 0 24 24"
144 >
145 <path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z" />
146 </svg>
147 ) : (
148 <svg
149 className="w-4 h-4"
150 fill="currentColor"
151 viewBox="0 0 24 24"
152 >
153 <path d="M8 5v14l11-7z" />
154 </svg>
155 )}
156 </button>
157 )}
158 </div>
159 ))}
160 </div>
161 </div>
162
163 {/* Playlists */}
164 <div>
165 <h3 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-4">
166 Your Playlists
167 </h3>
168 <div className="space-y-2">
169 {playlists.map((playlist) => (
170 <div
171 key={playlist.id}
172 className="flex items-center justify-between p-3 rounded-lg hover:bg-white/5 cursor-pointer transition-colors"
173 >
174 <div className="flex items-center gap-3">
175 <div className="w-10 h-10 bg-linear-to-br from-purple-500 to-pink-500 rounded flex items-center justify-center">
176 <svg
177 className="w-5 h-5 text-white"
178 fill="currentColor"
179 viewBox="0 0 20 20"
180 >
181 <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" />
182 </svg>
183 </div>
184 <div>
185 <p className="text-sm font-medium text-white">
186 {playlist.name}
187 </p>
188 <p className="text-xs text-gray-400">
189 {playlist.songCount} songs
190 </p>
191 </div>
192 </div>
193 </div>
194 ))}
195 </div>
196 </div>
197 </div>
198 </div>
199 );
200};
201
202export default Sidebar;
Note: See TracBrowser for help on using the repository browser.