175 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-07-11 11:06:41 +08:00
import { isIosPwa, resizeImage } from '@/web/utils/common'
import player from '@/web/states/player'
2022-06-11 00:19:07 +08:00
import { State as PlayerState } from '@/web/utils/player'
2022-05-29 17:53:27 +08:00
import { useSnapshot } from 'valtio'
import useTracks from '@/web/api/hooks/useTracks'
import { css, cx } from '@emotion/css'
2022-06-06 01:00:25 +08:00
import Wave from './Wave'
2022-06-08 00:07:04 +08:00
import Icon from '@/web/components/Icon'
2022-06-12 15:29:14 +08:00
import { useWindowSize } from 'react-use'
import { playerWidth, topbarHeight } from '@/web/utils/const'
2022-07-11 11:06:41 +08:00
import useIsMobile from '@/web/hooks/useIsMobile'
import { Virtuoso } from 'react-virtuoso'
2022-08-22 16:51:23 +08:00
import toast from 'react-hot-toast'
import { openContextMenu } from '@/web/states/contextMenus'
2022-10-28 20:29:04 +08:00
import { useTranslation } from 'react-i18next'
2022-05-29 17:53:27 +08:00
2022-06-11 00:19:07 +08:00
const Header = () => {
2022-10-28 20:29:04 +08:00
const { t } = useTranslation()
2022-06-11 00:19:07 +08:00
return (
<div
className={cx(
2022-08-03 23:48:39 +08:00
'absolute top-0 left-0 z-20 flex w-full items-center justify-between bg-contain bg-repeat-x px-7 pb-6 text-14 font-bold text-neutral-700 dark:text-neutral-300 lg:px-0'
2022-06-11 00:19:07 +08:00
)}
>
<div className='flex'>
2022-07-11 11:06:41 +08:00
<div className='mr-2 h-4 w-1 bg-brand-700'></div>
2022-10-28 20:29:04 +08:00
{t`player.queue`}
2022-06-11 00:19:07 +08:00
</div>
<div className='flex'>
2022-08-22 16:51:23 +08:00
<div onClick={() => toast('开发中')} className='mr-2'>
2022-06-11 00:19:07 +08:00
<Icon name='repeat-1' className='h-7 w-7 opacity-40' />
</div>
2022-08-22 16:51:23 +08:00
<div onClick={() => toast('开发中')}>
2022-06-11 00:19:07 +08:00
<Icon name='shuffle' className='h-7 w-7 opacity-40' />
</div>
</div>
</div>
)
}
2022-05-29 17:53:27 +08:00
2022-06-11 00:19:07 +08:00
const Track = ({
track,
index,
playingTrackIndex,
state,
}: {
track?: Track
index: number
playingTrackIndex: number
state: PlayerState
}) => {
2022-05-29 17:53:27 +08:00
return (
2022-07-11 11:06:41 +08:00
<div
className='mb-5 flex items-center justify-between'
2022-06-11 00:19:07 +08:00
onClick={e => {
if (e.detail === 2 && track?.id) player.playTrack(track.id)
}}
2022-08-22 16:51:23 +08:00
onContextMenu={event => {
track?.id &&
openContextMenu({
event,
type: 'track',
dataSourceID: track.id,
options: {
useCursorPosition: true,
},
})
}}
2022-06-11 00:19:07 +08:00
>
{/* Cover */}
2022-07-11 11:06:41 +08:00
<img
2022-06-11 00:19:07 +08:00
alt='Cover'
2022-07-11 11:06:41 +08:00
className='mr-4 aspect-square h-14 w-14 flex-shrink-0 rounded-12'
2022-06-11 00:19:07 +08:00
src={resizeImage(track?.al?.picUrl || '', 'sm')}
/>
{/* Track info */}
2022-07-11 11:06:41 +08:00
<div className='mr-3 flex-grow'>
2022-06-11 00:19:07 +08:00
<div
className={cx(
'line-clamp-1 text-16 font-medium ',
playingTrackIndex === index
? 'text-brand-700'
: 'text-neutral-700 dark:text-neutral-200'
)}
>
{track?.name}
2022-06-08 00:07:04 +08:00
</div>
2022-08-22 16:51:23 +08:00
<div className='line-clamp-1 mt-1 text-14 font-bold text-neutral-200 dark:text-white/25'>
2022-06-11 00:19:07 +08:00
{track?.ar.map(a => a.name).join(', ')}
2022-06-08 00:07:04 +08:00
</div>
2022-05-29 17:53:27 +08:00
</div>
2022-06-08 00:07:04 +08:00
2022-06-11 00:19:07 +08:00
{/* Wave icon */}
{playingTrackIndex === index ? (
<Wave playing={state === 'playing'} />
) : (
2022-07-11 11:06:41 +08:00
<div className='text-16 font-medium text-neutral-700 dark:text-neutral-200'>
2022-06-11 00:19:07 +08:00
{String(index + 1).padStart(2, '0')}
</div>
)}
2022-07-11 11:06:41 +08:00
</div>
2022-06-11 00:19:07 +08:00
)
}
2022-06-12 15:29:14 +08:00
const TrackList = ({ className }: { className?: string }) => {
2022-06-11 00:19:07 +08:00
const { trackList, trackIndex, state } = useSnapshot(player)
2022-06-12 15:29:14 +08:00
const { data: tracksRaw } = useTracks({ ids: trackList })
const tracks = tracksRaw?.songs || []
const { height } = useWindowSize()
2022-07-11 11:06:41 +08:00
const isMobile = useIsMobile()
2022-06-12 15:29:14 +08:00
2022-07-11 11:06:41 +08:00
const listHeight = height - topbarHeight - playerWidth - 24 // 24是封面与底部间距
const listHeightMobile = height - 154 - 110 - (isIosPwa ? 34 : 0) // 154是列表距离底部的距离,110是顶部的距离
2022-06-12 15:29:14 +08:00
2022-06-11 00:19:07 +08:00
return (
<>
2022-05-29 17:53:27 +08:00
<div
2022-07-11 11:06:41 +08:00
className={css`
mask-image: linear-gradient(
to bottom,
transparent 22px,
black 42px
); // 顶部渐变遮罩
`}
2022-05-29 17:53:27 +08:00
>
2022-07-11 11:06:41 +08:00
<Virtuoso
2022-06-12 15:29:14 +08:00
style={{
2022-07-11 11:06:41 +08:00
height: `${isMobile ? listHeightMobile : listHeight}px`,
2022-06-12 15:29:14 +08:00
}}
2022-07-11 11:06:41 +08:00
totalCount={tracks.length}
className={cx(
'no-scrollbar relative z-10 w-full overflow-auto',
className,
css`
mask-image: linear-gradient(
to top,
transparent 8px,
black 42px
); // 底部渐变遮罩
`
)}
fixedItemHeight={76}
data={tracks}
overscan={10}
components={{
Header: () => <div className='h-8'></div>,
2022-07-12 22:42:50 +08:00
Footer: () => <div className='h-8'></div>,
2022-07-11 11:06:41 +08:00
}}
itemContent={(index, track) => (
<Track
key={index}
track={track}
index={index}
playingTrackIndex={trackIndex}
state={state}
/>
)}
></Virtuoso>
2022-05-29 17:53:27 +08:00
</div>
2022-06-12 15:29:14 +08:00
</>
)
}
2022-08-03 23:48:39 +08:00
const PlayingNext = () => {
2022-06-12 15:29:14 +08:00
return (
<>
<Header />
2022-08-03 23:48:39 +08:00
<TrackList />
2022-05-29 17:53:27 +08:00
</>
)
}
export default PlayingNext