import { HTMLAttributes, useMemo } from "react"
import { TrackCard as SubframeTrackCard } from "@/ui"
import { Image } from "./Image"
export interface TrackCardProps extends HTMLAttributes<HTMLDivElement> {
title: string
artist: string
length: number // in seconds
genre: string
coverImage: string
isFavorite: boolean
onFavoriteClick: () => void
}
function lengthToString(length: number): string {
const minutes = Math.floor(length / 60)
const seconds = length % 60
return `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`
}
const TrackCard = ({ title, artist, length, genre, coverImage, isFavorite, onFavoriteClick }: TrackCardProps) => {
// Convert track length in seconds to MM:SS format
const formattedLength = useMemo(() => lengthToString(length), [length])
// Determine the icon based on favorite status
const icon = useMemo(() => (isFavorite ? "FeatherHeartOff" : "FeatherHeart"), [isFavorite])
return (
<SubframeTrackCard
title={title}
artist={artist}
length={formattedLength}
favoriteButtonSlot={<SubframeTrackCard.FavoriteButton icon={icon} onClick={onFavoriteClick} />}
imageSlot={<Image src={coverImage} width={500} height={500} />}
genre={genre}
/>
)
}
export default TrackCard