YesPlayMusic/packages/web/components/ArtistsInline.tsx

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-05-12 02:45:43 +08:00
import { useNavigate } from 'react-router-dom'
import cx from 'classnames'
2022-03-13 14:40:38 +08:00
const ArtistInline = ({
artists,
className,
2022-03-23 01:21:22 +08:00
disableLink,
onClick,
2022-03-13 14:40:38 +08:00
}: {
artists: Artist[]
className?: string
2022-03-23 01:21:22 +08:00
disableLink?: boolean
onClick?: (artistId: number) => void
2022-03-13 14:40:38 +08:00
}) => {
if (!artists) return <div></div>
2022-03-23 01:21:22 +08:00
const navigate = useNavigate()
const handleClick = (id: number) => {
if (id === 0 || disableLink) return
if (!onClick) {
navigate(`/artist/${id}`)
} else {
onClick(id)
}
2022-03-23 01:21:22 +08:00
}
2022-03-13 14:40:38 +08:00
return (
<div
2022-05-12 02:45:43 +08:00
className={cx(
!className?.includes('line-clamp') && 'line-clamp-1',
className
)}
>
2022-03-13 14:40:38 +08:00
{artists.map((artist, index) => (
2022-03-23 01:21:22 +08:00
<span key={`${artist.id}-${artist.name}`}>
<span
onClick={() => handleClick(artist.id)}
2022-05-12 02:45:43 +08:00
className={cx({
2022-03-23 01:21:22 +08:00
'hover:underline': !!artist.id && !disableLink,
})}
>
{artist.name}
</span>
2022-03-13 14:40:38 +08:00
{index < artists.length - 1 ? ', ' : ''}&nbsp;
</span>
))}
</div>
)
}
export default ArtistInline