From b2549336baf4931b91d25fad4955f1350a939e47 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Sun, 15 Nov 2026 12:14:57 +0100 Subject: [PATCH] feat(web): the menu in groups The left menu was a flat list of 14 links. It is now in groups, in the order of what a person does: find (Home, Search, Discover), the library (Artists, Albums, Songs, Playlists, Smart playlists, Mixes), their listening (History, Import) and Friends, with Settings and Admin at the foot. Only the library and the listening have a quiet heading; space tells the groups apart. The phone follows it: the Library page lists the same rows in the same order with a "Your listening" section, Discover (which has no tab) is a row at the end of Home, and More keeps Settings and Admin. --- web/src/components/layout/PhoneTabs.test.tsx | 8 +- web/src/components/layout/PhoneTabs.tsx | 6 +- web/src/components/layout/Sidebar.test.tsx | 44 +++++- web/src/components/layout/Sidebar.tsx | 157 +++++++++++++------ web/src/i18n/locales/en.json | 1 + web/src/i18n/locales/it.json | 1 + web/src/pages/HomePage.test.tsx | 8 + web/src/pages/HomePage.tsx | 6 + web/src/pages/LibraryPage.test.tsx | 15 +- web/src/pages/LibraryPage.tsx | 14 +- web/src/pages/MorePage.tsx | 5 +- 11 files changed, 200 insertions(+), 65 deletions(-) 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 ( +
  • + + 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 }) => ( + <> + +
  • + ) +} + +/** + * 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 ( ) } diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 6967524..3e00646 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -19,6 +19,7 @@ "friends": "Friends", "admin": "Admin", "settings": "Settings", + "listening": "Your listening", "history": "History", "import": "Import", "more": "More", diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index f9a8449..b080d9b 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -19,6 +19,7 @@ "friends": "Amici", "admin": "Admin", "settings": "Impostazioni", + "listening": "I tuoi ascolti", "history": "Cronologia", "import": "Importa", "more": "Altro", diff --git a/web/src/pages/HomePage.test.tsx b/web/src/pages/HomePage.test.tsx index e677d7f..614d901 100644 --- a/web/src/pages/HomePage.test.tsx +++ b/web/src/pages/HomePage.test.tsx @@ -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() +}) diff --git a/web/src/pages/HomePage.tsx b/web/src/pages/HomePage.tsx index e2157e6..a197a3b 100644 --- a/web/src/pages/HomePage.tsx +++ b/web/src/pages/HomePage.tsx @@ -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. */} +
    + +
    ) } diff --git a/web/src/pages/LibraryPage.test.tsx b/web/src/pages/LibraryPage.test.tsx index 0cf4ce5..79caa46 100644 --- a/web/src/pages/LibraryPage.test.tsx +++ b/web/src/pages/LibraryPage.test.tsx @@ -18,20 +18,23 @@ test('Library is a list of the places of the music, each a row that leads there' , ) 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( @@ -39,7 +42,7 @@ test('More has Import and Settings, and Admin only for an admin', () => { , ) 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() diff --git a/web/src/pages/LibraryPage.tsx b/web/src/pages/LibraryPage.tsx index fd3283a..63e571c 100644 --- a/web/src/pages/LibraryPage.tsx +++ b/web/src/pages/LibraryPage.tsx @@ -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') }, ]} /> +
    + +
    ) } diff --git a/web/src/pages/MorePage.tsx b/web/src/pages/MorePage.tsx index 24efb15..308e437 100644 --- a/web/src/pages/MorePage.tsx +++ b/web/src/pages/MorePage.tsx @@ -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 (