YesPlayMusic/packages/web/components/PlayerMobile.tsx

156 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-07-11 11:06:41 +08:00
import player from '@/web/states/player'
2022-06-08 00:07:04 +08:00
import { css, cx } from '@emotion/css'
import { useSnapshot } from 'valtio'
2022-10-28 20:29:04 +08:00
import Image from '@/web/components/Image'
2022-06-08 00:07:04 +08:00
import Icon from '@/web/components/Icon'
import useCoverColor from '@/web/hooks/useCoverColor'
import { resizeImage } from '@/web/utils/common'
2022-07-11 11:06:41 +08:00
import { motion, PanInfo } from 'framer-motion'
2022-06-08 11:48:22 +08:00
import { useLockBodyScroll } from 'react-use'
import { useState } from 'react'
2022-06-30 00:02:21 +08:00
import useUserLikedTracksIDs, {
useMutationLikeATrack,
} from '@/web/api/hooks/useUserLikedTracksIDs'
2022-07-11 11:06:41 +08:00
import uiStates from '@/web/states/uiStates'
import { ease } from '@/web/utils/const'
2022-06-30 00:02:21 +08:00
const LikeButton = () => {
const { track } = useSnapshot(player)
const { data: likedIDs } = useUserLikedTracksIDs()
const isLiked = !!likedIDs?.ids?.find(id => id === track?.id)
const likeATrack = useMutationLikeATrack()
return (
<button
2022-07-11 11:06:41 +08:00
className='flex h-full items-center'
2022-06-30 00:02:21 +08:00
onClick={() => track?.id && likeATrack.mutateAsync(track.id)}
>
<Icon
name={isLiked ? 'heart' : 'heart-outline'}
className='h-7 w-7 text-white/10'
/>
</button>
)
}
2022-06-08 00:07:04 +08:00
const PlayerMobile = () => {
2022-06-11 00:19:07 +08:00
const { track, state } = useSnapshot(player)
const bgColor = useCoverColor(track?.al?.picUrl ?? '')
2022-06-08 11:48:22 +08:00
const [locked, setLocked] = useState(false)
useLockBodyScroll(locked)
2022-07-11 11:06:41 +08:00
const { mobileShowPlayingNext } = useSnapshot(uiStates)
2022-06-08 00:07:04 +08:00
const onDragEnd = (
event: MouseEvent | TouchEvent | PointerEvent,
info: PanInfo
) => {
2022-06-08 11:48:22 +08:00
console.log(JSON.stringify(info))
2022-06-25 13:47:07 +08:00
const { x, y } = info.offset
2022-06-08 00:07:04 +08:00
const offset = 100
2022-06-25 13:47:07 +08:00
if (y > -100) {
if (x > offset) player.prevTrack()
if (x < -offset) player.nextTrack()
}
2022-06-08 11:48:22 +08:00
setLocked(false)
2022-06-08 00:07:04 +08:00
}
return (
<div
className={cx(
2022-06-25 13:47:07 +08:00
'relative z-20 flex h-16 w-full items-center rounded-20 px-3',
2022-06-08 00:07:04 +08:00
css`
background-color: ${bgColor.to};
`
)}
>
2022-07-11 11:06:41 +08:00
{/* Handler */}
{!mobileShowPlayingNext && (
<motion.div
onClick={() => {
uiStates.mobileShowPlayingNext = true
}}
className={cx(
'absolute right-0 left-0 flex justify-center',
css`
--height: 20px;
height: var(--height);
top: calc(var(--height) * -1);
`
)}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ ease, duration: 0.2 }}
>
<Icon name='player-handler' className='h-2.5 text-brand-700' />
</motion.div>
)}
2022-06-30 00:02:21 +08:00
2022-07-11 11:06:41 +08:00
{/* Cover */}
2022-06-08 11:48:22 +08:00
<div className='h-full py-2.5'>
<Image
2022-06-11 00:19:07 +08:00
src={resizeImage(track?.al.picUrl || '', 'sm')}
2022-07-11 11:06:41 +08:00
className='z-10 aspect-square h-full rounded-lg'
2022-06-08 11:48:22 +08:00
/>
</div>
2022-06-08 00:07:04 +08:00
2022-06-25 13:47:07 +08:00
{/* Track info */}
2022-07-11 11:06:41 +08:00
<div className='relative flex h-full flex-grow items-center overflow-hidden px-3'>
2022-06-08 00:07:04 +08:00
<motion.div
drag='x'
dragConstraints={{ left: 0, right: 0 }}
2022-06-08 11:48:22 +08:00
onDragStart={() => setLocked(true)}
2022-06-08 00:07:04 +08:00
onDragEnd={onDragEnd}
2022-07-11 11:06:41 +08:00
dragDirectionLock={true}
className='flex h-full flex-grow items-center '
2022-06-08 00:07:04 +08:00
>
2022-06-08 11:48:22 +08:00
<div className='flex-shrink-0'>
2022-07-11 11:06:41 +08:00
<div className='line-clamp-1 text-14 font-bold text-white'>
2022-06-11 00:19:07 +08:00
{track?.name}
2022-06-08 11:48:22 +08:00
</div>
2022-07-11 11:06:41 +08:00
<div className='line-clamp-1 mt-1 text-12 font-bold text-white/60'>
2022-06-11 00:19:07 +08:00
{track?.ar?.map(a => a.name).join(', ')}
2022-06-08 11:48:22 +08:00
</div>
</div>
2022-07-11 11:06:41 +08:00
<div className='h-full flex-grow'></div>
2022-06-08 00:07:04 +08:00
</motion.div>
<div
className={cx(
'absolute left-0 top-0 bottom-0 w-3 ',
css`
background: linear-gradient(to right, ${bgColor.to}, transparent);
`
)}
></div>
<div
className={cx(
'absolute right-0 top-0 bottom-0 w-3 bg-red-200',
css`
background: linear-gradient(to left, ${bgColor.to}, transparent);
`
)}
></div>
</div>
2022-06-25 13:47:07 +08:00
{/* Like */}
2022-06-30 00:02:21 +08:00
<LikeButton />
2022-06-08 00:07:04 +08:00
2022-06-25 13:47:07 +08:00
{/* Play or pause */}
2022-06-08 00:07:04 +08:00
<button
onClick={() => player.playOrPause()}
className='ml-2.5 flex items-center justify-center rounded-full bg-white/20 p-2.5'
>
<Icon
2022-06-11 00:19:07 +08:00
name={state === 'playing' ? 'pause' : 'play'}
2022-07-11 11:06:41 +08:00
className='h-6 w-6 text-white/80'
2022-06-08 00:07:04 +08:00
/>
</button>
</div>
)
}
export default PlayerMobile