From 817b869af7a2ab63321eacb88b5e35cb05ef22f4 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Fri, 13 Nov 2026 12:44:53 +0100 Subject: [PATCH] perf(web): the player parts follow only the state they show --- web/src/components/layout/NowPlaying.tsx | 40 +++++++++++--------- web/src/components/layout/Player.test.tsx | 18 +++++++++ web/src/components/layout/PlayerBar.tsx | 32 +++++++++------- web/src/components/layout/PlayerControls.tsx | 35 ++++++++++------- 4 files changed, 80 insertions(+), 45 deletions(-) diff --git a/web/src/components/layout/NowPlaying.tsx b/web/src/components/layout/NowPlaying.tsx index 8015b21..85fe160 100644 --- a/web/src/components/layout/NowPlaying.tsx +++ b/web/src/components/layout/NowPlaying.tsx @@ -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 ( +
+ +
+ {formatTime(positionMs)} + {formatTime(durationMs)} +
+
+ ) +} + /** 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 ( @@ -79,18 +96,7 @@ export default function NowPlaying() { )} -
- -
- {formatTime(player.positionMs)} - {formatTime(track.duration_ms)} -
-
+ @@ -102,8 +108,8 @@ export default function NowPlaying() { player.jumpTo(player.index + 1 + k)} + index={index + 1 + k} + onPlay={() => jumpTo(index + 1 + k)} /> ))} diff --git a/web/src/components/layout/Player.test.tsx b/web/src/components/layout/Player.test.tsx index 141ff21..56bdf3f 100644 --- a/web/src/components/layout/Player.test.tsx +++ b/web/src/components/layout/Player.test.tsx @@ -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 { 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( + + commits++}> + + + , + ) + const before = commits + act(() => usePlayer.getState().tick(5_000)) + act(() => usePlayer.getState().tick(6_000)) + expect(commits).toBe(before) +}) diff --git a/web/src/components/layout/PlayerBar.tsx b/web/src/components/layout/PlayerBar.tsx index d56926b..010c61b 100644 --- a/web/src/components/layout/PlayerBar.tsx +++ b/web/src/components/layout/PlayerBar.tsx @@ -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 ( +
+ {formatTime(track ? positionMs : 0)} +
+ +
+ {formatTime(track?.duration_ms ?? 0)} +
+ ) +} + /** 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() { -
- {formatTime(track ? player.positionMs : 0)} -
- -
- {formatTime(track?.duration_ms ?? 0)} -
+
diff --git a/web/src/components/layout/PlayerControls.tsx b/web/src/components/layout/PlayerControls.tsx index 9ece9b1..3105a3c 100644 --- a/web/src/components/layout/PlayerControls.tsx +++ b/web/src/components/layout/PlayerControls.tsx @@ -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 (
- -
)