import { useState } from "react" import { useQuery } from "@tanstack/react-query" import { timeSince } from "~/utils/utils" import ArtistLinks from "./ArtistLinks" import { deleteListen, getLastListens, type getItemsArgs, type Listen } from "api/api" import { Link } from "react-router" import { useAppContext } from "~/providers/AppProvider" interface Props { limit: number artistId?: Number albumId?: Number trackId?: number hideArtists?: boolean } export default function LastPlays(props: Props) { const { user } = useAppContext() const { isPending, isError, data, error } = useQuery({ queryKey: ['last-listens', { limit: props.limit, period: 'all_time', artist_id: props.artistId, album_id: props.albumId, track_id: props.trackId }], queryFn: ({ queryKey }) => getLastListens(queryKey[1] as getItemsArgs), }) const [items, setItems] = useState(null) const handleDelete = async (listen: Listen) => { if (!data) return try { const res = await deleteListen(listen) if (res.ok || (res.status >= 200 && res.status < 300)) { setItems((prev) => (prev ?? data.items).filter((i) => i.time !== listen.time)) } else { console.error("Failed to delete listen:", res.status) } } catch (err) { console.error("Error deleting listen:", err) } } if (isPending) { return (

Last Played

Loading...

) } if (isError) { return

Error: {error.message}

} const listens = items ?? data.items let params = '' params += props.artistId ? `&artist_id=${props.artistId}` : '' params += props.albumId ? `&album_id=${props.albumId}` : '' params += props.trackId ? `&track_id=${props.trackId}` : '' return (

Last Played

{listens.map((item) => ( ))}
{timeSince(new Date(item.time))} {props.hideArtists ? null : ( <> –{' '} )} {item.track.title}
) }