From 32184327901ca1376a07ae0172192eb98fc0bead Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Tue, 3 Jun 2025 14:33:12 -0600 Subject: [PATCH] Failsafe --- src/components/SpotifyIcon.tsx | 38 +++++++++++++++++++++++++++----- src/pages/api/spotify/config.ts | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/pages/api/spotify/config.ts diff --git a/src/components/SpotifyIcon.tsx b/src/components/SpotifyIcon.tsx index 2a6a9fa..60b8db5 100644 --- a/src/components/SpotifyIcon.tsx +++ b/src/components/SpotifyIcon.tsx @@ -35,8 +35,34 @@ const SpotifySVG = ({ className }: { className?: string }) => ( export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: SpotifyIconProps) { const currentTrack = useSignal(null); const isPlaying = useSignal(false); + const isDynamicEnabled = useSignal(false); useEffect(() => { + // First, check if Spotify is properly configured + const checkConfiguration = async () => { + try { + const response = await fetch('/api/spotify/config'); + const { configured } = await response.json(); + + if (!configured) { + console.log('Spotify dynamic features disabled - missing or invalid environment variables'); + isDynamicEnabled.value = false; + return; + } + + isDynamicEnabled.value = true; + initializeSpotifyConnection(); + + } catch (error) { + console.log('Spotify dynamic features disabled - configuration check failed:', error); + isDynamicEnabled.value = false; + } + }; + + checkConfiguration(); + }, []); + + const initializeSpotifyConnection = () => { // Set up Server-Sent Events connection const eventSource = new EventSource('/api/spotify/stream'); @@ -75,7 +101,7 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: return () => { eventSource.close(); }; - }, []); + }; // Fallback polling function if SSE fails const startPolling = () => { @@ -113,7 +139,7 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: }; const getTooltipText = () => { - if (currentTrack.value && isPlaying.value) { + if (isDynamicEnabled.value && currentTrack.value && isPlaying.value) { const artists = currentTrack.value.artists.map(artist => artist.name).join(', '); return `♪ ${currentTrack.value.name} by ${artists} (click to open in Spotify)`; } @@ -122,7 +148,7 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: const getSpotifyUrl = () => { // If we have a current track with external URL, use that - if (currentTrack.value && isPlaying.value && currentTrack.value.external_urls?.spotify) { + if (isDynamicEnabled.value && currentTrack.value && isPlaying.value && currentTrack.value.external_urls?.spotify) { return currentTrack.value.external_urls.spotify; } // Otherwise, use the provided profile URL or fallback to general Spotify @@ -140,15 +166,15 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: >
- {isPlaying.value && ( + {isDynamicEnabled.value && isPlaying.value && (
)} diff --git a/src/pages/api/spotify/config.ts b/src/pages/api/spotify/config.ts new file mode 100644 index 0000000..06549eb --- /dev/null +++ b/src/pages/api/spotify/config.ts @@ -0,0 +1,39 @@ +import type { APIRoute } from 'astro'; + +export const GET: APIRoute = async () => { + try { + const clientId = import.meta.env.SPOTIFY_CLIENT_ID; + const clientSecret = import.meta.env.SPOTIFY_CLIENT_SECRET; + const refreshToken = import.meta.env.SPOTIFY_REFRESH_TOKEN; + + const isConfigured = !!(clientId && clientSecret && refreshToken); + + if (!isConfigured) { + console.log('Spotify integration disabled - missing environment variables:', { + hasClientId: !!clientId, + hasClientSecret: !!clientSecret, + hasRefreshToken: !!refreshToken + }); + } + + return new Response(JSON.stringify({ + configured: isConfigured + }), { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); + + } catch (error) { + console.error('Error checking Spotify configuration:', error); + return new Response(JSON.stringify({ + configured: false + }), { + status: 200, + headers: { + 'Content-Type': 'application/json', + }, + }); + } +}; \ No newline at end of file