From 2ad6585a070d86fda91955caf12ccb08ad80f8b0 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Thu, 12 Jun 2025 14:05:10 -0600 Subject: [PATCH] Spotify fallback --- src/components/SpotifyIcon.tsx | 35 ++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/components/SpotifyIcon.tsx b/src/components/SpotifyIcon.tsx index 367b14e..414cfef 100644 --- a/src/components/SpotifyIcon.tsx +++ b/src/components/SpotifyIcon.tsx @@ -36,25 +36,33 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: const currentTrack = useSignal(null); const isPlaying = useSignal(false); const isDynamicEnabled = useSignal(false); + const hasLoggedError = useSignal(false); useEffect(() => { // First, check if Spotify is properly configured const checkConfiguration = async () => { try { const response = await fetch('/api/spotify/config'); + if (!response.ok) { + throw new Error(`Spotify config endpoint returned status ${response.status}`); + } const { configured } = await response.json(); if (!configured) { - console.log('Spotify dynamic features disabled - missing or invalid environment variables'); + if (!hasLoggedError.value) { + console.warn('[Spotify] Dynamic features disabled: missing or invalid environment variables.'); + hasLoggedError.value = true; + } isDynamicEnabled.value = false; return; } - isDynamicEnabled.value = true; initializeSpotifyConnection(); - - } catch (error) { - console.log('Spotify dynamic features disabled - configuration check failed:', error); + } catch (error: any) { + if (!hasLoggedError.value) { + console.error('[Spotify] Could not enable dynamic features:', error?.message || error); + hasLoggedError.value = true; + } isDynamicEnabled.value = false; } }; @@ -67,13 +75,15 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: const eventSource = new EventSource('/api/spotify/stream'); eventSource.onopen = () => { - console.log('Spotify SSE connected'); + // Only log on first open + if (!hasLoggedError.value) { + console.info('[Spotify] SSE connected for dynamic icon.'); + } }; eventSource.onmessage = (event) => { try { const data: SpotifyResponse = JSON.parse(event.data); - if (data.is_playing && data.item) { currentTrack.value = data.item; isPlaying.value = data.is_playing; @@ -82,15 +92,20 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: isPlaying.value = false; } } catch (err) { - console.error('Error parsing SSE data:', err); + if (!hasLoggedError.value) { + console.error('[Spotify] Error parsing SSE data:', err); + hasLoggedError.value = true; + } // Fail silently - will revert to static mode } }; eventSource.onerror = (event) => { - console.error('Spotify SSE error:', event); + if (!hasLoggedError.value) { + console.error('[Spotify] SSE connection error. Falling back to static icon.'); + hasLoggedError.value = true; + } // If SSE fails, we just fall back to static mode - console.log('Spotify SSE failed, reverting to static mode'); }; // Cleanup on unmount