mira tengo esto: import React, { useState, useEffect, useRef } from "react"; import…
- User
mira tengo esto:
import React, { useState, useEffect, useRef } from "react"; import YouTube, { YouTubeProps, YouTubePlayer } from "react-youtube"; import { Play, Pause, SkipBack, SkipForward, Shuffle, Repeat, Volume1, Volume2, VolumeX, Maximize2, Home, Search, Library, Menu, X, } from "lucide-react"; import { Button } from "./components/ui/button"; // Asegúrate de que la ruta sea correcta
// Interfaz para las canciones interface Song { title: string; artist: string; album: string; added: string; duration?: number; albumCover?: string; youtubeId?: string; }
// Lista de canciones con youtubeId const songs: Song[] = [ { title: "Las Piponas (Remix)", artist: "Kris R., Blessed", album: "Las Piponas (Remix)", added: "hace 2 horas", duration: 244, albumCover: "https://i.postimg.cc/y8KhsW1p/imagen-blessd.jpg", youtubeId: "Dftj21lTUIA", }, // Puedes agregar más canciones aquí ];
const SpotifyClone = () => { const [currentSong, setCurrentSong] = useState(songs[0]); const [isPlaying, setIsPlaying] = useState(false); const [isSidebarOpen, setIsSidebarOpen] = useState(true); const [isMobile, setIsMobile] = useState(window.innerWidth < 1024);
const playerRef = useRef<YouTubePlayer | null>(null);
const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0);
const [volume, setVolume] = useState(100);
useEffect(() => { const handleResize = () => { if (window.innerWidth < 1024) { setIsMobile(true); setIsSidebarOpen(false); } else { setIsMobile(false); setIsSidebarOpen(true); } };
handleResize(); window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize);}, []);
useEffect(() => { if (playerRef.current && currentSong.youtubeId) { playerRef.current.loadVideoById(currentSong.youtubeId); setCurrentTime(0); setDuration(0); if (isPlaying) { playerRef.current.playVideo(); } } }, [currentSong]);
useEffect(() => { if (playerRef.current) { if (isPlaying) { playerRef.current.playVideo(); } else { playerRef.current.pauseVideo(); } } }, [isPlaying]);
// Efecto para actualizar el tiempo actual y la duración useEffect(() => { let intervalId: NodeJS.Timeout | null = null;
if (playerRef.current && isPlaying) { intervalId = setInterval(() => { setCurrentTime(playerRef.current!.getCurrentTime()); setDuration(playerRef.current!.getDuration()); }, 1000); } return () => { if (intervalId) clearInterval(intervalId); };}, [isPlaying, playerRef.current]);
const handlePlayPause = () => { if (isPlaying) { playerRef.current?.pauseVideo(); } else { if (currentSong.youtubeId) { playerRef.current?.playVideo(); } } setIsPlaying(!isPlaying); };
- Assistant