Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,13 @@ every session. CLAUDE.md keeps a one-line index of these topics.
`DirectPlayable` in SQL) + `/admin/books/facets` (each dimension counted without its
own filter, the unfiltered yes/no ones in one pass);
`POST /admin/books/bulk` (one edit over <= 1000 books, all or nothing);
`POST /admin/books/merge` (`{field, sources, target, library_id?}`) selects exact whole credits
or main series spellings and edits all matching books in one writer transaction (at most
`catalog.MaxMergeBooks`, else 400 `too_large`; an unknown `library_id` 404, a podcast one 409). Its
`books` reply identifies each changed path and its prior override value and source; Undo posts it
back with `field` and `target` to `POST /admin/books/merge/undo` (`catalog.UndoMerge`, one
transaction): a book still carrying `target` as its override goes back, one gone (`not_found`) or
edited since (`changed`) is left and listed in `skipped`, beside `restored`;
`GET /admin/authors|narrators` (each person a credit names, `catalog.creditNames`
over `names.Split`, so a co-credit counts for both; `merge_suggestions` stay over
WHOLE credits, from `personKey`, keyed by `match.Fold`, which keeps every script's
Expand Down Expand Up @@ -3194,8 +3201,8 @@ every session. CLAUDE.md keeps a one-line index of these topics.
its library spells its main series another way, same `series_key`; listed per series by `GET
/admin/issues/series-spellings[?library_id=&ignored=true]` -> `{groups: [{library_id, suggested,
spellings: [{name, books}], books}]}`, from the same predicate, `suggested` being `seriesName`;
open = a book not ignored, ignored view = a book ignored; the console's fix is the author merge's bulk
`series` edit), `split_discs` (one book split across disc folders, listed by
open = a book not ignored, ignored view = a book ignored; the console merges the listed spellings with
`POST /admin/books/merge`), `split_discs` (one book split across disc folders, listed by
its lowest disc number, with ties ordered by path. The ignore belongs to `books.split_parent`. That folder must have no override; the console's fix sets
`book` on it, which joins them; its discs never also group as duplicates). `split_parent` is
worked out during discovery (`library.markSplitDiscs`: a folder book whose folder is a disc
Expand Down
13 changes: 13 additions & 0 deletions admin-ui/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import type {
SessionPage,
NarratorsResponse,
PeopleResponse,
MergedBook,
MergeField,
MergeUndone,
PersonField,
SeriesCount,
AdminLibrary,
Expand Down Expand Up @@ -837,6 +840,16 @@ export const api = {
/** One field edit over many books, all or nothing (at most BULK_LIMIT). */
bulkEdit: (books: BookRef[], edit: Omit<BookEditRequest, 'chapters' | 'match'>) =>
request<{ updated: number }>('POST', '/admin/books/bulk', { books, ...edit }),
mergeSpellings: (field: MergeField, sources: string[], target: string, libraryId?: number) =>
request<{ books: MergedBook[] }>('POST', '/admin/books/merge', {
field,
sources,
target,
library_id: libraryId,
}),
/** Puts a merge's books back (its reply sent back) in one transaction. */
undoMerge: (field: MergeField, target: string, books: MergedBook[]) =>
request<MergeUndone>('POST', '/admin/books/merge/undo', { field, target, books }),
/** GET /admin/authors or /admin/narrators, as one shape. */
people: async (field: PersonField, libraryId?: number): Promise<PeopleResponse> => {
const query = bookQuery({ library_id: libraryId });
Expand Down
16 changes: 16 additions & 0 deletions admin-ui/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,22 @@ export interface NarratorsResponse {
/** The two people aggregates (each person a credit names is one; see PersonCount). */
export type PersonField = 'author' | 'narrator';

export type MergeField = PersonField | 'series';

/** One changed book from POST /admin/books/merge. A null previous value means
* Undo should remove the field override and follow the book's metadata again. */
export interface MergedBook extends BookRef {
previous: string | null;
source?: 'edited' | 'community';
}

/** POST /admin/books/merge/undo: how many books went back, and the books left as
* they are (gone from the index, or their field edited since the merge). */
export interface MergeUndone {
restored: number;
skipped: (BookRef & { reason: 'not_found' | 'changed' })[];
}

/** Either people aggregate as the console reads it (api.people; not a wire shape). */
export interface PeopleResponse {
people: PersonCount[];
Expand Down
22 changes: 13 additions & 9 deletions admin-ui/src/features/health/health.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,14 @@ describe('library health', () => {
'GET /admin/books': (req) => ({
body: { books: req.query.get('series') ? tagged : [] },
}),
'POST /admin/books/bulk': { body: { updated: 0 } },
'POST /admin/books/merge': {
body: {
books: [
{ library_id: 1, path: 'M/1', previous: null },
{ library_id: 1, path: 'M/3', previous: null },
],
},
},
'POST /admin/issues/ignore': { status: 204 },
}),
);
Expand All @@ -1437,17 +1444,14 @@ describe('library health', () => {
const keep = screen.getByRole('combobox', { name: 'Spelling to keep' });
await user.selectOptions(keep, 'He Who Fights With Monsters');
expect(screen.getByText(/2 books are tagged with the other spelling/)).toBeInTheDocument();
await user.selectOptions(keep, 'He Who Fights with Monsters');

await user.click(screen.getByRole('button', { name: 'Use this spelling' }));
await waitFor(() =>
expect(calls.find((c) => c.path === '/admin/books/bulk')?.body).toEqual({
books: [
{ library_id: 1, path: 'M/2' },
{ library_id: 1, path: 'M/3' },
{ library_id: 1, path: 'M/4' },
],
set: { series: 'He Who Fights with Monsters' },
expect(calls.find((c) => c.path === '/admin/books/merge')?.body).toEqual({
library_id: 1,
field: 'series',
sources: ['He Who Fights with Monsters', 'He Who Fights WIth Monsters'],
target: 'He Who Fights With Monsters',
}),
);

Expand Down
39 changes: 19 additions & 20 deletions admin-ui/src/features/health/series-spellings.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Eye, EyeOff, Loader2, Merge } from 'lucide-react';
import { api } from '@/api/client';
import { useSeriesSpellings } from '@/api/hooks';
import type { AdminBook, SeriesSpellingGroup } from '@/api/types';
import { Notice } from '@/components/notice';
import { QueryError } from '@/components/query-error';
import { Button } from '@/components/ui/button';
import { NativeSelect } from '@/components/ui/native-select';
import { otherSpellings } from '@/features/library/people/people-model';
import { booksWhere, useMerge } from '@/features/library/people/use-merge';
import { useMerge } from '@/features/library/people/use-merge';
import { toastError } from '@/lib/errors';
import { counted, formatList } from '@/lib/format';
import { seriesKey } from '@/lib/series-key';
Expand All @@ -18,7 +18,7 @@ import { useIssueActions } from './use-issue-actions';
/**
* Health > Series spelled several ways: one notice per series a library's books
* spell more than one way, offering to set one spelling on every book of it (the
* Authors page's merge: a bulk edit, files untouched), the Series page's name
* Authors page's merge: one server transaction, files untouched), the Series page's name
* first. Ignore stops a series being suggested; with `ignored`, those series, each
* with Show again.
*/
Expand Down Expand Up @@ -58,12 +58,21 @@ export function SeriesSpellings({ ignored }: { ignored: boolean }) {
* spelling (its main series; the series filter also finds the books listing it
* among their other series, which a rename doesn't touch).
*/
function seriesBooks(g: SeriesSpellingGroup): Promise<AdminBook[]> {
async function seriesBooks(g: SeriesSpellingGroup): Promise<AdminBook[]> {
const key = seriesKey(g.suggested);
return booksWhere(
{ series: g.suggested, library_id: g.library_id },
(b) => b.series !== '' && seriesKey(b.series) === key,
);
const books: AdminBook[] = [];
let cursor: string | undefined;
do {
const page = await api.adminBooks({
series: g.suggested,
library_id: g.library_id,
limit: 200,
cursor,
});
books.push(...page.books.filter((b) => b.series !== '' && seriesKey(b.series) === key));
cursor = page.next_cursor;
} while (cursor);
return books;
}

function SpellingNotice({ group: g, ignored }: { group: SeriesSpellingGroup; ignored: boolean }) {
Expand All @@ -75,7 +84,7 @@ function SpellingNotice({ group: g, ignored }: { group: SeriesSpellingGroup; ign
const [ignoring, setIgnoring] = useState(false);
const names = g.spellings.map((s) => s.name);
const chosen = names.includes(keep) ? keep : g.suggested;
const others = otherSpellings({ names, suggested: chosen, books: g.books, other_books: 0 });
const others = names.filter((name) => name !== chosen);
const rewrites = g.books - (g.spellings.find((s) => s.name === chosen)?.books ?? 0);
const busy = merging !== null || ignoring;

Expand Down Expand Up @@ -126,17 +135,7 @@ function SpellingNotice({ group: g, ignored }: { group: SeriesSpellingGroup; ign
</option>
))}
</NativeSelect>
<Button
size="sm"
disabled={busy}
// The series filter finds every spelling at once: one lookup.
onClick={() =>
void merge(
{ names, suggested: chosen, books: g.books, other_books: rewrites },
() => seriesBooks(g),
)
}
>
<Button size="sm" disabled={busy} onClick={() => void merge(chosen, others)}>
{merging ? <Loader2 className="animate-spin" aria-hidden="true" /> : null}
{merging ? t('people-merge.merging') : t('health.spellings.use')}
</Button>
Expand Down
6 changes: 5 additions & 1 deletion admin-ui/src/features/library/people/merge-suggestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export function MergeSuggestions({
suggested: s.suggested,
})}
actions={
<Button size="sm" disabled={merging !== null} onClick={() => void merge(s)}>
<Button
size="sm"
disabled={merging !== null}
onClick={() => void merge(s.suggested, others)}
>
{busy ? <Loader2 className="animate-spin" aria-hidden="true" /> : null}
{busy
? t('people-merge.merging')
Expand Down
Loading
Loading