From ce3c8598c8346307f612d42d47f64e880bfa7964 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Fri, 13 Nov 2026 19:52:36 +0100 Subject: [PATCH] feat(web): a switch in Settings to turn the spectrogram off The choice stays on the device, like the theme. When it is off the player draws nothing and the spectrogram is not there at all. --- web/src/components/Spectrogram.test.tsx | 8 ++++++++ web/src/components/Spectrogram.tsx | 7 +++++-- web/src/i18n/locales/en.json | 2 ++ web/src/i18n/locales/it.json | 2 ++ web/src/pages/SettingsPage.test.tsx | 14 ++++++++++++++ web/src/pages/SettingsPage.tsx | 12 ++++++++++++ web/src/player/prefs.test.ts | 19 +++++++++++++++++++ web/src/player/prefs.ts | 24 ++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 web/src/player/prefs.test.ts create mode 100644 web/src/player/prefs.ts diff --git a/web/src/components/Spectrogram.test.tsx b/web/src/components/Spectrogram.test.tsx index 162248e..a908604 100644 --- a/web/src/components/Spectrogram.test.tsx +++ b/web/src/components/Spectrogram.test.tsx @@ -1,11 +1,13 @@ import { render, screen } from '@testing-library/react' import { beforeEach, expect, test, vi } from 'vitest' import { initI18n } from '@/i18n' +import { useSpectrogramPref } from '@/player/prefs' import Spectrogram from './Spectrogram' beforeEach(async () => { vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') await initI18n() + useSpectrogramPref.setState({ on: true }) }) test('it is a picture with a name, and a canvas where the browser can draw', () => { @@ -25,3 +27,9 @@ test('it takes the colour of this server, or of a friend', () => { rerender() expect(screen.getByRole('img').className).toContain('text-friend') }) + +test('turned off in Settings, it is not there at all', () => { + useSpectrogramPref.setState({ on: false }) + render() + expect(screen.queryByRole('img')).not.toBeInTheDocument() +}) diff --git a/web/src/components/Spectrogram.tsx b/web/src/components/Spectrogram.tsx index fc25ed5..c9c78e2 100644 --- a/web/src/components/Spectrogram.tsx +++ b/web/src/components/Spectrogram.tsx @@ -1,6 +1,7 @@ import { useEffect, useRef } from 'react' import { useTranslation } from 'react-i18next' import { hasAnalyser, readBins } from '@/player/analyser' +import { useSpectrogramPref } from '@/player/prefs' import { currentTrack, usePlayer } from '@/player/store' import { ColumnBuffer, rowsFromBins } from '@/player/spectrum' import { cn } from '@/lib/utils' @@ -55,13 +56,14 @@ interface SpectrogramProps { export default function Spectrogram({ columns = 48, rows = 12, friend = false, className }: SpectrogramProps) { const { t } = useTranslation() const canvas = useRef(null) + const wanted = useSpectrogramPref((s) => s.on) const playing = usePlayer((s) => s.playing) const now = usePlayer(currentTrack) const key = now ? `${now.friend ?? 0}:${now.id}` : '' useEffect(() => { const el = canvas.current - if (!el) return + if (!el || !wanted) return const buffer = new ColumnBuffer(columns, rows) draw(el, buffer.columns(), rows) if (!playing) return @@ -81,8 +83,9 @@ export default function Spectrogram({ columns = 48, rows = 12, friend = false, c frame = requestAnimationFrame(tick) return () => cancelAnimationFrame(frame) // A new track starts from an empty picture. - }, [playing, key, columns, rows]) + }, [wanted, playing, key, columns, rows]) + if (!wanted) return null return (