diff --git a/web/src/components/layout/PhoneTabs.test.tsx b/web/src/components/layout/PhoneTabs.test.tsx index c7684ec..d06591e 100644 --- a/web/src/components/layout/PhoneTabs.test.tsx +++ b/web/src/components/layout/PhoneTabs.test.tsx @@ -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) diff --git a/web/src/components/layout/PhoneTabs.tsx b/web/src/components/layout/PhoneTabs.tsx index 408c3d3..81858f4 100644 --- a/web/src/components/layout/PhoneTabs.tsx +++ b/web/src/components/layout/PhoneTabs.tsx @@ -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 }] : []), diff --git a/web/src/components/layout/Sidebar.test.tsx b/web/src/components/layout/Sidebar.test.tsx index 1a0bccd..3db5673 100644 --- a/web/src/components/layout/Sidebar.test.tsx +++ b/web/src/components/layout/Sidebar.test.tsx @@ -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) + } }) diff --git a/web/src/components/layout/Sidebar.tsx b/web/src/components/layout/Sidebar.tsx index af98e44..e52d9d4 100644 --- a/web/src/components/layout/Sidebar.tsx +++ b/web/src/components/layout/Sidebar.tsx @@ -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 ( +