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:
tgiachi 2026-11-12 11:23:24 +01:00
commit 3ffd07c546
15 changed files with 246 additions and 4 deletions

View file

@ -1,9 +1,17 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import AuthGate from './auth/AuthGate'
import { routes } from './router'
const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: 30_000, retry: 1 } } })
const router = createBrowserRouter(routes)
export default function App() {
return (
<AuthGate>
<h1>Sonda</h1>
</AuthGate>
<QueryClientProvider client={queryClient}>
<AuthGate>
<RouterProvider router={router} />
</AuthGate>
</QueryClientProvider>
)
}

View 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', '/')
})

View 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>
)
}

View 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>
)
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function AlbumPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.albums')}</h1>
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function AlbumsPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.albums')}</h1>
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function ArtistPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.artists')}</h1>
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function ArtistsPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.artists')}</h1>
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function HomePage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.home')}</h1>
}

View file

@ -0,0 +1,14 @@
import { Link } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
export default function NotFoundPage() {
const { t } = useTranslation()
return (
<div className="flex flex-col items-start gap-4">
<h1 className="text-3xl font-extrabold tracking-tight">{t('common.notFound')}</h1>
<Link to="/" className="font-semibold text-primary underline-offset-4 hover:underline">
{t('common.backHome')}
</Link>
</div>
)
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function SearchPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.search')}</h1>
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function SettingsPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.settings')}</h1>
}

View file

@ -0,0 +1,6 @@
import { useTranslation } from 'react-i18next'
export default function SongsPage() {
const { t } = useTranslation()
return <h1 className="text-3xl font-extrabold tracking-tight">{t('nav.songs')}</h1>
}

28
web/src/router.tsx Normal file
View file

@ -0,0 +1,28 @@
import type { RouteObject } from 'react-router-dom'
import Shell from '@/components/layout/Shell'
import AlbumPage from '@/pages/AlbumPage'
import AlbumsPage from '@/pages/AlbumsPage'
import ArtistPage from '@/pages/ArtistPage'
import ArtistsPage from '@/pages/ArtistsPage'
import HomePage from '@/pages/HomePage'
import NotFoundPage from '@/pages/NotFoundPage'
import SearchPage from '@/pages/SearchPage'
import SettingsPage from '@/pages/SettingsPage'
import SongsPage from '@/pages/SongsPage'
export const routes: RouteObject[] = [
{
element: <Shell />,
children: [
{ path: '/', element: <HomePage /> },
{ path: '/search', element: <SearchPage /> },
{ path: '/artists', element: <ArtistsPage /> },
{ path: '/artists/:id', element: <ArtistPage /> },
{ path: '/albums', element: <AlbumsPage /> },
{ path: '/albums/:id', element: <AlbumPage /> },
{ path: '/songs', element: <SongsPage /> },
{ path: '/settings', element: <SettingsPage /> },
{ path: '*', element: <NotFoundPage /> },
],
},
]

View file

@ -1,10 +1,22 @@
import { render, screen } from '@testing-library/react'
import { expect, test } from 'vitest'
import { beforeEach, expect, test, vi } from 'vitest'
import App from './App'
import { sessionStore } from './auth/session'
import { initI18n } from './i18n'
beforeEach(async () => {
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
})
test('a signed-in user sees the app', () => {
sessionStore.setState({ status: 'authenticated', user: { id: 1, username: 'tom', is_admin: true } })
render(<App />)
expect(screen.getByText('Sonda')).toBeInTheDocument()
})
test('a signed-out user sees the sign-in form instead', () => {
sessionStore.setState({ status: 'anonymous', user: null })
render(<App />)
expect(screen.getByLabelText('Username')).toBeInTheDocument()
})