Merge pull request 'A bar of tabs at the bottom of the phone' (#151) from feat/ui-phone into main

This commit is contained in:
squidd 2026-11-13 17:59:30 +01:00
commit 7a9aeab7d1
13 changed files with 257 additions and 7 deletions

View file

@ -0,0 +1,19 @@
import { Link } from 'react-router-dom'
/** A list of places, each a row of words that leads there, separated by a line. */
export default function LinkRows({ links }: { links: { to: string; label: string }[] }) {
return (
<ul className="flex flex-col">
{links.map((link) => (
<li key={link.to}>
<Link
to={link.to}
className="flex min-h-14 items-center border-b border-border text-lg font-bold hover:text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
>
{link.label}
</Link>
</li>
))}
</ul>
)
}

View file

@ -0,0 +1,68 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, within } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import { beforeEach, expect, test, vi } from 'vitest'
import { initI18n } from '@/i18n'
import { mockFetch } from '@/test/mockFetch'
import PhoneTabs from './PhoneTabs'
beforeEach(async () => {
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
})
function renderTabs(path = '/', friends: { id: number; name: string }[] = []) {
mockFetch({ 'GET /api/v1/friends': () => ({ friends }) })
render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<MemoryRouter initialEntries={[path]}>
<PhoneTabs />
</MemoryRouter>
</QueryClientProvider>,
)
return screen.getByRole('navigation', { name: 'Tabs' })
}
test('the tabs are Home, Search, Library and More, and only on a phone', () => {
const nav = renderTabs()
for (const name of ['Home', 'Search', 'Library', 'More']) expect(within(nav).getByRole('link', { name })).toBeInTheDocument()
expect(within(nav).queryByRole('link', { name: 'Friends' })).not.toBeInTheDocument()
expect(nav).toHaveClass('sm:hidden')
})
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')
})
test('the tab of the page that is open is the current one', () => {
const nav = renderTabs('/search')
expect(within(nav).getByRole('link', { name: 'Search' })).toHaveAttribute('aria-current', 'page')
expect(within(nav).getByRole('link', { name: 'Home' })).not.toHaveAttribute('aria-current')
})
test.each(['/library', '/artists', '/albums/4', '/songs', '/mixes', '/playlists/2', '/smart-playlists', '/discover'])(
'Library is the current tab on %s, one of the pages it leads to',
(path) => {
const nav = renderTabs(path)
expect(within(nav).getByRole('link', { name: 'Library' })).toHaveAttribute('aria-current', 'page')
},
)
test.each(['/settings', '/admin/users'])('More is the current tab on %s', (path) => {
const nav = renderTabs(path)
expect(within(nav).getByRole('link', { name: 'More' })).toHaveAttribute('aria-current', 'page')
})
test('with a friend there is a Friends tab, marked in the friend colour', async () => {
const nav = renderTabs('/friends/3', [{ id: 3, name: 'Anna' }])
const friends = await within(nav).findByRole('link', { name: 'Friends' })
expect(friends).toHaveAttribute('aria-current', 'page')
expect(friends.querySelector('[data-bar]')).toHaveClass('bg-friend')
expect(within(nav).getByRole('link', { name: 'Home' }).querySelector('[data-bar]')).toHaveClass('bg-transparent')
})
test('the bar of the current tab is the colour of this server', () => {
const nav = renderTabs('/')
expect(within(nav).getByRole('link', { name: 'Home' }).querySelector('[data-bar]')).toHaveClass('bg-primary')
})

View file

@ -0,0 +1,51 @@
import { useTranslation } from 'react-i18next'
import { Link, useLocation } from 'react-router-dom'
import { useFriends } from '@/api/friends'
import { cn } from '@/lib/utils'
/** 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 MORE = ['/more', '/settings', '/admin']
const under = (path: string, prefixes: string[]) => prefixes.some((p) => path === p || path.startsWith(`${p}/`))
/**
* The bar of tabs at the bottom of a phone, under the player: Home, Search, Library, Friends (only when there is a
* friend to browse) and More. The menu on the left of a wide screen does not show on a phone, and this does not show on
* a wide screen. The mark on top of the current tab is the colour of this server; the Friends tab has the friend colour.
*/
export default function PhoneTabs() {
const { t } = useTranslation()
const { pathname } = useLocation()
const hasFriends = (useFriends().data?.length ?? 0) > 0
const tabs = [
{ to: '/', label: t('nav.home'), current: pathname === '/' },
{ 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 }] : []),
{ to: '/more', label: t('nav.more'), current: under(pathname, MORE) },
]
return (
<nav aria-label={t('nav.tabs')} className="flex border-t border-border bg-background sm:hidden">
{tabs.map((tab) => (
<Link
key={tab.to}
to={tab.to}
aria-current={tab.current ? 'page' : undefined}
className={cn(
'flex min-h-14 flex-1 flex-col items-center justify-center gap-1.5 text-[13px] focus-visible:outline-2 focus-visible:outline-offset-[-2px] focus-visible:outline-ring',
tab.current ? 'font-bold text-foreground' : 'font-medium text-muted-foreground',
)}
>
<span
data-bar
aria-hidden="true"
className={cn('h-[3px] w-6 rounded-full', 'friend' in tab ? 'bg-friend' : tab.current ? 'bg-primary' : 'bg-transparent')}
/>
<span>{tab.label}</span>
</Link>
))}
</nav>
)
}

View file

@ -156,6 +156,8 @@ test.each([
['/songs', 'Songs'],
['/discover', 'Discover'],
['/settings', 'Settings'],
['/library', 'Library'],
['/more', 'More'],
])('the route %s opens a page headed %s', async (path, heading) => {
await renderAt(path)
expect(screen.getByRole('heading', { level: 1, name: heading })).toBeInTheDocument()

View file

@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next'
import { Outlet } from 'react-router-dom'
import { usePlayerEngine } from '@/player/usePlayer'
import UpdateNotice from '@/pwa/UpdateNotice'
import PhoneTabs from './PhoneTabs'
import PlayerBar from './PlayerBar'
import Sidebar from './Sidebar'
@ -38,6 +39,7 @@ export default function Shell() {
{/* Sticky here, not on the bar: a sticky element only sticks inside its parent. */}
<div className="sticky bottom-0 z-10">
<PlayerBar />
<PhoneTabs />
</div>
<UpdateNotice />
</div>

View file

@ -79,3 +79,8 @@ test('the dot of Friends is the friend colour also when it is the page that is o
expect(link).toHaveAttribute('aria-current', 'page')
expect(link.querySelector('[data-dot]')).toHaveClass('bg-friend')
})
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')
})

View file

@ -23,7 +23,7 @@ const LINKS: { to: string; key: string; end: boolean; adminOnly: boolean; friend
/**
* 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 menu is a row of links that scrolls sideways.
* people's music is told apart before it is opened. On a phone the links are left to the bar of tabs (PhoneTabs).
*/
export default function Sidebar() {
const { t } = useTranslation()
@ -35,13 +35,13 @@ export default function Sidebar() {
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:gap-3 max-sm:border-r-0 max-sm:border-b max-sm:p-3"
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"
>
<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 one row that scrolls sideways, so that the page keeps the screen. */}
<ul className="flex flex-col gap-0.5 max-sm:-mx-3 max-sm:min-w-0 max-sm:flex-row max-sm:gap-0 max-sm:overflow-x-auto max-sm:px-3 max-sm:pb-1 max-sm:[scrollbar-width:none]">
{/* 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} className="max-sm:shrink-0">
<li key={link.to}>
<NavLink
to={link.to}
end={link.end}

View file

@ -12,12 +12,15 @@
"artists": "Artists",
"albums": "Albums",
"songs": "Songs",
"library": "Library",
"mixes": "Mixes",
"playlists": "Playlists",
"smartPlaylists": "Smart playlists",
"friends": "Friends",
"admin": "Admin",
"settings": "Settings"
"settings": "Settings",
"more": "More",
"tabs": "Tabs"
},
"common": {
"loading": "Loading",

View file

@ -12,12 +12,15 @@
"artists": "Artisti",
"albums": "Album",
"songs": "Brani",
"library": "Libreria",
"mixes": "Mix",
"playlists": "Playlist",
"smartPlaylists": "Playlist smart",
"friends": "Amici",
"admin": "Admin",
"settings": "Impostazioni"
"settings": "Impostazioni",
"more": "Altro",
"tabs": "Schede"
},
"common": {
"loading": "Caricamento",

View file

@ -0,0 +1,53 @@
import { render, screen, within } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import { beforeEach, expect, test, vi } from 'vitest'
import { sessionStore } from '@/auth/session'
import { initI18n } from '@/i18n'
import LibraryPage from './LibraryPage'
import MorePage from './MorePage'
beforeEach(async () => {
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
})
test('Library is a list of the places of the music, each a row that leads there', () => {
render(
<MemoryRouter>
<LibraryPage />
</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')])
expect(links).toEqual([
['Artists', '/artists'],
['Albums', '/albums'],
['Songs', '/songs'],
['Mixes', '/mixes'],
['Playlists', '/playlists'],
['Smart playlists', '/smart-playlists'],
['Discover', '/discover'],
])
})
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>
<MorePage />
</MemoryRouter>,
)
expect(screen.getByRole('heading', { level: 1, name: 'More' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Settings' })).toHaveAttribute('href', '/settings')
expect(screen.queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument()
unmount()
sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'tom@example.org', is_admin: true } })
render(
<MemoryRouter>
<MorePage />
</MemoryRouter>,
)
expect(screen.getByRole('link', { name: 'Admin' })).toHaveAttribute('href', '/admin')
})

View file

@ -0,0 +1,24 @@
import { useTranslation } from 'react-i18next'
import LinkRows from '@/components/LinkRows'
import PageTitle from '@/components/PageTitle'
/** Where the music is: one row for each way into it. It is the Library tab of the phone. */
export default function LibraryPage() {
const { t } = useTranslation()
return (
<div className="flex flex-col gap-6">
<PageTitle>{t('nav.library')}</PageTitle>
<LinkRows
links={[
{ 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') },
]}
/>
</div>
)
}

View file

@ -0,0 +1,18 @@
import { useTranslation } from 'react-i18next'
import { useSession } from '@/auth/session'
import LinkRows from '@/components/LinkRows'
import PageTitle from '@/components/PageTitle'
/** What is left: the settings and, for an admin, the admin area. It is the More tab of the phone. */
export default function MorePage() {
const { t } = useTranslation()
const { user } = useSession()
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">
<PageTitle>{t('nav.more')}</PageTitle>
<LinkRows links={links} />
</div>
)
}

View file

@ -21,6 +21,8 @@ export const routes: RouteObject[] = [
{ path: '/albums', lazy: page(() => import('@/pages/AlbumsPage')) },
{ path: '/albums/:id', lazy: page(() => import('@/pages/AlbumPage')) },
{ path: '/songs', lazy: page(() => import('@/pages/SongsPage')) },
{ path: '/library', lazy: page(() => import('@/pages/LibraryPage')) },
{ path: '/more', lazy: page(() => import('@/pages/MorePage')) },
{ path: '/friends', lazy: page(() => import('@/pages/FriendsPage')) },
{ path: '/friends/:friend', lazy: page(() => import('@/pages/FriendPage')) },
{ path: '/mixes', lazy: page(() => import('@/pages/MixesPage')) },