Merge pull request 'feat(web): a sidebar that collapses' (#111) from feat/web-collapsible-sidebar into main
This commit is contained in:
commit
2b7635c936
5 changed files with 164 additions and 34 deletions
|
|
@ -347,7 +347,7 @@ npm install
|
|||
SONDA_URL=http://127.0.0.1:8080 npm run dev # open http://localhost:5173, /api is proxied to Sonda
|
||||
```
|
||||
|
||||
What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Playlists have a page of their own: make one, put a song in it with the button next to any song (or make a new playlist right there), play it, rename it, delete it, move a song up or down and take one out; a song whose files are gone is shown as not available and skipped when the playlist plays. Mixes have a page too: a card for each mix, with a collage of the covers of its albums (the ones from your listening first, then one for each genre), and on a mix its songs, Play, Shuffle again (another set of songs) and Save as playlist, which keeps what you see as a playlist of your own. Smart playlists have their own page too, which begins with the ones Sonda makes for you (ready to play, to keep as a playlist, or to make your own with a click), and then yours: make one with an editor of the rules (genres, format and bitrate, when it was added, your listening and taste, the length, the order and the limit) and a live preview of the songs it gives, play it, shuffle it again (for the random order), keep what you see as a playlist, edit it or delete it; rules by artist and album are in the API only, and an edit keeps them. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings.
|
||||
What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Playlists have a page of their own: make one, put a song in it with the button next to any song (or make a new playlist right there), play it, rename it, delete it, move a song up or down and take one out; a song whose files are gone is shown as not available and skipped when the playlist plays. Mixes have a page too: a card for each mix, with a collage of the covers of its albums (the ones from your listening first, then one for each genre), and on a mix its songs, Play, Shuffle again (another set of songs) and Save as playlist, which keeps what you see as a playlist of your own. Smart playlists have their own page too, which begins with the ones Sonda makes for you (ready to play, to keep as a playlist, or to make your own with a click), and then yours: make one with an editor of the rules (genres, format and bitrate, when it was added, your listening and taste, the length, the order and the limit) and a live preview of the songs it gives, play it, shuffle it again (for the random order), keep what you see as a playlist, edit it or delete it; rules by artist and album are in the API only, and an edit keeps them. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. The menu on the left folds to a narrow bar with the icons of the pages (a button in it, remembered in the browser; on a phone it stays a row of links). There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings.
|
||||
|
||||
More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). `npm run build` writes to `internal/webui/dist` (git-ignored), not to `web/dist`.
|
||||
|
||||
|
|
|
|||
71
web/src/components/layout/Sidebar.test.tsx
Normal file
71
web/src/components/layout/Sidebar.test.tsx
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { render, screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import { beforeEach, expect, test, vi } from 'vitest'
|
||||
import { sessionStore } from '@/auth/session'
|
||||
import { initI18n } from '@/i18n'
|
||||
import Sidebar from './Sidebar'
|
||||
|
||||
const KEY = 'sonda.sidebar'
|
||||
|
||||
beforeEach(async () => {
|
||||
localStorage.clear()
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } })
|
||||
})
|
||||
|
||||
function renderSidebar() {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/albums']}>
|
||||
<Sidebar />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
return screen.getByRole('navigation', { name: 'Main' })
|
||||
}
|
||||
|
||||
test('the sidebar starts open, with a button that folds it', () => {
|
||||
const nav = renderSidebar()
|
||||
expect(nav).toHaveAttribute('data-collapsed', 'false')
|
||||
expect(within(nav).getByRole('button', { name: 'Collapse the sidebar' })).toBeInTheDocument()
|
||||
expect(within(nav).getByText('Albums')).toBeVisible()
|
||||
})
|
||||
|
||||
test('folded, it keeps every link with its name and a tooltip, and the same button opens it again', async () => {
|
||||
const nav = renderSidebar()
|
||||
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse the sidebar' }))
|
||||
expect(nav).toHaveAttribute('data-collapsed', 'true')
|
||||
const albums = within(nav).getByRole('link', { name: 'Albums' })
|
||||
expect(albums).toHaveAttribute('aria-current', 'page')
|
||||
expect(albums).toHaveAttribute('title', 'Albums')
|
||||
for (const name of ['Home', 'Search', 'Mixes', 'Playlists', 'Smart playlists', 'Admin', 'Settings']) {
|
||||
expect(within(nav).getByRole('link', { name })).toBeInTheDocument()
|
||||
}
|
||||
await userEvent.click(within(nav).getByRole('button', { name: 'Expand the sidebar' }))
|
||||
expect(nav).toHaveAttribute('data-collapsed', 'false')
|
||||
expect(within(nav).getByRole('link', { name: 'Albums' })).not.toHaveAttribute('title')
|
||||
})
|
||||
|
||||
test('the choice is remembered in the browser', async () => {
|
||||
const first = renderSidebar()
|
||||
await userEvent.click(within(first).getByRole('button', { name: 'Collapse the sidebar' }))
|
||||
expect(JSON.parse(localStorage.getItem(KEY) ?? 'null')).toBe('collapsed')
|
||||
document.body.innerHTML = ''
|
||||
const second = renderSidebar()
|
||||
expect(second).toHaveAttribute('data-collapsed', 'true')
|
||||
await userEvent.click(within(second).getByRole('button', { name: 'Expand the sidebar' }))
|
||||
expect(JSON.parse(localStorage.getItem(KEY) ?? 'null')).toBe('open')
|
||||
})
|
||||
|
||||
test('a browser that will not keep it still folds and opens the sidebar', async () => {
|
||||
vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
|
||||
throw new Error('blocked')
|
||||
})
|
||||
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
||||
throw new Error('blocked')
|
||||
})
|
||||
const nav = renderSidebar()
|
||||
expect(nav).toHaveAttribute('data-collapsed', 'false')
|
||||
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse the sidebar' }))
|
||||
expect(nav).toHaveAttribute('data-collapsed', 'true')
|
||||
})
|
||||
|
|
@ -1,58 +1,113 @@
|
|||
import {
|
||||
Compass, Disc3, House, ListMusic, Mic2, Music, PanelLeftClose, PanelLeftOpen, Search, Settings, ShieldCheck, Sparkles, Wand2, type LucideIcon,
|
||||
} from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { NavLink } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useSession } from '@/auth/session'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const LINKS = [
|
||||
{ to: '/', key: 'nav.home', end: true, adminOnly: false },
|
||||
{ to: '/search', key: 'nav.search', end: false, adminOnly: false },
|
||||
{ to: '/discover', key: 'nav.discover', end: false, adminOnly: false },
|
||||
{ to: '/artists', key: 'nav.artists', end: false, adminOnly: false },
|
||||
{ to: '/albums', key: 'nav.albums', end: false, adminOnly: false },
|
||||
{ to: '/songs', key: 'nav.songs', end: false, adminOnly: false },
|
||||
{ to: '/mixes', key: 'nav.mixes', end: false, adminOnly: false },
|
||||
{ to: '/playlists', key: 'nav.playlists', end: false, adminOnly: false },
|
||||
{ to: '/smart-playlists', key: 'nav.smartPlaylists', end: false, adminOnly: false },
|
||||
{ to: '/admin', key: 'nav.admin', end: false, adminOnly: true },
|
||||
{ to: '/settings', key: 'nav.settings', end: false, adminOnly: false },
|
||||
const LINKS: { to: string; key: string; icon: LucideIcon; end: boolean; adminOnly: boolean }[] = [
|
||||
{ to: '/', key: 'nav.home', icon: House, end: true, adminOnly: false },
|
||||
{ to: '/search', key: 'nav.search', icon: Search, end: false, adminOnly: false },
|
||||
{ to: '/discover', key: 'nav.discover', icon: Compass, end: false, adminOnly: false },
|
||||
{ to: '/artists', key: 'nav.artists', icon: Mic2, end: false, adminOnly: false },
|
||||
{ to: '/albums', key: 'nav.albums', icon: Disc3, end: false, adminOnly: false },
|
||||
{ to: '/songs', key: 'nav.songs', icon: Music, end: false, adminOnly: false },
|
||||
{ to: '/mixes', key: 'nav.mixes', icon: Sparkles, end: false, adminOnly: false },
|
||||
{ to: '/playlists', key: 'nav.playlists', icon: ListMusic, end: false, adminOnly: false },
|
||||
{ to: '/smart-playlists', key: 'nav.smartPlaylists', icon: Wand2, end: false, adminOnly: false },
|
||||
{ to: '/admin', key: 'nav.admin', icon: ShieldCheck, end: false, adminOnly: true },
|
||||
{ to: '/settings', key: 'nav.settings', icon: Settings, end: false, adminOnly: false },
|
||||
]
|
||||
|
||||
const KEY = 'sonda.sidebar'
|
||||
|
||||
function readCollapsed(): boolean {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(KEY) ?? 'null') === 'collapsed'
|
||||
} catch {
|
||||
return false // a browser that will not say: open
|
||||
}
|
||||
}
|
||||
|
||||
function writeCollapsed(collapsed: boolean) {
|
||||
try {
|
||||
localStorage.setItem(KEY, JSON.stringify(collapsed ? 'collapsed' : 'open'))
|
||||
} catch {
|
||||
// The sidebar still folds; it just opens again at the next visit.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The main menu. On a screen wide enough it can fold to a narrow bar with the icons of the pages, and the choice is
|
||||
* remembered in the browser. On a phone it is a row of links and never folds.
|
||||
*/
|
||||
export default function Sidebar() {
|
||||
const { t } = useTranslation()
|
||||
const { user } = useSession()
|
||||
const [collapsed, setCollapsed] = useState(readCollapsed)
|
||||
const links = LINKS.filter((link) => !link.adminOnly || user?.is_admin)
|
||||
|
||||
function toggle() {
|
||||
writeCollapsed(!collapsed)
|
||||
setCollapsed(!collapsed)
|
||||
}
|
||||
|
||||
// Folding only counts from the `sm` width up: below it the sidebar is a row, which has no room to gain.
|
||||
return (
|
||||
<nav
|
||||
aria-label={t('nav.main')}
|
||||
className="flex flex-[0_0_248px] flex-col gap-6 rounded-xl bg-card p-5 text-card-foreground max-sm:flex-[1_1_100%] max-sm:gap-3 max-sm:p-3"
|
||||
data-collapsed={collapsed}
|
||||
className={cn(
|
||||
'flex flex-[0_0_248px] flex-col gap-6 rounded-xl bg-card p-5 text-card-foreground max-sm:flex-[1_1_100%] max-sm:gap-3 max-sm:p-3',
|
||||
collapsed && 'sm:flex-[0_0_72px] sm:items-center sm:p-3',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<svg width="28" height="28" viewBox="0 0 28 28" fill="none" aria-hidden="true">
|
||||
<div className={cn('flex items-center gap-3', collapsed && 'sm:flex-col')}>
|
||||
<svg width="28" height="28" viewBox="0 0 28 28" fill="none" aria-hidden="true" className="shrink-0">
|
||||
<circle cx="14" cy="14" r="3" fill="var(--primary)" />
|
||||
<circle cx="14" cy="14" r="8" stroke="var(--primary)" strokeWidth="2" opacity="0.75" />
|
||||
<circle cx="14" cy="14" r="12.5" stroke="var(--primary)" strokeWidth="1.5" opacity="0.35" />
|
||||
</svg>
|
||||
<span className="text-[22px] font-extrabold tracking-tight">{t('app.name')}</span>
|
||||
<span className={cn('text-[22px] font-extrabold tracking-tight', collapsed && 'sm:sr-only')}>{t('app.name')}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={toggle}
|
||||
aria-label={t(collapsed ? 'nav.expand' : 'nav.collapse')}
|
||||
className={cn(
|
||||
'ml-auto flex size-9 items-center justify-center rounded-lg text-muted-foreground hover:bg-secondary hover:text-foreground max-sm:hidden',
|
||||
collapsed && 'sm:ml-0',
|
||||
)}
|
||||
>
|
||||
{collapsed ? <PanelLeftOpen className="size-5" aria-hidden="true" /> : <PanelLeftClose className="size-5" aria-hidden="true" />}
|
||||
</button>
|
||||
</div>
|
||||
<ul className="flex flex-col gap-1 max-sm:flex-row max-sm:flex-wrap">
|
||||
{links.map((link) => (
|
||||
<li key={link.to}>
|
||||
<NavLink
|
||||
to={link.to}
|
||||
end={link.end}
|
||||
className={({ isActive }) =>
|
||||
cn(
|
||||
'flex min-h-11 items-center rounded-lg px-3 font-semibold',
|
||||
isActive ? 'bg-secondary text-foreground' : 'text-muted-foreground hover:text-foreground',
|
||||
)
|
||||
}
|
||||
>
|
||||
{t(link.key)}
|
||||
</NavLink>
|
||||
</li>
|
||||
))}
|
||||
{links.map((link) => {
|
||||
const label = t(link.key)
|
||||
return (
|
||||
<li key={link.to}>
|
||||
<NavLink
|
||||
to={link.to}
|
||||
end={link.end}
|
||||
title={collapsed ? label : undefined}
|
||||
className={({ isActive }) =>
|
||||
cn(
|
||||
'flex min-h-11 items-center gap-3 rounded-lg px-3 font-semibold',
|
||||
collapsed && 'sm:justify-center sm:px-0 sm:size-11',
|
||||
isActive ? 'bg-secondary text-foreground' : 'text-muted-foreground hover:text-foreground',
|
||||
)
|
||||
}
|
||||
>
|
||||
<link.icon className="size-5 shrink-0 max-sm:hidden" aria-hidden="true" />
|
||||
<span className={cn(collapsed && 'sm:sr-only')}>{label}</span>
|
||||
</NavLink>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
<p className="mt-auto rounded-lg bg-secondary px-3 py-2.5 text-[13px] text-muted-foreground max-sm:hidden">
|
||||
<p className={cn('mt-auto rounded-lg bg-secondary px-3 py-2.5 text-[13px] text-muted-foreground max-sm:hidden', collapsed && 'sm:hidden')}>
|
||||
{t('app.privacy')}
|
||||
</p>
|
||||
</nav>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
"privacy": "Nothing leaves this server unless an admin turns a provider on."
|
||||
},
|
||||
"nav": {
|
||||
"collapse": "Collapse the sidebar",
|
||||
"expand": "Expand the sidebar",
|
||||
"main": "Main",
|
||||
"home": "Home",
|
||||
"search": "Search",
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
"privacy": "Niente esce da questo server finché un admin non accende un provider."
|
||||
},
|
||||
"nav": {
|
||||
"collapse": "Comprimi la barra laterale",
|
||||
"expand": "Espandi la barra laterale",
|
||||
"main": "Principale",
|
||||
"home": "Home",
|
||||
"search": "Cerca",
|
||||
|
|
|
|||
Loading…
Reference in a new issue