perf(web): the player parts follow only the state they show

This commit is contained in:
tgiachi 2026-11-13 12:44:53 +01:00
commit 817b869af7
4 changed files with 80 additions and 45 deletions

View file

@ -16,19 +16,36 @@ import PlayerControls from './PlayerControls'
const UP_NEXT = 3
/** The big seek bar and the times. The part of this screen that follows the playback every moment. */
function NowPlayingSeek({ durationMs }: { durationMs: number }) {
const positionMs = usePlayer((s) => s.positionMs)
const seek = usePlayer((s) => s.seek)
return (
<div className="flex flex-col gap-1">
<Scrubber positionMs={positionMs} durationMs={durationMs} onSeek={seek} variant="bars" />
<div className="flex justify-between text-sm text-muted-foreground tabular-nums">
<span>{formatTime(positionMs)}</span>
<span>{formatTime(durationMs)}</span>
</div>
</div>
)
}
/** 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 player = usePlayer()
const track = currentTrack(player)
const track = usePlayer(currentTrack)
const queue = usePlayer((s) => s.queue)
const index = usePlayer((s) => s.index)
const jumpTo = usePlayer((s) => s.jumpTo)
// Nothing left to show (the queue was emptied): close.
useEffect(() => {
if (open && !track) setOpen(false)
}, [open, track, setOpen])
const upNext = player.queue.slice(player.index + 1, player.index + 1 + UP_NEXT)
const upNext = queue.slice(index + 1, index + 1 + UP_NEXT)
return (
<Dialog open={open && track !== null} onOpenChange={setOpen}>
@ -79,18 +96,7 @@ export default function NowPlaying() {
)}
</div>
<div className="flex flex-col gap-1">
<Scrubber
positionMs={player.positionMs}
durationMs={track.duration_ms}
onSeek={player.seek}
variant="bars"
/>
<div className="flex justify-between text-sm text-muted-foreground tabular-nums">
<span>{formatTime(player.positionMs)}</span>
<span>{formatTime(track.duration_ms)}</span>
</div>
</div>
<NowPlayingSeek durationMs={track.duration_ms} />
<PlayerControls large />
<VolumeControl className="-mt-3" />
@ -102,8 +108,8 @@ export default function NowPlaying() {
<TrackRow
key={`${next.id}-${k}`}
track={next}
index={player.index + 1 + k}
onPlay={() => player.jumpTo(player.index + 1 + k)}
index={index + 1 + k}
onPlay={() => jumpTo(index + 1 + k)}
/>
))}
</section>

View file

@ -1,3 +1,4 @@
import { Profiler } from 'react'
import { act, fireEvent, render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter } from 'react-router-dom'
@ -11,6 +12,7 @@ import { usePlayer } from '@/player/store'
import { useNowPlayingUi } from '@/player/ui'
import { fakeFullscreen } from '@/test/fullscreen'
import PlayerBar from './PlayerBar'
import PlayerControls from './PlayerControls'
function track(n: number, over: Partial<Track> = {}): Track {
return {
@ -303,3 +305,19 @@ test('Tab stays inside the full-screen player', async () => {
for (const el of outside) if (el) expect(el.contains(document.activeElement)).toBe(false)
}
})
test('the controls and the title do not render again when only the position moves', () => {
usePlayer.getState().playTracks([a, b], 0)
let commits = 0
render(
<MemoryRouter>
<Profiler id="controls" onRender={() => commits++}>
<PlayerControls />
</Profiler>
</MemoryRouter>,
)
const before = commits
act(() => usePlayer.getState().tick(5_000))
act(() => usePlayer.getState().tick(6_000))
expect(commits).toBe(before)
})

View file

@ -2,6 +2,7 @@ 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'
@ -28,12 +29,26 @@ function usePlayPauseKey() {
}, [])
}
/** 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 player = usePlayer()
const setOpen = useNowPlayingUi((s) => s.setOpen)
const track = currentTrack(player)
const track = usePlayer(currentTrack)
usePlayPauseKey()
return (
@ -74,18 +89,7 @@ export default function PlayerBar() {
<PlayerControls />
</div>
<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 ? player.positionMs : 0)}</span>
<div className="flex-1">
<Scrubber
positionMs={player.positionMs}
durationMs={track?.duration_ms ?? 0}
onSeek={player.seek}
variant="line"
/>
</div>
<span>{formatTime(track?.duration_ms ?? 0)}</span>
</div>
<PlayerSeek track={track} />
<div className="player-extras flex items-center justify-end max-sm:hidden">
<VolumeControl />

View file

@ -7,45 +7,52 @@ import { currentTrack, usePlayer } from '@/player/store'
/** Shuffle, previous, play/pause, next and repeat. Used by the player bar and the full-screen player. */
export default function PlayerControls({ large = false }: { large?: boolean }) {
const { t } = useTranslation()
const player = usePlayer()
const none = currentTrack(player) === null
const none = usePlayer((s) => currentTrack(s) === null)
const playing = usePlayer((s) => s.playing)
const shuffle = usePlayer((s) => s.shuffle)
const repeat = usePlayer((s) => s.repeat)
const toggleShuffle = usePlayer((s) => s.toggleShuffle)
const prev = usePlayer((s) => s.prev)
const togglePlay = usePlayer((s) => s.togglePlay)
const next = usePlayer((s) => s.next)
const cycleRepeat = usePlayer((s) => s.cycleRepeat)
const small = large ? 'size-12' : 'size-11'
const repeatLabel = { off: 'player.repeatOff', all: 'player.repeatAll', one: 'player.repeatOne' }[player.repeat]
const repeatLabel = { off: 'player.repeatOff', all: 'player.repeatAll', one: 'player.repeatOne' }[repeat]
return (
<div className="flex items-center gap-1 sm:gap-3">
<Button
variant="ghost"
className={cn(small, player.shuffle ? 'text-primary' : 'text-muted-foreground')}
className={cn(small, shuffle ? 'text-primary' : 'text-muted-foreground')}
aria-label={t('player.shuffle')}
aria-pressed={player.shuffle}
aria-pressed={shuffle}
disabled={none}
onClick={player.toggleShuffle}
onClick={toggleShuffle}
>
<Shuffle />
</Button>
<Button variant="ghost" className={small} aria-label={t('player.previous')} disabled={none} onClick={player.prev}>
<Button variant="ghost" className={small} aria-label={t('player.previous')} disabled={none} onClick={prev}>
<SkipBack />
</Button>
<Button
className={cn(large ? 'size-16' : 'size-11', 'rounded-md')}
aria-label={player.playing ? t('player.pause') : t('player.play')}
aria-label={playing ? t('player.pause') : t('player.play')}
disabled={none}
onClick={player.togglePlay}
onClick={togglePlay}
>
{player.playing ? <Pause /> : <Play />}
{playing ? <Pause /> : <Play />}
</Button>
<Button variant="ghost" className={small} aria-label={t('player.next')} disabled={none} onClick={player.next}>
<Button variant="ghost" className={small} aria-label={t('player.next')} disabled={none} onClick={next}>
<SkipForward />
</Button>
<Button
variant="ghost"
className={cn(small, player.repeat === 'off' ? 'text-muted-foreground' : 'text-primary')}
className={cn(small, repeat === 'off' ? 'text-muted-foreground' : 'text-primary')}
aria-label={t(repeatLabel)}
disabled={none}
onClick={player.cycleRepeat}
onClick={cycleRepeat}
>
{player.repeat === 'one' ? <Repeat1 /> : <Repeat />}
{repeat === 'one' ? <Repeat1 /> : <Repeat />}
</Button>
</div>
)