From d88710fd7394df068cd49d3035ce25030de9e6c1 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 14:44:08 +0100 Subject: [PATCH] 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. --- README.md | 6 ++--- web/src/components/layout/Shell.test.tsx | 29 ++++++++++++++++++++++++ web/src/components/layout/Sidebar.tsx | 20 +++++++++------- web/src/i18n/locales/en.json | 1 + web/src/i18n/locales/it.json | 1 + web/src/pages/admin/LibrariesPage.tsx | 16 +++++++++++++ web/src/router.tsx | 12 +++++++++- 7 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 web/src/pages/admin/LibrariesPage.tsx diff --git a/README.md b/README.md index e3e5a13..82da6b7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/web/src/components/layout/Shell.test.tsx b/web/src/components/layout/Shell.test.tsx index 49d0547..700674f 100644 --- a/web/src/components/layout/Shell.test.tsx +++ b/web/src/components/layout/Shell.test.tsx @@ -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') diff --git a/web/src/components/layout/Sidebar.tsx b/web/src/components/layout/Sidebar.tsx index 3b1c16f..6937ac8 100644 --- a/web/src/components/layout/Sidebar.tsx +++ b/web/src/components/layout/Sidebar.tsx @@ -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 (