diff --git a/admin/src/App.tsx b/admin/src/App.tsx index b27a6b9..c3b4dcb 100644 --- a/admin/src/App.tsx +++ b/admin/src/App.tsx @@ -1,8 +1,13 @@ import { Route, Routes } from 'react-router' import { Shell } from '@/components/Shell' +import { ListPage } from '@/pages/ListPage' import { ListsPage } from '@/pages/ListsPage' import { LoginPage } from '@/pages/LoginPage' import { NotFoundPage } from '@/pages/NotFoundPage' +import { ArchiveTab } from '@/tabs/ArchiveTab' +import { HeldTab } from '@/tabs/HeldTab' +import { MembersTab } from '@/tabs/MembersTab' +import { SettingsTab } from '@/tabs/SettingsTab' import { Toaster } from '@/components/ui/sonner' /** Routes only: providers (query client, router, auth) are set up by the caller. */ @@ -13,6 +18,12 @@ export function App() { } /> }> } /> + }> + } /> + } /> + } /> + } /> + } /> diff --git a/admin/src/components/SelectField.tsx b/admin/src/components/SelectField.tsx new file mode 100644 index 0000000..02f2942 --- /dev/null +++ b/admin/src/components/SelectField.tsx @@ -0,0 +1,40 @@ +import { Label } from '@/components/ui/label' +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' + +export interface Option { + value: T + label: string +} + +/** A labelled select. The label is also the accessible name of the control. */ +export function SelectField({ + id, + label, + value, + options, + onChange, +}: { + id: string + label: string + value: T + options: Option[] + onChange: (value: T) => void +}) { + return ( +
+ + +
+ ) +} diff --git a/admin/src/pages/ListPage.test.tsx b/admin/src/pages/ListPage.test.tsx new file mode 100644 index 0000000..20203dd --- /dev/null +++ b/admin/src/pages/ListPage.test.tsx @@ -0,0 +1,161 @@ +import { screen, waitFor, within } from '@testing-library/react' +import { describe, expect, it } from 'vitest' +import { aList, installFakeApi, newState } from '@/test/fakeApi' +import { renderApp } from '@/test/render' + +const dev = () => aList({ id: 1, name: 'dev', title: 'Developers' }) + +describe('list page', () => { + it('shows the list header and the tabs', async () => { + installFakeApi(newState({ lists: [dev()] })) + renderApp('/lists/1', { token: 'good' }) + expect(await screen.findByRole('heading', { name: 'dev@example.org' })).toBeInTheDocument() + expect(screen.getByText('Developers')).toBeInTheDocument() + for (const name of ['Settings', 'Members', 'Held', 'Archive']) { + expect(screen.getByRole('tab', { name: new RegExp(`^${name}`) })).toBeInTheDocument() + } + expect(screen.getByRole('link', { name: /all lists/i })).toHaveAttribute('href', '/') + }) + + it('says so when the list does not exist', async () => { + installFakeApi(newState()) + renderApp('/lists/42', { token: 'good' }) + expect(await screen.findByText(/list not found/i)).toBeInTheDocument() + }) + + it('treats a non-numeric id as not found without calling the API', async () => { + const state = installFakeApi(newState()) + renderApp('/lists/abc', { token: 'good' }) + expect(await screen.findByText(/list not found/i)).toBeInTheDocument() + expect(state.calls).toEqual([]) + }) + + it('moves between tabs', async () => { + installFakeApi(newState({ lists: [dev()] })) + const { user } = renderApp('/lists/1', { token: 'good' }) + await user.click(await screen.findByRole('tab', { name: /^members/i })) + expect(await screen.findByRole('heading', { name: /members/i, level: 2 })).toBeInTheDocument() + expect(screen.getByRole('tab', { name: /^members/i })).toHaveAttribute('aria-selected', 'true') + await user.click(screen.getByRole('tab', { name: /^settings/i })) + expect(await screen.findByLabelText(/^title$/i)).toBeInTheDocument() + }) + + it('opens the tab named in the URL', async () => { + installFakeApi(newState({ lists: [dev()] })) + renderApp('/lists/1/archive', { token: 'good' }) + expect(await screen.findByRole('tab', { name: /^archive/i })).toHaveAttribute('aria-selected', 'true') + }) + + it('shows how many messages wait in the held queue', async () => { + installFakeApi( + newState({ + lists: [dev()], + held: { 1: [{ id: 5, sender: 'a@x.com', subject: 's', size: 10, created_at: '2030-01-01T00:00:00Z' }] }, + }), + ) + renderApp('/lists/1', { token: 'good' }) + expect(await screen.findByRole('tab', { name: /^held\s*1$/i })).toBeInTheDocument() + }) +}) + +describe('settings tab', () => { + async function setup(list = dev()) { + const state = installFakeApi(newState({ lists: [list] })) + const ctx = renderApp('/lists/1', { token: 'good' }) + await screen.findByLabelText(/^title$/i) + return { ...ctx, state } + } + + it('shows the current settings', async () => { + await setup(aList({ id: 1, title: 'Developers', post_policy: 'moderated', join_policy: 'closed', archive_policy: 'public' })) + expect(screen.getByLabelText(/^title$/i)).toHaveValue('Developers') + expect(screen.getByRole('combobox', { name: /posting/i })).toHaveTextContent(/moderated/i) + expect(screen.getByRole('combobox', { name: /joining/i })).toHaveTextContent(/closed/i) + expect(screen.getByRole('combobox', { name: /archive/i })).toHaveTextContent(/public/i) + }) + + it('cannot be saved until something changes', async () => { + await setup() + expect(screen.getByRole('button', { name: /save changes/i })).toBeDisabled() + }) + + it('saves only the fields that changed', async () => { + const { user, state } = await setup() + await user.clear(screen.getByLabelText(/^title$/i)) + await user.type(screen.getByLabelText(/^title$/i), 'Core developers') + await user.click(screen.getByRole('combobox', { name: /posting/i })) + await user.click(await screen.findByRole('option', { name: /moderated/i })) + await user.click(screen.getByRole('button', { name: /save changes/i })) + + await waitFor(() => expect(state.lists[0].title).toBe('Core developers')) + expect(state.lists[0].post_policy).toBe('moderated') + expect(state.lists[0].join_policy).toBe('open') + expect(state.calls).toContain('PATCH /lists/1') + expect(await screen.findByText(/settings saved/i)).toBeInTheDocument() + await waitFor(() => expect(screen.getByRole('button', { name: /save changes/i })).toBeDisabled()) + }) + + it('changes the archive policy', async () => { + const { user, state } = await setup() + await user.click(screen.getByRole('combobox', { name: /archive/i })) + await user.click(await screen.findByRole('option', { name: /public/i })) + await user.click(screen.getByRole('button', { name: /save changes/i })) + await waitFor(() => expect(state.lists[0].archive_policy).toBe('public')) + expect(state.lists[0].title).toBe('Developers') + }) + + it('refuses an empty title', async () => { + const { user, state } = await setup() + await user.clear(screen.getByLabelText(/^title$/i)) + await user.click(screen.getByRole('button', { name: /save changes/i })) + expect(await screen.findByText(/title is required/i)).toBeInTheDocument() + expect(state.calls).not.toContain('PATCH /lists/1') + }) + + it('shows a server error and keeps the edits', async () => { + const { user, state } = await setup() + state.fail['PATCH /lists/1'] = { status: 500, error: 'disk full' } + await user.type(screen.getByLabelText(/^title$/i), '!') + await user.click(screen.getByRole('button', { name: /save changes/i })) + expect(await screen.findByRole('alert')).toHaveTextContent('disk full') + expect(screen.getByLabelText(/^title$/i)).toHaveValue('Developers!') + }) +}) + +describe('deleting a list', () => { + async function setup() { + const state = installFakeApi(newState({ lists: [dev(), aList({ id: 2, name: 'news' })] })) + const ctx = renderApp('/lists/1', { token: 'good' }) + await ctx.user.click(await screen.findByRole('button', { name: /delete list/i })) + const dialog = await screen.findByRole('alertdialog') + return { ...ctx, state, dialog } + } + + it('asks for confirmation naming the list, and cancelling deletes nothing', async () => { + const { user, state, dialog } = await setup() + expect(dialog).toHaveTextContent('dev@example.org') + await user.click(within(dialog).getByRole('button', { name: /cancel/i })) + await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) + expect(state.calls).not.toContain('DELETE /lists/1') + expect(state.lists).toHaveLength(2) + }) + + it('deletes the list and goes back to the overview', async () => { + const { user, state, dialog } = await setup() + await user.click(within(dialog).getByRole('button', { name: /^delete$/i })) + expect(await screen.findByRole('heading', { name: /^lists$/i })).toBeInTheDocument() + expect(state.lists.map((l) => l.id)).toEqual([2]) + expect(screen.queryByRole('link', { name: 'dev@example.org' })).not.toBeInTheDocument() + expect(screen.getByRole('link', { name: 'news@example.org' })).toBeInTheDocument() + }) + + it('shows a server error and stays on the page', async () => { + const { user, state, dialog } = await setup() + state.fail['DELETE /lists/1'] = { status: 500, error: 'locked' } + await user.click(within(dialog).getByRole('button', { name: /^delete$/i })) + expect(await within(dialog).findByRole('alert')).toHaveTextContent('locked') + expect(screen.getByRole('alertdialog')).toBeInTheDocument() // still open: the user can retry + expect(screen.getByRole('heading', { name: 'dev@example.org', hidden: true })).toBeInTheDocument() + expect(state.lists).toHaveLength(2) + }) +}) diff --git a/admin/src/pages/ListPage.tsx b/admin/src/pages/ListPage.tsx new file mode 100644 index 0000000..4d65251 --- /dev/null +++ b/admin/src/pages/ListPage.tsx @@ -0,0 +1,61 @@ +import { useQuery } from '@tanstack/react-query' +import { Link, Outlet, useLocation, useNavigate, useParams } from 'react-router' +import { Badge } from '@/components/ui/badge' +import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs' +import { api } from '@/lib/api' + +const tabs = ['settings', 'members', 'held', 'archive'] as const + +export function ListPage() { + const params = useParams() + const id = /^\d+$/.test(params.id ?? '') ? Number(params.id) : null + const navigate = useNavigate() + const { pathname } = useLocation() + + const list = useQuery({ queryKey: ['list', id], queryFn: () => api.lists.get(id!), enabled: id !== null }) + const held = useQuery({ queryKey: ['held', id], queryFn: () => api.held.list(id!), enabled: id !== null }) + + if (id === null || (list.isError && list.error.message === 'not found')) { + return + } + if (list.isPending) return

Loading…

+ if (list.isError) return

{list.error.message}

+ + const current = tabs.find((t) => pathname.endsWith(`/${t}`)) ?? 'settings' + const heldCount = held.data?.length ?? 0 + + return ( +
+
+ + All lists + +

{list.data.address}

+

{list.data.title}

+
+ navigate(t === 'settings' ? `/lists/${id}` : `/lists/${id}/${t}`)}> + + Settings + Members + + Held + {heldCount > 0 && {heldCount}} + + Archive + + + +
+ ) +} + +function NotFound() { + return ( +
+

List not found

+ + Back to lists + +
+ ) +} diff --git a/admin/src/tabs/ArchiveTab.tsx b/admin/src/tabs/ArchiveTab.tsx new file mode 100644 index 0000000..2b27c7d --- /dev/null +++ b/admin/src/tabs/ArchiveTab.tsx @@ -0,0 +1,3 @@ +export function ArchiveTab() { + return

Archive

+} diff --git a/admin/src/tabs/HeldTab.tsx b/admin/src/tabs/HeldTab.tsx new file mode 100644 index 0000000..3424afd --- /dev/null +++ b/admin/src/tabs/HeldTab.tsx @@ -0,0 +1,3 @@ +export function HeldTab() { + return

Held

+} diff --git a/admin/src/tabs/MembersTab.tsx b/admin/src/tabs/MembersTab.tsx new file mode 100644 index 0000000..cf0639f --- /dev/null +++ b/admin/src/tabs/MembersTab.tsx @@ -0,0 +1,3 @@ +export function MembersTab() { + return

Members

+} diff --git a/admin/src/tabs/SettingsTab.tsx b/admin/src/tabs/SettingsTab.tsx new file mode 100644 index 0000000..4502b12 --- /dev/null +++ b/admin/src/tabs/SettingsTab.tsx @@ -0,0 +1,157 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query' +import { useState, type FormEvent } from 'react' +import { useNavigate, useOutletContext } from 'react-router' +import { toast } from 'sonner' +import { SelectField, type Option } from '@/components/SelectField' +import { + AlertDialog, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from '@/components/ui/alert-dialog' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { Label } from '@/components/ui/label' +import { Separator } from '@/components/ui/separator' +import { api, type ArchivePolicy, type JoinPolicy, type List, type PostPolicy } from '@/lib/api' + +const postOptions: Option[] = [ + { value: 'members', label: 'Members only' }, + { value: 'moderated', label: 'Moderated (hold every post)' }, +] +const joinOptions: Option[] = [ + { value: 'open', label: 'Open (confirmed by email)' }, + { value: 'closed', label: 'Closed (admin adds members)' }, +] +const archiveOptions: Option[] = [ + { value: 'private', label: 'Private (API only)' }, + { value: 'public', label: 'Public (on the web)' }, + { value: 'off', label: 'Off (not stored)' }, +] + +export function SettingsTab() { + const list = useOutletContext() + return ( +
+ + + +
+ ) +} + +function SettingsForm({ list }: { list: List }) { + const queryClient = useQueryClient() + const [title, setTitle] = useState(list.title) + const [post, setPost] = useState(list.post_policy) + const [join, setJoin] = useState(list.join_policy) + const [archive, setArchive] = useState(list.archive_policy) + const [error, setError] = useState(null) + + // Send only what changed. + const patch: Parameters[1] = {} + if (title.trim() !== list.title) patch.title = title.trim() + if (post !== list.post_policy) patch.post_policy = post + if (join !== list.join_policy) patch.join_policy = join + if (archive !== list.archive_policy) patch.archive_policy = archive + const dirty = Object.keys(patch).length > 0 + + const save = useMutation({ + mutationFn: () => api.lists.update(list.id, patch), + onSuccess: async (updated) => { + queryClient.setQueryData(['list', list.id], updated) + await queryClient.invalidateQueries({ queryKey: ['lists'] }) + toast.success('Settings saved') + setError(null) + }, + onError: (e: Error) => setError(e.message), + }) + + function submit(e: FormEvent) { + e.preventDefault() + if (title.trim() === '') { + setError('Title is required') + return + } + setError(null) + save.mutate() + } + + return ( +
+
+ + setTitle(e.target.value)} /> +
+ + + + {error && ( +

+ {error} +

+ )} + + + ) +} + +function DangerZone({ list }: { list: List }) { + const queryClient = useQueryClient() + const navigate = useNavigate() + const [open, setOpen] = useState(false) + const [error, setError] = useState(null) + + const remove = useMutation({ + mutationFn: () => api.lists.remove(list.id), + onSuccess: async () => { + queryClient.removeQueries({ queryKey: ['list', list.id] }) + await queryClient.invalidateQueries({ queryKey: ['lists'] }) + toast.success(`Deleted ${list.address}`) + navigate('/') + }, + onError: (e: Error) => setError(e.message), + }) + + return ( +
+

Danger zone

+

+ Deleting a list also deletes its members, held messages and archive. This cannot be undone. +

+ + + + + Delete {list.address}? + All members, held messages and archived messages are lost. + + {error && ( +

+ {error} +

+ )} + + Cancel + + +
+
+
+ ) +}