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.
This commit is contained in:
parent
f17d9eb66d
commit
ce3c8598c8
8 changed files with 86 additions and 2 deletions
|
|
@ -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(<Spectrogram friend />)
|
||||
expect(screen.getByRole('img').className).toContain('text-friend')
|
||||
})
|
||||
|
||||
test('turned off in Settings, it is not there at all', () => {
|
||||
useSpectrogramPref.setState({ on: false })
|
||||
render(<Spectrogram className="h-10 w-40" />)
|
||||
expect(screen.queryByRole('img')).not.toBeInTheDocument()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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<HTMLCanvasElement>(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 (
|
||||
<div role="img" aria-label={t('player.spectrogram')} className={cn(friend ? 'text-friend' : 'text-primary', className)}>
|
||||
<canvas ref={canvas} aria-hidden="true" className="size-full" />
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@
|
|||
"appearance": "Appearance",
|
||||
"theme": "Theme",
|
||||
"language": "Language",
|
||||
"spectrogram": "Spectrogram in the player",
|
||||
"spectrogramHelp": "It draws the last seconds of the sound as you listen. Turn it off to keep a phone or a laptop cooler. The choice stays on this device.",
|
||||
"password": "Password",
|
||||
"currentPassword": "Current password",
|
||||
"newPassword": "New password",
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@
|
|||
"appearance": "Aspetto",
|
||||
"theme": "Tema",
|
||||
"language": "Lingua",
|
||||
"spectrogram": "Spettrogramma nel player",
|
||||
"spectrogramHelp": "Disegna gli ultimi secondi del suono mentre ascolti. Spegnilo per tenere più fresco un telefono o un portatile. La scelta resta su questo dispositivo.",
|
||||
"password": "Password",
|
||||
"currentPassword": "Password attuale",
|
||||
"newPassword": "Nuova password",
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { MemoryRouter } from 'react-router-dom'
|
|||
import { beforeEach, expect, test, vi } from 'vitest'
|
||||
import { sessionStore } from '@/auth/session'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { useSpectrogramPref } from '@/player/prefs'
|
||||
import { usePlayer } from '@/player/store'
|
||||
import { apiError, mockFetch } from '@/test/mockFetch'
|
||||
import { initTheme } from '@/theme/theme'
|
||||
|
|
@ -28,6 +29,8 @@ beforeEach(async () => {
|
|||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
initTheme()
|
||||
localStorage.removeItem('sonda.spectrogram')
|
||||
useSpectrogramPref.setState({ on: true })
|
||||
sessionStore.setState({
|
||||
status: 'authenticated',
|
||||
user: { id: 1, email: 'tom@example.org', is_admin: true },
|
||||
|
|
@ -195,3 +198,14 @@ test('when the browser offers it, the page can ask to install the app, once', as
|
|||
expect(offer.prompt).toHaveBeenCalledTimes(1)
|
||||
await vi.waitFor(() => expect(screen.queryByRole('button', { name: 'Install Sonda' })).not.toBeInTheDocument())
|
||||
})
|
||||
|
||||
test('the spectrogram of the player can be turned off, and the choice stays on this device', async () => {
|
||||
renderSettings()
|
||||
const toggle = screen.getByRole('switch', { name: 'Spectrogram in the player' })
|
||||
expect(toggle).toHaveAttribute('aria-checked', 'true')
|
||||
await userEvent.click(toggle)
|
||||
expect(toggle).toHaveAttribute('aria-checked', 'false')
|
||||
expect(localStorage.getItem('sonda.spectrogram')).toBe('off')
|
||||
await userEvent.click(toggle)
|
||||
expect(toggle).toHaveAttribute('aria-checked', 'true')
|
||||
})
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ import { Input } from '@/components/ui/input'
|
|||
import { Label } from '@/components/ui/label'
|
||||
import { errorMessageKey, useLanguage, type Lang } from '@/i18n'
|
||||
import { usePlayer } from '@/player/store'
|
||||
import { useSpectrogramPref } from '@/player/prefs'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { THEMES, useTheme, type ThemeId } from '@/theme/theme'
|
||||
import PageTitle from '@/components/PageTitle'
|
||||
|
||||
|
|
@ -105,6 +107,7 @@ export default function SettingsPage() {
|
|||
const { theme, setTheme } = useTheme()
|
||||
const { lang, setLanguage } = useLanguage()
|
||||
const app = useInstall()
|
||||
const spectrogram = useSpectrogramPref()
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-10">
|
||||
|
|
@ -138,6 +141,15 @@ export default function SettingsPage() {
|
|||
{ value: 'it', label: t('lang.it') },
|
||||
]}
|
||||
/>
|
||||
<div className="flex max-w-xl flex-col gap-2">
|
||||
<div className="flex items-center gap-4">
|
||||
<Switch checked={spectrogram.on} aria-labelledby="spectrogram-label" onClick={() => spectrogram.setOn(!spectrogram.on)} />
|
||||
<span id="spectrogram-label" className="font-semibold">
|
||||
{t('settings.spectrogram')}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">{t('settings.spectrogramHelp')}</p>
|
||||
</div>
|
||||
</Section>
|
||||
|
||||
{app.available && (
|
||||
|
|
|
|||
19
web/src/player/prefs.test.ts
Normal file
19
web/src/player/prefs.test.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { beforeEach, expect, test } from 'vitest'
|
||||
import { useSpectrogramPref } from './prefs'
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
useSpectrogramPref.setState({ on: true })
|
||||
})
|
||||
|
||||
test('the spectrogram is on until it is turned off', () => {
|
||||
expect(useSpectrogramPref.getState().on).toBe(true)
|
||||
})
|
||||
|
||||
test('turning it off is remembered on this device, turning it on again too', () => {
|
||||
useSpectrogramPref.getState().setOn(false)
|
||||
expect(useSpectrogramPref.getState().on).toBe(false)
|
||||
expect(localStorage.getItem('sonda.spectrogram')).toBe('off')
|
||||
useSpectrogramPref.getState().setOn(true)
|
||||
expect(localStorage.getItem('sonda.spectrogram')).toBe('on')
|
||||
})
|
||||
24
web/src/player/prefs.ts
Normal file
24
web/src/player/prefs.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { create } from 'zustand'
|
||||
|
||||
const KEY = 'sonda.spectrogram'
|
||||
|
||||
function read(): boolean {
|
||||
try {
|
||||
return localStorage.getItem(KEY) !== 'off'
|
||||
} catch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the player draws its spectrogram. A choice of this device, like the theme: on until it is turned off. */
|
||||
export const useSpectrogramPref = create<{ on: boolean; setOn(on: boolean): void }>()((set) => ({
|
||||
on: read(),
|
||||
setOn: (on) => {
|
||||
set({ on })
|
||||
try {
|
||||
localStorage.setItem(KEY, on ? 'on' : 'off')
|
||||
} catch {
|
||||
// The choice still applies for this page view.
|
||||
}
|
||||
},
|
||||
}))
|
||||
Loading…
Reference in a new issue