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 (