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 width?: number type: "album" | "track" | "artist"; } export default function TopItemList({ data, separators, type, width }: Props) { return (
{data.items.map((item, index) => { const key = `${type}-${item.id}`; return (
); })}
); } function ItemCard({ item, type }: { item: Item; type: "album" | "track" | "artist" }) { const itemClasses = `flex items-center gap-2 hover:text-(--color-fg-secondary)` const navigate = useNavigate(); const handleItemClick = (type: string, id: number) => { navigate(`/${type.toLowerCase()}/${id}`); }; const handleArtistClick = (event: React.MouseEvent) => { // Stop the click from navigating to the album page event.stopPropagation(); }; // Also stop keyboard events on the inner links from bubbling up const handleArtistKeyDown = (event: React.KeyboardEvent) => { event.stopPropagation(); } switch (type) { case "album": { const album = item as Album; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { handleItemClick("album", album.id); } }; return (
handleItemClick("album", album.id)} onKeyDown={handleKeyDown} role="link" tabIndex={0} aria-label={`View album: ${album.title}`} style={{ cursor: 'pointer' }} > {album.title}
{album.title}
{album.is_various_artists ? Various Artists :
}
{album.listen_count} plays
); } case "track": { const track = item as Track; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter') { handleItemClick("track", track.id); } }; return (
handleItemClick("track", track.id)} onKeyDown={handleKeyDown} role="link" tabIndex={0} aria-label={`View track: ${track.title}`} style={{ cursor: 'pointer' }} > {track.title}
{track.title}
{track.listen_count} plays
); } case "artist": { const artist = item as Artist; return (
{artist.name}
{artist.name}
{artist.listen_count} plays
); } } }