2022-08-03 23:48:39 +08:00
|
|
|
import useAlbum from '@/web/api/hooks/useAlbum'
|
2023-01-24 16:29:33 +08:00
|
|
|
import useUserAlbums, { useMutationLikeAAlbum } from '@/web/api/hooks/useUserAlbums'
|
2022-08-03 23:48:39 +08:00
|
|
|
import Icon from '@/web/components/Icon'
|
2022-10-28 20:29:04 +08:00
|
|
|
import TrackListHeader from '@/web/components/TrackListHeader'
|
2022-08-03 23:48:39 +08:00
|
|
|
import player from '@/web/states/player'
|
|
|
|
import { formatDuration } from '@/web/utils/common'
|
|
|
|
import dayjs from 'dayjs'
|
2023-03-26 02:16:01 +08:00
|
|
|
import { useCallback, useMemo } from 'react'
|
2022-08-03 23:48:39 +08:00
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
import { useParams } from 'react-router-dom'
|
2022-10-28 20:29:04 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2023-01-24 16:29:33 +08:00
|
|
|
import useAppleMusicAlbum from '@/web/api/hooks/useAppleMusicAlbum'
|
2023-03-03 03:12:27 +08:00
|
|
|
import { SupportedLanguage } from '@/web/i18n/i18n'
|
2022-08-03 23:48:39 +08:00
|
|
|
|
|
|
|
const Header = () => {
|
2022-10-28 20:29:04 +08:00
|
|
|
const { t, i18n } = useTranslation()
|
2022-08-03 23:48:39 +08:00
|
|
|
const params = useParams()
|
2022-10-28 20:29:04 +08:00
|
|
|
|
2022-08-03 23:48:39 +08:00
|
|
|
const { data: userLikedAlbums } = useUserAlbums()
|
|
|
|
|
2022-08-22 16:51:23 +08:00
|
|
|
const { data: albumRaw, isLoading: isLoadingAlbum } = useAlbum({
|
2022-08-03 23:48:39 +08:00
|
|
|
id: Number(params.id),
|
|
|
|
})
|
|
|
|
const album = useMemo(() => albumRaw?.album, [albumRaw])
|
|
|
|
|
2023-01-24 16:29:33 +08:00
|
|
|
const { data: appleMusicAlbum, isLoading: isLoadingAppleMusicAlbum } = useAppleMusicAlbum(
|
|
|
|
album?.id || 0
|
|
|
|
)
|
2022-08-03 23:48:39 +08:00
|
|
|
|
|
|
|
// For <Cover />
|
|
|
|
const cover = album?.picUrl
|
2023-01-24 16:29:33 +08:00
|
|
|
const videoCover = appleMusicAlbum?.editorialVideo
|
2022-08-03 23:48:39 +08:00
|
|
|
|
|
|
|
// For <Info />
|
|
|
|
const title = album?.name
|
|
|
|
const creatorName = album?.artist.name
|
|
|
|
const creatorLink = `/artist/${album?.artist.id}`
|
2023-03-03 03:12:27 +08:00
|
|
|
const description = useMemo(() => {
|
|
|
|
if (isLoadingAppleMusicAlbum) return ''
|
|
|
|
const fromApple =
|
|
|
|
appleMusicAlbum?.editorialNote?.[i18n.language.replace('-', '_') as 'zh_CN' | 'en_US']
|
|
|
|
if (fromApple) return fromApple
|
|
|
|
if (i18n.language === 'zh-CN' && album?.description) return album?.description
|
|
|
|
return appleMusicAlbum?.editorialNote?.en_US
|
|
|
|
}, [isLoadingAppleMusicAlbum, appleMusicAlbum, i18n.language, appleMusicAlbum])
|
2022-08-03 23:48:39 +08:00
|
|
|
const extraInfo = useMemo(() => {
|
|
|
|
const duration = album?.songs?.reduce((acc, cur) => acc + cur.dt, 0) || 0
|
2023-03-03 03:12:27 +08:00
|
|
|
const albumDuration = formatDuration(
|
|
|
|
duration,
|
|
|
|
i18n.language as SupportedLanguage,
|
|
|
|
'hh[hr] mm[min]'
|
|
|
|
)
|
2022-08-03 23:48:39 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{album?.mark === 1056768 && (
|
2023-01-24 16:29:33 +08:00
|
|
|
<Icon name='explicit' className='mb-px mr-1 h-3 w-3 lg:h-3.5 lg:w-3.5' />
|
2022-08-03 23:48:39 +08:00
|
|
|
)}{' '}
|
2022-10-28 20:29:04 +08:00
|
|
|
{dayjs(album?.publishTime || 0).year()} ·{' '}
|
2023-01-24 16:29:33 +08:00
|
|
|
{t('common.track-with-count', { count: album?.songs?.length })}, {albumDuration}
|
2022-08-03 23:48:39 +08:00
|
|
|
</>
|
|
|
|
)
|
2022-10-28 20:29:04 +08:00
|
|
|
}, [album?.mark, album?.publishTime, album?.songs, i18n.language, t])
|
2022-08-03 23:48:39 +08:00
|
|
|
|
|
|
|
// For <Actions />
|
|
|
|
const isLiked = useMemo(() => {
|
|
|
|
const id = Number(params.id)
|
|
|
|
if (!id) return false
|
2023-03-03 03:12:27 +08:00
|
|
|
return !!userLikedAlbums?.data?.find(item => item.id === id)
|
2022-08-03 23:48:39 +08:00
|
|
|
}, [params.id, userLikedAlbums?.data])
|
|
|
|
|
2023-03-26 02:16:01 +08:00
|
|
|
const onPlay = useCallback(
|
|
|
|
async (trackID: number | null = null) => {
|
|
|
|
if (!album?.id) {
|
|
|
|
toast('无法播放专辑,该专辑不存在')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
player.playAlbum(album.id, trackID)
|
|
|
|
},
|
|
|
|
[album?.id]
|
|
|
|
)
|
2022-08-03 23:48:39 +08:00
|
|
|
|
|
|
|
const likeAAlbum = useMutationLikeAAlbum()
|
2023-03-26 02:16:01 +08:00
|
|
|
const onLike = useCallback(async () => {
|
2022-08-03 23:48:39 +08:00
|
|
|
likeAAlbum.mutateAsync(album?.id || Number(params.id))
|
2023-03-26 02:16:01 +08:00
|
|
|
}, [likeAAlbum.mutateAsync, album?.id, params.id])
|
2022-08-03 23:48:39 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TrackListHeader
|
|
|
|
{...{
|
2022-08-22 16:51:23 +08:00
|
|
|
isLoading: isLoadingAlbum,
|
2022-08-03 23:48:39 +08:00
|
|
|
title,
|
|
|
|
creatorName,
|
|
|
|
creatorLink,
|
|
|
|
description,
|
|
|
|
extraInfo,
|
|
|
|
cover,
|
|
|
|
videoCover,
|
|
|
|
isLiked,
|
|
|
|
onLike,
|
|
|
|
onPlay,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Header
|