Merge pull request 'Organise the menu in groups' (#190) from feat/sidebar-groups into main

This commit is contained in:
squidd 2026-11-15 12:17:14 +01:00
commit d31d4da1bd
11 changed files with 199 additions and 64 deletions

View file

@ -30,6 +30,12 @@ test('the tabs are Home, Search, Library and More, and only on a phone', () => {
expect(nav).toHaveClass('sm:hidden')
})
test.each(['/discover'])('Home is the current tab on %s, which it leads to', (path) => {
const nav = renderTabs(path)
expect(within(nav).getByRole('link', { name: 'Home' })).toHaveAttribute('aria-current', 'page')
expect(within(nav).getByRole('link', { name: 'Library' })).not.toHaveAttribute('aria-current')
})
test('each tab is tall enough for a thumb', () => {
const nav = renderTabs()
for (const link of within(nav).getAllByRole('link')) expect(link).toHaveClass('min-h-14')
@ -41,7 +47,7 @@ test('the tab of the page that is open is the current one', () => {
expect(within(nav).getByRole('link', { name: 'Home' })).not.toHaveAttribute('aria-current')
})
test.each(['/library', '/artists', '/albums/4', '/songs', '/mixes', '/playlists/2', '/smart-playlists', '/discover'])(
test.each(['/library', '/artists', '/albums/4', '/songs', '/mixes', '/playlists/2', '/smart-playlists', '/history', '/import'])(
'Library is the current tab on %s, one of the pages it leads to',
(path) => {
const nav = renderTabs(path)

View file

@ -3,8 +3,10 @@ import { Link, useLocation } from 'react-router-dom'
import { useFriends } from '@/api/friends'
import { cn } from '@/lib/utils'
/** The pages the Home tab leads to (Home itself ends the list of them with a link to Discover). */
const HOME = ['/discover']
/** The pages the Library tab leads to: it is the current tab on all of them. */
const LIBRARY = ['/library', '/artists', '/albums', '/songs', '/mixes', '/playlists', '/smart-playlists', '/discover']
const LIBRARY = ['/library', '/artists', '/albums', '/songs', '/playlists', '/smart-playlists', '/mixes', '/history', '/import']
const MORE = ['/more', '/settings', '/admin']
const under = (path: string, prefixes: string[]) => prefixes.some((p) => path === p || path.startsWith(`${p}/`))
@ -19,7 +21,7 @@ export default function PhoneTabs() {
const { pathname } = useLocation()
const hasFriends = (useFriends().data?.length ?? 0) > 0
const tabs = [
{ to: '/', label: t('nav.home'), current: pathname === '/' },
{ to: '/', label: t('nav.home'), current: pathname === '/' || under(pathname, HOME) },
{ to: '/search', label: t('nav.search'), current: under(pathname, ['/search']) },
{ to: '/library', label: t('nav.library'), current: under(pathname, LIBRARY) },
...(hasFriends ? [{ to: '/friends', label: t('nav.friends'), current: under(pathname, ['/friends']), friend: true }] : []),

View file

@ -82,5 +82,47 @@ test('the dot of Friends is the friend colour also when it is the page that is o
test('on a phone the links are left to the bar of tabs: only the logo stays', () => {
const nav = renderSidebar()
expect(within(nav).getByRole('list')).toHaveClass('max-sm:hidden')
expect(nav.querySelector(':scope > div')).toHaveClass('max-sm:hidden')
})
const names = (nav: HTMLElement) => within(nav).getAllByRole('link').map((a) => a.textContent)
test('the links are in groups, in the order of what you do: find, the library, your listening, friends, then the foot', async () => {
const nav = renderSidebar([{ id: 3, name: 'Anna' }])
await within(nav).findByRole('link', { name: 'Friends' })
expect(names(nav)).toEqual([
'Home', 'Search', 'Discover',
'Artists', 'Albums', 'Songs', 'Playlists', 'Smart playlists', 'Mixes',
'History', 'Import',
'Friends',
'Settings', 'Admin',
])
})
test('the library and the listening have a quiet heading, and the groups that explain themselves have none', () => {
const nav = renderSidebar()
const library = within(nav).getByRole('list', { name: 'Library' })
expect(within(library).getAllByRole('link').map((a) => a.textContent)).toEqual(['Artists', 'Albums', 'Songs', 'Playlists', 'Smart playlists', 'Mixes'])
const listening = within(nav).getByRole('list', { name: 'Your listening' })
expect(within(listening).getAllByRole('link').map((a) => a.textContent)).toEqual(['History', 'Import'])
// The headings are words, not links, and not headings of the page.
expect(within(nav).queryByRole('heading')).not.toBeInTheDocument()
expect(within(nav).queryByRole('link', { name: 'Your listening' })).not.toBeInTheDocument()
})
test('Settings and Admin are at the foot, and a person who is not an admin has Settings alone', () => {
sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } })
const nav = renderSidebar()
const all = names(nav)
expect(all.slice(-1)).toEqual(['Settings'])
expect(all).not.toContain('Admin')
})
test('a page inside a section lights the section: an artist, a new smart playlist, a friend', () => {
for (const [path, name] of [['/artists/7', 'Artists'], ['/smart-playlists/new', 'Smart playlists'], ['/playlists/2', 'Playlists'], ['/admin/users', 'Admin']] as const) {
document.body.innerHTML = ''
const nav = renderSidebar([], path)
expect(within(nav).getByRole('link', { name })).toHaveAttribute('aria-current', 'page')
expect(within(nav).getAllByRole('link').filter((a) => a.getAttribute('aria-current') === 'page')).toHaveLength(1)
}
})

View file

@ -5,70 +5,131 @@ import { useSession } from '@/auth/session'
import Logo from '@/components/Logo'
import { cn } from '@/lib/utils'
const LINKS: { to: string; key: string; end: boolean; adminOnly: boolean; friends?: boolean }[] = [
{ 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: '/history', key: 'nav.history', end: false, adminOnly: false },
{ to: '/import', key: 'nav.import', end: false, adminOnly: false },
{ to: '/friends', key: 'nav.friends', end: false, adminOnly: false, friends: true },
{ to: '/admin', key: 'nav.admin', end: false, adminOnly: true },
{ to: '/settings', key: 'nav.settings', end: false, adminOnly: false },
]
interface Item {
to: string
key: string
end?: boolean
adminOnly?: boolean
friends?: boolean
}
interface Group {
/** The key of the heading, when the group has one: only the groups that are not obvious from their words. */
heading?: string
items: Item[]
}
/**
* The main menu: words, one for each page, and a dot that says which one is open. The dot is the colour of this
* server; for Friends it is the colour of what comes from elsewhere, always, so that the one page that shows other
* people's music is told apart before it is opened. On a phone the links are left to the bar of tabs (PhoneTabs).
* Where to find things, then what is in the library, then what you listened to, then the friends. The settings and
* the admin area are not a group: they stay at the foot.
*/
const GROUPS: Group[] = [
{
items: [
{ to: '/', key: 'nav.home', end: true },
{ to: '/search', key: 'nav.search' },
{ to: '/discover', key: 'nav.discover' },
],
},
{
heading: 'nav.library',
items: [
{ to: '/artists', key: 'nav.artists' },
{ to: '/albums', key: 'nav.albums' },
{ to: '/songs', key: 'nav.songs' },
{ to: '/playlists', key: 'nav.playlists' },
{ to: '/smart-playlists', key: 'nav.smartPlaylists' },
{ to: '/mixes', key: 'nav.mixes' },
],
},
{
heading: 'nav.listening',
items: [
{ to: '/history', key: 'nav.history' },
{ to: '/import', key: 'nav.import' },
],
},
{ items: [{ to: '/friends', key: 'nav.friends', friends: true }] },
]
const FOOT: Item[] = [
{ to: '/settings', key: 'nav.settings' },
{ to: '/admin', key: 'nav.admin', adminOnly: true },
]
function Row({ item }: { item: Item }) {
const { t } = useTranslation()
return (
<li>
<NavLink
to={item.to}
end={item.end ?? false}
className={({ isActive }) =>
cn(
'flex min-h-10 items-center gap-3 rounded-lg px-3 font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
isActive ? 'bg-card font-bold text-foreground' : 'text-muted-foreground hover:text-foreground',
)
}
>
{({ isActive }) => (
<>
<span
data-dot
aria-hidden="true"
className={cn('size-2 shrink-0 rounded-full', item.friends ? 'bg-friend' : isActive ? 'bg-primary' : 'bg-transparent')}
/>
<span>{t(item.key)}</span>
</>
)}
</NavLink>
</li>
)
}
/**
* The main menu: words, in groups, and a dot that says which page is open. The dot is the colour of this server; for
* Friends it is the colour of what comes from elsewhere, always, so that the one page that shows other people's music
* is told apart before it is opened. Space tells the groups apart; the two that need a name have a quiet one. On a
* phone the links are left to the bar of tabs (PhoneTabs).
*/
export default function Sidebar() {
const { t } = useTranslation()
const { user } = useSession()
// Friends is in the menu only when there is a friend to browse (federation on, and a friendship both admins agreed to).
const hasFriends = (useFriends().data?.length ?? 0) > 0
const links = LINKS.filter((link) => (!link.adminOnly || user?.is_admin) && (!link.friends || hasFriends))
const shown = (item: Item) => (!item.adminOnly || user?.is_admin) && (!item.friends || hasFriends)
const groups = GROUPS.map((g) => ({ ...g, items: g.items.filter(shown) })).filter((g) => g.items.length > 0)
const foot = FOOT.filter(shown)
return (
<nav
aria-label={t('nav.main')}
className="flex flex-[0_0_232px] flex-col gap-0.5 border-r border-border px-5 py-8 max-sm:min-w-0 max-sm:flex-[1_1_100%] max-sm:border-r-0 max-sm:border-b max-sm:p-3"
className="flex flex-[0_0_232px] flex-col border-r border-border px-5 py-8 max-sm:min-w-0 max-sm:flex-[1_1_100%] max-sm:border-r-0 max-sm:border-b max-sm:p-3"
>
<Logo name={t('app.name')} className="mx-3 mb-7 h-8 w-auto self-start max-sm:mb-0" />
{/* On a phone the links are the bar of tabs at the bottom; only the logo stays on top. */}
<ul className="flex flex-col gap-0.5 max-sm:hidden">
{links.map((link) => (
<li key={link.to}>
<NavLink
to={link.to}
end={link.end}
className={({ isActive }) =>
cn(
'flex min-h-11 items-center gap-3 rounded-lg px-3 font-medium focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
isActive ? 'bg-card font-bold text-foreground' : 'text-muted-foreground hover:text-foreground',
)
}
>
{({ isActive }) => (
<>
<span
data-dot
aria-hidden="true"
className={cn('size-2 shrink-0 rounded-full', link.friends ? 'bg-friend' : isActive ? 'bg-primary' : 'bg-transparent')}
/>
<span>{t(link.key)}</span>
</>
)}
</NavLink>
</li>
<div className="flex min-h-0 flex-1 flex-col gap-5 overflow-y-auto max-sm:hidden">
{groups.map((group) => (
<div key={group.items[0].to} className="flex flex-col">
{group.heading && (
<p id={`nav-group-${group.heading}`} className="flex min-h-7 items-center pl-8 text-[13px] font-medium text-muted-foreground">
{t(group.heading)}
</p>
)}
<ul aria-labelledby={group.heading ? `nav-group-${group.heading}` : undefined} className="flex flex-col gap-1">
{group.items.map((item) => (
<Row key={item.to} item={item} />
))}
</ul>
</div>
))}
</ul>
<p className="mt-auto px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">{t('app.privacy')}</p>
<ul className="mt-auto flex flex-col gap-1">
{foot.map((item) => (
<Row key={item.to} item={item} />
))}
</ul>
</div>
<p className="px-3 pt-7 text-[13px] leading-snug text-muted-foreground max-sm:hidden">{t('app.privacy')}</p>
</nav>
)
}

View file

@ -19,6 +19,7 @@
"friends": "Friends",
"admin": "Admin",
"settings": "Settings",
"listening": "Your listening",
"history": "History",
"import": "Import",
"more": "More",

View file

@ -19,6 +19,7 @@
"friends": "Amici",
"admin": "Admin",
"settings": "Impostazioni",
"listening": "I tuoi ascolti",
"history": "Cronologia",
"import": "Importa",
"more": "Altro",

View file

@ -169,3 +169,11 @@ test('when the history cannot be read the shelves of albums and artists still sh
expect(await screen.findByRole('link', { name: /OK Computer/ })).toBeInTheDocument()
expect(await screen.findByRole('alert')).toHaveTextContent('Something went wrong. Try again.')
})
test('Home ends with a way to Discover, which has no tab on a phone and is hidden on a wide screen', async () => {
mockFetch(routes())
renderHome()
const link = await screen.findByRole('link', { name: 'Discover' })
expect(link).toHaveAttribute('href', '/discover')
expect(link.closest('div.sm\\:hidden')).not.toBeNull()
})

View file

@ -6,6 +6,7 @@ import { useAlbumList, useArtistList, type Track } from '@/api/catalog'
import { api, ApiError } from '@/api/client'
import { useRecentListens } from '@/api/listens'
import { AlbumCard, ArtistCard, CARD_GRID } from '@/components/cards'
import LinkRows from '@/components/LinkRows'
import Section from '@/components/Section'
import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status'
import { errorMessageKey } from '@/i18n'
@ -134,6 +135,11 @@ export default function HomePage() {
)}
</>
)}
{/* On a phone Discover has no tab of its own: this is the way to it. */}
<div className="sm:hidden">
<LinkRows links={[{ to: '/discover', label: t('nav.discover') }]} />
</div>
</div>
)
}

View file

@ -18,20 +18,23 @@ test('Library is a list of the places of the music, each a row that leads there'
</MemoryRouter>,
)
expect(screen.getByRole('heading', { level: 1, name: 'Library' })).toBeInTheDocument()
const list = within(screen.getByRole('list'))
const links = list.getAllByRole('link').map((a) => [a.textContent, a.getAttribute('href')])
const links = screen.getAllByRole('link').map((a) => [a.textContent, a.getAttribute('href')])
// The same order as the menu of a wide screen: the music, then what you listened to. Discover is on Home.
expect(links).toEqual([
['Artists', '/artists'],
['Albums', '/albums'],
['Songs', '/songs'],
['Mixes', '/mixes'],
['Playlists', '/playlists'],
['Smart playlists', '/smart-playlists'],
['Discover', '/discover'],
['Mixes', '/mixes'],
['History', '/history'],
['Import', '/import'],
])
expect(within(screen.getByRole('region', { name: 'Your listening' })).getAllByRole('link')).toHaveLength(2)
expect(screen.queryByRole('link', { name: 'Discover' })).not.toBeInTheDocument()
})
test('More has Import and Settings, and Admin only for an admin', () => {
test('More has Settings, and Admin only for an admin', () => {
sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'ann@example.org', is_admin: false } })
const { unmount } = render(
<MemoryRouter>
@ -39,7 +42,7 @@ test('More has Import and Settings, and Admin only for an admin', () => {
</MemoryRouter>,
)
expect(screen.getByRole('heading', { level: 1, name: 'More' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Import' })).toHaveAttribute('href', '/import')
expect(screen.queryByRole('link', { name: 'Import' })).not.toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Settings' })).toHaveAttribute('href', '/settings')
expect(screen.queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument()
unmount()

View file

@ -1,8 +1,9 @@
import { useTranslation } from 'react-i18next'
import LinkRows from '@/components/LinkRows'
import PageTitle from '@/components/PageTitle'
import Section from '@/components/Section'
/** Where the music is: one row for each way into it. It is the Library tab of the phone. */
/** Where the music is, and what you listened to: one row for each way into it. It is the Library tab of the phone. */
export default function LibraryPage() {
const { t } = useTranslation()
return (
@ -13,12 +14,19 @@ export default function LibraryPage() {
{ to: '/artists', label: t('nav.artists') },
{ to: '/albums', label: t('nav.albums') },
{ to: '/songs', label: t('nav.songs') },
{ to: '/mixes', label: t('nav.mixes') },
{ to: '/playlists', label: t('nav.playlists') },
{ to: '/smart-playlists', label: t('nav.smartPlaylists') },
{ to: '/discover', label: t('nav.discover') },
{ to: '/mixes', label: t('nav.mixes') },
]}
/>
<Section title={t('nav.listening')}>
<LinkRows
links={[
{ to: '/history', label: t('nav.history') },
{ to: '/import', label: t('nav.import') },
]}
/>
</Section>
</div>
)
}

View file

@ -7,10 +7,7 @@ import PageTitle from '@/components/PageTitle'
export default function MorePage() {
const { t } = useTranslation()
const { user } = useSession()
const links = [
{ to: '/import', label: t('nav.import') },
{ to: '/settings', label: t('nav.settings') },
]
const links = [{ to: '/settings', label: t('nav.settings') }]
if (user?.is_admin) links.push({ to: '/admin', label: t('nav.admin') })
return (
<div className="flex flex-col gap-6">