53 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-08-03 23:48:39 +08:00
import player from '@/web/states/player'
import { resizeImage } from '@/web/utils/common'
import { ease } from '@/web/utils/const'
import { cx } from '@emotion/css'
import { useAnimation, motion } from 'framer-motion'
import { useState, useRef, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { useSnapshot } from 'valtio'
2023-01-28 11:54:57 +08:00
import { subscribeKey } from 'valtio/utils'
2022-08-03 23:48:39 +08:00
const Cover = () => {
2022-10-28 20:29:04 +08:00
const { track } = useSnapshot(player)
const [cover, setCover] = useState(track?.al.picUrl)
2022-08-03 23:48:39 +08:00
const controls = useAnimation()
const navigate = useNavigate()
useEffect(() => {
2023-01-28 11:54:57 +08:00
const unsubscribe = subscribeKey(player, 'track', async () => {
const coverUrl = player.track?.al.picUrl
await controls.start({
opacity: 0,
transition: { duration: 0.2 },
})
setCover(coverUrl)
if (!coverUrl) return
const img = new Image()
img.onload = () => {
controls.start({
opacity: 1,
transition: { duration: 0.2 },
})
}
img.src = coverUrl
2022-08-03 23:48:39 +08:00
})
2023-01-28 11:54:57 +08:00
return unsubscribe
}, [])
2022-08-03 23:48:39 +08:00
return (
<motion.img
animate={controls}
2023-01-28 11:54:57 +08:00
transition={{ ease }}
className='absolute inset-0 w-full'
2022-08-03 23:48:39 +08:00
src={cover}
onClick={() => {
2022-10-28 20:29:04 +08:00
const id = track?.al.id
2022-08-03 23:48:39 +08:00
if (id) navigate(`/album/${id}`)
}}
/>
)
}
export default Cover