-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {Component, createEffect, createSignal, onCleanup} from "solid-js"; | ||
import type {SpotifyTrack} from "../../lib/spotify"; | ||
|
||
const SpotifyNowPlaying: Component = () => { | ||
const [track, setTrack] = createSignal<SpotifyTrack | null>(null); | ||
const [error, setError] = createSignal<string | null>(null); | ||
const [isVisible, setIsVisible] = createSignal(false); | ||
|
||
const fetchNowPlaying = async () => { | ||
try { | ||
const response = await fetch("/api/spotify"); | ||
if (!response.ok) throw new Error("Failed to fetch"); | ||
const data = await response.json(); | ||
setTrack(data); | ||
setError(null); | ||
} catch (err) { | ||
setError("Failed to load track data"); | ||
console.error(err); | ||
} | ||
}; | ||
|
||
createEffect(() => { | ||
fetchNowPlaying(); | ||
const interval = setInterval(fetchNowPlaying, 30000); | ||
onCleanup(() => clearInterval(interval)); | ||
}); | ||
|
||
const getAlbumArt = () => { | ||
const images = track()?.image || []; | ||
const mediumImage = images.find((img) => img.size === "medium"); | ||
return mediumImage?.["#text"] || ""; | ||
}; | ||
|
||
const handleImageLoad = () => { | ||
setTimeout(() => setIsVisible(true), 1); | ||
}; | ||
|
||
return ( | ||
<div class="fixed bottom-20 w-full px-6"> | ||
<div class="max-w-sm mx-auto"> | ||
{error() ? null : track() ? ( | ||
<a | ||
href={track()?.url} | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
class={`group flex items-center gap-4 bg-neutral-900/50 backdrop-blur-sm p-4 | ||
rounded-lg border border-neutral-800 hover:border-neutral-700 | ||
transition-all duration-300 | ||
${isVisible() ? "animate-fade-in" : "opacity-0"}`}> | ||
<div class="relative min-w-16 h-16"> | ||
<img src={getAlbumArt()} alt={`${track()?.album} album art`} class="w-16 h-16 rounded-md object-cover" onLoad={handleImageLoad} /> | ||
{track()?.isPlaying && isVisible() && ( | ||
<div class="absolute -bottom-2 -right-2 flex items-center gap-[3px] bg-neutral-900/90 p-1.5 rounded-md"> | ||
<span class="w-[3px] h-3 bg-rose-400/80 animate-spotify-bar" /> | ||
<span class="w-[3px] h-3 bg-rose-400/80 animate-spotify-bar delay-[0.25s]" /> | ||
<span class="w-[3px] h-3 bg-rose-400/80 animate-spotify-bar delay-[0.5s]" /> | ||
</div> | ||
)} | ||
</div> | ||
<div class="flex flex-col min-w-0"> | ||
<span class="font-medium text-neutral-200 truncate group-hover:text-rose-400/80 transition-colors">{track()?.name}</span> | ||
<span class="text-neutral-400 text-sm truncate">{track()?.artist}</span> | ||
<span class="text-neutral-500 text-sm truncate">{track()?.album}</span> | ||
</div> | ||
</a> | ||
) : null} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SpotifyNowPlaying; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export interface SpotifyImage { | ||
"#text": string; | ||
size: "small" | "medium" | "large" | "extralarge"; | ||
} | ||
|
||
export interface SpotifyTrack { | ||
name: string; | ||
artist: string; | ||
album: string; | ||
isPlaying: boolean; | ||
url: string; | ||
image: SpotifyImage[]; | ||
} | ||
|
||
export interface SpotifyResponse { | ||
track: { | ||
"@attr"?: { | ||
nowplaying: string; | ||
}; | ||
name: string; | ||
url: string; | ||
artist: { | ||
"#text": string; | ||
}; | ||
album: { | ||
"#text": string; | ||
}; | ||
image: SpotifyImage[]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {APIEvent} from "@solidjs/start/server"; | ||
import type {SpotifyResponse} from "../../lib/spotify"; | ||
|
||
export async function GET({request}: APIEvent) { | ||
try { | ||
const response = await fetch("https://lastfm-last-played.biancarosa.com.br/rexdotsh/latest-song"); | ||
const data: SpotifyResponse = await response.json(); | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
isPlaying: data.track["@attr"]?.nowplaying === "true", | ||
name: data.track.name, | ||
artist: data.track.artist["#text"], | ||
album: data.track.album["#text"], | ||
url: data.track.url, | ||
image: data.track.image, | ||
}), | ||
{ | ||
status: 200, | ||
headers: { | ||
"content-type": "application/json", | ||
"cache-control": "public, s-maxage=60, stale-while-revalidate=30", | ||
}, | ||
} | ||
); | ||
} catch (error) { | ||
console.error("Spotify API Error:", error); | ||
return new Response( | ||
JSON.stringify({ | ||
error: error instanceof Error ? error.message : "Unknown error occurred", | ||
}), | ||
{ | ||
status: 500, | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters