import { Link, useNavigate } from "react-router"; import ArtistLinks from "./ArtistLinks"; import { imageUrl, type Album, type Artist, type Track, type PaginatedResponse, } from "api/api"; type Item = Album | Track | Artist; interface Props { data: PaginatedResponse; separators?: ConstrainBoolean; ranked?: boolean; type: "album" | "track" | "artist"; className?: string; } export default function TopItemList({ data, separators, type, className, ranked, }: Props) { const currentParams = new URLSearchParams(location.search); const page = Math.max(parseInt(currentParams.get("page") || "1"), 1); let lastRank = 0; const calculateRank = (data: Item[], page: number, index: number): number => { if ( index === 0 || data[index] == undefined || !(data[index].listen_count === data[index - 1].listen_count) ) { lastRank = index + 1 + (page - 1) * 100; } return lastRank; }; return (
{data.items.map((item, index) => { const key = `${type}-${item.id}`; return (
); })}
); } function ItemCard({ item, type, rank, ranked, }: { item: Item; type: "album" | "track" | "artist"; rank: number; ranked?: boolean; }) { const itemClasses = `flex items-center gap-2`; switch (type) { case "album": { const album = item as Album; return (
{ranked &&
{rank}
} {album.title}
{album.title}
{album.is_various_artists ? ( Various Artists ) : (
)}
{album.listen_count} plays
); } case "track": { const track = item as Track; return (
{ranked &&
{rank}
} {track.title}
{track.title}
{track.listen_count} plays
); } case "artist": { const artist = item as Artist; return (
{ranked &&
{rank}
} {artist.name}
{artist.name}
{artist.listen_count} plays
); } } }