sonda/web/src/components/layout/PlayerBar.tsx
tgiachi 75a7898c68 feat(web): make Sonda feel like an app on a phone
A review of the phone experience (the issue) found a desktop page made
smaller. What changes:

feat:
- The player bar is one row on a phone (cover, title, play and next, a thin
  line of progress) and is not there while nothing plays. A tap or a swipe up
  on it opens the full-screen player.
- The Back gesture closes the full-screen player instead of leaving the page
  under it; a swipe down closes it too. The full queue is listed on a phone.
- The queue is kept in the browser: a reload, or a tab that Android throws
  away, comes back paused on the same song at the same second. It belongs to
  the person signed in and is forgotten on sign-out.
- Tabs have icons; the page starts at its top on a new route and back goes to
  where the list was; a swipe on the page no longer reloads it.

fix:
- The player now knows when the system pauses the audio (another app, a
  headset), so the button and the lock screen no longer say it plays.
- The lock screen has a working seek bar and the right play/pause state.
- The player bar was not hidden by max-sm:hidden: its grid rules were outside
  the Tailwind layers and won. They are in the components layer now.
- Song rows give the title the room on a phone (no number column).
- The album header is centred and the greeting is smaller on a phone; recent
  songs on Home show the artist.
- Thumbnails ask for the small cover (300 px) and not the 1000 px one.
- Pages use dvh, the viewport covers the notch, and the tabs keep clear of
  the gesture bar; the update notice no longer sits on the bar.
- No grey tap flash, no double-tap delay, no image menu on a long press.
2026-11-16 17:38:58 +01:00

153 lines
6.3 KiB
TypeScript

import { ListMusic } from 'lucide-react'
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
import Cover from '@/components/Cover'
import FavoriteButton from '@/components/FavoriteButton'
import FriendChip from '@/components/FriendChip'
import FullscreenButton from '@/components/FullscreenButton'
import Scrubber from '@/components/Scrubber'
import Visualizer from '@/components/Visualizer'
import VolumeControl from '@/components/VolumeControl'
import { Button } from '@/components/ui/button'
import { usePeaks } from '@/api/peaks'
import { formatTime } from '@/lib/time'
import { cn } from '@/lib/utils'
import { artistHref, type QueuedTrack } from '@/player/media'
import { useSwipe } from '@/lib/useSwipe'
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: QueuedTrack | null }) {
const positionMs = usePlayer((s) => s.positionMs)
const seek = usePlayer((s) => s.seek)
// A friend's track has its shape from the friend's server, which works it out the first time.
const peaks = usePeaks(track?.id ?? null, { friend: track?.friend })
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 max-sm:hidden">
<span>{formatTime(track ? positionMs : 0)}</span>
<div className="flex-1">
<Scrubber
positionMs={positionMs}
durationMs={track?.duration_ms ?? 0}
onSeek={seek}
size="bar"
peaks={peaks}
friend={track?.friend !== undefined}
/>
</div>
<span>{formatTime(track?.duration_ms ?? 0)}</span>
</div>
)
}
/** On a phone the position is a thin line along the top of the bar: it shows how far the song is and cannot be touched. */
function PhoneProgress({ track }: { track: QueuedTrack | null }) {
const positionMs = usePlayer((s) => s.positionMs)
const duration = track?.duration_ms ?? 0
const share = duration > 0 ? Math.min(100, Math.max(0, (positionMs / duration) * 100)) : 0
return (
<div data-testid="phone-progress" aria-hidden="true" className="absolute inset-x-0 top-0 h-0.5 bg-border sm:hidden">
<div className={track?.friend !== undefined ? 'h-full bg-friend' : 'h-full bg-primary'} style={{ width: `${share}%` }} />
</div>
)
}
/**
* The bar at the bottom of every page: what is playing, the controls and the position. On a phone it is one row, and
* it is not there at all while nothing plays; the full-screen player has the rest.
*/
export default function PlayerBar() {
const { t } = useTranslation()
const setOpen = useNowPlayingUi((s) => s.setOpen)
const track = usePlayer(currentTrack)
usePlayPauseKey()
// A swipe up on a phone opens the full-screen player, as the cover does.
const swipe = useSwipe({ onUp: () => track && setOpen(true) })
return (
<footer
{...swipe}
aria-label={t('player.label')}
className={cn(
'player-bar relative gap-x-4 gap-y-1 border-t border-border bg-card px-4 py-2 text-card-foreground sm:gap-x-6 sm:px-8 sm:py-3',
!track && 'max-sm:hidden',
)}
>
<PhoneProgress track={track} />
<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} friend={track.friend} 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="flex min-w-0 items-center gap-2 text-sm text-muted-foreground">
<span className="truncate">
{track.artists.length === 0
? '—'
: track.artists.map((a, i) => (
<span key={a.id}>
{i > 0 && ', '}
<Link to={artistHref(track, a)} className="hover:underline">
{a.name}
</Link>
</span>
))}
</span>
{track.friend !== undefined && <FriendChip friend={track.friend} />}
</span>
</div>
</>
) : (
<span className="text-muted-foreground">{t('player.nothingPlaying')}</span>
)}
</div>
<div className="player-controls flex justify-end sm:justify-center">
<PlayerControls phoneMini />
</div>
<PlayerSeek track={track} />
<div className="player-extras flex items-center justify-end max-sm:hidden">
{track && track.friend === undefined && <FavoriteButton kind="tracks" id={track.id} className="size-11 text-muted-foreground" />}
<Visualizer size="bar" friend={track?.friend !== undefined} className="mr-3 h-10 w-36 max-lg: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>
)
}