feat(web): the Admin area with libraries and scanning, and an Admin link for admins
The Admin link is shown only to an admin and /admin opens the libraries. Someone else who opens the address gets a notice and the server is not asked anything. README describes what the admin area covers so far.
This commit is contained in:
parent
3dd85fc230
commit
d88710fd73
7 changed files with 73 additions and 12 deletions
|
|
@ -27,7 +27,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr
|
|||
|
||||
## What is not there yet
|
||||
|
||||
Radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client exists (see [Web app](#web-app)) but `sondad` does not serve it yet, and it has no admin screens yet.
|
||||
Radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client exists (see [Web app](#web-app)) but `sondad` does not serve it yet, and its admin area only covers libraries and scanning so far (users, providers and the bulk jobs are still API-only).
|
||||
|
||||
## Requirements
|
||||
|
||||
|
|
@ -328,9 +328,9 @@ npm install
|
|||
SONDA_URL=http://127.0.0.1:8080 npm run dev # open http://localhost:5173, /api is proxied to Sonda
|
||||
```
|
||||
|
||||
What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings.
|
||||
What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings.
|
||||
|
||||
More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). The admin screens and serving the app from the binary come later.
|
||||
More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). The rest of the admin screens (users, providers, bulk jobs) and serving the app from the binary come later.
|
||||
|
||||
## Development
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ 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 { sessionStore } from '@/auth/session'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { routes } from '@/router'
|
||||
import { mockFetch } from '@/test/mockFetch'
|
||||
|
|
@ -33,6 +34,34 @@ test('at /artists the Artists link is the current page and the others are not',
|
|||
}
|
||||
})
|
||||
|
||||
const adminRoutes = {
|
||||
'GET /api/v1/libraries': () => ({ libraries: [] }),
|
||||
'GET /api/v1/library/scan': () => ({ running: false }),
|
||||
}
|
||||
|
||||
test('an admin has an Admin link, and a user who is not one has none', () => {
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 1, username: 'tom', is_admin: false } })
|
||||
renderAt('/artists')
|
||||
expect(within(mainNav()).queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the Admin link opens the libraries and stays current inside the admin area', async () => {
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 1, username: 'tom', is_admin: true } })
|
||||
mockFetch(adminRoutes)
|
||||
renderAt('/admin')
|
||||
expect(await screen.findByRole('region', { name: 'Libraries' })).toBeInTheDocument()
|
||||
expect(within(mainNav()).getByRole('link', { name: 'Admin' })).toHaveAttribute('aria-current', 'page')
|
||||
})
|
||||
|
||||
test('someone who is not an admin who opens the admin address gets a notice and no request', async () => {
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 2, username: 'ann', is_admin: false } })
|
||||
const { calls } = mockFetch(adminRoutes)
|
||||
renderAt('/admin/libraries')
|
||||
expect(screen.getByText('Only admins can see this page.')).toBeInTheDocument()
|
||||
await new Promise((r) => setTimeout(r, 30))
|
||||
expect(calls.filter((c) => c.url.pathname.includes('librar'))).toHaveLength(0)
|
||||
})
|
||||
|
||||
test('Home is the current page only on /', () => {
|
||||
renderAt('/')
|
||||
expect(within(mainNav()).getByRole('link', { name: 'Home' })).toHaveAttribute('aria-current', 'page')
|
||||
|
|
|
|||
|
|
@ -1,19 +1,23 @@
|
|||
import { NavLink } from 'react-router-dom'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useSession } from '@/auth/session'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
const LINKS = [
|
||||
{ to: '/', key: 'nav.home', end: true },
|
||||
{ to: '/search', key: 'nav.search', end: false },
|
||||
{ to: '/discover', key: 'nav.discover', 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 },
|
||||
{ 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: '/admin', key: 'nav.admin', end: false, adminOnly: true },
|
||||
{ to: '/settings', key: 'nav.settings', end: false, adminOnly: false },
|
||||
]
|
||||
|
||||
export default function Sidebar() {
|
||||
const { t } = useTranslation()
|
||||
const { user } = useSession()
|
||||
const links = LINKS.filter((link) => !link.adminOnly || user?.is_admin)
|
||||
return (
|
||||
<nav
|
||||
aria-label={t('nav.main')}
|
||||
|
|
@ -28,7 +32,7 @@ export default function Sidebar() {
|
|||
<span className="text-[22px] font-extrabold tracking-tight">{t('app.name')}</span>
|
||||
</div>
|
||||
<ul className="flex flex-col gap-1 max-sm:flex-row max-sm:flex-wrap">
|
||||
{LINKS.map((link) => (
|
||||
{links.map((link) => (
|
||||
<li key={link.to}>
|
||||
<NavLink
|
||||
to={link.to}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
"artists": "Artists",
|
||||
"albums": "Albums",
|
||||
"songs": "Songs",
|
||||
"admin": "Admin",
|
||||
"settings": "Settings"
|
||||
},
|
||||
"common": {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
"artists": "Artisti",
|
||||
"albums": "Album",
|
||||
"songs": "Brani",
|
||||
"admin": "Admin",
|
||||
"settings": "Impostazioni"
|
||||
},
|
||||
"common": {
|
||||
|
|
|
|||
16
web/src/pages/admin/LibrariesPage.tsx
Normal file
16
web/src/pages/admin/LibrariesPage.tsx
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { useState } from 'react'
|
||||
import AddLibraryForm from './AddLibraryForm'
|
||||
import LibraryList from './LibraryList'
|
||||
import ScanPanel from './ScanPanel'
|
||||
|
||||
/** Libraries and scanning. "Read every file again" is shared by the scan of everything and of one library. */
|
||||
export default function LibrariesPage() {
|
||||
const [full, setFull] = useState(false)
|
||||
return (
|
||||
<div className="flex flex-col gap-10">
|
||||
<LibraryList full={full} />
|
||||
<AddLibraryForm />
|
||||
<ScanPanel full={full} onFullChange={setFull} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import type { RouteObject } from 'react-router-dom'
|
||||
import { Navigate, type RouteObject } from 'react-router-dom'
|
||||
import Shell from '@/components/layout/Shell'
|
||||
import AlbumPage from '@/pages/AlbumPage'
|
||||
import AlbumsPage from '@/pages/AlbumsPage'
|
||||
|
|
@ -6,6 +6,8 @@ import ArtistPage from '@/pages/ArtistPage'
|
|||
import ArtistsPage from '@/pages/ArtistsPage'
|
||||
import DiscoverPage from '@/pages/DiscoverPage'
|
||||
import HomePage from '@/pages/HomePage'
|
||||
import AdminLayout from '@/pages/admin/AdminLayout'
|
||||
import LibrariesPage from '@/pages/admin/LibrariesPage'
|
||||
import NotFoundPage from '@/pages/NotFoundPage'
|
||||
import SearchPage from '@/pages/SearchPage'
|
||||
import SettingsPage from '@/pages/SettingsPage'
|
||||
|
|
@ -24,6 +26,14 @@ export const routes: RouteObject[] = [
|
|||
{ path: '/albums/:id', element: <AlbumPage /> },
|
||||
{ path: '/songs', element: <SongsPage /> },
|
||||
{ path: '/settings', element: <SettingsPage /> },
|
||||
{
|
||||
path: '/admin',
|
||||
element: <AdminLayout />,
|
||||
children: [
|
||||
{ index: true, element: <Navigate to="/admin/libraries" replace /> },
|
||||
{ path: 'libraries', element: <LibrariesPage /> },
|
||||
],
|
||||
},
|
||||
{ path: '*', element: <NotFoundPage /> },
|
||||
],
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue