import dayjs from 'dayjs'
import { NavLink } from 'react-router-dom'
import Button, { Color as ButtonColor } from '@/components/Button'
import CoverRow, { Subtitle } from '@/components/CoverRow'
import Skeleton from '@/components/Skeleton'
import SvgIcon from '@/components/SvgIcon'
import TracksAlbum from '@/components/TracksAlbum'
import useAlbum from '@/hooks/useAlbum'
import useArtistAlbums from '@/hooks/useArtistAlbums'
import { player } from '@/store'
import { State as PlayerState } from '@/utils/player'
import {
formatDate,
formatDuration,
resizeImage,
scrollToTop,
} from '@/utils/common'
import useTracks from '@/hooks/useTracks'
import useUserAlbums, { useMutationLikeAAlbum } from '@/hooks/useUserAlbums'
import useUser from '@/hooks/useUser'
const PlayButton = ({
album,
handlePlay,
isLoading,
}: {
album: Album | undefined
isLoading: boolean
handlePlay: () => void
}) => {
const playerSnapshot = useSnapshot(player)
const isPlaying = useMemo(
() => playerSnapshot.state === PlayerState.PLAYING,
[playerSnapshot.state]
)
const isThisAlbumPlaying = useMemo(
() =>
playerSnapshot.trackListSource?.type === 'album' &&
playerSnapshot.trackListSource?.id === album?.id,
[playerSnapshot.trackListSource, album?.id]
)
const wrappedHandlePlay = () => {
if (isPlaying && isThisAlbumPlaying) {
player.pause()
return
}
if (!isPlaying && isThisAlbumPlaying) {
player.play()
return
}
handlePlay()
}
return (
)
}
const Header = ({
album,
isLoading,
handlePlay,
}: {
album: Album | undefined
isLoading: boolean
handlePlay: () => void
}) => {
const coverUrl = resizeImage(album?.picUrl || '', 'lg')
const albumDuration = useMemo(() => {
const duration = album?.songs?.reduce((acc, cur) => acc + cur.dt, 0) || 0
return formatDuration(duration, 'zh-CN', 'hh[hr] mm[min]')
}, [album?.songs])
const [isCoverError, setCoverError] = useState(false)
const { data: userAlbums } = useUserAlbums()
const isThisAlbumLiked = useMemo(() => {
if (!album) return false
return !!userAlbums?.data?.find(a => a.id === album.id)
}, [album, userAlbums?.data])
const mutationLikeAAlbum = useMutationLikeAAlbum()
return (
<>
{/* Header background */}
{coverUrl && !isCoverError && (
<>

>
)}
{/* Cover */}
{/* Neon shadow */}
{!isLoading && coverUrl && !isCoverError && (
)}
{!isLoading && isCoverError ? (
// Fallback cover
) : (
coverUrl && (

setCoverError(true)}
/>
)
)}
{isLoading &&
}
{/* Info */}
{/* Name */}
{isLoading ? (
PLACEHOLDER
) : (
{album?.name}
)}
{/* Artist */}
{isLoading ? (
PLACEHOLDER
) : (
Album ·{' '}
{album?.artist.name}
)}
{/* Release date & track count & album duration */}
{isLoading ? (
PLACEHOLDER
) : (
{album?.mark === 1056768 && (
)}
{dayjs(album?.publishTime || 0).year()} · {album?.size} 首歌 ·{' '}
{albumDuration}
)}
{/* Description */}
{isLoading ? (
PLACEHOLDER
) : (
{album?.description}
)}
{/* Buttons */}
>
)
}
const MoreAlbum = ({ album }: { album: Album | undefined }) => {
// Fetch artist's albums
const { data: albums, isLoading } = useArtistAlbums({
id: album?.artist.id ?? 0,
limit: 1000,
})
const filteredAlbums = useMemo((): Album[] => {
if (!albums) return []
const allReleases = albums?.hotAlbums || []
const filteredAlbums = allReleases.filter(
album =>
['专辑', 'EP/Single', 'EP'].includes(album.type) && album.size > 1
)
const singles = allReleases.filter(album => album.type === 'Single')
const qualifiedAlbums = [...filteredAlbums, ...singles]
const formatName = (name: string) =>
name.toLowerCase().replace(/(\s|deluxe|edition|\(|\))/g, '')
const uniqueAlbums: Album[] = []
qualifiedAlbums.forEach(a => {
// 去除当前页面的专辑
if (formatName(a.name) === formatName(album?.name ?? '')) return
// 去除重复的专辑(包含 deluxe edition 的专辑会视为重复)
if (
uniqueAlbums.findIndex(aa => {
return formatName(a.name) === formatName(aa.name)
}) !== -1
) {
return
}
// 去除 remix 专辑
if (
a.name.toLowerCase().includes('remix)') ||
a.name.toLowerCase().includes('remixes)')
) {
return
}
uniqueAlbums.push(a)
})
return uniqueAlbums.slice(0, 5)
}, [album?.name, albums])
return (
More by{' '}
{album?.artist.name}
)
}
const Album = () => {
const params = useParams()
const { data: album, isLoading } = useAlbum({
id: Number(params.id) || 0,
})
const { data: tracks } = useTracks({
ids: album?.songs?.map(track => track.id) ?? [],
})
const handlePlay = async (trackID: number | null = null) => {
if (!album?.album.id) {
toast('无法播放专辑,该专辑不存在')
return
}
await player.playAlbum(album.album.id, trackID)
}
return (
{album?.album && (
Released {formatDate(album.album.publishTime || 0, 'en')}
{album.album.company && (
© {album.album.company}
)}
)}
{!isLoading &&
}
)
}
export default Album