feat(web): app shell, sidebar and routes
- sidebar with the current page marked, privacy note, and a main area that wraps on narrow screens - routes for home, search, artists, albums, songs and settings, and a not-found page
This commit is contained in:
parent
1250fd4eac
commit
3ffd07c546
3 changed files with 246 additions and 4 deletions
69
web/src/components/layout/Shell.test.tsx
Normal file
69
web/src/components/layout/Shell.test.tsx
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { render, screen, within } from '@testing-library/react'
|
||||
import { createMemoryRouter, RouterProvider } from 'react-router-dom'
|
||||
import { beforeEach, expect, test, vi } from 'vitest'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { routes } from '@/router'
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
})
|
||||
|
||||
function renderAt(path: string) {
|
||||
const router = createMemoryRouter(routes, { initialEntries: [path] })
|
||||
render(
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
<RouterProvider router={router} />
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
}
|
||||
|
||||
function mainNav() {
|
||||
return screen.getByRole('navigation', { name: 'Main' })
|
||||
}
|
||||
|
||||
test('at /artists the Artists link is the current page and the others are not', () => {
|
||||
renderAt('/artists')
|
||||
const nav = within(mainNav())
|
||||
expect(nav.getByRole('link', { name: 'Artists' })).toHaveAttribute('aria-current', 'page')
|
||||
for (const name of ['Home', 'Search', 'Albums', 'Songs', 'Settings']) {
|
||||
expect(nav.getByRole('link', { name })).not.toHaveAttribute('aria-current')
|
||||
}
|
||||
})
|
||||
|
||||
test('Home is the current page only on /', () => {
|
||||
renderAt('/')
|
||||
expect(within(mainNav()).getByRole('link', { name: 'Home' })).toHaveAttribute('aria-current', 'page')
|
||||
})
|
||||
|
||||
test('an artist page keeps the Artists link current', () => {
|
||||
renderAt('/artists/5')
|
||||
expect(within(mainNav()).getByRole('link', { name: 'Artists' })).toHaveAttribute('aria-current', 'page')
|
||||
})
|
||||
|
||||
test('the page has one main landmark and says what the server does with data', () => {
|
||||
renderAt('/songs')
|
||||
expect(screen.getAllByRole('main')).toHaveLength(1)
|
||||
expect(screen.getByText('Nothing leaves this server unless an admin turns a provider on.')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test.each([
|
||||
['/', 'Home'],
|
||||
['/search', 'Search'],
|
||||
['/artists', 'Artists'],
|
||||
['/artists/5', 'Artists'],
|
||||
['/albums', 'Albums'],
|
||||
['/albums/5', 'Albums'],
|
||||
['/songs', 'Songs'],
|
||||
['/settings', 'Settings'],
|
||||
])('the route %s opens a page headed %s', (path, heading) => {
|
||||
renderAt(path)
|
||||
expect(screen.getByRole('heading', { level: 1, name: heading })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('an unknown path shows the not-found page with a way back', () => {
|
||||
renderAt('/no/such/page')
|
||||
expect(screen.getByRole('heading', { name: 'This page does not exist' })).toBeInTheDocument()
|
||||
expect(screen.getByRole('link', { name: 'Back to Home' })).toHaveAttribute('href', '/')
|
||||
})
|
||||
16
web/src/components/layout/Shell.tsx
Normal file
16
web/src/components/layout/Shell.tsx
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { Outlet } from 'react-router-dom'
|
||||
import Sidebar from './Sidebar'
|
||||
|
||||
/** The frame of the app: the sidebar, the page, and (from the player task on) the player bar. */
|
||||
export default function Shell() {
|
||||
return (
|
||||
<div className="flex min-h-screen flex-col bg-background text-foreground">
|
||||
<div className="flex flex-1 flex-wrap gap-2 p-2">
|
||||
<Sidebar />
|
||||
<main className="min-w-0 flex-[999_1_560px] rounded-xl bg-card p-6 text-card-foreground">
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
47
web/src/components/layout/Sidebar.tsx
Normal file
47
web/src/components/layout/Sidebar.tsx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { NavLink } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const LINKS = [
|
||||
{ to: '/', key: 'nav.home', end: true },
|
||||
{ to: '/search', key: 'nav.search', end: false },
|
||||
{ to: '/artists', key: 'nav.artists', end: false },
|
||||
{ to: '/albums', key: 'nav.albums', end: false },
|
||||
{ to: '/songs', key: 'nav.songs', end: false },
|
||||
{ to: '/settings', key: 'nav.settings', end: false },
|
||||
]
|
||||
|
||||
export default function Sidebar() {
|
||||
const { t } = useTranslation()
|
||||
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">
|
||||
<div className="flex items-center gap-3">
|
||||
<svg width="28" height="28" viewBox="0 0 28 28" fill="none" aria-hidden="true">
|
||||
<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>
|
||||
</div>
|
||||
<ul className="flex flex-col gap-1">
|
||||
{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>
|
||||
))}
|
||||
</ul>
|
||||
<p className="mt-auto rounded-lg bg-secondary px-3 py-2.5 text-[13px] text-muted-foreground">{t('app.privacy')}</p>
|
||||
</nav>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in a new issue