diff --git a/web/src/App.tsx b/web/src/App.tsx index 9530940..1060a12 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -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 ( - -

Sonda

-
+ + + + + ) } diff --git a/web/src/components/layout/Shell.test.tsx b/web/src/components/layout/Shell.test.tsx new file mode 100644 index 0000000..2e14bd3 --- /dev/null +++ b/web/src/components/layout/Shell.test.tsx @@ -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( + + + , + ) +} + +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', '/') +}) diff --git a/web/src/components/layout/Shell.tsx b/web/src/components/layout/Shell.tsx new file mode 100644 index 0000000..bdb1b25 --- /dev/null +++ b/web/src/components/layout/Shell.tsx @@ -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 ( +
+
+ +
+ +
+
+
+ ) +} diff --git a/web/src/components/layout/Sidebar.tsx b/web/src/components/layout/Sidebar.tsx new file mode 100644 index 0000000..c36775e --- /dev/null +++ b/web/src/components/layout/Sidebar.tsx @@ -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 ( + + ) +} diff --git a/web/src/pages/AlbumPage.tsx b/web/src/pages/AlbumPage.tsx new file mode 100644 index 0000000..675e4ab --- /dev/null +++ b/web/src/pages/AlbumPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function AlbumPage() { + const { t } = useTranslation() + return

{t('nav.albums')}

+} diff --git a/web/src/pages/AlbumsPage.tsx b/web/src/pages/AlbumsPage.tsx new file mode 100644 index 0000000..ec16c4a --- /dev/null +++ b/web/src/pages/AlbumsPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function AlbumsPage() { + const { t } = useTranslation() + return

{t('nav.albums')}

+} diff --git a/web/src/pages/ArtistPage.tsx b/web/src/pages/ArtistPage.tsx new file mode 100644 index 0000000..0dc76e9 --- /dev/null +++ b/web/src/pages/ArtistPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function ArtistPage() { + const { t } = useTranslation() + return

{t('nav.artists')}

+} diff --git a/web/src/pages/ArtistsPage.tsx b/web/src/pages/ArtistsPage.tsx new file mode 100644 index 0000000..dbd2d9f --- /dev/null +++ b/web/src/pages/ArtistsPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function ArtistsPage() { + const { t } = useTranslation() + return

{t('nav.artists')}

+} diff --git a/web/src/pages/HomePage.tsx b/web/src/pages/HomePage.tsx new file mode 100644 index 0000000..bbbbd9c --- /dev/null +++ b/web/src/pages/HomePage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function HomePage() { + const { t } = useTranslation() + return

{t('nav.home')}

+} diff --git a/web/src/pages/NotFoundPage.tsx b/web/src/pages/NotFoundPage.tsx new file mode 100644 index 0000000..5e1a305 --- /dev/null +++ b/web/src/pages/NotFoundPage.tsx @@ -0,0 +1,14 @@ +import { Link } from 'react-router-dom' +import { useTranslation } from 'react-i18next' + +export default function NotFoundPage() { + const { t } = useTranslation() + return ( +
+

{t('common.notFound')}

+ + {t('common.backHome')} + +
+ ) +} diff --git a/web/src/pages/SearchPage.tsx b/web/src/pages/SearchPage.tsx new file mode 100644 index 0000000..434868d --- /dev/null +++ b/web/src/pages/SearchPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function SearchPage() { + const { t } = useTranslation() + return

{t('nav.search')}

+} diff --git a/web/src/pages/SettingsPage.tsx b/web/src/pages/SettingsPage.tsx new file mode 100644 index 0000000..22c2afc --- /dev/null +++ b/web/src/pages/SettingsPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function SettingsPage() { + const { t } = useTranslation() + return

{t('nav.settings')}

+} diff --git a/web/src/pages/SongsPage.tsx b/web/src/pages/SongsPage.tsx new file mode 100644 index 0000000..0218dd9 --- /dev/null +++ b/web/src/pages/SongsPage.tsx @@ -0,0 +1,6 @@ +import { useTranslation } from 'react-i18next' + +export default function SongsPage() { + const { t } = useTranslation() + return

{t('nav.songs')}

+} diff --git a/web/src/router.tsx b/web/src/router.tsx new file mode 100644 index 0000000..1771082 --- /dev/null +++ b/web/src/router.tsx @@ -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: , + children: [ + { path: '/', element: }, + { path: '/search', element: }, + { path: '/artists', element: }, + { path: '/artists/:id', element: }, + { path: '/albums', element: }, + { path: '/albums/:id', element: }, + { path: '/songs', element: }, + { path: '/settings', element: }, + { path: '*', element: }, + ], + }, +] diff --git a/web/src/smoke.test.tsx b/web/src/smoke.test.tsx index cd1e8d1..a98ac42 100644 --- a/web/src/smoke.test.tsx +++ b/web/src/smoke.test.tsx @@ -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() expect(screen.getByText('Sonda')).toBeInTheDocument() }) + +test('a signed-out user sees the sign-in form instead', () => { + sessionStore.setState({ status: 'anonymous', user: null }) + render() + expect(screen.getByLabelText('Username')).toBeInTheDocument() +})