sonda/web/src/components/layout/NowPlaying.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

188 lines
9.3 KiB
TypeScript

import { ChevronDown, ListMusic, Sparkles, X } from 'lucide-react'
import { useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
import { useFriends } from '@/api/friends'
import { usePeaks } from '@/api/peaks'
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 Milkdrop from '@/components/Milkdrop'
import Visualizer from '@/components/Visualizer'
import TrackRow from '@/components/TrackRow'
import VolumeControl from '@/components/VolumeControl'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogDescription, DialogTitle } from '@/components/ui/dialog'
import { formatTime } from '@/lib/time'
import { useSwipe } from '@/lib/useSwipe'
import { albumHref, artistHref, type QueuedTrack } from '@/player/media'
import { useMilkdrop } from '@/player/milkdrop'
import { useVisualizerPref } from '@/player/prefs'
import { currentTrack, usePlayer } from '@/player/store'
import { useNowPlayingUi } from '@/player/ui'
import PlayerControls from './PlayerControls'
/** How many of the next tracks a wide screen lists; a phone lists them all, up to MAX_UP_NEXT. */
const UP_NEXT = 3
const MAX_UP_NEXT = 100
/** The big seek bar and the times. The part of this screen that follows the playback every moment. */
function NowPlayingSeek({ track }: { track: QueuedTrack }) {
const durationMs = track.duration_ms
const positionMs = usePlayer((s) => s.positionMs)
const seek = usePlayer((s) => s.seek)
const peaks = usePeaks(track.id, { friend: track.friend })
return (
<div className="flex flex-col gap-1">
<Scrubber positionMs={positionMs} durationMs={durationMs} onSeek={seek} size="big" peaks={peaks} friend={track.friend !== undefined} />
<div className="flex justify-between text-sm text-muted-foreground tabular-nums">
<span>{formatTime(positionMs)}</span>
<span>{formatTime(durationMs)}</span>
</div>
</div>
)
}
/** On a friend's track: the audio comes through this server, and the friend does not see who listens. */
function FriendNote({ friend }: { friend: number }) {
const { t } = useTranslation()
const name = useFriends().data?.find((f) => f.id === friend)?.name
if (!name) return null
return <p className="text-[13px] leading-snug text-muted-foreground text-pretty">{t('player.friendNote', { name })}</p>
}
/** The full-screen player: a big cover, the controls and what comes next. */
export default function NowPlaying() {
const { t } = useTranslation()
const { open, setOpen } = useNowPlayingUi()
const track = usePlayer(currentTrack)
const queue = usePlayer((s) => s.queue)
const index = usePlayer((s) => s.index)
const jumpTo = usePlayer((s) => s.jumpTo)
const radio = usePlayer((s) => s.radio)
const upNextRef = useRef<HTMLElement>(null)
const milkdrop = useVisualizerPref((s) => s.mode) === 'milkdrop'
const milkdropRunning = useMilkdrop((s) => s.running)
const nextPicture = useMilkdrop((s) => s.skip)
// Nothing left to show (the queue was emptied): close.
useEffect(() => {
if (open && !track) setOpen(false)
}, [open, track, setOpen])
// A swipe down from the top of the page closes the player, as the arrow does.
const swipe = useSwipe({ onDown: () => setOpen(false), allow: (e) => e.currentTarget.scrollTop === 0 })
const upNext = queue.slice(index + 1, index + 1 + MAX_UP_NEXT)
return (
<Dialog open={open && track !== null} onOpenChange={setOpen}>
<DialogContent
{...swipe}
showCloseButton={false}
className="inset-0 h-dvh w-screen max-w-none translate-x-0 translate-y-0 grid-cols-[minmax(0,1fr)] content-start overflow-y-auto sm:content-center rounded-none bg-background p-6 sm:max-w-none md:p-12 motion-reduce:data-closed:animate-none motion-reduce:data-open:animate-none"
>
{track && (
<>
{/* Milkdrop fills the whole player, behind everything. A veil keeps the words easy to read over it. */}
{milkdrop && (
<div className="pointer-events-none absolute inset-0 -z-10">
<Milkdrop fallback={null} className="size-full" />
<div className="absolute inset-0 bg-background/45" />
</div>
)}
{/* On a phone: close on the left, the friend in the middle, the queue on the right. Wide: the buttons at the corner. */}
<div data-phone-top className="-mx-3 -mt-3 flex items-center justify-between gap-2 sm:absolute sm:top-4 sm:right-4 sm:m-0 sm:flex-row-reverse sm:justify-start sm:gap-1">
<Button variant="ghost" className="size-11" aria-label={t('player.close')} onClick={() => setOpen(false)}>
<ChevronDown className="sm:hidden" />
<X className="max-sm:hidden" />
</Button>
{track.friend !== undefined && <FriendChip friend={track.friend} className="px-3.5 text-sm sm:hidden" />}
{milkdrop && milkdropRunning && (
<Button variant="ghost" className="size-11 sm:order-last" aria-label={t('player.nextPicture')} onClick={nextPicture}>
<Sparkles />
</Button>
)}
<Button
variant="ghost"
className="size-11 sm:hidden"
aria-label={t('player.queue')}
onClick={() => upNextRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' })}
>
<ListMusic />
</Button>
<FullscreenButton className="max-sm:hidden" />
</div>
<DialogDescription className="sr-only">{t('player.nowPlaying')}</DialogDescription>
<div className="mx-auto flex w-full max-w-5xl flex-wrap items-center justify-center gap-x-16 gap-y-6 sm:gap-y-8 sm:pt-10">
<div className="flex min-w-0 flex-[1_1_320px] justify-center max-sm:max-w-72">
<Cover albumId={track.album?.id ?? null} friend={track.friend} size={520} fluid />
</div>
<div className="flex min-w-0 flex-[1_1_380px] flex-col gap-5 sm:gap-7">
<div className="flex flex-col gap-2">
<div className="flex items-start gap-3">
<DialogTitle className="min-w-0 text-3xl leading-tight font-extrabold tracking-tight sm:text-4xl md:text-6xl">
{track.title}
</DialogTitle>
{track.friend === undefined && <FavoriteButton kind="tracks" id={track.id} className="size-11 flex-none text-muted-foreground" />}
</div>
<p className="flex flex-wrap items-center gap-x-3 gap-y-1 text-xl font-semibold">
{track.artists.length === 0
? '—'
: track.artists.map((a, i) => (
<span key={a.id}>
{i > 0 && ', '}
<Link to={artistHref(track, a)} onClick={() => setOpen(false)} className="hover:underline">
{a.name}
</Link>
</span>
))}
{track.friend !== undefined && <FriendChip friend={track.friend} className="text-sm font-semibold max-sm:hidden" />}
</p>
{track.album && (
<Link
to={albumHref(track, track.album)}
onClick={() => setOpen(false)}
className="text-muted-foreground hover:underline"
>
{track.album.title}
</Link>
)}
{radio && <p className="text-sm font-semibold text-primary">{t('radio.now', { name: radio.title })}</p>}
</div>
<Visualizer size="full" friend={track.friend !== undefined} className="h-20 w-full sm:h-36" />
<NowPlayingSeek track={track} />
<PlayerControls large className="w-full justify-between sm:w-auto sm:justify-start" />
<VolumeControl className="-mt-3 max-sm:hidden" />
{upNext.length > 0 && (
<section ref={upNextRef} aria-label={t('player.upNext')} className="flex scroll-mt-4 flex-col gap-1 border-t pt-4">
<h2 className="pb-2 font-bold">{t('player.upNext')}</h2>
{upNext.map((next, k) => {
const row = <TrackRow track={next} index={index + 1 + k} onPlay={() => jumpTo(index + 1 + k)} />
// Past the first few, only a phone has the rows: `contents` leaves the row where it was.
return k < UP_NEXT ? (
<div key={`${next.id}-${k}`} className="contents">{row}</div>
) : (
<div key={`${next.id}-${k}`} className="contents sm:hidden">{row}</div>
)
})}
</section>
)}
{track.friend !== undefined && <FriendNote friend={track.friend} />}
</div>
</div>
</>
)}
</DialogContent>
</Dialog>
)
}