import React, { useState } from 'react'; import { searchSpotifyTracks, isLoggedIn } from '../utils/spotifyAuth.js'; function AddSongModal({ onClose }) { const [searchQuery, setSearchQuery] = useState(''); const [searchResults, setSearchResults] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const handleBackdropClick = (e) => { if (e.target === e.currentTarget) { onClose(); } }; const handleSearch = async (e) => { const query = e.target.value; setSearchQuery(query); setError(''); if (query.length > 2) { if (!isLoggedIn()) { setError('Please login to Spotify to search for songs'); return; } setIsLoading(true); try { const results = await searchSpotifyTracks(query); setSearchResults(results); } catch (err) { setError(err.message === 'Token expired' ? 'Session expired. Please login again.' : 'Search failed. Please try again.'); setSearchResults([]); if (err.message === 'Token expired') { setTimeout(() => window.location.reload(), 2000); } } finally { setIsLoading(false); } } else { setSearchResults([]); } }; const handleKeyPress = async (e) => { if (e.key === 'Enter' && searchQuery.trim() && searchQuery.length > 2) { if (!isLoggedIn()) { setError('Please login to Spotify to search for songs'); return; } setIsLoading(true); try { const results = await searchSpotifyTracks(searchQuery); setSearchResults(results); setError(''); } catch (err) { setError(err.message === 'Token expired' ? 'Session expired. Please login again.' : 'Search failed. Please try again.'); setSearchResults([]); } finally { setIsLoading(false); } } }; return (
{error}
Search for music to add to your station
Enter song title, artist, or album name
Type at least 3 characters to search
No results found for "{searchQuery}"
Try different keywords
{song.artist}
{song.album}