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