sonda/web/src/components/layout/PlayerBar.tsx

111 lines
4.2 KiB
TypeScript

import { ListMusic } from 'lucide-react'
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
import type { Track } from '@/api/catalog'
import Cover from '@/components/Cover'
import FullscreenButton from '@/components/FullscreenButton'
import Scrubber from '@/components/Scrubber'
import VolumeControl from '@/components/VolumeControl'
import { Button } from '@/components/ui/button'
import { formatTime } from '@/lib/time'
import { currentTrack, usePlayer } from '@/player/store'
import { useNowPlayingUi } from '@/player/ui'
import NowPlaying from './NowPlaying'
import PlayerControls from './PlayerControls'
/** Space plays and pauses, unless the keypress belongs to a field, a button or a link. */
function usePlayPauseKey() {
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.code !== 'Space' || e.defaultPrevented || e.ctrlKey || e.metaKey || e.altKey) return
const target = e.target instanceof HTMLElement ? e.target : null
if (target?.closest('input, textarea, select, button, a, summary, [contenteditable="true"]')) return
e.preventDefault()
usePlayer.getState().togglePlay()
}
window.addEventListener('keydown', onKeyDown)
return () => window.removeEventListener('keydown', onKeyDown)
}, [])
}
/** The position, the seek bar and the length. The only part of the bar that follows the playback every moment. */
function PlayerSeek({ track }: { track: Track | null }) {
const positionMs = usePlayer((s) => s.positionMs)
const seek = usePlayer((s) => s.seek)
return (
<div className="player-seek flex w-full max-w-xl items-center gap-3 justify-self-center text-[13px] text-muted-foreground tabular-nums">
<span>{formatTime(track ? positionMs : 0)}</span>
<div className="flex-1">
<Scrubber positionMs={positionMs} durationMs={track?.duration_ms ?? 0} onSeek={seek} variant="line" />
</div>
<span>{formatTime(track?.duration_ms ?? 0)}</span>
</div>
)
}
/** The bar at the bottom of every page: what is playing, the controls and the position. */
export default function PlayerBar() {
const { t } = useTranslation()
const setOpen = useNowPlayingUi((s) => s.setOpen)
const track = usePlayer(currentTrack)
usePlayPauseKey()
return (
<footer
aria-label={t('player.label')}
className="player-bar gap-x-4 gap-y-1 rounded-xl bg-card px-4 py-2 text-card-foreground sm:gap-x-6 sm:px-5 sm:py-3"
>
<div className="player-info flex min-w-0 items-center gap-3">
{track ? (
<>
<button type="button" aria-label={t('player.open')} onClick={() => setOpen(true)} className="rounded-md">
<Cover albumId={track.album?.id ?? null} size={56} />
</button>
<div className="flex min-w-0 flex-col">
<button type="button" onClick={() => setOpen(true)} className="truncate text-left font-bold">
{track.title}
</button>
<span className="truncate text-sm text-muted-foreground">
{track.artists.length === 0
? '—'
: track.artists.map((a, i) => (
<span key={a.id}>
{i > 0 && ', '}
<Link to={`/artists/${a.id}`} className="hover:underline">
{a.name}
</Link>
</span>
))}
</span>
</div>
</>
) : (
<span className="text-muted-foreground">{t('player.nothingPlaying')}</span>
)}
</div>
<div className="player-controls flex justify-end sm:justify-center">
<PlayerControls />
</div>
<PlayerSeek track={track} />
<div className="player-extras flex items-center justify-end max-sm:hidden">
<VolumeControl />
<FullscreenButton />
<Button
variant="ghost"
className="size-11 text-muted-foreground"
aria-label={t('player.queue')}
disabled={!track}
onClick={() => setOpen(true)}
>
<ListMusic />
</Button>
</div>
<NowPlaying />
</footer>
)
}