import { useEffect, useState } from "react"; import { Link, useParams } from "react-router-dom"; import { Album, ArrowLeft, ExternalLink, LoaderCircle } from "lucide-react"; import { Button } from "../components/Button"; import { DownloadButton } from "../components/DownloadButton"; import { useSpotifyDownload } from "../helpers/useSpotifyDownload"; import { getSpotifyArtistAlbums, type SpotifyArtistAlbum, type OutputType as ArtistAlbumsResult, } from "../endpoints/spotify-artist-albums_GET.schema"; import styles from "./artist.$id.module.css"; import { MobileNav } from "../components/MobileNav"; export default function ArtistDiscographyPage() { const params = useParams(); const artistId = params.id || ""; const [data, setData] = useState(); const downloads = useSpotifyDownload(); const [albums, setAlbums] = useState([]); const [nextOffset, setNextOffset] = useState(); const [loading, setLoading] = useState(false); const [error, setError] = useState(); useEffect(() => { if (!artistId) return; void load(0, false); }, [artistId]); async function load(offset: number, append: boolean) { if (!artistId) return; setLoading(true); setError(undefined); try { const result = await getSpotifyArtistAlbums(artistId, offset); setData(result); setAlbums((current) => append ? [...current, ...result.albums] : result.albums); setNextOffset(result.nextOffset); } catch (reason) { setError(reason instanceof Error ? reason.message : "Unable to load artist discography."); } finally { setLoading(false); } } return (
My Spotify Artist Discography
{error ?
{error}
: null}
{data?.artist.imageUrl ? ( ) : ( )}
SPOTIFY ARTIST

{data?.artist.name || "Artist discography"}

{typeof data?.total === "number" ? data.total + " releases returned by Spotify" : "Albums, singles and compilations"}

{data?.artist.externalUrl ? ( Spotify Open artist ) : null}
{albums.map((album) => (
{album.imageUrl ? : }
{(album.albumType || "release").toUpperCase()} {album.name} {album.releaseDate || "Date unknown"} {album.totalTracks ? " · " + album.totalTracks + " tracks" : ""}
{album.externalUrl ? ( ) : null} {album.externalUrl ? ( ) : null}
))}
{loading ? (
Loading releases…
) : null} {!loading && nextOffset !== undefined ? (
) : null}
); }