import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import axiosInstance from "../api/axiosInstance"; import { useAuth } from "../context/authContext"; import { usePlayer } from "../context/playerContext"; import type { BasicPlaylist, BasicSong, SidebarProps } from "../utils/types"; import { toEmbedUrl } from "../utils/utils"; const Sidebar = ({ isOpen, onClose }: SidebarProps) => { const { user } = useAuth(); const navigate = useNavigate(); const { play, currentSong } = usePlayer(); const [recentlyListened, setRecentlyListened] = useState([]); const [playlists, setPlaylists] = useState([]); useEffect(() => { const fetchData = async () => { try { const data = await axiosInstance.get("/songs/recent"); setRecentlyListened(data.data); } catch (error) { console.error("Error fetching recently listened songs:", error); // todo: show toast } try { const data = await axiosInstance.get("/playlists/user"); setPlaylists(data.data); } catch (error) { console.error("Error fetching playlists:", error); // todo: show toast } }; if (user) { fetchData(); } else { setRecentlyListened([]); setPlaylists([]); } }, [user]); return (
{/* Sidebar Header */}

Library

{/* Recently Listened */}

Recently Played

{recentlyListened.map((song) => (
{song.title} { (e.target as HTMLImageElement).src = "/favicon.png"; }} />

navigate(`/songs/${song.id}`)} className="text-sm font-medium text-white truncate hover:underline cursor-pointer" > {song.title}

{ e.stopPropagation(); if (song.artistUsername) { navigate(`/users/${song.artistUsername}`); } }} className={`truncate ${ song.artistUsername ? "hover:underline cursor-pointer hover:text-white" : "" }`} > {song.artist} {song.album && ( <> { e.stopPropagation(); if (song.albumId) { navigate(`/collection/album/${song.albumId}`); } }} className={`truncate ${ song.albumId ? "hover:underline cursor-pointer hover:text-white" : "" }`} > {song.album} )}
{song.link && ( )}
))}
{/* Playlists */}

Your Playlists

{playlists.map((playlist) => (

{playlist.name}

{playlist.songCount} songs

))}
); }; export default Sidebar;