Compare commits

99 Commits

Author SHA1 Message Date
logaritmisk 97c63ac25b merge: UI polish bundle (#73)
CI / web (push) Successful in 6m4s
2026-06-10 13:47:28 +02:00
logaritmisk 62c569741f fix(web): UI polish — select placeholder, locked-field note, list overflow, sidebar toggle, heading wrap (#73)
Five small design/layout nits from the UI sweep:

- form.selectPlaceholder "— select —" → "Select…" / "Välj…", matching
  the affordance style of every other placeholder (Filter…, Search…).
- FieldForm in edit mode now explains its locked controls with a muted
  fields.lockedNote caption ("Key and type can't be changed after
  creation.") instead of leaving four silently disabled inputs.
- FieldList rows truncate long labels (min-w-0 on the row button +
  truncate on the label, shrink-0 on the badge and required marker)
  instead of overflowing the 20rem column.
- The sidebar collapse toggle is hidden on narrow viewports (hidden
  md:flex) instead of rendered permanently disabled/grayed — the rail
  is forced collapsed there anyway.
- PageTitle gains text-balance so long titles wrap evenly.

Closes #73

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 13:47:22 +02:00
logaritmisk 3ad0e56ecd merge: deep-linkable field selection (#72)
CI / web (push) Successful in 6m14s
2026-06-10 13:44:08 +02:00
logaritmisk ada5d06dad feat(web): deep-linkable field selection via /fields/:key (#72)
FieldsPage kept the selected field definition in component state, so
reload lost the selection, fields couldn't be linked/shared, and
back/forward didn't navigate selections — inconsistent with
/vocabularies/:id and /objects/:id.

Move selection into the URL: the route becomes /fields/:key?
(optional segment), FieldList selection navigates, cancel/done
navigates back to /fields, and the page derives the selected def from
the already-cached field-defs query. An unknown or stale key (e.g.
after deleting the selected field) falls back to the create form.

Tests: deep link opens the locked edit form, select→cancel round-trips
through the URL, unknown key falls back to create.

Closes #72

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 13:44:02 +02:00
logaritmisk 3a57c0a77c merge: reduced-motion support + overscroll containment (#71)
CI / web (push) Successful in 4m48s
2026-06-10 13:38:44 +02:00
logaritmisk 9a896bb5f6 fix(web): honor prefers-reduced-motion; contain overscroll in modal surfaces (#71)
Nothing in the app respected prefers-reduced-motion — the kit's
data-open/closed animations, the skeleton pulse, and the sidebar width
transition all ran unconditionally. Add a global base-layer rule that
collapses animation/transition durations to a single frame when the OS
asks for reduced motion; one rule covers current and future additions.

Add overscroll-y-contain to the scrollable modal/popup surfaces
(DrawerContent, SelectContent, ComboboxPopup) so flicking past the end
of their content no longer chain-scrolls the page beneath, and to
AlertDialogContent for when it gains scrollable content.

Verified in the built CSS: the media query and
overscroll-behavior-y:contain both compile into dist/assets.

Closes #71

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 13:38:37 +02:00
logaritmisk 78f5afad35 merge: pending-state feedback for delete confirms + login (#70)
CI / web (push) Successful in 4m37s
2026-06-10 13:35:34 +02:00
logaritmisk 27205c65ef fix(web): disable delete confirms while pending + Signing in… feedback (#70)
The delete dialogs (DeleteObjectDialog and the shared
DeleteConfirmDialog) left their confirm button enabled during the
in-flight request, so a double-click fired a second DELETE that 404'd
and surfaced a spurious error. Disable cancel + confirm while pending
and swap the confirm label to a new actions.deleting ("Deleting…" /
"Tar bort…").

The login button disabled itself during login.isPending but kept the
"Sign in" label; it now shows auth.signingIn ("Signing in…" /
"Loggar in…") so slow networks get visible feedback.

Each fix is covered by a gated-MSW (or gated-promise) test asserting
the pending label + disabled state before releasing the request.

Closes #70

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 13:35:27 +02:00
logaritmisk 091a1a651d merge: focus-visible rings + live search count (#69)
CI / web (push) Successful in 5m7s
2026-06-10 13:31:15 +02:00
logaritmisk ec11c9dc76 fix(web): focus-visible rings on remaining controls + live search count (#69)
Keyboard focus was invisible on the objects-table sort headers and
page-size select, breadcrumb links, the external-URI link, and the
combobox input/clear/trigger. Apply the shared focusRing helper in app
code and the kit's inline focus-visible classes (matching input.tsx)
in ui/combobox.

Make the search result count a role="status" live region so screen
readers announce updated counts while typing; the existing search test
now asserts the count through getByRole("status").

Closes #69

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 13:29:27 +02:00
logaritmisk 1d19ddfd96 merge: dark-mode tokens for popup primitives + theme-color/color-scheme sync (#68)
CI / web (push) Successful in 5m47s
2026-06-10 13:23:47 +02:00
logaritmisk 79a6567530 fix(web): dark-mode tokens for popup primitives + theme-color/color-scheme sync (#68)
Tooltip, toast, and combobox popups still hardcoded light colors
(bg-white, neutral-*, indigo-50) and rendered as white boxes in dark
mode; the objects-table page-size select did the same in app code.
Swap all of them to theme tokens (popover/accent/muted/destructive/
success) and replace the toast's literal "×" with the lucide X icon.

Wire browser chrome into the theme: color-scheme via CSS on
:root/.dark (follows the in-app toggle, not just the OS), a
theme-color meta kept in sync by the preload script and applyTheme(),
plus a unit test for the meta sync.

Extend check-no-raw-colors to also flag shadeless white/black
utilities outside components/ui/ so the objects-table case can't
recur.

Closes #68

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 09:42:57 +02:00
logaritmisk fe448034ac merge: instance-timezone timestamp formatter (#42)
CI / web (push) Successful in 5m29s
Add shared formatTimestamp(value, timeZone, locale) helper — date + short
time in the instance tz/locale, with a UTC fallback on an invalid IANA zone.
Route the objects-table 'Updated' column through it (was inline, date-only,
unguarded). Display-only; storage stays UTC.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 21:14:45 +02:00
logaritmisk 67c5da57bf feat(web): render objects 'Updated' as a tz-aware timestamp via formatTimestamp (#42) 2026-06-09 21:11:11 +02:00
logaritmisk 53405d7831 feat(web): formatTimestamp helper (instance tz + locale, UTC fallback) (#42) 2026-06-09 21:07:13 +02:00
logaritmisk e615260422 docs(plans): instance-timezone timestamp formatter — 2-task plan (#42) 2026-06-09 20:58:34 +02:00
logaritmisk 3b6441688f docs(specs): instance-timezone timestamp formatter (#42) 2026-06-09 15:34:02 +02:00
logaritmisk a0b7dcdc2d merge: responsive master/detail for vocabularies, search, fields (#58)
CI / web (push) Successful in 5m3s
2026-06-09 15:22:46 +02:00
logaritmisk 7f9cf9fe60 feat(web): responsive Fields page (stacks on narrow) (#58) 2026-06-09 15:18:39 +02:00
logaritmisk b83149e0bb feat(web): responsive Search master/detail (drawer on narrow) (#58) 2026-06-09 15:15:44 +02:00
logaritmisk 80c2aad298 feat(web): responsive Vocabularies master/detail (drawer on narrow) (#58) 2026-06-09 15:12:45 +02:00
logaritmisk b5756e16b5 refactor(web): shared DetailDrawer; objects-page uses it (#58) 2026-06-09 15:09:37 +02:00
logaritmisk b3f061ced7 docs(plans): responsive master/detail — 4-task plan (#58) 2026-06-09 15:06:42 +02:00
logaritmisk eec3a261b4 docs(specs): responsive master/detail for vocab/search/fields (#58) 2026-06-09 14:11:14 +02:00
logaritmisk 390f6897a8 merge: bundle vendor-split + test-gap fills (#67)
CI / web (push) Successful in 5m13s
2026-06-09 13:48:46 +02:00
logaritmisk 8b881f369b test(web): add a Storybook story for the combobox primitive (#67) 2026-06-09 12:32:06 +02:00
logaritmisk aef5000543 test(web): cover prune-fields, labels, format-date, delete-in-use dialog (#67) 2026-06-09 12:28:48 +02:00
logaritmisk 878db9a37b build(web): split framework deps into cache-stable vendor chunks (#67) 2026-06-09 12:24:47 +02:00
logaritmisk 0b44bc0855 docs(plans): bundle vendor-split + test gaps — 3-task plan (#67) 2026-06-09 12:16:23 +02:00
logaritmisk 79ee402b33 docs(specs): bundle vendor-split + test-gap fills (#67) 2026-06-09 12:09:02 +02:00
logaritmisk 64f35e5a57 merge: fix CI — Node 22, Playwright install, deterministic pending-state tests, testTimeout (#25)
CI / web (push) Successful in 7m32s
2026-06-09 11:57:32 +02:00
logaritmisk 3aff10557c ci: make the logout pending-state test deterministic (gate, not delay)
CI / web (push) Successful in 4m35s
Same timing-window race as object-new-page: the 50ms delay let the logout
resolve (menu unmounts on me=null) before findByText caught 'Signing out…'
on the slow CI runner. Hold the logout open with a promise released only
after the pending state is asserted.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 11:51:35 +02:00
logaritmisk e8fe24f755 ci: raise vitest testTimeout to 20s for the resource-constrained runner
CI / web (push) Failing after 3m50s
The 'narrow: detail renders inside a portaled drawer' test lazy-loads the
drawer chunk and exceeded the 5s default on the slow CI container (setup
alone took ~486s). Bump testTimeout on both vitest projects.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 11:44:30 +02:00
logaritmisk fc170ccf10 ci: install Playwright chromium for the storybook vitest project; deterministic in-flight test
CI / web (push) Failing after 4m5s
- CI runs the @vitest/browser-playwright (storybook) project, which needs the
  chromium browser downloaded — add 'playwright install --with-deps chromium'.
- object-new-page in-flight test held the create mutation open with a 50ms
  delay and raced the pending-state assertion (failed under CI timing); gate it
  on a promise released only after asserting 'saving…', so it's deterministic.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 11:37:20 +02:00
logaritmisk 3ae9d87e6e ci: bump Node 20 → 22 so pnpm 11 (needs Node ≥22.13) runs
CI / web (push) Failing after 2m25s
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 11:29:58 +02:00
logaritmisk 3dbede6bc2 and use correct container image
CI / web (push) Failing after 55s
2026-06-09 11:25:18 +02:00
logaritmisk ba238ca962 run ci on correct runner
CI / web (push) Failing after 4s
2026-06-09 11:24:08 +02:00
logaritmisk 7cabebc338 merge: design-kit consistency — useLang, class recipes, kit adoption (#66)
CI / web (push) Has been cancelled
2026-06-09 00:02:33 +02:00
logaritmisk 74cde67a54 refactor(web): kit consistency — focusRing, PageTitle, Badge, size-4, icon buttons (#66)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 23:49:35 +02:00
logaritmisk 900f85f8ac refactor(web): adopt useLang + segmentClass/rowStateClass across sites (#66) 2026-06-08 23:45:24 +02:00
logaritmisk 00a7ce772e feat(web): useLang + segmentClass/rowStateClass helpers; delete dead Card (#66) 2026-06-08 23:41:08 +02:00
logaritmisk 71dee23028 docs(plans): design-kit consistency — 3-task plan (#66) 2026-06-08 23:31:35 +02:00
logaritmisk 91716e628a docs(specs): design-kit consistency — useLang, class recipes, kit adoption (#66) 2026-06-08 22:32:34 +02:00
logaritmisk 002af9d1f8 merge: split queries.ts — errors + key factory + domain modules; invalidate search on object writes (#65)
CI / web (push) Has been cancelled
2026-06-08 22:26:41 +02:00
logaritmisk d8d8035850 refactor(web): split queries.ts into api/queries/ domain modules behind a barrel (#65) 2026-06-08 21:35:02 +02:00
logaritmisk 704b159d48 refactor(web): central query-key factory + invalidate search on object writes (#65)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 21:30:57 +02:00
logaritmisk c1bddb47c4 refactor(web): extract API error classes to api/errors.ts (#65) 2026-06-08 21:27:21 +02:00
logaritmisk a21ab85576 docs(plans): split queries.ts — 3-task plan (#65) 2026-06-08 20:46:29 +02:00
logaritmisk 7ddf6967ce docs(specs): split queries.ts — errors + key factory + domain modules (#65) 2026-06-08 20:37:11 +02:00
logaritmisk 404cf67f35 merge: unify vocabulary + authority CRUD into shared components (#64)
CI / web (push) Has been cancelled
2026-06-08 20:22:36 +02:00
logaritmisk 50d2512123 refactor(web): term/authority rows + pages adopt shared CRUD components (#64) 2026-06-08 20:16:17 +02:00
logaritmisk c689b8c0e9 feat(web): shared FilteredRecordList component (#64) 2026-06-08 20:11:29 +02:00
logaritmisk acdaf8d07f feat(web): shared LabelledRecordCreateForm component (#64) 2026-06-08 20:08:10 +02:00
logaritmisk 77c56f7a9d feat(web): shared LabelledRecordRow component (#64) 2026-06-08 20:05:05 +02:00
logaritmisk 030472c2da docs(plans): unify vocab + authority CRUD — 4-task plan (#64) 2026-06-08 19:56:42 +02:00
logaritmisk f1eb6a9ba5 docs(specs): unify vocabulary + authority CRUD (#64) 2026-06-08 19:52:35 +02:00
logaritmisk 285a1323ad merge: accessibility defect bundle — label-id, table rows, drawer/breadcrumb names, announced states (#62)
CI / web (push) Has been cancelled
2026-06-08 19:47:58 +02:00
logaritmisk da3e078fbc fix(web): objects-table a11y — real-link rows, pill focus ring, announced load/error (#62) 2026-06-08 19:07:00 +02:00
logaritmisk 0def81ab42 fix(web): a11y labelling — useId, named drawer/breadcrumb, translated combobox (#62) 2026-06-08 19:00:28 +02:00
logaritmisk 546680017d docs(plans): a11y defect bundle — 2-task plan (#62) 2026-06-08 18:52:21 +02:00
logaritmisk 3efb7e175d docs(specs): accessibility defect bundle (#62) 2026-06-08 18:49:27 +02:00
logaritmisk 56076c4daa merge: consistent status-aware mutation error feedback (#63)
CI / web (push) Has been cancelled
2026-06-08 18:41:37 +02:00
logaritmisk aeb1b084d9 feat(web): adopt MutationError across create/object forms; distinguish edit-form fetch error (#63) 2026-06-08 17:32:36 +02:00
logaritmisk 6e02ac874f feat(web): inline status-aware errors on term/authority edit rows + delete dialog (#63) 2026-06-08 17:27:02 +02:00
logaritmisk dd131ee740 feat(web): mutations throw HttpError(status) so failures are status-aware (#63) 2026-06-08 17:21:50 +02:00
logaritmisk cad5a980c5 feat(web): shared status-aware error-message helper + MutationError component (#63) 2026-06-08 17:17:14 +02:00
logaritmisk 17bfd3e9d8 docs(plans): mutation error feedback — 4-task plan (#63) 2026-06-08 16:43:10 +02:00
logaritmisk d90aa75468 docs(specs): consistent status-aware mutation error feedback (#63) 2026-06-08 16:22:45 +02:00
logaritmisk 7a43f794e5 merge: session-expiry soft redirect + auth feedback (#48)
CI / web (push) Has been cancelled
2026-06-08 15:13:03 +02:00
logaritmisk af3f1a5367 feat(web): return-to-destination on auth redirect; logout pending state (#48) 2026-06-08 15:06:50 +02:00
logaritmisk ec6e90ef5b feat(web): login reason banner + return-to + empty-field guard (#48) 2026-06-08 15:02:55 +02:00
logaritmisk 3c59f47f81 feat(web): soft-redirect to login on 401 via a navigate bridge (#48) 2026-06-08 14:58:25 +02:00
logaritmisk 76f65a95dd docs(plans): session-expiry soft redirect — 3-task plan (#48) 2026-06-08 14:56:44 +02:00
logaritmisk a0aab6571f docs(specs): session-expiry soft redirect + auth feedback (#48) 2026-06-08 14:52:16 +02:00
logaritmisk 6e72f24f0a merge: group object-form flexible fields by definition group (#45 follow-up)
CI / web (push) Has been cancelled
2026-06-08 14:21:00 +02:00
logaritmisk d447e2d8a8 feat(web): group object-form flexible fields by definition group (#45)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-08 14:03:33 +02:00
logaritmisk a9a0c4d477 refactor(web): extract groupDefinitions helper; object-detail uses it (#45)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-08 13:58:12 +02:00
logaritmisk c0c86a5859 docs(plans): object-form field grouping — 2-task plan (#45) 2026-06-08 13:56:07 +02:00
logaritmisk faca2670a4 docs(specs): object-form flexible-field grouping via shared helper (#45 follow-up) 2026-06-08 13:54:53 +02:00
logaritmisk c68bbb9460 merge: search-row recording_date + softened estimated count (#61)
CI / web (push) Has been cancelled
2026-06-08 13:49:31 +02:00
logaritmisk 30da072d96 feat(web): show recording_date on search rows; flag estimated count as approximate (#61) 2026-06-08 13:45:35 +02:00
logaritmisk 1cdfa21259 feat(search): index + return recording_date on search hits (#61)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 13:41:17 +02:00
logaritmisk d37ac821f0 docs(plans): search-row recording_date + count copy — 2-task plan (#61) 2026-06-08 13:38:13 +02:00
logaritmisk 150ca63fc0 docs(specs): search-row recording_date + softened estimated count (#61) 2026-06-08 10:03:15 +02:00
logaritmisk d082836529 merge: a11y — focus rings, route focus, skip link, honest semantics, html lang (#52)
CI / web (push) Has been cancelled
2026-06-08 09:54:17 +02:00
logaritmisk 69d3d2be15 feat(web): skip link + route focus management + html lang sync (#52) 2026-06-08 09:46:17 +02:00
logaritmisk 57504c941d feat(web): focus-visible rings on custom controls; honest authority links + lang group (#52) 2026-06-08 09:42:33 +02:00
logaritmisk 4530004d87 docs(plans): a11y focus/route/skip/semantics — 2-task plan (#52) 2026-06-08 09:39:28 +02:00
logaritmisk 1948d09d16 docs(specs): a11y — focus rings, route focus, skip link, honest semantics, html lang (#52) 2026-06-08 09:36:44 +02:00
logaritmisk 4c24f0387c merge: enforce en/sv i18n key parity test (#60)
CI / web (push) Has been cancelled
2026-06-08 09:31:14 +02:00
logaritmisk 0209638552 docs: consolidate frontend guardrails + test-harness gotchas
CI / web (push) Has been cancelled
Adds web/GUARDRAILS.md capturing the recurring CI-guardrail and
test-harness lessons in one place: the check:size (250 KB-gz largest
chunk) and check:colors (design-token) guards, the jsdom/storybook
vitest split, MSW onUnhandledRequest:"error" overrides, RTL
accessible-name collisions, Storybook nested-router/portal handling,
and the components/ui code-style split. Wires a pointer from CLAUDE.md.

All claims verified against the live scripts, ci.yaml, and src/test/.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 09:21:16 +02:00
logaritmisk 2b6ea1b4a4 test(web): enforce en/sv i18n key parity + non-empty values (#60) 2026-06-08 09:15:35 +02:00
logaritmisk 3575282dc2 merge: reference-data scannability + parity — sort/filter/external_uri/counts (#50)
CI / web (push) Has been cancelled
2026-06-08 09:10:33 +02:00
logaritmisk 882d0c828f feat(web): field-list filter, within-group label sort, group order, count badges (#50) 2026-06-08 09:06:17 +02:00
logaritmisk 75e7cf9047 feat(web): authorities sort+filter, create external_uri, external_uri in rows, url input (#50)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 09:02:13 +02:00
logaritmisk 76b2cbde1d feat(web): vocab list/terms sort+filter, external_uri in rows, rename guard, url input (#50)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 08:57:52 +02:00
logaritmisk 6c2fa63cac feat(web): collator sort helpers + ExternalUriLink + filter/uri i18n (#50) 2026-06-08 08:54:04 +02:00
logaritmisk a4fb05a175 docs(plans): reference-data scannability + parity — 4-task plan (#50) 2026-06-08 08:18:58 +02:00
logaritmisk 0678cefd13 docs(specs): reference-data scannability + parity (sort/filter/uri/counts) (#50) 2026-06-08 07:37:40 +02:00
153 changed files with 9266 additions and 1276 deletions
+5 -2
View File
@@ -7,7 +7,9 @@ on:
jobs:
web:
runs-on: ubuntu-latest
runs-on: aceofba-cluster
container:
image: ghcr.io/catthehacker/ubuntu:act-22.04
defaults:
run:
working-directory: web
@@ -18,12 +20,13 @@ jobs:
version: 11
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: pnpm
cache-dependency-path: web/pnpm-lock.yaml
- run: pnpm install --frozen-lockfile
- run: pnpm typecheck
- run: pnpm lint
- run: pnpm exec playwright install --with-deps chromium
- run: pnpm test
- run: pnpm build
- run: pnpm check:size
+1
View File
@@ -28,3 +28,4 @@ cargo clippy --workspace --all-targets -- -D warnings # lint before committing
- **Code navigation:** prefer the insikt LSP server over grep/glob — it resolves macro-generated symbols that text search misses. (insikt runs standalone, not via the gateway MCP.)
- **Dependencies:** manage via the `cargo-mcp` server rather than editing `Cargo.toml` by hand.
- **Formatting:** `cargo +nightly fmt` (nightly toolchain required).
- **Frontend guardrails:** before touching `web/`, read **[web/GUARDRAILS.md](web/GUARDRAILS.md)** — it covers the CI gate (`check:size` 250 KB-gz budget, `check:colors` design-token enforcement) and the test-harness quirks (MSW `onUnhandledRequest: "error"`, the jsdom/storybook vitest split, RTL accessible-name collisions, Storybook nested-router and portal handling, and the `components/ui/` code-style split).
+2
View File
@@ -30,6 +30,7 @@ pub(crate) struct SearchHitView {
pub brief_description: Option<String>,
#[schema(value_type = domain::Visibility)]
pub visibility: String,
pub recording_date: Option<String>,
pub snippet: Option<String>,
}
@@ -103,6 +104,7 @@ pub(crate) async fn search_objects(
object_name: h.object_name,
brief_description: h.brief_description,
visibility: h.visibility,
recording_date: h.recording_date,
snippet: h.snippet,
})
.collect(),
+4
View File
@@ -34,6 +34,7 @@ pub struct SearchDocument {
pub brief_description: Option<String>,
pub current_owner: Option<String>,
pub recorder: Option<String>,
pub recording_date: Option<String>,
/// Filterable: "draft" | "internal" | "public".
pub visibility: String,
/// Flexible field values flattened to searchable text.
@@ -55,6 +56,7 @@ pub struct SearchHit {
pub object_name: String,
pub brief_description: Option<String>,
pub visibility: String,
pub recording_date: Option<String>,
pub snippet: Option<String>,
}
@@ -233,6 +235,7 @@ impl SearchClient {
object_name: doc.object_name,
brief_description: doc.brief_description,
visibility: doc.visibility,
recording_date: doc.recording_date,
snippet,
}
})
@@ -367,6 +370,7 @@ pub async fn build_document(
brief_description: object.brief_description.clone(),
current_owner: object.current_owner.clone(),
recorder: object.recorder.clone(),
recording_date: object.recording_date.map(|d| d.to_string()),
visibility: object.visibility.as_str().to_owned(),
fields_text,
})
+3
View File
@@ -19,6 +19,7 @@ fn doc(id: &str, object_name: &str, fields_text: &[&str]) -> SearchDocument {
brief_description: None,
current_owner: None,
recorder: None,
recording_date: None,
visibility: "draft".to_string(),
fields_text: fields_text.iter().map(|s| s.to_string()).collect(),
}
@@ -66,6 +67,7 @@ async fn search_objects_returns_hits_with_highlight_filter_and_paging() {
&["cast bronze with green patina"],
);
bronze_a.visibility = "public".to_string();
bronze_a.recording_date = Some("1962-04-03".to_string());
let mut bronze_b = doc(&b.to_string(), "Ceremonial bowl", &["bronze alloy rim"]);
bronze_b.visibility = "public".to_string();
let mut bronze_c = doc(&c.to_string(), "Door fitting", &["bronze hinge"]);
@@ -87,6 +89,7 @@ async fn search_objects_returns_hits_with_highlight_filter_and_paging() {
"snippet must mark the match"
);
assert!(snippet.contains(search::HL_POST));
assert_eq!(hit.recording_date.as_deref(), Some("1962-04-03"));
let public = client
.search_objects("bronze", Some("public"), 0, 20)
@@ -0,0 +1,256 @@
# Accessibility Defect Bundle — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Fix five remaining a11y defects — label-id collision, unnamed drawer/breadcrumb, untranslated combobox strings (Task 1); invalid table-row semantics, missing pill focus ring, unannounced table load/error states (Task 2).
**Architecture:** Task 1 is a labelling/i18n cluster across four small components plus 5 new i18n keys. Task 2 reworks the objects-table data rows to use a real `<Link>` with `aria-current`, restores `focusRing` on the filter pills, and adds `aria-busy` + a live `<caption>` + `role="alert"` for load/error announcement.
**Tech Stack:** React 19 + TS + pnpm, React Router 7, Base UI, react-i18next, Vitest 4 (jsdom) + RTL + MSW.
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; en/sv parity; app source double-quote+semicolon; `components/ui/*` untouched; token classes only (`focusRing` is token-based). Run a single test pass.
**Spec:** `docs/superpowers/specs/2026-06-08-a11y-defect-bundle-design.md`
**Key facts:**
- Existing i18n keys: `common.noMatches` ("No matches"), `common.loading` ("Loading"), `nav.objects`, `objects.loadError` ("Could not load objects"), `actions.closeDetail`. NEW keys to add (en/sv): `common.clear`, `common.open`, `nav.breadcrumb`, `objects.detailTitle`, `objects.tableLabel`.
- `lib/focus-ring.ts` exports `focusRing` (a class string). Imported elsewhere as `import { focusRing } from "../lib/focus-ring";`.
- `components/label-editor.test.tsx`, `objects/options-combobox.test.tsx`, `shell/breadcrumb.test.tsx` exist. `objects/object-detail-drawer.test.tsx` does NOT.
- `objects/objects-table.test.tsx`: imports `renderApp`, `objectsPage` (from `../test/fixtures`), `ObjectsTable`, `ObjectDetail`, `i18n`, `Routes`/`Route`. Its `tree()` mounts `ObjectsTable` at `/objects` and `ObjectDetail` at `/objects/:id` as siblings. Fixtures: `objectsPage.items[0]` = `{ object_number: "LM-0042", object_name: "Amphora", … }`, `[1]` = `"LM-0043"`/`"Bronze fibula"`. The "clicking a row deep-links…" test clicks the **name** ("Amphora"), which stays plain text — it survives unchanged.
- `combobox.tsx` wrapper: `ComboboxClear`/`ComboboxTrigger` pass `aria-label` through to Base UI; `ComboboxEmpty` renders children. Do NOT modify `components/ui/combobox.tsx`.
- `object-detail-drawer.tsx`: `DrawerContent` spreads `...props` onto the Base UI `Drawer.Popup`, so `aria-label` passes through.
---
# Task 1: Labelling + i18n cluster (label-editor, combobox, breadcrumb, drawer)
**Files:** Modify `web/src/i18n/en.json`, `web/src/i18n/sv.json`, `web/src/components/label-editor.tsx`, `web/src/objects/options-combobox.tsx`, `web/src/shell/breadcrumb.tsx`, `web/src/objects/object-detail-drawer.tsx`; tests `web/src/components/label-editor.test.tsx`, `web/src/objects/options-combobox.test.tsx`, `web/src/shell/breadcrumb.test.tsx`.
- [ ] **Step 1: Add the 5 i18n keys (both locales, parity).** In `web/src/i18n/en.json`, add to the relevant blocks: under `common``"clear": "Clear", "open": "Open"`; under `nav``"breadcrumb": "Breadcrumb"`; under `objects``"detailTitle": "Object detail", "tableLabel": "Objects"`. In `web/src/i18n/sv.json`, the same keys: `common.clear` = `"Rensa"`, `common.open` = `"Öppna"`, `nav.breadcrumb` = `"Brödsmulor"`, `objects.detailTitle` = `"Objektdetalj"`, `objects.tableLabel` = `"Objekt"`. (Valid JSON; mind commas. Place each new key beside its existing siblings in the same nested object.)
- [ ] **Step 2: `label-editor.tsx` — `useId()`.** Add `useId` to the React import (`import { useId } from "react";`). Inside the component, add `const inputId = useId();` and change the two lines to:
```tsx
<Label htmlFor={inputId}>{t("labels.label")}</Label>
<Input id={inputId} value={current} onChange={(e) => set(e.target.value)} />
```
- [ ] **Step 3: `options-combobox.tsx` — translate.** Add `import { useTranslation } from "react-i18next";` and `const { t } = useTranslation();` at the top of the component body. Change:
```tsx
<ComboboxClear aria-label={t("common.clear")} />
<ComboboxTrigger aria-label={t("common.open")} />
```
and
```tsx
<ComboboxEmpty>{t("common.noMatches")}</ComboboxEmpty>
```
- [ ] **Step 4: `breadcrumb.tsx` — translate the nav label.** Add `import { useTranslation } from "react-i18next";` and `const { t } = useTranslation();` inside `Breadcrumb` (before the `if (trail.length === 0)` guard). Change `<nav aria-label="Breadcrumb" …>` to `<nav aria-label={t("nav.breadcrumb")} …>`.
- [ ] **Step 5: `object-detail-drawer.tsx` — name the dialog.** Change `<DrawerContent>` to `<DrawerContent aria-label={t("objects.detailTitle")}>` (the `t` from `useTranslation` is already in scope in this file).
- [ ] **Step 6: Tests.**
- **`label-editor.test.tsx`** — append (reuse the file's existing render harness / providers, e.g. `renderApp` or whatever wraps `useConfig`; read the top of the file first):
```tsx
test("each LabelEditor instance gets a unique input id", () => {
renderApp(
<>
<LabelEditor value={[]} onChange={() => {}} />
<LabelEditor value={[]} onChange={() => {}} />
</>,
);
const inputs = screen.getAllByLabelText(/label/i);
expect(inputs).toHaveLength(2);
expect(inputs[0].id).not.toBe("");
expect(inputs[0].id).not.toBe(inputs[1].id);
});
```
(If `LabelEditor` needs config context that `renderApp` doesn't provide, mirror the wrapper the existing tests in this file use. Keep existing tests green.)
- **`options-combobox.test.tsx`** — append (mirror the file's existing render of `OptionsCombobox`):
```tsx
test("the clear and open controls and empty text are translated", async () => {
// render OptionsCombobox with empty options so the empty state is reachable,
// using the same harness the other tests in this file use.
// …render…
expect(screen.getByRole("button", { name: /open/i })).toBeInTheDocument();
});
```
(Adapt to the existing test's setup. The key assertion: the open/clear controls have accessible names from `t()`. If the existing test already opens the popup, also assert `screen.getByText("No matches")`.)
- **`breadcrumb.test.tsx`** — append:
```tsx
test("the breadcrumb nav has a translated accessible name", () => {
// render Breadcrumb with a non-empty trail using the file's existing harness
// (it needs a BreadcrumbContext provider with a trail).
expect(screen.getByRole("navigation", { name: /breadcrumb/i })).toBeInTheDocument();
});
```
(Mirror the existing breadcrumb test's provider setup; if the file already renders a trail, just add the `getByRole("navigation", { name })` assertion.)
- [ ] **Step 7: Verify (vitest ONCE), typecheck, lint:**
```bash
cd web && pnpm vitest run src/components/label-editor.test.tsx src/objects/options-combobox.test.tsx src/shell/breadcrumb.test.tsx src/i18n && pnpm typecheck && pnpm lint
```
Expected: green (incl. i18n parity covering the 5 new keys). Keep all existing tests in those files green.
- [ ] **Step 8: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/i18n/en.json web/src/i18n/sv.json web/src/components/label-editor.tsx web/src/objects/options-combobox.tsx web/src/shell/breadcrumb.tsx web/src/objects/object-detail-drawer.tsx web/src/components/label-editor.test.tsx web/src/objects/options-combobox.test.tsx web/src/shell/breadcrumb.test.tsx
git commit -m "fix(web): a11y labelling — useId, named drawer/breadcrumb, translated combobox (#62)"
```
---
# Task 2: objects-table — real-link rows, pill focus ring, announced load/error
**Files:** Modify `web/src/objects/objects-table.tsx`, `web/src/objects/objects-table.test.tsx`.
- [ ] **Step 1: Import `focusRing`.** Add `import { focusRing } from "../lib/focus-ring";` to `objects-table.tsx`. (`Link` is already imported from `react-router-dom`.)
- [ ] **Step 2: Filter pills — add the focus ring.** In the `toolbar`, change the pill `className` to:
```tsx
className={`${focusRing} rounded-md px-2 py-1 ${active ? "bg-primary text-primary-foreground" : "border"}`}
```
- [ ] **Step 3: Rows — real link + `aria-current`, plain `<tr>`.** Replace the data-row `<tr>` (the `role="link"` one) with:
```tsx
<tr
key={object.id}
onClick={() => navigate(`/objects/${object.id}?${params}`)}
className={`cursor-pointer border-b text-sm ${
selected ? "bg-primary/10" : "hover:bg-muted"
}`}
>
<td className="px-3 py-2 text-muted-foreground">
<Link
to={`/objects/${object.id}?${params}`}
aria-current={selected ? "page" : undefined}
onClick={(event) => event.stopPropagation()}
className={`${focusRing} rounded-sm hover:underline`}
>
{object.object_number}
</Link>
</td>
<td className="px-3 py-2 font-medium">{object.object_name}</td>
<td className="px-3 py-2">
<VisibilityBadge visibility={object.visibility} />
</td>
<td className="px-3 py-2 text-muted-foreground">{object.current_location ?? "—"}</td>
<td className="px-3 py-2 text-right tabular-nums">{object.number_of_objects}</td>
<td className="px-3 py-2 text-muted-foreground">{formatUpdated(object.updated_at)}</td>
</tr>
```
(Drops `role="link"`, `tabIndex={0}`, `aria-selected`, and `onKeyDown` from the `<tr>`; the object-number cell now holds the `<Link>`. Every other cell is unchanged.)
- [ ] **Step 4: Error cell — `role="alert"`.** In the `isError` branch, change the error `<td>` to:
```tsx
<td colSpan={6} role="alert" className="px-3 py-6 text-center text-sm text-destructive">
{t("objects.loadError")}
</td>
```
- [ ] **Step 5: Table — `aria-busy` + live caption.** Change the `<table>` element and add the caption as its first child:
```tsx
<table className="w-full border-collapse" aria-busy={isLoading || undefined}>
<caption className="sr-only" aria-live="polite">
{isLoading ? t("common.loading") : t("objects.tableLabel")}
</caption>
{columns}
{body}
</table>
```
- [ ] **Step 6: Tests — extend `objects-table.test.tsx`.** Add a nested-route helper (so `ObjectsTable` is mounted WITH a `:id` param, mirroring the real `ObjectsPage` nesting) and the new assertions. Add near the existing `tree()`:
```tsx
function nestedTree() {
return (
<Routes>
<Route
path="/objects"
element={
<>
<ObjectsTable />
<Outlet />
</>
}
>
<Route path=":id" element={<div>detail pane</div>} />
</Route>
</Routes>
);
}
```
(Add `Outlet` to the `react-router-dom` import.) Then add these tests:
```tsx
test("the object number cell is a real link", async () => {
renderApp(tree(), { route: "/objects" });
expect(await screen.findByRole("link", { name: "LM-0042" })).toBeInTheDocument();
});
test("the selected row's link is marked aria-current=page", async () => {
// objectsPage.items[0] has object_number "LM-0042"; read its id from the fixture.
const first = objectsPage.items[0];
renderApp(nestedTree(), { route: `/objects/${first.id}` });
const link = await screen.findByRole("link", { name: first.object_number });
expect(link).toHaveAttribute("aria-current", "page");
// a different row's link is not current
const other = await screen.findByRole("link", { name: objectsPage.items[1].object_number });
expect(other).not.toHaveAttribute("aria-current");
});
test("the table is marked aria-busy while loading", async () => {
server.use(
http.get("/api/admin/objects", async () => {
await delay(50);
return HttpResponse.json(objectsPage);
}),
);
renderApp(tree(), { route: "/objects" });
expect(screen.getByRole("table")).toHaveAttribute("aria-busy", "true");
await screen.findByRole("link", { name: "LM-0042" });
expect(screen.getByRole("table")).not.toHaveAttribute("aria-busy");
});
test("a failed objects fetch is announced via role=alert", async () => {
server.use(http.get("/api/admin/objects", () => new HttpResponse(null, { status: 500 })));
renderApp(tree(), { route: "/objects" });
expect(await screen.findByRole("alert")).toHaveTextContent(/could not load/i);
});
```
(Add `delay` to the `msw` import: `import { delay, http, HttpResponse } from "msw";`. The existing "clicking a row deep-links…" test clicks "Amphora" — the name cell, still plain text + whole-row `onClick` — so it stays green. If `objectsPage.items[0]` doesn't carry an `id`, read `src/test/fixtures.ts` to use the correct id field.)
- [ ] **Step 7: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk (gz), and the `check:colors` line.
- [ ] **Step 8: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 9: Manual smoke (recommended).** `pnpm dev`: tab into the objects table — the visibility pills show a focus ring; Tab reaches each row's object-number link (Enter opens; Cmd/middle-click opens a new tab); the open object's row link is `aria-current`; a slow/failed load is announced.
- [ ] **Step 10: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/objects/objects-table.tsx web/src/objects/objects-table.test.tsx
git commit -m "fix(web): objects-table a11y — real-link rows, pill focus ring, announced load/error (#62)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 LabelEditor useId (T1 S2); AC2 row real-link + aria-current + plain tr + pill focusRing (T2 S2-S3); AC3 aria-busy + live caption + role=alert (T2 S4-S5); AC4 drawer + breadcrumb names + combobox translation (T1 S3-S5); AC5 gate/parity/codename (T2 S7-S8, T1 S1/S7). ✓
**Placeholder scan:** every code step shows full code; tests give concrete role/name assertions; the two "mirror the existing harness" notes (label-editor/options-combobox/breadcrumb tests) point at named existing files to copy from, not vague TODOs; the fixture-id note names the exact field to read. No TBD. ✓
**Type/consistency:** `focusRing` (string) imported once in T2 and used on pills + row link; `aria-current={selected ? "page" : undefined}` consistent; the 5 i18n keys added in T1 S1 are consumed in T1 S3-S5 (`common.clear/open`, `nav.breadcrumb`, `objects.detailTitle`) and T2 S5 (`objects.tableLabel`). ✓
## Notes
- No new dependency. `components/ui/*` untouched (combobox/drawer wrappers unchanged; only props passed from callers). `check:colors` stays green — `focusRing` uses `ring-ring` tokens, no raw palette.
- The combobox wrapper's own raw-palette internals and the segmented-control extraction are #66, not here.
@@ -0,0 +1,217 @@
# Accessibility — Focus, Route Management, Honest Semantics — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Give every custom control a keyboard focus ring, make the authority "tabs" + lang switch honest, add a skip link + route focus management, and sync `<html lang>` on language change.
**Architecture:** A shared `focusRing` class is applied to the five bare controls. Authority tabs become honest `NavLink`s (`aria-current`), the lang switch gains a `role="group"`. The app-shell adds a skip link, a focusable `<main>`, and a route-change focus effect. A single `i18n.on("languageChanged")` listener syncs `document.documentElement.lang`.
**Tech Stack:** React 19 + TS + pnpm, react-router 7 (`NavLink`/`useLocation`), react-i18next, Tailwind v4, Vitest + RTL. Test runner: `pnpm test` (single pass).
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; en/sv parity (2 new keys); app source double-quote+semicolon; token classes only (`focus-visible:ring-ring` is a token); `focus-visible:` (not `:focus`) so mouse clicks don't ring.
**Spec:** `docs/superpowers/specs/2026-06-08-a11y-focus-design.md`
**Key facts (from code):**
- Bare controls lacking a ring: `lang-switch.tsx` (2 buttons, no `type`, inactive `text-muted-foreground`), `theme-switch.tsx` (3 icon buttons, `cn(...)`), `search-panel.tsx` facet chips (`className={\`rounded-md px-2 py-0.5 ${active ? … : "border"}\`}`), `field-list.tsx` row `<button className="flex flex-1 items-center gap-2 text-left">`, `authorities-page.tsx` tab `NavLink`s.
- `authorities-page.tsx`: `<div role="tablist" className="mb-3 flex gap-2">` + `NavLink ... role="tab" aria-selected={k === currentKind}`. `NavLink` adds `aria-current="page"` to the active link by default.
- `app-shell.tsx`: `<main className="flex-1 overflow-hidden"><Outlet/></main>`, no id/tabIndex/skip link/route effect.
- `i18n/index.ts`: i18n init; `i18n.language`; no html-lang sync.
- `ui/button.tsx` ring: `focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50`.
- Tests to update: `authorities.test.tsx` ("kind tabs link…" + "aria-selected…" use `getByRole("tab")`/`aria-selected`); `app-shell.test.tsx` (`tree()` has `/objects` + `/login`; nav + lang tests).
---
# Task 1: Focus rings + honest control semantics
**Files:** `web/src/lib/focus-ring.ts` (new), `web/src/shell/lang-switch.tsx`, `web/src/shell/theme-switch.tsx`, `web/src/search/search-panel.tsx`, `web/src/fields/field-list.tsx`, `web/src/authorities/authorities-page.tsx`, `web/src/i18n/en.json`, `web/src/i18n/sv.json`, `web/src/authorities/authorities.test.tsx`.
- [ ] **Step 1: `web/src/lib/focus-ring.ts`:**
```ts
export const focusRing = "outline-none focus-visible:ring-3 focus-visible:ring-ring/50";
```
- [ ] **Step 2: i18n** — add `"language"` to the `common` namespace (en "Language" / sv "Språk"), both locales (parity).
- [ ] **Step 3: lang-switch.tsx** — wrap in a labelled group + add `type="button"` + ring (import `useTranslation`, `focusRing`, `cn`):
```tsx
import { useTranslation } from "react-i18next";
import { useLocale } from "../i18n/use-locale";
import { focusRing } from "../lib/focus-ring";
import { cn } from "@/lib/utils";
export function LangSwitch() {
const { t } = useTranslation();
const { locale, setLocale } = useLocale();
const base = locale.startsWith("sv") ? "sv" : "en";
return (
<div role="group" aria-label={t("common.language")} className="flex gap-1 text-xs">
{(["sv", "en"] as const).map((lng) => (
<button
key={lng}
type="button"
onClick={() => setLocale(lng)}
aria-pressed={base === lng}
className={cn("rounded-sm px-1", focusRing, base === lng ? "font-bold" : "text-muted-foreground")}
>
{lng.toUpperCase()}
</button>
))}
</div>
);
}
```
- [ ] **Step 4: theme-switch.tsx** — add `focusRing` to the button `cn(...)`: change the `cn("rounded-md p-1 transition-colors", active ? … : …)` to include `focusRing` as a class arg. Import `focusRing`.
- [ ] **Step 5: search-panel.tsx** — the facet chip `<button>` className: add `focusRing`. Use `cn` (import it) or append the string:
`className={cn("rounded-md px-2 py-0.5", focusRing, active ? "bg-primary text-primary-foreground" : "border")}`. Import `focusRing` + `cn`.
- [ ] **Step 6: field-list.tsx** — the row `<button className="flex flex-1 items-center gap-2 text-left">`: add `rounded-sm` + `focusRing` (import `focusRing` + `cn`): `className={cn("flex flex-1 items-center gap-2 rounded-sm text-left", focusRing)}`.
- [ ] **Step 7: authorities-page.tsx — honest semantics + ring.** Replace the `<div role="tablist">` block:
```tsx
<nav aria-label={t("nav.authorities")} className="mb-3 flex gap-2">
{KINDS.map((k) => (
<NavLink
key={k}
to={`/authorities/${k}`}
className={({ isActive }) =>
cn("rounded-md px-3 py-1 text-sm", focusRing, isActive ? "bg-primary text-primary-foreground" : "border")
}
>
{t(`authorities.${k}`)}
</NavLink>
))}
</nav>
```
Drop `role="tab"` + `aria-selected` (NavLink applies `aria-current="page"` to the active link automatically). Import `focusRing` + `cn`.
- [ ] **Step 8: Update `authorities.test.tsx`** — the two tab tests:
- "kind tabs link to the other kinds": `findByRole("tab", { name: /place/i })``findByRole("link", { name: /place/i })` (still assert `href="/authorities/place"`).
- "aria-selected…": rename to active-kind via `aria-current`: `expect(await screen.findByRole("link", { name: /^person$/i })).toHaveAttribute("aria-current", "page");` and `expect(screen.getByRole("link", { name: /^place$/i })).not.toHaveAttribute("aria-current");`.
(Confirm no link-name ambiguity — the page renders only the 3 kind links + the breadcrumb/PageTitle; if the harness includes other "person/place"-named links, scope with `within`. Don't weaken.)
- [ ] **Step 9: Verify (vitest ONCE):** `cd web && pnpm vitest run src/authorities src/shell src/search src/fields && pnpm typecheck && pnpm lint && pnpm check:colors`. PASS. (The ring classes are token-based → check:colors clean. The other tests must stay green.)
- [ ] **Step 10: Commit**
```bash
git add web/src/lib/focus-ring.ts web/src/shell/lang-switch.tsx web/src/shell/theme-switch.tsx web/src/search/search-panel.tsx web/src/fields/field-list.tsx web/src/authorities/authorities-page.tsx web/src/i18n/en.json web/src/i18n/sv.json web/src/authorities/authorities.test.tsx
git commit -m "feat(web): focus-visible rings on custom controls; honest authority links + lang group (#52)"
```
---
# Task 2: Skip link + route focus + html lang sync
**Files:** `web/src/shell/app-shell.tsx`, `web/src/i18n/index.ts`, `web/src/i18n/en.json`, `web/src/i18n/sv.json`, `web/src/shell/app-shell.test.tsx`, `web/src/i18n/i18n.test.tsx`.
- [ ] **Step 1: i18n** — add `"skipToContent"` to `common` (en "Skip to content" / sv "Hoppa till innehåll"), both locales (parity).
- [ ] **Step 2: app-shell.tsx — skip link + focusable main + route focus.**
```tsx
import { useEffect, useRef } from "react";
import { Outlet, useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
// …existing imports…
export function AppShell() {
const { t } = useTranslation();
const location = useLocation();
const mainRef = useRef<HTMLElement>(null);
const didMount = useRef(false);
useEffect(() => {
if (!didMount.current) {
didMount.current = true;
return;
}
mainRef.current?.focus();
}, [location.pathname]);
return (
<div className="flex min-h-screen">
<a
href="#main-content"
className="sr-only focus:not-sr-only focus:absolute focus:left-2 focus:top-2 focus:z-50 focus:rounded-md focus:border focus:bg-background focus:px-3 focus:py-2 focus:text-sm focus:ring-3 focus:ring-ring/50"
>
{t("common.skipToContent")}
</a>
<Sidebar />
<BreadcrumbProvider>
<div className="flex flex-1 flex-col">
<header className="flex items-center gap-4 border-b px-4 py-2">
<Breadcrumb />
<HeaderSearch />
<ThemeSwitch />
<LangSwitch />
<UserMenu />
</header>
<main ref={mainRef} id="main-content" tabIndex={-1} className="flex-1 overflow-hidden outline-none">
<Outlet />
</main>
</div>
</BreadcrumbProvider>
</div>
);
}
```
Verify `sr-only`/`focus:not-sr-only` exist in this Tailwind v4 setup (they're standard utilities; if the focus reveal doesn't work, use an explicit visually-hidden style and confirm by running the test). The skip link is the FIRST focusable element.
- [ ] **Step 3: i18n/index.ts — html lang sync.** After the `i18n.init(...)` call, add:
```ts
function syncHtmlLang(lng: string) {
if (typeof document !== "undefined") {
document.documentElement.lang = lng.startsWith("sv") ? "sv" : "en";
}
}
i18n.on("languageChanged", syncHtmlLang);
syncHtmlLang(i18n.language);
```
(Place before `export default i18n;`.)
- [ ] **Step 4: Tests.**
- **app-shell.test.tsx — skip link + route focus.** Add:
- skip link: `expect(screen.getByRole("link", { name: /skip to content/i })).toHaveAttribute("href", "#main-content");` and the `<main>` has `id="main-content"` (query `document.getElementById("main-content")` → truthy, `tabIndex === -1`).
- route focus: extend `tree()` with a second route under `<AppShell>` (e.g. `<Route path="/fields" element={<div>fields outlet</div>} />`); render at `/objects`, click the sidebar **Fields** link (`screen.getByRole("link", { name: /fields/i })`), `await screen.findByText("fields outlet")`, then assert `document.activeElement === document.getElementById("main-content")` (the route change focused main). (Initial mount must NOT focus main — optionally assert activeElement is body/not-main right after the first render.)
- **i18n.test.tsx — html lang.** Add a test: after `await i18n.changeLanguage("sv")`, `expect(document.documentElement.lang).toBe("sv")`; after `await i18n.changeLanguage("en")`, `toBe("en")`. (The file already toggles language; the `afterEach` resets to en, so assert within the test.)
- [ ] **Step 5: FULL GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk, check:colors line. (Storybook-cache flake remedy if needed: `rm -rf node_modules/.cache/storybook node_modules/.vite`, re-run ONCE.)
- [ ] **Step 6: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
- [ ] **Step 7: Manual smoke (recommended).** `pnpm dev`: Tab from the top → "Skip to content" appears first and jumps focus to the content; every custom control shows a focus ring on keyboard focus; navigating routes moves focus to the content region; the authority kind links read as links with the current one marked; switching to SV sets `<html lang="sv">` (check devtools).
- [ ] **Step 8: Commit**
```bash
git add web/src/shell/app-shell.tsx web/src/i18n/index.ts web/src/i18n/en.json web/src/i18n/sv.json web/src/shell/app-shell.test.tsx web/src/i18n/i18n.test.tsx
git commit -m "feat(web): skip link + route focus management + html lang sync (#52)"
```
---
## Self-Review (completed)
**Spec coverage:** focusRing + 5 controls (T1 S1,S3S7); lang group + authority honest links (T1 S3,S7); i18n common.language/skipToContent (T1 S2, T2 S1); skip link + focusable main + route focus (T2 S2); html lang sync (T2 S3); tests for tabs→links, skip link, route focus, html lang (T1 S8, T2 S4); gate (T2 S5). Acceptance criteria 15 mapped. ✓
**Placeholder scan:** the `sr-only`/`focus:not-sr-only` reveal is "verify it works by running" (a real validation, with an explicit fallback), not a TODO. Test steps name exact queries + the harness extension. No vague steps. ✓
**Type/consistency:** `focusRing` (string) defined in T1 S1, imported by all 5 controls + applied via `cn`; `NavLink` `aria-current` (native) replaces `role="tab"`/`aria-selected` consistently in the component + the test; `mainRef`/`didMount` refs + `useLocation().pathname` dependency consistent. ✓
## Notes
- No new dependency; 2 new i18n keys (`common.language`, `common.skipToContent`), en+sv.
- `focus-visible:` (keyboard) vs `:focus` — rings only on keyboard focus.
- `<main tabIndex={-1}>` + `outline-none` is programmatically focusable but not in the tab order and shows no container outline; the skip link + route effect both target it.
- The i18n parity test (#60) will guard the 2 new keys.
@@ -0,0 +1,211 @@
# Design-Kit Consistency — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Add three shared helpers (`useLang`, `segmentClass`, `rowStateClass`), adopt them across the duplicated sites, and apply behavior-preserving kit one-offs (delete dead Card, sidebar focusRing, login PageTitle, field-list Badge, size-4, icon dismiss buttons).
**Architecture:** Task 1 creates the helpers + deletes Card (additive/safe). Task 2 adopts the 3 helpers across 6 + 3 + 4 sites. Task 3 applies the one-off cleanups + full gate. Behavior-preserving throughout; `check:colors`/`check:size`/existing component tests are the guards.
**Tech Stack:** React 19 + TS + pnpm, Tailwind v4 (token classes + `cn`), react-i18next, Base UI, Vitest 4 + RTL.
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; double-quote+semicolon; token classes only (`check:colors`). `tsconfig` has `noUnusedLocals`, so remove any destructure that becomes unused.
**Spec:** `docs/superpowers/specs/2026-06-08-design-kit-consistency-design.md`
**Key facts (verified current):**
- `lib/focus-ring.ts` exports `focusRing = "outline-none focus-visible:ring-3 focus-visible:ring-ring/50"`. `cn` is `@/lib/utils`.
- `Button` (`@/components/ui/button`) has sizes incl. `icon-sm`. `Badge` (`@/components/ui/badge`) has a `secondary` variant. `PageTitle` (`@/components/ui/page-title`) is an `<h1>` styled `text-2xl font-semibold tracking-tight`.
- `components/ui/card.tsx` has ZERO importers and no `card.stories`.
- `useLang` sites (each currently `const lang = i18n.language.startsWith("sv") ? "sv" : "en";`): `objects/object-detail.tsx:59`, `objects/field-input.tsx:32`, `vocab/vocabulary-terms.tsx:13`, `vocab/vocabulary-list.tsx:17`, `fields/field-list.tsx:27`, `authorities/authorities-page.tsx:19`.
- `segmentClass` sites: `objects/objects-table.tsx:174` (`` className={`${focusRing} rounded-md px-2 py-1 ${active ? "bg-primary text-primary-foreground" : "border"}`} ``), `search/search-panel.tsx:76` (`className={cn("rounded-md px-2 py-0.5", focusRing, active ? "bg-primary text-primary-foreground" : "border")}`), `authorities/authorities-page.tsx:41` (`cn("rounded-md px-3 py-1 text-sm", focusRing, isActive ? "bg-primary text-primary-foreground" : "border")`).
- `rowStateClass` sites: `objects/objects-table.tsx:252` (`selected ? "bg-primary/10" : "hover:bg-muted"`), `vocab/vocabulary-list.tsx:113` (`isActive ? "bg-primary/10" : "hover:bg-muted"`), `search/search-result-row.tsx:15` (`isActive ? "bg-primary/10" : "hover:bg-muted"`), `fields/field-list.tsx:86` (`def.key === selectedKey ? "bg-primary/10" : ""` — note the missing idle hover).
---
# Task 1: Create helpers + delete dead Card
**Files:** Create `web/src/lib/use-lang.ts`, `web/src/lib/class-recipes.ts`, `web/src/lib/class-recipes.test.ts`; Delete `web/src/components/ui/card.tsx`.
- [ ] **Step 1: `web/src/lib/use-lang.ts`:**
```ts
import { useTranslation } from "react-i18next";
/** The instance's active UI language, narrowed to the two supported locales. */
export function useLang(): "sv" | "en" {
const { i18n } = useTranslation();
return i18n.language.startsWith("sv") ? "sv" : "en";
}
```
- [ ] **Step 2: `web/src/lib/class-recipes.ts`:**
```ts
import { cn } from "@/lib/utils";
import { focusRing } from "./focus-ring";
/** Segmented-control / filter-pill item. Unifies the active/inactive token recipe +
* focus ring; callers pass their contextual padding/size via `className`. */
export function segmentClass(active: boolean, className?: string): string {
return cn("rounded-md", focusRing, active ? "bg-primary text-primary-foreground" : "border", className);
}
/** Selected vs idle row background for master-detail / list rows. */
export function rowStateClass(active: boolean): string {
return active ? "bg-primary/10" : "hover:bg-muted";
}
```
- [ ] **Step 3: `web/src/lib/class-recipes.test.ts`** (write + run):
```ts
import { expect, test } from "vitest";
import { rowStateClass, segmentClass } from "./class-recipes";
test("segmentClass active uses the primary tokens + focus ring", () => {
const cls = segmentClass(true, "px-2 py-1");
expect(cls).toContain("bg-primary");
expect(cls).toContain("text-primary-foreground");
expect(cls).toContain("focus-visible:ring-ring/50");
expect(cls).toContain("px-2");
});
test("segmentClass inactive uses border, not the primary fill", () => {
const cls = segmentClass(false);
expect(cls).toContain("border");
expect(cls).not.toContain("bg-primary");
});
test("rowStateClass toggles selected vs idle-hover", () => {
expect(rowStateClass(true)).toBe("bg-primary/10");
expect(rowStateClass(false)).toBe("hover:bg-muted");
});
```
Run: `cd web && pnpm vitest run src/lib/class-recipes.test.ts` → 3 passing.
- [ ] **Step 4: Delete the dead Card component:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git rm web/src/components/ui/card.tsx
```
(Confirm no references first: `git grep -n "components/ui/card\"" web/src` returns nothing.)
- [ ] **Step 5: Verify + lint:**
```bash
cd web && pnpm vitest run src/lib/class-recipes.test.ts && pnpm typecheck && pnpm lint
```
Expected: green (Card had no importers, so its deletion can't break typecheck/lint).
- [ ] **Step 6: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/lib/use-lang.ts web/src/lib/class-recipes.ts web/src/lib/class-recipes.test.ts
git rm -q web/src/components/ui/card.tsx 2>/dev/null; git add -A web/src/components/ui
git commit -m "feat(web): useLang + segmentClass/rowStateClass helpers; delete dead Card (#66)"
```
---
# Task 2: Adopt the helpers across the duplicated sites
**Files:** Modify `objects/object-detail.tsx`, `objects/field-input.tsx`, `vocab/vocabulary-terms.tsx`, `vocab/vocabulary-list.tsx`, `fields/field-list.tsx`, `authorities/authorities-page.tsx`, `objects/objects-table.tsx`, `search/search-panel.tsx`, `search/search-result-row.tsx`.
- [ ] **Step 1: Adopt `useLang()` in the 6 components.** In each of `objects/object-detail.tsx`, `objects/field-input.tsx`, `vocab/vocabulary-terms.tsx`, `vocab/vocabulary-list.tsx`, `fields/field-list.tsx`, `authorities/authorities-page.tsx`: add `import { useLang } from "../lib/use-lang";` and replace `const lang = i18n.language.startsWith("sv") ? "sv" : "en";` with `const lang = useLang();`. Then, if `i18n` is no longer referenced anywhere else in that component, change `const { t, i18n } = useTranslation();` to `const { t } = useTranslation();` (the `noUnusedLocals` typecheck will fail otherwise — so this removal is required wherever `i18n` becomes unused). Note `authorities/authorities-page.tsx` also imports `focusRing` and uses `cn` — leave those.
- [ ] **Step 2: Adopt `segmentClass` at the 3 segmented sites.**
- `objects/objects-table.tsx`: add `import { segmentClass } from "../lib/class-recipes";`; change the pill `className` (currently `` `${focusRing} rounded-md px-2 py-1 ${active ? "bg-primary text-primary-foreground" : "border"}` ``) to `className={segmentClass(active, "px-2 py-1")}`. If `focusRing` is now unused in this file, remove its import. (The object-number `<Link>` also uses `focusRing` — if so, KEEP the import.)
- `search/search-panel.tsx`: add the import; change `className={cn("rounded-md px-2 py-0.5", focusRing, active ? "bg-primary text-primary-foreground" : "border")}` to `className={segmentClass(active, "px-2 py-0.5")}`. Remove now-unused `focusRing`/`cn` imports if they're unused elsewhere in the file.
- `authorities/authorities-page.tsx`: add the import; change the NavLink className callback body `cn("rounded-md px-3 py-1 text-sm", focusRing, isActive ? "bg-primary text-primary-foreground" : "border")` to `segmentClass(isActive, "px-3 py-1 text-sm")`. Remove now-unused `focusRing`/`cn` imports if unused elsewhere.
- [ ] **Step 3: Adopt `rowStateClass` at the 4 selected-row sites.** Add `import { rowStateClass } from "…/lib/class-recipes";` (or extend the existing class-recipes import) to each:
- `objects/objects-table.tsx`: in the row `className`, change `${selected ? "bg-primary/10" : "hover:bg-muted"}` to `${rowStateClass(selected)}`.
- `vocab/vocabulary-list.tsx`: change `${isActive ? "bg-primary/10" : "hover:bg-muted"}` to `${rowStateClass(isActive)}`.
- `search/search-result-row.tsx`: change `${isActive ? "bg-primary/10" : "hover:bg-muted"}` to `${rowStateClass(isActive)}`.
- `fields/field-list.tsx`: change `${def.key === selectedKey ? "bg-primary/10" : ""}` to `${rowStateClass(def.key === selectedKey)}` (this ADDS the `hover:bg-muted` idle hover the others have — an intended consistency fix).
- [ ] **Step 4: Verify (vitest ONCE for the affected suites), typecheck, lint:**
```bash
cd web && pnpm vitest run src/objects src/vocab src/fields src/authorities src/search && pnpm typecheck && pnpm lint
```
Expected: green. These are class-string-equivalent changes (segmentClass/rowStateClass produce the same token sets; `cn` ordering is irrelevant to Tailwind), so the existing component tests pass unchanged. `field-list`'s row now also carries `hover:bg-muted` (additive). If a test asserted the exact old className string, update it to match the new equivalent (unlikely — tests query by role/text).
- [ ] **Step 5: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/objects/object-detail.tsx web/src/objects/field-input.tsx web/src/vocab/vocabulary-terms.tsx web/src/vocab/vocabulary-list.tsx web/src/fields/field-list.tsx web/src/authorities/authorities-page.tsx web/src/objects/objects-table.tsx web/src/search/search-panel.tsx web/src/search/search-result-row.tsx
git commit -m "refactor(web): adopt useLang + segmentClass/rowStateClass across sites (#66)"
```
---
# Task 3: One-off kit cleanups + full gate
**Files:** Modify `shell/sidebar.tsx`, `auth/login-page.tsx`, `fields/field-list.tsx`, `shell/theme-switch.tsx`, `shell/user-menu.tsx`, `shell/header-search.tsx`, `objects/objects-page.tsx`, `objects/object-detail-drawer.tsx`.
- [ ] **Step 1: `shell/sidebar.tsx`** — use the `focusRing` constant. Add `import { focusRing } from "../lib/focus-ring";` (if not already imported). At the two `cn(...)` sites (lines ~46 and ~88) replace the literal `"focus-visible:ring-3 focus-visible:ring-ring/50"` entry with `focusRing`. (Both are inside `cn(...)` lists, so just swap the string for the constant.)
- [ ] **Step 2: `auth/login-page.tsx`** — use `PageTitle`. Add `import { PageTitle } from "@/components/ui/page-title";` and change `<h1 className="text-2xl font-semibold">{app_name}</h1>` to `<PageTitle>{app_name}</PageTitle>`.
- [ ] **Step 3: `fields/field-list.tsx`** — type-tag → `Badge`. Add `import { Badge } from "@/components/ui/badge";` and change the type-tag `<span className="rounded-md bg-muted px-1.5 py-0.5 text-xs text-muted-foreground">{…}</span>` (line ~97) to `<Badge variant="secondary">{…}</Badge>` (keep the inner expression/children unchanged).
- [ ] **Step 4: Icon sizing → `size-4`** in the 3 app-source sites: `shell/theme-switch.tsx:39` (`<Icon className="h-4 w-4" …>``className="size-4"`), `shell/user-menu.tsx:27` (`<CircleUser className="h-4 w-4" …>``size-4`), `shell/header-search.tsx:23` (the search icon's `… h-4 w-4 …` → replace `h-4 w-4` with `size-4`, keeping the other classes). Do NOT touch `components/ui/select.tsx`.
- [ ] **Step 5: Icon dismiss buttons → kit Button.**
- `objects/objects-page.tsx:54`: add `import { Button } from "@/components/ui/button";` (if absent) and change the `<button type="button" onClick={closeDetail} aria-label={t("actions.closeDetail")} className="rounded-md p-1 text-muted-foreground hover:bg-muted hover:text-foreground"><X className="size-4" aria-hidden="true" /></button>` to:
```tsx
<Button
variant="ghost"
size="icon-sm"
onClick={closeDetail}
aria-label={t("actions.closeDetail")}
>
<X className="size-4" aria-hidden="true" />
</Button>
```
- `objects/object-detail-drawer.tsx:31-36`: add `import { Button } from "@/components/ui/button";` and render the `DrawerClose` AS the kit Button via the render prop:
```tsx
<DrawerClose
aria-label={t("actions.closeDetail")}
render={<Button variant="ghost" size="icon-sm" />}
>
<X className="size-4" aria-hidden="true" />
</DrawerClose>
```
(This mirrors the `AlertDialogTrigger render={<Button … />}` pattern in `components/delete-confirm-dialog.tsx`; the `DrawerClose` keeps its close-on-click behaviour and the `aria-label`.)
- [ ] **Step 6: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk (gz) from check:size (should be ≤ the prior ~216.5 KB — the Card delete only removes dead code), and the `check:colors` line. The existing `user-menu`, `objects-table`, `object-detail`/drawer, `login-page`, sidebar, `field-list`, search tests must pass unchanged (the icon buttons keep their `aria-label`s; the drawer still closes; login still renders an `<h1>` via PageTitle).
- [ ] **Step 7: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 8: Manual smoke (recommended).** `pnpm dev`: the visibility pills / authority tabs / search facets look unchanged and keep their focus rings; the selected list rows (objects, vocab, search, fields) highlight identically and field rows now have a hover; the object-detail close buttons (wide pane + drawer) work; the login title and field-list type tag look right.
- [ ] **Step 9: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/shell/sidebar.tsx web/src/auth/login-page.tsx web/src/fields/field-list.tsx web/src/shell/theme-switch.tsx web/src/shell/user-menu.tsx web/src/shell/header-search.tsx web/src/objects/objects-page.tsx web/src/objects/object-detail-drawer.tsx
git commit -m "refactor(web): kit consistency — focusRing, PageTitle, Badge, size-4, icon buttons (#66)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 `useLang` + 6 sites (T1 S1, T2 S1); AC2 `segmentClass`/`rowStateClass` + adoption + field-list hover fix (T1 S2-S3, T2 S2-S3); AC3 Card deleted (T1 S4); AC4 one-offs — sidebar focusRing, login PageTitle, field-list Badge, size-4, icon buttons (T3 S1-S5); AC5 gate/check:size/codename (T3 S6-S7). ✓
**Placeholder scan:** every edit gives the exact before string + after code; helper bodies are complete; the test has concrete assertions. The "remove `i18n` if unused" instructions are concrete (driven by `noUnusedLocals`). No TBD. ✓
**Type/consistency:** `useLang()` (T1) returns `"sv" | "en"` consumed as `const lang` (T2 S1); `segmentClass(active, className?)` / `rowStateClass(active)` (T1) called with the exact args in T2 S2-S3; `Button size="icon-sm"`, `Badge variant="secondary"`, `PageTitle` all confirmed to exist. ✓
## Notes
- No new dependency, no new i18n keys. `check:colors` stays green — `segmentClass`/`rowStateClass` and all edits use tokens (`bg-primary`, `border`, `ring-ring`, `bg-muted`). Card deletion only removes dead code.
- `cn()` (tailwind-merge) makes class ordering irrelevant, so the helper outputs are visually identical to the prior inline strings (except field-list's intended added hover).
- The `<SegmentedControl>` component and the form-spacing scale are deferred (out of scope).
@@ -0,0 +1,191 @@
# Object-Form Flexible-Field Grouping — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Group the object form's flexible-field inputs by `def.group` (definition order, "Other" last) with subheadings, reusing one shared helper so the form and the detail view group identically.
**Architecture:** Extract the detail view's defs-grouping into `lib/group-fields.ts` (`groupDefinitions`), unit-test it, refactor `object-detail.tsx` to use it (output-preserving), then render the form's flexible block grouped via the same helper.
**Tech Stack:** React 19 + TS + pnpm, react-hook-form, react-i18next, Vitest + RTL. Test runner: `pnpm test` (single pass).
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; en/sv parity (no new keys); app source double-quote+semicolon; token classes only; no behavior change to the form (inputs/validation/submission).
**Spec:** `docs/superpowers/specs/2026-06-08-form-field-grouping-design.md`
**Key facts:**
- `object-detail.tsx` builds groups inline (`const other = t("fields.other"); const present = (definitions ?? []).filter(d => object.fields[d.key] != null); const groups: { group, defs }[] = []; for (const def of present) { … } ` + orphan push + `groups.sort((a,b) => Number(a.group===other) - Number(b.group===other));`). Type alias `FieldDefinitionView = components["schemas"]["FieldDefinitionView"]` already imported.
- `object-form.tsx` flexible block: `<fieldset className="space-y-3 border-t pt-3"><legend className="label-caption">{t("form.flexibleHeading")}</legend>{definitions.map((def) => <div key={def.key}><FieldInput definition={def} form={form} />{errors.fields?.[def.key] && <p role="alert" className="text-xs text-destructive">{errors.fields[def.key]?.message ?? t("form.required")}</p>}</div>)}</fieldset>`.
- Field-def fixtures have `group: "Description"` (inscription) and `group: null` (the rest).
- `sr-only` is a valid utility (used in the #52 skip link). `fields.other` + `form.flexibleHeading` are existing i18n keys.
---
# Task 1: Shared `groupDefinitions` helper + unit test + detail refactor
**Files:** `web/src/lib/group-fields.ts` (new), `web/src/lib/group-fields.test.ts` (new), `web/src/objects/object-detail.tsx`.
- [ ] **Step 1: `web/src/lib/group-fields.ts`:**
```ts
import type { components } from "../api/schema";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
export type FieldGroup = { group: string; defs: FieldDefinitionView[] };
/** Group field definitions by `def.group` (trimmed), preserving definition order
* within and across groups; ungrouped defs fall into a trailing `otherLabel` bucket. */
export function groupDefinitions(
definitions: FieldDefinitionView[],
otherLabel: string,
): FieldGroup[] {
const groups: FieldGroup[] = [];
for (const def of definitions) {
const group = def.group?.trim() ? def.group : otherLabel;
let bucket = groups.find((g) => g.group === group);
if (!bucket) {
bucket = { group, defs: [] };
groups.push(bucket);
}
bucket.defs.push(def);
}
groups.sort((a, b) => Number(a.group === otherLabel) - Number(b.group === otherLabel));
return groups;
}
```
- [ ] **Step 2: `web/src/lib/group-fields.test.ts`** (write + run, must pass):
```ts
import { expect, test } from "vitest";
import { groupDefinitions } from "./group-fields";
type Def = { key: string; group?: string | null };
const def = (key: string, group: string | null): Def => ({ key, group });
function keysByGroup(defs: Def[]) {
// cast through unknown — the helper only reads key/group
return groupDefinitions(defs as never, "Other").map((g) => ({
group: g.group,
keys: g.defs.map((d) => (d as unknown as Def).key),
}));
}
test("preserves definition order within and across groups; Other is last", () => {
const result = keysByGroup([
def("a", "Description"),
def("b", null),
def("c", "Description"),
def("d", "Provenance"),
def("e", " "),
]);
expect(result).toEqual([
{ group: "Description", keys: ["a", "c"] },
{ group: "Provenance", keys: ["d"] },
{ group: "Other", keys: ["b", "e"] },
]);
});
test("all-ungrouped → a single trailing Other group", () => {
expect(keysByGroup([def("x", null), def("y", null)])).toEqual([
{ group: "Other", keys: ["x", "y"] },
]);
});
```
Run: `cd web && pnpm vitest run src/lib/group-fields.test.ts` → PASS (2 tests). (If the `as never`/`as unknown` casts trip lint, type the test `def` as the real `FieldDefinitionView` partial via `Partial<…> as …` — keep it lint-clean and `any`-free; the helper only reads `key`/`group`.)
- [ ] **Step 3: Refactor `object-detail.tsx`** to use the helper. Add `import { groupDefinitions } from "../lib/group-fields";`. Replace the inline group-building loop + the final `groups.sort(...)` with:
```tsx
const other = t("fields.other");
const present = (definitions ?? []).filter((d) => object.fields[d.key] != null);
const groups = groupDefinitions(present, other);
```
Keep the orphan handling exactly as-is AFTER this (`const definedKeys = …; const orphans = …; if (orphans.length > 0 && !groups.some((g) => g.group === other)) groups.push({ group: other, defs: [] });`). The appended Other bucket remains last (the helper already put any Other last, and appending when absent adds it at the end). Do NOT re-add a `groups.sort(...)` — appending keeps Other last. The render (`groups.map(...)`) is unchanged.
- [ ] **Step 4: Verify (vitest ONCE):** `cd web && pnpm vitest run src/lib/group-fields.test.ts src/objects/object-detail.test.tsx && pnpm typecheck && pnpm lint`. PASS — the object-detail tests must stay green (output-preserving refactor).
- [ ] **Step 5: Commit**
```bash
git add web/src/lib/group-fields.ts web/src/lib/group-fields.test.ts web/src/objects/object-detail.tsx
git commit -m "refactor(web): extract groupDefinitions helper; object-detail uses it (#45)"
```
---
# Task 2: Group the object-form flexible inputs + test + gate
**Files:** `web/src/objects/object-form.tsx`, `web/src/objects/object-form.test.tsx`.
- [ ] **Step 1: Group the flexible block** in `object-form.tsx`. Add `import { groupDefinitions } from "../lib/group-fields";`. Replace the flexible `<fieldset>` body:
```tsx
{definitions && definitions.length > 0 && (
<fieldset className="space-y-3 border-t pt-3">
<legend className="sr-only">{t("form.flexibleHeading")}</legend>
{groupDefinitions(definitions, t("fields.other")).map((g) => (
<div key={g.group} className="space-y-3">
<div className="label-caption">{g.group}</div>
{g.defs.map((def) => (
<div key={def.key}>
<FieldInput definition={def} form={form} />
{errors.fields?.[def.key] && (
<p role="alert" className="text-xs text-destructive">
{errors.fields[def.key]?.message ?? t("form.required")}
</p>
)}
</div>
))}
</div>
))}
</fieldset>
)}
```
Change ONLY this block: the `<legend>` goes from visible `label-caption` to `sr-only`; the flat `definitions.map` becomes grouped. Field inputs + error markup are identical, just nested under group wrappers. No change anywhere else (core fields, visibility, footer, submit logic).
- [ ] **Step 2: Test** — extend `object-form.test.tsx`. The field-def fixtures have `inscription` in group `"Description"` and the rest ungrouped → "Other". Add a test that renders `<ObjectForm mode="create" …>` and asserts the group subheadings + membership:
```tsx
test("groups flexible fields by definition group with subheadings", async () => {
renderApp(<ObjectForm mode="create" onSubmit={() => {}} onCancel={() => {}} />);
// the "Description" group heading and the "Other" group heading both render
expect(await screen.findByText("Description")).toBeInTheDocument();
expect(screen.getByText(/^Other$/)).toBeInTheDocument();
// the Description-grouped field input is present (Inscription) and appears before an ungrouped one (Material)
const inscription = screen.getByLabelText(/inscription/i);
const material = screen.getByLabelText(/material/i);
expect(inscription.compareDocumentPosition(material) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
});
```
(Adjust field labels to the fixtures' actual rendered labels — read `web/src/test/fixtures.ts` `fieldDefinitions` for the exact `labels`/keys and `web/src/objects/field-input.tsx` for how each renders its label, so the `getByLabelText`/`findByText` queries match. The key assertion: a named group heading + the "Other" heading both appear, and a grouped field precedes an ungrouped one in the DOM.) Keep the existing object-form tests green.
- [ ] **Step 3: FULL GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk, check:colors line.
- [ ] **Step 4: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
- [ ] **Step 5: Manual smoke (recommended).** `pnpm dev`: open New object / edit — the flexible fields now appear under group subheadings (e.g. "Description", "Other") matching the detail view's grouping; inputs/validation/submit still work.
- [ ] **Step 6: Commit**
```bash
git add web/src/objects/object-form.tsx web/src/objects/object-form.test.tsx
git commit -m "feat(web): group object-form flexible fields by definition group (#45)"
```
---
## Self-Review (completed)
**Spec coverage:** shared `groupDefinitions` + unit test (T1 S1S2); detail refactor output-preserving (T1 S3S4); form grouped via the helper with `sr-only` legend + visible subheadings (T2 S1); form grouping test (T2 S2); gate (T2 S3). Acceptance criteria 15 mapped. ✓
**Placeholder scan:** the form test says "adjust labels to the fixtures' actual rendered labels" with the files named (fixtures.ts, field-input.tsx) — a concrete match-the-data step, not a TODO; the core assertion (named + Other headings, order) is explicit. The helper-test cast note keeps it `any`-free. No vague steps. ✓
**Type/consistency:** `groupDefinitions(defs, otherLabel): FieldGroup[]` defined in T1, consumed by detail (`present`) and form (all `definitions`); detail's orphan-Other append stays last; the form reuses the existing `FieldInput`/error markup unchanged. ✓
## Notes
- No new dependency; no new i18n keys (`fields.other` + `form.flexibleHeading` exist).
- The refactor of object-detail is output-preserving — its tests are the guard.
- Field-list's AZ grouping is intentionally NOT unified (different purpose).
@@ -0,0 +1,421 @@
# Consistent, Status-Aware Mutation Error Feedback — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Replace the generic, inconsistent mutation-error feedback with one status-aware mapping rendered consistently inline across every create/edit/delete surface.
**Architecture:** A shared `errorMessageKey(error)` maps `InUseError`/`HttpError(status)` → i18n keys (single source of truth). A shared `<MutationError error>` renders the inline alert. Mutations throw `HttpError(status)` so the status reaches the helper. All inline sites adopt the helper/component; the two non-suppressed update mutations suppress the toast and show inline like their siblings.
**Tech Stack:** React 19 + TS + pnpm, TanStack Query v5, openapi-fetch, react-i18next, Vitest 4 (jsdom) + RTL + MSW. Test runner: `pnpm test` (single pass).
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; en/sv parity; app source double-quote+semicolon; `ui/` files untouched; token classes only. Run a single test pass.
**Spec:** `docs/superpowers/specs/2026-06-08-mutation-error-feedback-design.md`
**Key facts:**
- `HttpError`/`InUseError`/`FieldRejection` are exported from `web/src/api/queries.ts` (lines 6, 13, 20). `HttpError(status)` carries `.status`; `InUseError(count)` carries `.count`.
- `web/src/api/query-client.ts:11-23` `mutationErrorMessage(error, meta)` currently special-cases `InUseError`, `HttpError 503 → search.unavailable`, `meta.errorMessage`, else `toast.error`. The `MutationCache.onError` skips when `meta.suppressErrorToast`.
- **16 of 18 mutations already `suppressErrorToast`.** Only `useUpdateTerm` (`queries.ts:444`) and `useUpdateAuthority` (`:521`) do not — they have `meta: { successMessage: "toast.saved" }`.
- The 16 **mutation** `throw new Error("…")` sites: `queries.ts:184,203,230,248,279,306,331,382,441,458,475,492,518,535,562,579`. `useCreateObject` (`:184`) and `useCreateVocabulary` (`:279`) destructure `{ data, error }` (no `response`) — add `response`. Lines `38,73,90,105,152,169,264` are **query** fetch errors and `:121` is the login map — **do not change these**. `useSearch` (`:360`) already throws `HttpError`.
- Inline generic-error sites (all render `{X.isError && <p role="alert" className="text-xs text-destructive">{t("form.rejected")}</p>}`): `authorities-page.tsx:144-148` (`create`), `vocabulary-terms.tsx:119-123` (`addTerm`), `vocabulary-list.tsx:57-61` (`create`) + `:109-113` (`renameVocabulary`), `field-form.tsx:201-205` (`failed = isEdit ? update.isError : create.isError`, line 98). `delete-confirm-dialog.tsx:40` sets `err instanceof InUseError ? actions.inUse : form.rejected`. `object-new-page.tsx:36-39` and `object-edit-form.tsx:86-88` set `t("form.rejected")` in a catch else.
- `objects.loadError` ("Could not load objects") already exists in both locales. `term-row.tsx`/`authority-row.tsx` Edit-button `onClick` currently does `setLabels(...); setUri(...); setEditing(true);`.
- Test env: jsdom project, MSW `onUnhandledRequest:"error"`. `renderApp` (`src/test/render.tsx`) mounts `ui` at `path:"*"` in a memory router. `src/api/mutation-feedback.test.tsx` exists (tests toast wiring via `makeQueryClient` + `ToastRegion` + `renderHook`).
---
# Task 1: Shared helper + `<MutationError>` + i18n + query-client rewire
**Files:** Create `web/src/api/error-message.ts`, `web/src/api/error-message.test.ts`, `web/src/components/mutation-error.tsx`, `web/src/components/mutation-error.test.tsx`; Modify `web/src/api/query-client.ts`, `web/src/api/mutation-feedback.test.tsx`, `web/src/i18n/en.json`, `web/src/i18n/sv.json`.
- [ ] **Step 1: Add the 5 i18n keys (both locales, parity).** Add a new top-level `"errors"` object to `web/src/i18n/en.json`:
```json
"errors": {
"forbidden": "You don't have permission to do that.",
"notFound": "That item no longer exists.",
"conflict": "That conflicts with existing data.",
"validation": "Some values weren't accepted.",
"server": "The server had a problem. Please try again."
},
```
and to `web/src/i18n/sv.json`:
```json
"errors": {
"forbidden": "Du har inte behörighet att göra det.",
"notFound": "Objektet finns inte längre.",
"conflict": "Det står i konflikt med befintliga data.",
"validation": "Vissa värden godtogs inte.",
"server": "Servern hade ett problem. Försök igen."
},
```
(Valid JSON — mind commas. Place it consistently in both files, e.g. before `toast`.)
- [ ] **Step 2: Create `web/src/api/error-message.ts`:**
```ts
import { HttpError, InUseError } from "./queries";
/** Maps a caught mutation error to an i18n key (+ interpolation opts). The single
* source of truth shared by the global toast fallback and every inline display. */
export function errorMessageKey(error: unknown): { key: string; opts?: Record<string, unknown> } {
if (error instanceof InUseError) return { key: "actions.inUse", opts: { count: error.count } };
if (error instanceof HttpError) return { key: statusKey(error.status) };
return { key: "toast.error" };
}
function statusKey(status: number): string {
if (status === 403) return "errors.forbidden";
if (status === 404) return "errors.notFound";
if (status === 409) return "errors.conflict";
if (status === 422) return "errors.validation";
if (status >= 500) return "errors.server";
return "toast.error";
}
```
- [ ] **Step 3: Create `web/src/api/error-message.test.ts`** (write + run):
```ts
import { expect, test } from "vitest";
import { errorMessageKey } from "./error-message";
import { HttpError, InUseError } from "./queries";
test("maps HTTP statuses to specific keys", () => {
expect(errorMessageKey(new HttpError(403))).toEqual({ key: "errors.forbidden" });
expect(errorMessageKey(new HttpError(404))).toEqual({ key: "errors.notFound" });
expect(errorMessageKey(new HttpError(409))).toEqual({ key: "errors.conflict" });
expect(errorMessageKey(new HttpError(422))).toEqual({ key: "errors.validation" });
expect(errorMessageKey(new HttpError(500))).toEqual({ key: "errors.server" });
expect(errorMessageKey(new HttpError(502))).toEqual({ key: "errors.server" });
});
test("an unmapped status falls back to the generic key", () => {
expect(errorMessageKey(new HttpError(418))).toEqual({ key: "toast.error" });
});
test("InUseError carries the count", () => {
expect(errorMessageKey(new InUseError(3))).toEqual({ key: "actions.inUse", opts: { count: 3 } });
});
test("a bare Error or unknown maps to the generic key", () => {
expect(errorMessageKey(new Error("boom"))).toEqual({ key: "toast.error" });
expect(errorMessageKey(null)).toEqual({ key: "toast.error" });
});
```
Run: `cd web && pnpm vitest run src/api/error-message.test.ts` → 4 passing.
- [ ] **Step 4: Create `web/src/components/mutation-error.tsx`:**
```tsx
import { useTranslation } from "react-i18next";
import { errorMessageKey } from "../api/error-message";
/** Renders a caught mutation error as an inline alert, or nothing when error is falsy.
* Replaces the duplicated `<p role="alert" className="text-xs text-destructive">` markup. */
export function MutationError({ error }: { error: unknown }) {
const { t } = useTranslation();
if (!error) return null;
const { key, opts } = errorMessageKey(error);
return (
<p role="alert" className="text-xs text-destructive">
{t(key, opts)}
</p>
);
}
```
- [ ] **Step 5: Create `web/src/components/mutation-error.test.tsx`** (write + run):
```tsx
import { expect, test } from "vitest";
import { render, screen } from "@testing-library/react";
import "../i18n";
import { MutationError } from "./mutation-error";
import { HttpError, InUseError } from "../api/queries";
test("renders the status-specific message for an HttpError", () => {
render(<MutationError error={new HttpError(403)} />);
expect(screen.getByRole("alert")).toHaveTextContent(/permission/i);
});
test("renders the in-use count for an InUseError", () => {
render(<MutationError error={new InUseError(2)} />);
expect(screen.getByRole("alert")).toHaveTextContent(/2/);
});
test("renders nothing when there is no error", () => {
const { container } = render(<MutationError error={null} />);
expect(container).toBeEmptyDOMElement();
});
```
Run: `cd web && pnpm vitest run src/components/mutation-error.test.tsx` → 3 passing.
- [ ] **Step 6: Rewire `web/src/api/query-client.ts`.** Change the import line `import { HttpError, InUseError } from "./queries";` to `import { errorMessageKey } from "./error-message";`, and replace the whole `mutationErrorMessage` function body with:
```ts
function mutationErrorMessage(
error: unknown,
meta: MutationMeta | undefined,
): string {
if (meta?.errorMessage) return i18n.t(meta.errorMessage);
const { key, opts } = errorMessageKey(error);
return i18n.t(key, opts);
}
```
(Leave the `MutationCache` `onError`/`onSuccess` wiring unchanged.)
- [ ] **Step 7: Rework the obsolete toast test in `web/src/api/mutation-feedback.test.tsx`.** The "a non-suppressed mutation failing shows the catch-all error toast" test uses `useUpdateTerm`, which will suppress in Task 3 — decouple it now by driving the `MutationCache` fallback with a synthetic non-suppressed mutation. Add `useMutation` to the `@tanstack/react-query` import and `HttpError` to the queries import, then replace that single test with:
```tsx
test("a non-suppressed mutation failing shows the status-mapped error toast", async () => {
const { result, unmount } = renderHook(
() =>
useMutation({
mutationFn: async () => {
throw new HttpError(500);
},
}),
{ wrapper: makeWrapper() },
);
await expect(result.current.mutateAsync()).rejects.toThrow();
await waitFor(() => {
expect(
within(document.body).getByText(i18n.t("errors.server")),
).toBeInTheDocument();
});
unmount();
});
```
(Keep the other two tests — success toast via `useUpdateTerm`, and suppressed `useDeleteVocabulary` adds no toast — unchanged. Imports: add `useMutation`; add `HttpError` from `./queries`.)
- [ ] **Step 8: Verify (vitest ONCE for the touched files), then typecheck + lint:**
```bash
cd web && pnpm vitest run src/api/error-message.test.ts src/components/mutation-error.test.tsx src/api/mutation-feedback.test.tsx src/i18n && pnpm typecheck && pnpm lint
```
Expected: all green (helper 4, component 3, feedback 3, i18n parity covering the 5 new keys).
- [ ] **Step 9: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/api/error-message.ts web/src/api/error-message.test.ts web/src/components/mutation-error.tsx web/src/components/mutation-error.test.tsx web/src/api/query-client.ts web/src/api/mutation-feedback.test.tsx web/src/i18n/en.json web/src/i18n/sv.json
git commit -m "feat(web): shared status-aware error-message helper + MutationError component (#63)"
```
---
# Task 2: Make mutation errors load-bearing (throw `HttpError(status)`)
**Files:** Modify `web/src/api/queries.ts`.
- [ ] **Step 1: Convert the 16 mutation throws to `HttpError`.** In `web/src/api/queries.ts`, at each of these lines replace `throw new Error("…")` with `throw new HttpError(response.status)` — keeping every surrounding `InUseError` (409) and `FieldRejection` (422) branch exactly as-is:
- `:203` `useUpdateObject``if (response.status !== 204) throw new HttpError(response.status);`
- `:230` `useSetFields` — the final fallback after the 204/422 checks: `throw new HttpError(response.status);`
- `:248` `useDeleteObject``throw new HttpError(response.status);`
- `:306` `useAddTerm``if (response.status !== 201) throw new HttpError(response.status);`
- `:331` `useCreateAuthority``throw new HttpError(response.status);`
- `:382` `useCreateFieldDefinition``if (response.status !== 201 || !data) throw new HttpError(response.status);`
- `:441` `useUpdateTerm``throw new HttpError(response.status);`
- `:458` `useDeleteTerm` — the post-409 fallback: `if (response.status !== 204) throw new HttpError(response.status);`
- `:475` `useRenameVocabulary``throw new HttpError(response.status);`
- `:492` `useDeleteVocabulary` — post-409 fallback: `throw new HttpError(response.status);`
- `:518` `useUpdateAuthority``throw new HttpError(response.status);`
- `:535` `useDeleteAuthority` — post-409 fallback: `throw new HttpError(response.status);`
- `:562` `useUpdateFieldDefinition``throw new HttpError(response.status);`
- `:579` `useDeleteFieldDefinition` — post-409 fallback: `throw new HttpError(response.status);`
- [ ] **Step 2: The two POSTs that don't destructure `response`.** For `useCreateObject` (`:184`) and `useCreateVocabulary` (`:279`), change `const { data, error } = await api.POST(...)` to `const { data, error, response } = await api.POST(...)` and replace `if (error || !data) throw new Error("…")` with `if (error || !data) throw new HttpError(response.status);`.
- [ ] **Step 3: Do NOT touch** the query fetch errors (`:38,73,90,105,152,169,264`) or the login map (`:121`) — they keep `throw new Error(...)` (consumed by component `isError`/login mapping, not the toast). `HttpError` is already imported in this file (it's defined here), so no import change.
- [ ] **Step 4: Verify (vitest ONCE), typecheck, lint:**
```bash
cd web && pnpm vitest run src/api/queries.test.ts src/api/mutation-feedback.test.tsx && pnpm typecheck && pnpm lint
```
Expected: green. `HttpError` extends `Error`, so any `.rejects.toThrow()` assertions still pass (no test asserts the old message strings). If `queries.test.ts` asserts a specific thrown type/message that now differs, update it to assert `HttpError`/status (do not weaken).
- [ ] **Step 5: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/api/queries.ts
git commit -m "feat(web): mutations throw HttpError(status) so failures are status-aware (#63)"
```
---
# Task 3: Edit-row consistency + delete-dialog adoption
**Files:** Modify `web/src/api/queries.ts`, `web/src/vocab/term-row.tsx`, `web/src/authorities/authority-row.tsx`, `web/src/components/delete-confirm-dialog.tsx`, and their tests / `web/src/api/mutation-feedback.test.tsx` consumers.
- [ ] **Step 1: Suppress the two update toasts.** In `web/src/api/queries.ts`, change `useUpdateTerm`'s meta (`:444`) from `meta: { successMessage: "toast.saved" }` to `meta: { successMessage: "toast.saved", suppressErrorToast: true }`, and the same for `useUpdateAuthority` (`:521`).
- [ ] **Step 2: Inline error + reset in `web/src/vocab/term-row.tsx`.** Add the import `import { MutationError } from "../components/mutation-error";`. In the editing view, add `<MutationError error={updateTerm.error} />` immediately after the save/cancel `<div className="flex gap-2">…</div>` (still inside the `<li>`). In the non-editing view's Edit `<Button>` `onClick`, add `updateTerm.reset();` as the first statement (before `setLabels(...)`):
```tsx
onClick={() => {
updateTerm.reset();
setLabels(term.labels as LabelInput[]);
setUri(term.external_uri ?? "");
setEditing(true);
}}
```
- [ ] **Step 2b: Same for `web/src/authorities/authority-row.tsx`.** Import `MutationError`; add `<MutationError error={updateAuthority.error} />` after the save/cancel `<div className="flex gap-2">…</div>`; add `updateAuthority.reset();` as the first statement in the Edit button's `onClick`.
- [ ] **Step 3: Status-aware delete dialog (`web/src/components/delete-confirm-dialog.tsx`).** Add `import { errorMessageKey } from "../api/error-message";`, remove the now-unused `import { InUseError } from "../api/queries";`, and change the `catch` block (lines ~37-41) from the `err instanceof InUseError ? … : t("form.rejected")` line to:
```tsx
} catch (err) {
// Keep the dialog open; show the blocking reason. Never let the rejected
// mutation escape as an unhandled rejection.
const { key, opts } = errorMessageKey(err);
setMessage(t(key, opts));
return;
}
```
(`errorMessageKey` already maps `InUseError``actions.inUse` with the count, so the in-use message is preserved and a 403/404 delete now shows a specific message.)
- [ ] **Step 4: Tests.** Add a failing-update test to `web/src/vocab/term-row.tsx`'s area — create `web/src/vocab/term-row.test.tsx` if absent (check first), else extend. Render a `TermRow` inside `renderApp` with the `makeQueryClient` toast wrapper is overkill — use the existing `renderApp` and MSW. Concretely:
```tsx
import { expect, test } from "vitest";
import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw";
import { server } from "../test/server";
import { renderApp } from "../test/render";
import { TermRow } from "./term-row";
const term = { id: "t1", external_uri: null, labels: [{ lang: "en", label: "Bronze" }] };
test("a failed term update shows an inline error and keeps the row editable", async () => {
server.use(
http.patch("/api/admin/vocabularies/:id/terms/:term_id", () => new HttpResponse(null, { status: 403 })),
);
renderApp(<ul><TermRow vocabularyId="v1" term={term as never} lang="en" /></ul>);
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByRole("alert")).toHaveTextContent(/permission/i);
// still editable: the save button is still present (editor did not close)
expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();
});
test("re-entering edit after a failure clears the stale error", async () => {
server.use(
http.patch("/api/admin/vocabularies/:id/terms/:term_id", () => new HttpResponse(null, { status: 403 })),
);
renderApp(<ul><TermRow vocabularyId="v1" term={term as never} lang="en" /></ul>);
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByRole("alert")).toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: /cancel/i }));
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
expect(screen.queryByRole("alert")).toBeNull();
});
```
(Adjust the `lang`/`term` cast to match `TermView`. If a `term-row.test.tsx` already exists, append these and keep existing tests green. The `mutation-feedback.test.tsx` test #1`useUpdateTerm` success → `toast.saved` — still passes since suppress only affects errors; confirm it stays green.)
- [ ] **Step 5: Verify (vitest ONCE):**
```bash
cd web && pnpm vitest run src/vocab/term-row.test.tsx src/authorities src/components/delete-confirm-dialog.test.tsx src/api/mutation-feedback.test.tsx && pnpm typecheck && pnpm lint
```
Expected: green (new row tests pass; delete-dialog story/test still green; authority tests green). If `delete-confirm-dialog.test.tsx` doesn't exist, drop it from the command.
- [ ] **Step 6: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/api/queries.ts web/src/vocab/term-row.tsx web/src/authorities/authority-row.tsx web/src/components/delete-confirm-dialog.tsx web/src/vocab/term-row.test.tsx
git commit -m "feat(web): inline status-aware errors on term/authority edit rows + delete dialog (#63)"
```
---
# Task 4: Create/rename/object form adoption + fetch fix + full gate
**Files:** Modify `web/src/authorities/authorities-page.tsx`, `web/src/vocab/vocabulary-terms.tsx`, `web/src/vocab/vocabulary-list.tsx`, `web/src/fields/field-form.tsx`, `web/src/objects/object-new-page.tsx`, `web/src/objects/object-edit-form.tsx` (+ a test).
- [ ] **Step 1: Adopt `<MutationError>` at the create/rename inline sites.** In each file, add `import { MutationError } from "../components/mutation-error";` (path `../components/mutation-error` from these dirs) and replace the `{X.isError && <p role="alert" className="text-xs text-destructive">{t("form.rejected")}</p>}` block with `<MutationError error={X.error} />`:
- `authorities-page.tsx:144-148``<MutationError error={create.error} />`
- `vocabulary-terms.tsx:119-123``<MutationError error={addTerm.error} />`
- `vocabulary-list.tsx:57-61``<MutationError error={create.error} />`; and `:109-113``<MutationError error={renameVocabulary.error} />`
- `field-form.tsx:201-205``<MutationError error={isEdit ? update.error : create.error} />`; then delete the now-unused `const failed = isEdit ? update.isError : create.isError;` (`:98`). Keep `const pending = …` and the `{error && …form.required}` validation block.
- [ ] **Step 2: Object create/edit catch-else via the helper.** In `web/src/objects/object-new-page.tsx`, add `import { errorMessageKey } from "../api/error-message";` and change the create catch (`:36-39`) from `} catch { setError(t("form.rejected")); return false; }` to:
```tsx
} catch (e) {
const { key, opts } = errorMessageKey(e);
setError(t(key, opts));
return false;
}
```
In `web/src/objects/object-edit-form.tsx`, add the same import and change the non-`FieldRejection` else (`:86-88`) from `setError(t("form.rejected"));` to:
```tsx
} else {
const { key, opts } = errorMessageKey(e);
setError(t(key, opts));
}
```
- [ ] **Step 3: Fetch-error fix in `web/src/objects/object-edit-form.tsx`.** Change `const { data: object, isLoading } = useObject(id!);` to `const { data: object, isLoading, isError } = useObject(id!);` and insert, between the `isLoading` and `!object` guards:
```tsx
if (isError) return <p className="p-4 text-sm text-destructive">{t("objects.loadError")}</p>;
```
- [ ] **Step 4: Test the fetch fix + one create-form adoption.** Add to `web/src/objects/object-edit-form.test.tsx` (create if absent):
```tsx
test("renders a load error (not 'not found') when the object fetch fails", async () => {
server.use(http.get("/api/admin/objects/:id", () => new HttpResponse(null, { status: 500 })));
renderApp(<Routes><Route path="/objects/:id/edit" element={<ObjectEditForm />} /></Routes>, {
route: "/objects/abc/edit",
});
expect(await screen.findByText(/could not load/i)).toBeInTheDocument();
expect(screen.queryByText(/not found/i)).toBeNull();
});
```
(Use the file's existing imports/harness; add `http`/`HttpResponse` from `msw`, `Routes`/`Route`, `server`, `renderApp`, `ObjectEditForm` as needed. If the file exists, append and keep its tests green.) Also add to `web/src/authorities/authorities-page.test.tsx` (or the nearest existing authorities test) a case asserting a failed create shows the status message:
```tsx
test("a failed create shows a status-aware inline error", async () => {
server.use(http.post("/api/admin/authorities", () => new HttpResponse(null, { status: 403 })));
// …render the authorities page, fill a label, submit…
expect(await screen.findByText(/permission/i)).toBeInTheDocument();
});
```
(Wire the render/fill/submit to match the existing authorities test harness; if that harness isn't readily reusable, cover the create-error path through `field-form` or skip this second assertion — the `MutationError` component test already proves the rendering, and `error-message.test.ts` proves the mapping. Do NOT add a brittle test just to hit a number.)
- [ ] **Step 5: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk (gz), and the `check:colors` line.
- [ ] **Step 6: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 7: Manual smoke (recommended).** With the stack + server + `pnpm dev`: cause a failing save (e.g. stop the server, or a 409 on a duplicate) on a term/authority edit row → inline status message appears at the row, row stays editable; a failed create shows the inline message; loading an edit form whose object 500s shows "Could not load objects" not "not found."
- [ ] **Step 8: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/authorities/authorities-page.tsx web/src/vocab/vocabulary-terms.tsx web/src/vocab/vocabulary-list.tsx web/src/fields/field-form.tsx web/src/objects/object-new-page.tsx web/src/objects/object-edit-form.tsx web/src/objects/object-edit-form.test.tsx web/src/authorities/authorities-page.test.tsx
git commit -m "feat(web): adopt MutationError across create/object forms; distinguish edit-form fetch error (#63)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 `errorMessageKey` + unit test (T1 S2-S3); AC2 `<MutationError>` + adoption at delete-dialog (T3 S3), create/rename forms (T4 S1), object-form `formError` via helper (T4 S2), edit rows (T3 S2); AC3 16 throws → `HttpError` + query-client rewire (T2, T1 S6); AC4 update suppress + inline + reset (T3 S1-S2); AC5 edit-form fetch error (T4 S3); AC6 gate + parity + codename (T4 S5-S6, T1 S1). ✓
**Placeholder scan:** every code step shows complete code; tests have concrete assertions and exact statuses (403/500); the one soft spot (T4 S4 second assertion) is explicitly bounded with "don't add a brittle test to hit a number," and the mapping/rendering are already proven by T1's two test files. No TODO/TBD. ✓
**Type/consistency:** `errorMessageKey(error: unknown): { key: string; opts? }` defined in T1, consumed identically by `query-client.ts`, `MutationError`, `delete-confirm-dialog`, `object-new-page`, `object-edit-form`. `HttpError(status)` (T2) feeds `errorMessageKey`'s `instanceof HttpError` branch. `suppressErrorToast` added (T3 S1) matches the sibling mutations' meta shape. `objects.loadError` pre-exists. ✓
## Notes
- No new dependency. `ui/*` untouched. en/sv parity preserved (5 new `errors.*` keys, guarded by the #60 parity test).
- After Task 2 (before Task 3), `useUpdateTerm`/`useUpdateAuthority` are still non-suppressed and now throw `HttpError`, so a failed update shows a status-mapped **toast** — a transient mid-milestone state; Task 3 moves it inline. Each commit is independently green.
- Error classes stay in `queries.ts`; relocating them to `api/errors.ts` is tracked in #65.
@@ -0,0 +1,295 @@
# Reference-Data Scannability + Parity — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Make the three reference-data lists scannable (locale-aware sort + filter), show `external_uri` in read rows, add field-group count badges, and close the small parity/validation gaps — no layout/edit-modality change, no backend change.
**Architecture:** A shared `lib/sort.ts` (memoized `Intl.Collator` comparators) + a tiny `ExternalUriLink` are consumed by the vocab/authorities/fields list components. Each list gets a client-side filter `useState` + `<Input>`; rows are filtered then sorted before render.
**Tech Stack:** React 19 + TS + pnpm, react-i18next, Vitest + RTL + MSW. Test runner: `pnpm test` (single pass).
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; en/sv parity (3 new keys); app source double-quote+semicolon; token classes only; **don't mutate query-cache arrays — sort a copy** (`[...list].sort(...)`).
**Spec:** `docs/superpowers/specs/2026-06-08-refdata-scannability-design.md`
**Key facts (from the code):**
- `labelText(labels, lang)` in `lib/labels.ts`. `lang = i18n.language.startsWith("sv") ? "sv" : "en"`.
- term/authority view = `{ id, labels, external_uri?: string|null }` (authority also `kind`); vocabulary = `{ id, key }`; field-def = `{ key, labels, data_type, group?, required, … }`.
- `term-row.tsx`/`authority-row.tsx`: read mode `<span className="flex-1">{labelText(...)}</span>` + Edit + DeleteConfirmDialog; edit mode has the `external_uri` `<Input>` (no `type`/placeholder).
- `vocabulary-list.tsx`: create form (has empty-guard) + list; rename `<form onSubmit>` calls `renameVocabulary.mutate({ id, key: draftKey.trim() })` with NO empty-guard.
- `authorities-page.tsx`: tabs + list + create form; create calls `create.mutate({ kind, external_uri: null, labels })` — hardcoded null, no URI field.
- `field-list.tsx`: groups built into a `Map`; sorted with `t("fields.other")` last (no AZ); group header `<div className="… label-caption">{group}</div>`; rows show label+key+type+required.
- `common` i18n: `{ yes, no, close, loading }`. `labels`: `{ label, externalUri, otherLanguages }`.
---
# Task 1: Shared `sort.ts` + `ExternalUriLink` + i18n + unit test
**Files:** `web/src/lib/sort.ts` (new), `web/src/lib/sort.test.ts` (new), `web/src/components/external-uri-link.tsx` (new), `web/src/i18n/en.json`, `web/src/i18n/sv.json`.
- [ ] **Step 1: i18n (both locales, parity).**
- `common`: add `"filter"` (en "Filter…" / sv "Filtrera…") and `"noMatches"` (en "No matches" / sv "Inga träffar").
- `labels`: add `"uriPlaceholder": "https://…"` (same string in both).
- [ ] **Step 2: `web/src/lib/sort.ts`:**
```ts
import type { components } from "../api/schema";
import { labelText } from "./labels";
type LabelView = components["schemas"]["LabelView"];
const collators = new Map<string, Intl.Collator>();
function collatorFor(lang: string): Intl.Collator {
let c = collators.get(lang);
if (!c) {
c = new Intl.Collator(lang, { sensitivity: "base", numeric: true });
collators.set(lang, c);
}
return c;
}
export function compareStrings(lang: string, a: string, b: string): number {
return collatorFor(lang).compare(a, b);
}
export function byLabel(lang: string) {
return (a: { labels: LabelView[] }, b: { labels: LabelView[] }) =>
compareStrings(lang, labelText(a.labels, lang), labelText(b.labels, lang));
}
export function byKey(lang: string) {
return (a: { key: string }, b: { key: string }) => compareStrings(lang, a.key, b.key);
}
```
- [ ] **Step 3: `web/src/lib/sort.test.ts`** (write failing first, then it passes with Step 2):
```ts
import { expect, test } from "vitest";
import { byKey, byLabel, compareStrings } from "./sort";
const L = (label: string) => ({ labels: [{ lang: "en", label }] });
test("byLabel sorts case-insensitively and locale-aware", () => {
const sorted = [L("Iron"), L("bronze"), L("Amber")].sort(byLabel("en")).map((x) => x.labels[0].label);
expect(sorted).toEqual(["Amber", "bronze", "Iron"]);
});
test("byKey sorts keys with numeric awareness", () => {
const sorted = [{ key: "item10" }, { key: "item2" }, { key: "item1" }].sort(byKey("en")).map((x) => x.key);
expect(sorted).toEqual(["item1", "item2", "item10"]);
});
test("compareStrings is case-insensitive", () => {
expect(compareStrings("en", "bronze", "BRONZE")).toBe(0);
});
```
Run: `cd web && pnpm vitest run src/lib/sort.test.ts` → PASS (3 tests).
- [ ] **Step 4: `web/src/components/external-uri-link.tsx`** (app-source style):
```tsx
export function ExternalUriLink({ uri }: { uri: string }) {
return (
<a
href={uri}
target="_blank"
rel="noopener noreferrer"
className="block truncate text-xs text-muted-foreground hover:text-foreground"
>
{uri}
</a>
);
}
```
- [ ] **Step 5: Verify (vitest ONCE):** `cd web && pnpm vitest run src/lib/sort.test.ts && pnpm typecheck && pnpm lint`. PASS, clean.
- [ ] **Step 6: Commit**
```bash
git add web/src/lib/sort.ts web/src/lib/sort.test.ts web/src/components/external-uri-link.tsx web/src/i18n/en.json web/src/i18n/sv.json
git commit -m "feat(web): collator sort helpers + ExternalUriLink + filter/uri i18n (#50)"
```
---
# Task 2: Vocabularies — filter + sort + URI + rename guard
**Files:** `web/src/vocab/vocabulary-list.tsx`, `web/src/vocab/vocabulary-terms.tsx`, `web/src/vocab/term-row.tsx`, `web/src/vocab/vocabularies.test.tsx`.
- [ ] **Step 1: `vocabulary-list.tsx`** — add `i18n` to `useTranslation` (`const { t, i18n } = …`) and `const lang = i18n.language.startsWith("sv") ? "sv" : "en";`; import `byKey` from `../lib/sort` and `Input` is already imported. Add `const [filter, setFilter] = useState("");`.
- Add a filter `<Input>` between the create `<form>` and the list block:
```tsx
<div className="border-b p-2">
<Input
aria-label={t("common.filter")}
placeholder={t("common.filter")}
value={filter}
onChange={(e) => setFilter(e.target.value)}
/>
</div>
```
- In the loaded `<ul>`, replace `{data?.map((v) => (…` with a filtered+sorted list:
```tsx
{(() => {
const q = filter.trim().toLowerCase();
const rows = [...(data ?? [])]
.filter((v) => !q || v.key.toLowerCase().includes(q))
.sort(byKey(lang));
if (data && data.length > 0 && rows.length === 0)
return <li className="p-3 text-sm text-muted-foreground">{t("common.noMatches")}</li>;
return rows.map((v) => (/* the EXISTING <li> row markup, unchanged */));
})()}
```
Keep the `isError`/empty(`data?.length === 0`) branches as-is. (Prefer a clean inline: compute `rows` before the `return`, but the IIFE keeps it local — implementer may hoist `const rows = …` above the JSX instead, whichever is cleaner and lint-clean.)
- **Rename empty-guard:** in the rename `<form onSubmit>`, add a guard before mutate:
```tsx
onSubmit={(e) => {
e.preventDefault();
if (!draftKey.trim()) return;
renameVocabulary.mutate({ id: v.id, key: draftKey.trim() }, { onSuccess: () => setEditingId(null) });
}}
```
- [ ] **Step 2: `vocabulary-terms.tsx`** — add `const [filter, setFilter] = useState("");`; import `byLabel` from `../lib/sort`. Add a filter `<Input>` above the terms list (the component already has `Input` imported; place it above the `{isLoading ? … : <ul>}` block). In the `<ul>`, replace `{terms?.map((term) => …}` with filtered (`labelText(term.labels, lang).toLowerCase().includes(q)`) + `.sort(byLabel(lang))`; filtered-empty (terms exist) → `common.noMatches` `<li>`. Add `type="url"` + `placeholder={t("labels.uriPlaceholder")}` to the add-term `external_uri` `<Input id="term-uri">`.
- [ ] **Step 3: `term-row.tsx`** — import `ExternalUriLink` from `../components/external-uri-link`. In read mode, wrap the label + URI in a flex-1 column so the URI shows under the label:
```tsx
<div className="flex-1">
<div>{labelText(term.labels, lang)}</div>
{term.external_uri && <ExternalUriLink uri={term.external_uri} />}
</div>
```
(Replace the existing `<span className="flex-1">{labelText(...)}</span>`; keep the Edit + DeleteConfirmDialog.) Add `type="url"` + `placeholder={t("labels.uriPlaceholder")}` to the edit-mode `external_uri` `<Input>`.
- [ ] **Step 4: Tests** (`vocabularies.test.tsx`) — extend (mirror the existing MSW/`tree()` setup; check the term/vocab fixtures for labels to assert order):
- vocabularies render **sorted by key** (seed/confirm the fixture has out-of-order keys; assert DOM order).
- typing in the vocab **filter** narrows the list (type a substring → only matching vocab shows).
- **rename with an empty key** does NOT call the rename endpoint (override the PATCH/PUT handler with a spy; clear the rename input and submit; assert no request).
- a **term read row shows its `external_uri`** as a link (seed a term fixture with `external_uri`; assert `getByRole("link", { name: /<uri>/ })`). (If the term fixture lacks a URI, add one — check `materialTerms` in `web/src/test/fixtures.ts`.)
Keep existing vocab tests green. Don't weaken.
- [ ] **Step 5: Verify (vitest ONCE):** `cd web && pnpm vitest run src/vocab && pnpm typecheck && pnpm lint`. PASS.
- [ ] **Step 6: Commit**
```bash
git add web/src/vocab/vocabulary-list.tsx web/src/vocab/vocabulary-terms.tsx web/src/vocab/term-row.tsx web/src/vocab/vocabularies.test.tsx
git commit -m "feat(web): vocab list/terms sort+filter, external_uri in rows, rename guard, url input (#50)"
```
---
# Task 3: Authorities — filter + sort + create URI + read URI
**Files:** `web/src/authorities/authorities-page.tsx`, `web/src/authorities/authority-row.tsx`, `web/src/authorities/authorities.test.tsx`.
- [ ] **Step 1: `authorities-page.tsx`** — add `const [filter, setFilter] = useState("");` and `const [uri, setUri] = useState("");`; import `byLabel` from `../lib/sort` and `Input`/`Label` (`Input` may need importing — check; `Button` already imported).
- **Filter `<Input>`** above the list block (below the tablist):
```tsx
<div className="mb-3">
<Input aria-label={t("common.filter")} placeholder={t("common.filter")} value={filter} onChange={(e) => setFilter(e.target.value)} />
</div>
```
- In the loaded `<ul>`, replace `{authorities?.map((a) => …}` with filtered (`labelText(a.labels, lang).toLowerCase().includes(q)`) + `.sort(byLabel(lang))`; filtered-empty (authorities exist) → `common.noMatches` `<li>`.
- **Create form gains an `external_uri` field** (after the `<LabelEditor>`):
```tsx
<div className="space-y-1">
<Label htmlFor="auth-create-uri">{t("labels.externalUri")}</Label>
<Input id="auth-create-uri" type="url" placeholder={t("labels.uriPlaceholder")} value={uri} onChange={(e) => setUri(e.target.value)} />
</div>
```
- In `onCreate`, send the URI and reset it:
```tsx
create.mutate(
{ kind: kind as string, external_uri: uri.trim() || null, labels },
{ onSuccess: () => { setLabels([]); setUri(""); } },
);
```
- [ ] **Step 2: `authority-row.tsx`** — same as term-row: import `ExternalUriLink`; read mode shows label + `ExternalUriLink` (when `authority.external_uri`) in a `flex-1` column; edit `external_uri` `<Input>` gets `type="url"` + `placeholder={t("labels.uriPlaceholder")}`.
- [ ] **Step 3: Tests** (`authorities.test.tsx`) — extend (mirror existing setup; check `personAuthorities` fixture):
- list **sorted by label** (assert DOM order; ensure fixture has out-of-order labels or seed it).
- **filter** narrows the list.
- the **create form has an `external_uri` field** and a created authority **posts the entered URI** (spy the POST body; type a URI; assert `body.external_uri`).
- a **read row shows the `external_uri`** link (seed a fixture authority with a URI).
Keep existing authorities tests green (tabs, aria-selected, create-without-label alert, 500, redirect).
- [ ] **Step 4: Verify (vitest ONCE):** `cd web && pnpm vitest run src/authorities && pnpm typecheck && pnpm lint`. PASS.
- [ ] **Step 5: Commit**
```bash
git add web/src/authorities/authorities-page.tsx web/src/authorities/authority-row.tsx web/src/authorities/authorities.test.tsx
git commit -m "feat(web): authorities sort+filter, create external_uri, external_uri in rows, url input (#50)"
```
---
# Task 4: Fields — filter + within-group sort + group order + count badges + gate
**Files:** `web/src/fields/field-list.tsx`, `web/src/fields/fields.test.tsx`.
- [ ] **Step 1: `field-list.tsx`** — import `byLabel` and `compareStrings` from `../lib/sort` and `Badge` from `@/components/ui/badge`. Add `const [filter, setFilter] = useState("");` (add `useState` to the React import). After the early returns, before grouping:
- Filter `data` by label/key: `const q = filter.trim().toLowerCase(); const filtered = (data ?? []).filter((d) => !q || labelText(d.labels, lang).toLowerCase().includes(q) || d.key.toLowerCase().includes(q));`
- Build the `groups` Map from `filtered` (not `data`).
- **Sort group entries** named AZ (collator), `t("fields.other")` last:
```tsx
const otherLabel = t("fields.other");
const entries = [...groups.entries()].sort((a, b) => {
if (a[0] === otherLabel) return 1;
if (b[0] === otherLabel) return -1;
return compareStrings(lang, a[0], b[0]);
});
```
- **Sort each group's defs** by `byLabel(lang)`: when rendering `defs.map`, use `[...defs].sort(byLabel(lang)).map(...)`.
- **Count badge** in each group header:
```tsx
<div className="flex items-center justify-between border-b bg-muted px-3 py-1 label-caption">
<span>{group}</span>
<Badge variant="secondary">{defs.length}</Badge>
</div>
```
- Render a **filter `<Input>`** above the `<ul>` (wrap the return in a fragment/column): a `<div className="border-b p-2"><Input aria-label={t("common.filter")} placeholder={t("common.filter")} value={filter} onChange={(e) => setFilter(e.target.value)} /></div>` then the `<ul>`. If `filtered.length === 0` (data exists), show `<p className="p-3 text-sm text-muted-foreground">{t("common.noMatches")}</p>` instead of the `<ul>`. (Import `Input` from `@/components/ui/input`.)
Note: the filter Input must remain visible during the empty-filter state so the user can clear it.
- [ ] **Step 2: Tests** (`fields.test.tsx`) — extend (mirror existing setup; `fieldDefinitions` fixture):
- fields render **sorted within their group by label** (assert relative DOM order of two fields in the same group).
- a **group header shows a count badge** (assert the count number is present near a group label).
- typing in the **filter** narrows the visible fields (and/or shows `common.noMatches` when nothing matches).
Keep the existing fields tests green (grouped list, create text field, reveal pickers).
- [ ] **Step 3: FULL GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk (KB gz), check:colors line.
- [ ] **Step 4: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
- [ ] **Step 5: Manual smoke (recommended).** `pnpm dev`: each reference list is alphabetized and has a working filter; term/authority rows show their external_uri as a muted link; field groups show counts; the authority create form has a URI field; renaming a vocab to empty does nothing; URI inputs are `type=url` with a placeholder.
- [ ] **Step 6: Commit**
```bash
git add web/src/fields/field-list.tsx web/src/fields/fields.test.tsx
git commit -m "feat(web): field-list filter, within-group label sort, group order, count badges (#50)"
```
---
## Self-Review (completed)
**Spec coverage:** sort.ts collator + byLabel/byKey + unit test (T1); ExternalUriLink + i18n (T1); vocab list/terms sort+filter + rename guard + read URI + url input (T2); authorities sort+filter + create URI + read URI + url input (T3); fields filter + within-group sort + group AZ + count badges (T4); gate (T4). Acceptance criteria 15 mapped. ✓
**Placeholder scan:** the list-rewrite steps say "the EXISTING row markup, unchanged" with the files quoted, and tests say "check the fixture / seed a URI" with the fixture files named — concrete, not vague. The IIFE-vs-hoist choice is explicitly the implementer's (both lint-clean). No TODOs. ✓
**Type/consistency:** `byLabel(lang)`/`byKey(lang)`/`compareStrings(lang,a,b)` defined in T1, consumed in T2/T3/T4; `ExternalUriLink({uri})` used in term-row + authority-row; the filter `useState`/predicate pattern is uniform across the four lists; `[...arr].sort(...)` (copy, never mutate cache) everywhere. ✓
## Notes
- No new dependency; 3 new i18n keys (`common.filter`, `common.noMatches`, `labels.uriPlaceholder`), en+sv.
- Counts: only field-group counts (client-side). Per-vocab term & per-kind authority counts need backend → follow-up.
- Always sort a COPY of the react-query data (never mutate the cached array).
@@ -0,0 +1,140 @@
# Search-Result Date Meta + Estimated-Count Copy — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Thread `recording_date` through the search index/hit/view (Rust), show it on the result row, and soften the estimated-count copy.
**Architecture:** Backend adds `recording_date: Option<String>` (`YYYY-MM-DD`) to `SearchDocument` (projected in `build_document`), `SearchHit` (mapped in `search_objects`), and `SearchHitView` (API). The frontend type comes from the regenerated/edited `schema.d.ts`; the row renders the date and the count copy gains a `~`.
**Tech Stack:** Rust (search + api crates, Meilisearch, utoipa), React + TS + pnpm. Tests need the docker stack (Postgres :5442, Meilisearch :7700 — already up). Rust test runner: `cargo nextest`. Frontend: `pnpm test`.
**Conventions:** `cargo +nightly fmt` + `cargo clippy --workspace --all-targets -- -D warnings` for Rust; pnpm + no `any`/`eslint-disable` + en/sv parity for web; no codename; manage deps via cargo-mcp if any (none here). Run a single test pass.
**Spec:** `docs/superpowers/specs/2026-06-08-search-row-date-design.md`
**Key facts:**
- `crates/search/src/lib.rs`: `SearchDocument` (`:30`), `SearchHit` (`:52`), `build_document` (`:302`, returns the `SearchDocument { … }` literal around `:363`), `search_objects` hit map (`~:220`). `domain::Date` Display = ISO `YYYY-MM-DD`; `CatalogueObject.recording_date: Option<Date>`.
- `crates/api/src/admin_search.rs`: `SearchHitView` (`:26`) + map closure (`:100`).
- `crates/search/tests/search.rs`: a `doc(...)` helper builds a `SearchDocument` literal; `search_objects_returns_hits_…` (`:55`) seeds 3 docs + asserts. `tests/sync.rs`/`tests/reindex.rs` construct `CatalogueObject` (already has `recording_date` — unaffected) but may also build `SearchDocument` — check.
- `web/src/api/schema.d.ts` `SearchHitView` block (`:601`): keys are alphabetized — insert `recording_date?: string | null;` between `object_number` and `snippet`.
- `web/src/search/search-result-row.tsx`: meta line `<span>{hit.object_number}</span> <VisibilityBadge/>`.
- i18n `search.resultCount_one/_other`; `web/src/test/fixtures.ts` `searchHits`; `web/src/search/search.test.tsx` asserts `/25 results/i`.
---
# Task 1: Backend — `recording_date` through index → hit → view (+ schema)
**Files:** `crates/search/src/lib.rs`, `crates/api/src/admin_search.rs`, `crates/search/tests/search.rs` (+ any test with a `SearchDocument`/`SearchHit` literal), `web/src/api/schema.d.ts`.
- [ ] **Step 1: `SearchDocument` + `build_document`** (`crates/search/src/lib.rs`):
- Add `pub recording_date: Option<String>,` to `SearchDocument` (after `recorder`, before `visibility` — keep a sensible order).
- In the `Ok(SearchDocument { … })` literal at the end of `build_document`, add `recording_date: object.recording_date.map(|d| d.to_string()),`.
- [ ] **Step 2: `SearchHit` + `search_objects` mapping** (`crates/search/src/lib.rs`):
- Add `pub recording_date: Option<String>,` to `SearchHit` (after `visibility`, before `snippet` or near it).
- In `search_objects`, the `SearchHit { id, object_number, object_name, brief_description, visibility, snippet }` literal: add `recording_date: doc.recording_date,`.
- [ ] **Step 3: `SearchHitView`** (`crates/api/src/admin_search.rs`):
- Add `pub recording_date: Option<String>,` to `SearchHitView`.
- In the `.map(|h| SearchHitView { … })` closure: add `recording_date: h.recording_date,`.
- [ ] **Step 4: Fix test literals + extend the search test** (`crates/search/tests/search.rs`):
- The `doc(...)` helper builds a `SearchDocument` literal — add `recording_date: None,` to it (compile fix). Grep the search + api test dirs for other `SearchDocument {`/`SearchHit {` literals and add `recording_date: None` where needed (object fixtures that build `CatalogueObject` are unaffected — they already have `recording_date`).
- In `search_objects_returns_hits_with_highlight_filter_and_paging`, set a date on one seeded doc before indexing: `bronze_a.recording_date = Some("1962-04-03".to_string());` and after the existing `hit` lookup assert: `assert_eq!(hit.recording_date.as_deref(), Some("1962-04-03"));`.
- [ ] **Step 5: Build + lint + test (stack is up).**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
cargo build --workspace
cargo +nightly fmt
cargo clippy --workspace --all-targets -- -D warnings
cargo nextest run -p search -p api
```
Expected: compiles, fmt clean, clippy clean, tests pass (incl. the new `recording_date` assertion). Run the test suite ONCE (single Postgres — no concurrent cargo test runs). If clippy/fmt change files, include them.
- [ ] **Step 6: Update `web/src/api/schema.d.ts`** — add the field to the `SearchHitView` block (alphabetical, between `object_number` and `snippet`):
```ts
SearchHitView: {
brief_description?: string | null;
id: string;
object_name: string;
object_number: string;
recording_date?: string | null;
snippet?: string | null;
visibility: components["schemas"]["Visibility"];
};
```
(This mirrors what `pnpm gen:api` would emit now that the backend returns the field; a later regen reproduces it identically. Optionally verify with `pnpm gen:api` if a server is running — not required.)
- [ ] **Step 7: Commit**
```bash
git add crates/search/src/lib.rs crates/api/src/admin_search.rs crates/search/tests/search.rs web/src/api/schema.d.ts
# (+ any other test files you fixed)
git commit -m "feat(search): index + return recording_date on search hits (#61)"
```
---
# Task 2: Frontend — row date + softened count + tests + gate
**Files:** `web/src/search/search-result-row.tsx`, `web/src/i18n/en.json`, `web/src/i18n/sv.json`, `web/src/test/fixtures.ts`, `web/src/search/search.test.tsx`.
- [ ] **Step 1: `search-result-row.tsx`** — show the date on the meta line when present:
```tsx
<div className="mt-0.5 flex items-center gap-2 text-xs text-muted-foreground">
<span>{hit.object_number}</span>
{hit.recording_date && <span>· {hit.recording_date}</span>}
<VisibilityBadge visibility={hit.visibility} />
</div>
```
(Render the `YYYY-MM-DD` string verbatim — it's a recording date, not a UTC timestamp.)
- [ ] **Step 2: i18n — soften the count** (both locales, parity; the `#60` parity test guards this):
- en.json `search`: `"resultCount_one": "~{{count}} result"`, `"resultCount_other": "~{{count}} results"`.
- sv.json `search`: `"resultCount_one": "~{{count}} träff"`, `"resultCount_other": "~{{count}} träffar"`.
(No code change in `search-panel.tsx` — the key already interpolates `count`.)
- [ ] **Step 3: Fixtures** (`web/src/test/fixtures.ts`) — add `recording_date` to the first `searchHits` entry:
`recording_date: "1962-04-03",` (the generated rest can be `recording_date: null` or omit it — it's optional; set `null` on the mapped `Array.from(...)` items to satisfy the type cleanly).
- [ ] **Step 4: Tests** (`web/src/search/search.test.tsx`):
- Assert the first result row shows the date: after results render, `expect(screen.getByText("1962-04-03")).toBeInTheDocument();` (or scope within the row).
- Tighten the count assertion to require the `~`: change `getByText(/25 results/i)``getByText(/~\s*25 results/i)` (or assert `getByText(/~25 results/i)`).
Keep the existing load-more + visibility-filter tests green.
- [ ] **Step 5: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk, check:colors line.
- [ ] **Step 6: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src crates; echo "codename-exit=$?"
git status --short
```
- [ ] **Step 7: Manual smoke (recommended).** With the stack + server + web dev up: search the collection — result rows show the recording date (for objects that have one and have been (re)indexed); the count reads "~N results".
- [ ] **Step 8: Commit**
```bash
git add web/src/search/search-result-row.tsx web/src/i18n/en.json web/src/i18n/sv.json web/src/test/fixtures.ts web/src/search/search.test.tsx
git commit -m "feat(web): show recording_date on search rows; flag estimated count as approximate (#61)"
```
---
## Self-Review (completed)
**Spec coverage:** recording_date in SearchDocument/build_document (T1 S1), SearchHit/search_objects (T1 S2), SearchHitView (T1 S3), schema.d.ts (T1 S6); Rust test literal fixes + flow assertion (T1 S4); row date (T2 S1); softened count i18n (T2 S2); fixtures + tests (T2 S3S4); Rust + frontend gates (T1 S5, T2 S5). Acceptance criteria 14 mapped. ✓
**Placeholder scan:** the schema.d.ts edit is given verbatim; the test changes name exact assertions; "grep for other SearchDocument/SearchHit literals" is a concrete compile-driven step (the compiler names them if missed). No vague steps. ✓
**Type/consistency:** `recording_date: Option<String>` (Rust) ↔ `recording_date?: string | null` (TS) consistent; `object.recording_date.map(|d| d.to_string())` (YYYY-MM-DD) matches `AdminObjectView`; the frontend reads `hit.recording_date` from the regenerated type. ✓
## Notes
- No new dependency. en/sv parity preserved (only `resultCount_*` values change; guarded by #60).
- Backfill: already-indexed objects show no date until `reindex_all`; new/edited objects index it via `sync_object`. A `reindex` CLI is a follow-up.
- Rust tests need the docker stack (up); run cargo tests ONCE (shared Postgres).
- `cargo nextest run -p search -p api` covers the touched crates; a full `cargo build --workspace` ensures nothing else broke from the struct change.
@@ -0,0 +1,448 @@
# Session-Expiry Soft Redirect + Auth Feedback — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Replace the full-page-reload 401 redirect with a router soft-redirect that preserves SPA state, carries a "session expired" reason + return-to path, and add the missing login/logout feedback.
**Architecture:** A non-React navigate-bridge module (`auth-redirect.ts`) holds a settable `navigate` ref; a one-line `NavigationBridge` component (mounted at the router root) registers React Router's navigate into it. The openapi-fetch 401 middleware calls `redirectToLogin()`, which soft-navigates to `/login?reason=expired&from=<path>`. The login page reads `reason`/`from` (validated against open redirects), `RequireAuth` captures the attempted path, and the user menu shows a logout-pending state.
**Tech Stack:** React 19 + TS + pnpm, React Router 7 (data router), TanStack Query v5, react-i18next, Base UI menu, Vitest 4 (jsdom project) + RTL + MSW. Test runner: `pnpm test` (single pass).
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; en/sv parity; app source double-quote+semicolon; token classes only; `ui/` files use no-semicolon style (do not touch `ui/menu.tsx`). Run a single test pass.
**Spec:** `docs/superpowers/specs/2026-06-08-session-expiry-ux-design.md`
**Key facts:**
- `web/src/api/auth-redirect.ts` currently: `redirectToLogin()``window.location.assign("/login")` (guarded by `pathname !== "/login"`). Called from `web/src/api/client.ts` middleware on `response.status === 401`. Do **not** change `client.ts`.
- `web/src/app.tsx` builds a data router via `createBrowserRouter(createRoutesFromElements(<>…</>))`. Top-level children: `<Route path="/login">`, `<Route element={<RequireAuth/>}>…</Route>`, `<Route path="*">`. Imports `Navigate`, `Route`, `Outlet` is **not** yet imported (add it).
- `web/src/test/render.tsx` `renderApp(ui, {route})` mounts `ui` at `path:"*"` in a `createMemoryRouter` — tests pass their own `<Routes>` tree. jsdom project url is `http://localhost`.
- `vi.stubGlobal("location", {...})` is the established way to stub `window.location` here (see `theme-switch.test.tsx` stubbing `matchMedia`); restore with `vi.unstubAllGlobals()`.
- `login-page.tsx`: `useLogin()` mutation; success currently `navigate("/objects", {replace:true})`; submit `disabled={login.isPending}`; existing error alert uses `role="alert"`. Existing tests: `login-page.test.tsx` (`tree()` with `/login` + `/objects` routes).
- `require-auth.tsx`: `useMe()`; `isLoading``<AppShellSkeleton/>`; `!user``<Navigate to="/login" replace/>`. Test: `require-auth.test.tsx`.
- `user-menu.tsx`: `useLogout()`, `onSignOut` navigates to `/login` on success; `<MenuItem onClick={onSignOut}>{t("auth.signOut")}</MenuItem>`; returns `null` when `!me`. Base UI `MenuItem` supports `closeOnClick` + `disabled`. Test: `user-menu.test.tsx`.
- i18n `auth` block keys: `email/password/signIn/signOut/invalid/networkError` in both `en.json` and `sv.json`.
---
# Task 1: Navigate bridge + soft redirect
**Files:**
- Modify: `web/src/api/auth-redirect.ts`
- Create: `web/src/api/auth-redirect.test.ts`
- Create: `web/src/shell/navigation-bridge.tsx`
- Modify: `web/src/app.tsx`
- [ ] **Step 1: Rewrite `web/src/api/auth-redirect.ts`:**
```ts
type NavigateFn = (to: string, opts?: { replace?: boolean }) => void;
let navigateFn: NavigateFn | null = null;
/** Register (or clear) the router's navigate fn. Called by NavigationBridge. */
export function setNavigate(fn: NavigateFn | null): void {
navigateFn = fn;
}
/** Soft-redirect to login on a 401, preserving SPA state and the return path.
* Falls back to a hard navigation when no router navigate is registered yet
* (e.g. a 401 during the very first load). No-op when already on /login. */
export function redirectToLogin(): void {
const { pathname, search } = window.location;
if (pathname === "/login") return;
const from = encodeURIComponent(pathname + search);
const target = `/login?reason=expired&from=${from}`;
if (navigateFn) {
navigateFn(target, { replace: true });
} else {
window.location.assign(target);
}
}
```
- [ ] **Step 2: Write the failing test `web/src/api/auth-redirect.test.ts`:**
```ts
import { afterEach, expect, test, vi } from "vitest";
import { redirectToLogin, setNavigate } from "./auth-redirect";
function stubLocation(pathname: string, search = "") {
const assign = vi.fn();
vi.stubGlobal("location", { pathname, search, assign });
return assign;
}
afterEach(() => {
setNavigate(null);
vi.unstubAllGlobals();
});
test("uses the registered navigate to soft-redirect with reason + from", () => {
const assign = stubLocation("/objects/abc", "?x=1");
const navigate = vi.fn();
setNavigate(navigate);
redirectToLogin();
expect(navigate).toHaveBeenCalledWith(
"/login?reason=expired&from=%2Fobjects%2Fabc%3Fx%3D1",
{ replace: true },
);
expect(assign).not.toHaveBeenCalled();
});
test("falls back to a hard navigation when no navigate is registered", () => {
const assign = stubLocation("/objects/abc");
redirectToLogin();
expect(assign).toHaveBeenCalledWith("/login?reason=expired&from=%2Fobjects%2Fabc");
});
test("does nothing when already on /login", () => {
stubLocation("/login");
const navigate = vi.fn();
setNavigate(navigate);
redirectToLogin();
expect(navigate).not.toHaveBeenCalled();
});
```
- [ ] **Step 3: Run the test — expect PASS** (the implementation in Step 1 already satisfies it):
```bash
cd web && pnpm vitest run src/api/auth-redirect.test.ts
```
Expected: 3 passing. (If you wrote the test before the impl, it would fail on the missing `setNavigate` export — either order is fine; end state is green.)
- [ ] **Step 4: Create `web/src/shell/navigation-bridge.tsx`:**
```tsx
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { setNavigate } from "../api/auth-redirect";
/** Bridges React Router's navigate to the non-React 401 handler. Renders nothing. */
export function NavigationBridge() {
const navigate = useNavigate();
useEffect(() => {
setNavigate(navigate);
return () => setNavigate(null);
}, [navigate]);
return null;
}
```
- [ ] **Step 5: Wrap the routes in `web/src/app.tsx`** so the bridge is always mounted. Add `Outlet` to the `react-router-dom` import and import the bridge:
```tsx
import { createBrowserRouter, createRoutesFromElements, Navigate, Outlet, Route, RouterProvider } from "react-router-dom";
```
```tsx
import { NavigationBridge } from "./shell/navigation-bridge";
```
Add this component above `const router = …`:
```tsx
function RootLayout() {
return (
<>
<NavigationBridge />
<Outlet />
</>
);
}
```
Wrap the existing top-level fragment children in a pathless `<Route element={<RootLayout />}>` — i.e. change `createRoutesFromElements(<> … </>)` so the outer element is `<Route element={<RootLayout />}> … </Route>` containing the existing `/login`, `RequireAuth`, and `*` routes unchanged:
```tsx
const router = createBrowserRouter(
createRoutesFromElements(
<Route element={<RootLayout />}>
<Route path="/login" element={<LoginPage />} />
<Route element={<RequireAuth />}>
{/* …AppShell subtree exactly as before… */}
</Route>
<Route path="*" element={<Navigate to="/objects" replace />} />
</Route>,
),
);
```
- [ ] **Step 6: Verify build + lint + existing app test (vitest ONCE for the touched files):**
```bash
cd web && pnpm vitest run src/api/auth-redirect.test.ts src/app.test.tsx && pnpm typecheck && pnpm lint
```
Expected: PASS. `app.test.tsx` must stay green (the pathless wrapper is transparent — same rendered routes).
- [ ] **Step 7: Commit**
```bash
git add web/src/api/auth-redirect.ts web/src/api/auth-redirect.test.ts web/src/shell/navigation-bridge.tsx web/src/app.tsx
git commit -m "feat(web): soft-redirect to login on 401 via a navigate bridge (#48)"
```
---
# Task 2: Login page — reason banner, return-to, empty-field guard
**Files:**
- Modify: `web/src/auth/login-page.tsx`
- Modify: `web/src/auth/login-page.test.tsx`
- Modify: `web/src/i18n/en.json`, `web/src/i18n/sv.json`
- [ ] **Step 1: Add i18n keys** (both locales, parity). In `web/src/i18n/en.json` `auth` block add:
```json
"sessionExpired": "Your session expired — please sign in again.",
"signingOut": "Signing out…"
```
In `web/src/i18n/sv.json` `auth` block add:
```json
"sessionExpired": "Din session har gått ut — logga in igen.",
"signingOut": "Loggar ut…"
```
(Place them after the existing `networkError` entry; mind trailing commas. `signingOut` is consumed in Task 3 — add both now so the parity test stays green.)
- [ ] **Step 2: Update `web/src/auth/login-page.tsx`.** Add `useSearchParams` to the import and a `safeFrom` helper; show the reason banner; route to `safeFrom` on success; guard the submit. Full changes:
Import line:
```tsx
import { useNavigate, useSearchParams } from "react-router-dom";
```
Add a module-level helper (above `export function LoginPage`):
```tsx
/** Accept only a single-leading-slash local path; reject protocol-relative
* ("//host") and absolute URLs to avoid an open redirect. */
function safeFrom(raw: string | null): string {
if (!raw) return "/objects";
return /^\/(?!\/)/.test(raw) ? raw : "/objects";
}
```
Inside the component, after `const navigate = useNavigate();`:
```tsx
const [params] = useSearchParams();
const sessionExpired = params.get("reason") === "expired";
```
Change the success navigation:
```tsx
{ onSuccess: () => navigate(safeFrom(params.get("from")), { replace: true }) },
```
Add the banner just inside the `<form>`, above the email field (after the `<h1>`):
```tsx
{sessionExpired && (
<p className="text-sm text-muted-foreground">{t("auth.sessionExpired")}</p>
)}
```
Change the submit button disabled condition:
```tsx
<Button type="submit" className="w-full" disabled={login.isPending || !email.trim() || !password}>
```
- [ ] **Step 3: Update `web/src/auth/login-page.test.tsx`.** Extend `tree()` with an `:id` destination and add tests. Replace the file's `tree()` and append the new tests:
```tsx
function tree() {
return (
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route path="/objects" element={<div>objects landing</div>} />
<Route path="/objects/:id" element={<div>object detail</div>} />
</Routes>
);
}
```
Add these tests (keep the two existing ones):
```tsx
test("shows the session-expired notice when reason=expired", async () => {
renderApp(tree(), { route: "/login?reason=expired" });
expect(await screen.findByText(/session expired/i)).toBeInTheDocument();
});
test("returns to the from path on success", async () => {
renderApp(tree(), { route: "/login?from=%2Fobjects%2F123" });
await userEvent.type(screen.getByLabelText(/email/i), "editor@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "pw-editor-123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
expect(await screen.findByText("object detail")).toBeInTheDocument();
});
test("rejects an off-site from and falls back to /objects", async () => {
renderApp(tree(), { route: "/login?from=%2F%2Fevil.com" });
await userEvent.type(screen.getByLabelText(/email/i), "editor@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "pw-editor-123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
expect(await screen.findByText("objects landing")).toBeInTheDocument();
});
test("disables submit until both fields are filled", async () => {
renderApp(tree(), { route: "/login" });
const button = screen.getByRole("button", { name: /sign in/i });
expect(button).toBeDisabled();
await userEvent.type(screen.getByLabelText(/email/i), "a@b.se");
expect(button).toBeDisabled();
await userEvent.type(screen.getByLabelText(/password/i), "pw");
expect(button).toBeEnabled();
});
```
(The existing "successful login navigates to /objects" test has no `from`, so `safeFrom(null)``/objects` keeps it green. The default MSW `/api/admin/login` handler returns 204 for `editor@example.com` / `pw-editor-123`.)
- [ ] **Step 4: Run the login + i18n tests (vitest ONCE):**
```bash
cd web && pnpm vitest run src/auth/login-page.test.tsx src/i18n
```
Expected: all green (login-page tests + the i18n parity test covering the 2 new keys).
- [ ] **Step 5: Commit**
```bash
git add web/src/auth/login-page.tsx web/src/auth/login-page.test.tsx web/src/i18n/en.json web/src/i18n/sv.json
git commit -m "feat(web): login reason banner + return-to + empty-field guard (#48)"
```
---
# Task 3: RequireAuth return-to + logout pending + full gate
**Files:**
- Modify: `web/src/auth/require-auth.tsx`
- Modify: `web/src/auth/require-auth.test.tsx`
- Modify: `web/src/shell/user-menu.tsx`
- Modify: `web/src/shell/user-menu.test.tsx`
- [ ] **Step 1: Update `web/src/auth/require-auth.tsx`** to capture the attempted path:
```tsx
import { Navigate, Outlet, useLocation } from "react-router-dom";
import { useMe } from "../api/queries";
import { AppShellSkeleton } from "@/components/ui/skeletons";
export function RequireAuth() {
const { data: user, isLoading } = useMe();
const location = useLocation();
if (isLoading) return <AppShellSkeleton />;
if (!user) {
const from = encodeURIComponent(location.pathname + location.search);
return <Navigate to={`/login?from=${from}`} replace />;
}
return <Outlet />;
}
```
- [ ] **Step 2: Update `web/src/auth/require-auth.test.tsx`** so the login stub echoes the search string, and assert `from` is carried. Replace the `tree()` and the redirect test:
```tsx
import { screen, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw";
import { expect, test } from "vitest";
import { Route, Routes, useLocation } from "react-router-dom";
import { server } from "../test/server";
import { renderApp } from "../test/render";
import { RequireAuth } from "./require-auth";
function LoginStub() {
const location = useLocation();
return <div>login page {location.search}</div>;
}
function tree() {
return (
<Routes>
<Route path="/login" element={<LoginStub />} />
<Route element={<RequireAuth />}>
<Route path="/objects" element={<div>secret objects</div>} />
</Route>
</Routes>
);
}
test("renders children when authenticated", async () => {
renderApp(tree(), { route: "/objects" });
expect(await screen.findByText("secret objects")).toBeInTheDocument();
});
test("redirects unauthenticated users to /login carrying the attempted path", async () => {
server.use(http.get("/api/admin/me", () => new HttpResponse(null, { status: 401 })));
renderApp(tree(), { route: "/objects" });
await waitFor(() => expect(screen.getByText(/from=%2Fobjects/)).toBeInTheDocument());
});
```
- [ ] **Step 3: Update `web/src/shell/user-menu.tsx`** to show a logout-pending state. Change only the `<MenuItem>`:
```tsx
<MenuItem closeOnClick={false} disabled={logout.isPending} onClick={onSignOut}>
{logout.isPending ? t("auth.signingOut") : t("auth.signOut")}
</MenuItem>
```
(Everything else in the file is unchanged. `onSignOut` already navigates to `/login` on success, which unmounts the menu since `useMe` becomes null.)
- [ ] **Step 4: Add a pending-state test to `web/src/shell/user-menu.test.tsx`.** Add `delay` to the msw import and append a test:
```tsx
import { delay, http, HttpResponse } from "msw";
```
```tsx
test("shows a pending state on Sign out while logging out", async () => {
server.use(
http.post("/api/admin/logout", async () => {
await delay(50);
return new HttpResponse(null, { status: 204 });
}),
);
renderApp(<UserMenu />);
const trigger = await screen.findByRole("button", { name: /editor@example.com/ });
await userEvent.click(trigger);
const menu = within(document.body);
await userEvent.click(await menu.findByText("Sign out"));
expect(await menu.findByText(/signing out/i)).toBeInTheDocument();
});
```
(The existing "signs out" test still passes: `closeOnClick={false}` keeps the item, the POST still fires, `loggedOut` flips true.)
- [ ] **Step 5: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report test totals, largest chunk (gz), and the `check:colors` line.
- [ ] **Step 6: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no codename matches (`codename-exit=1`).
- [ ] **Step 7: Manual smoke (recommended).** With the stack + server + `pnpm dev` up: open an object edit, let the session expire (or delete the session cookie) and trigger a save → you land on `/login` **without a full reload**, see "Your session expired…", and after re-login return to the same record. Deep-link to a route while logged out → login → returns there. Empty login form → Sign in disabled. Click Sign out → brief "Signing out…".
- [ ] **Step 8: Commit**
```bash
git add web/src/auth/require-auth.tsx web/src/auth/require-auth.test.tsx web/src/shell/user-menu.tsx web/src/shell/user-menu.test.tsx
git commit -m "feat(web): return-to-destination on auth redirect; logout pending state (#48)"
```
---
## Self-Review (completed)
**Spec coverage:** navigate bridge + soft redirect with reason/from (T1 — AC1); login reason banner +
validated return-to + empty-field guard (T2 — AC2, AC4 first half); `RequireAuth` return-to (T3 S1S2 —
AC3); logout pending state (T3 S3S4 — AC4 second half); i18n 2 keys + parity (T2 S1); full gate +
codename (T3 S5S6 — AC5). All 5 acceptance criteria mapped. ✓
**Placeholder scan:** every code step shows complete code; tests have concrete assertions and exact
encoded URLs (`%2Fobjects%2Fabc%3Fx%3D1`, `%2F%2Fevil.com`); `vi.stubGlobal("location", …)` is the
established stub. No TODO/TBD. ✓
**Type consistency:** `setNavigate(fn: NavigateFn | null)` defined in T1 and called with React Router's
`navigate` (compatible signature `(to, {replace})`) in `NavigationBridge`, and with `null` in cleanup;
`redirectToLogin()` signature unchanged so `client.ts` needs no edit; `safeFrom(raw: string | null)`
consumed only in `login-page.tsx`. i18n keys `auth.sessionExpired` / `auth.signingOut` added in T2 and
`auth.signingOut` consumed in T3. ✓
## Notes
- No new dependency. `ui/menu.tsx` is **not** modified (Base UI `MenuItem` already exposes `closeOnClick`
+ `disabled`). en/sv parity preserved (2 new keys, guarded by the #60 parity test).
- `client.ts` is intentionally untouched — only `redirectToLogin`'s behaviour changes.
- The in-place re-auth modal (full field-level preservation) is a deferred follow-up per the spec.
@@ -0,0 +1,344 @@
# Split queries.ts — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Extract the 4 error classes to `api/errors.ts`, add a `keys` query-key factory in `api/query-keys.ts` (and invalidate `["search"]` on object writes), then split `queries.ts` into `api/queries/{auth,objects,field-defs,vocab,authorities,search}.ts` behind a stable `api/queries/index.ts` barrel — behavior-preserving except the search invalidation.
**Architecture:** Three ordered, individually-green tasks. Task 1 extracts errors (queries.ts re-exports them). Task 2 adds the key factory + search invalidation (still monolithic). Task 3 moves the now-final hook bodies into domain modules behind a barrel that keeps `../api/queries` stable for all ~30 consumers.
**Tech Stack:** React 19 + TS + pnpm, TanStack Query v5, openapi-fetch, Vitest 4 (jsdom) + RTL + MSW.
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; double-quote+semicolon. Run a single test pass per task. Behavior-preserving except the one search-invalidation change.
**Spec:** `docs/superpowers/specs/2026-06-08-split-queries-design.md`
**Key facts:**
- `web/src/api/queries.ts` (584 lines) currently defines 4 error classes (`HttpError` :6, `FieldRejection` :13, `InUseError` :20, `VisibilityError` :394) + `type ObjectListParams` (:46) + all hooks.
- Error-class importers (non-test): `api/error-message.ts` (`HttpError, InUseError`), `objects/object-edit-form.tsx` + `objects/object-new-page.tsx` (`FieldRejection`), `objects/publish-control.tsx` (`VisibilityError`), `search/search-panel.tsx` (`HttpError`) — all via `../api/queries`. Tests: `mutation-error.test.tsx`, `labelled-record-row.test.tsx`, `error-message.test.ts` import error classes from `../api/queries`.
- ~30 files import hooks from `../api/queries`. Query-layer test suites: `queries.test.ts`, `queries.authoring.test.tsx`, `queries.fields.test.tsx`, `queries.search.test.tsx`, `queries.visibility.test.tsx`, `queries.vocab.test.tsx`, `mutation-feedback.test.tsx`.
- Key literals: `["me"]`, `["config"]` (in `config/config-provider.tsx:10`), `["objects", params]`, `["objects"]`, `["object", id]`, `["field-definitions"]`, `["terms", vocabularyId]`, `["authorities", kind]`, `["vocabularies"]`, `["search", term, visibility]`.
- `useTerms`/`useAuthorities` key on a `string | null | undefined` arg (enabled-gated), so `keys.terms`/`keys.authorities` must accept that union.
---
# Task 1: Extract error classes → `api/errors.ts`
**Files:** Create `web/src/api/errors.ts`; Modify `web/src/api/queries.ts`, `web/src/api/error-message.ts`.
- [ ] **Step 1: Create `web/src/api/errors.ts`** (move the 4 classes verbatim):
```ts
export class HttpError extends Error {
constructor(public readonly status: number) {
super(`HTTP ${status}`);
this.name = "HttpError";
}
}
export class FieldRejection extends Error {
constructor(public readonly field: string, public readonly code: string) {
super(`field rejected: ${field}`);
this.name = "FieldRejection";
}
}
export class InUseError extends Error {
constructor(public readonly count: number) {
super(`in use: ${count}`);
this.name = "InUseError";
}
}
/** Error carrying the HTTP status so callers can branch 422-gate vs 409-illegal. */
export class VisibilityError extends Error {
constructor(public status: number) {
super(`visibility change failed (${status})`);
this.name = "VisibilityError";
}
}
```
- [ ] **Step 2: Update `web/src/api/queries.ts`.** DELETE the 4 class definitions (lines ~6-25 `HttpError`/`FieldRejection`/`InUseError`, and ~393-399 `VisibilityError`). At the top of the file (after the existing `import` lines), add an import for use + a re-export for compatibility:
```ts
import { HttpError, FieldRejection, InUseError, VisibilityError } from "./errors";
export { HttpError, FieldRejection, InUseError, VisibilityError } from "./errors";
```
(The `import` binds them for the throw sites in this file; the `export … from` re-exports them so every consumer importing from `../api/queries` keeps working. Everything else in `queries.ts` is unchanged.)
- [ ] **Step 3: Repoint `web/src/api/error-message.ts`.** Change `import { HttpError, InUseError } from "./queries";` to `import { HttpError, InUseError } from "./errors";`. (This is the decoupling: the toast path no longer transitively loads the hook module.)
- [ ] **Step 4: Verify (vitest ONCE for the affected suites), typecheck, lint:**
```bash
cd web && pnpm vitest run src/api/error-message.test.ts src/api/mutation-feedback.test.tsx src/api/queries.test.ts src/components/mutation-error.test.tsx src/components/labelled-record-row.test.tsx && pnpm typecheck && pnpm lint
```
Expected: green. The error classes are now sourced from `errors.ts` but re-exported, so all importers resolve. If typecheck flags an unused import in `queries.ts`, ensure each of the 4 classes is actually thrown somewhere in the file (they are: `HttpError` many sites, `FieldRejection` in `useSetFields`, `InUseError` in the delete mutations, `VisibilityError` in `useSetVisibility`) — keep all four in the import.
- [ ] **Step 5: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/api/errors.ts web/src/api/queries.ts web/src/api/error-message.ts
git commit -m "refactor(web): extract API error classes to api/errors.ts (#65)"
```
---
# Task 2: Query-key factory + search invalidation
**Files:** Create `web/src/api/query-keys.ts`, `web/src/api/query-keys.test.ts`, `web/src/api/search-invalidation.test.tsx`; Modify `web/src/api/queries.ts`, `web/src/config/config-provider.tsx`.
- [ ] **Step 1: Create `web/src/api/query-keys.ts`:**
```ts
export type ObjectListParams = {
limit: number;
offset: number;
sort?: string;
order?: "asc" | "desc";
visibility?: string;
q?: string;
};
/** Central query-key factory — the single source of truth for cache keys, so
* query/invalidate/setQueryData sites can't drift. */
export const keys = {
me: () => ["me"] as const,
config: () => ["config"] as const,
objects: () => ["objects"] as const,
objectsPage: (params: ObjectListParams) => ["objects", params] as const,
object: (id: string) => ["object", id] as const,
fieldDefinitions: () => ["field-definitions"] as const,
vocabularies: () => ["vocabularies"] as const,
terms: (vocabularyId: string | null | undefined) => ["terms", vocabularyId] as const,
authorities: (kind: string | null | undefined) => ["authorities", kind] as const,
search: () => ["search"] as const,
searchResults: (term: string, visibility: string | null) => ["search", term, visibility] as const,
};
```
- [ ] **Step 2: Create `web/src/api/query-keys.test.ts`** (write + run):
```ts
import { expect, test } from "vitest";
import { keys } from "./query-keys";
test("the key factory produces the expected arrays", () => {
expect(keys.me()).toEqual(["me"]);
expect(keys.config()).toEqual(["config"]);
expect(keys.objects()).toEqual(["objects"]);
const p = { limit: 50, offset: 0 };
expect(keys.objectsPage(p)).toEqual(["objects", p]);
expect(keys.object("x")).toEqual(["object", "x"]);
expect(keys.fieldDefinitions()).toEqual(["field-definitions"]);
expect(keys.vocabularies()).toEqual(["vocabularies"]);
expect(keys.terms("v1")).toEqual(["terms", "v1"]);
expect(keys.authorities("person")).toEqual(["authorities", "person"]);
expect(keys.search()).toEqual(["search"]);
expect(keys.searchResults("q", null)).toEqual(["search", "q", null]);
});
test("objects() is a prefix of objectsPage() so invalidation matches", () => {
const prefix = keys.objects();
const full = keys.objectsPage({ limit: 50, offset: 0 });
expect(full.slice(0, prefix.length)).toEqual(prefix);
});
```
Run: `cd web && pnpm vitest run src/api/query-keys.test.ts`.
- [ ] **Step 3: Replace every key literal in `web/src/api/queries.ts` with `keys.*`.** Add `import { keys, type ObjectListParams } from "./query-keys";` and DELETE the local `export type ObjectListParams = {…};` block (now imported). Substitutions (every occurrence):
- `queryKey: ["me"]``queryKey: keys.me()`; `qc.invalidateQueries({ queryKey: ["me"] })``keys.me()`; `qc.setQueryData(["me"], null)``qc.setQueryData(keys.me(), null)`
- `queryKey: ["objects", params]``keys.objectsPage(params)`
- `["objects"]` (invalidations) → `keys.objects()`
- `["object", id]``keys.object(id)`
- `["field-definitions"]``keys.fieldDefinitions()`
- `["terms", vocabularyId]``keys.terms(vocabularyId)`
- `["authorities", kind]``keys.authorities(kind)`
- `["vocabularies"]``keys.vocabularies()`
- `queryKey: ["search", term, visibility]``keys.searchResults(term, visibility)`
Re-export the type so consumers importing `ObjectListParams` from `../api/queries` keep working: add `export type { ObjectListParams } from "./query-keys";` near the top.
- [ ] **Step 4: Add search invalidation (`web/src/api/queries.ts`).** In each of these `onSuccess` handlers add `void qc.invalidateQueries({ queryKey: keys.search() });`:
- `useUpdateObject` onSuccess (after the `objects`/`object` invalidations)
- `useDeleteObject` onSuccess (alongside the `objects` invalidation — convert it to a block: `onSuccess: () => { void qc.invalidateQueries({ queryKey: keys.objects() }); void qc.invalidateQueries({ queryKey: keys.search() }); }`)
- `useSetVisibility` onSuccess (after the `object`/`objects` invalidations)
- [ ] **Step 5: Update `web/src/config/config-provider.tsx`.** Add `import { keys } from "../api/query-keys";` and change `queryKey: ["config"]``queryKey: keys.config()`.
- [ ] **Step 6: Create `web/src/api/search-invalidation.test.tsx`** (write + run) — proves the new behavior:
```tsx
import { expect, test } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { http, HttpResponse } from "msw";
import type { ReactNode } from "react";
import { server } from "../test/server";
import { useSetVisibility } from "./queries";
import { keys } from "./query-keys";
test("changing an object's visibility invalidates the active search query", async () => {
server.use(
http.post("/api/admin/objects/:id/visibility", () => new HttpResponse(null, { status: 204 })),
);
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
qc.setQueryData(keys.searchResults("amphora", null), { pages: [], pageParams: [] });
const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={qc}>{children}</QueryClientProvider>
);
const { result } = renderHook(() => useSetVisibility(), { wrapper });
await result.current.mutateAsync({ id: "o1", visibility: "public" });
await waitFor(() =>
expect(qc.getQueryState(keys.searchResults("amphora", null))?.isInvalidated).toBe(true),
);
});
```
Run: `cd web && pnpm vitest run src/api/search-invalidation.test.tsx`. (If `isInvalidated` is flaky, assert `qc.getQueryState(keys.searchResults("amphora", null))` exists and was marked stale via `isInvalidated`; the mutation's `onSuccess` runs the invalidation synchronously after the 204.)
- [ ] **Step 7: Verify (vitest ONCE for the query suites), typecheck, lint:**
```bash
cd web && pnpm vitest run src/api/query-keys.test.ts src/api/search-invalidation.test.tsx src/api/queries.test.ts src/api/queries.authoring.test.tsx src/api/queries.fields.test.tsx src/api/queries.search.test.tsx src/api/queries.visibility.test.tsx src/api/queries.vocab.test.tsx src/config && pnpm typecheck && pnpm lint
```
Expected: green. The key arrays are identical to before, so all existing query tests pass unchanged.
- [ ] **Step 8: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/api/query-keys.ts web/src/api/query-keys.test.ts web/src/api/search-invalidation.test.tsx web/src/api/queries.ts web/src/config/config-provider.tsx
git commit -m "refactor(web): central query-key factory + invalidate search on object writes (#65)"
```
---
# Task 3: Split queries.ts into `api/queries/` domain modules
**Files:** Create `web/src/api/queries/{index,auth,objects,field-defs,vocab,authorities,search}.ts`; Delete `web/src/api/queries.ts`.
**Approach:** Move each hook (and its local `type X = components[...]` aliases) VERBATIM from the current `queries.ts` into its domain module — the bodies already use `keys.*` and the `errors.ts` classes after Tasks 1-2. Only the relative import paths change (`./client``../client`, `./schema``../schema`, `./errors`, `./query-keys`). Then add the barrel and delete `queries.ts`.
- [ ] **Step 1: `web/src/api/queries/auth.ts`** — header + move `useMe`, `useLogin`, `useLogout`:
```ts
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { keys } from "../query-keys";
type UserView = components["schemas"]["UserView"];
type LoginRequest = components["schemas"]["LoginRequest"];
```
(These three throw only plain `Error` — no `errors.ts` import needed here.)
- [ ] **Step 2: `web/src/api/queries/objects.ts`** — header + move `useObjectsPage`, `useObject`, `useCreateObject`, `useUpdateObject`, `useSetFields`, `useDeleteObject`, `useSetVisibility`:
```ts
import { keepPreviousData, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, FieldRejection, VisibilityError } from "../errors";
import { keys, type ObjectListParams } from "../query-keys";
type ObjectCreateRequest = components["schemas"]["ObjectCreateRequest"];
type ObjectUpdateRequest = components["schemas"]["ObjectUpdateRequest"];
type Visibility = "draft" | "internal" | "public";
```
(`ObjectListParams` now comes from `query-keys`. `useObjectsPage`/`useObject` query fns throw plain `Error`; the mutations use the imported error classes.)
- [ ] **Step 3: `web/src/api/queries/field-defs.ts`** — header + move `useFieldDefinitions`, `useCreateFieldDefinition`, `useUpdateFieldDefinition`, `useDeleteFieldDefinition`:
```ts
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, InUseError } from "../errors";
import { keys } from "../query-keys";
type NewFieldDefinitionRequest = components["schemas"]["NewFieldDefinitionRequest"];
type LabelInput = components["schemas"]["LabelInput"];
```
- [ ] **Step 4: `web/src/api/queries/vocab.ts`** — header + move `useVocabularies`, `useCreateVocabulary`, `useRenameVocabulary`, `useDeleteVocabulary`, `useTerms`, `useAddTerm`, `useUpdateTerm`, `useDeleteTerm`:
```ts
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, InUseError } from "../errors";
import { keys } from "../query-keys";
type NewVocabularyRequest = components["schemas"]["NewVocabularyRequest"];
type LabelInput = components["schemas"]["LabelInput"];
```
- [ ] **Step 5: `web/src/api/queries/authorities.ts`** — header + move `useAuthorities`, `useCreateAuthority`, `useUpdateAuthority`, `useDeleteAuthority`:
```ts
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, InUseError } from "../errors";
import { keys } from "../query-keys";
type LabelInput = components["schemas"]["LabelInput"];
```
- [ ] **Step 6: `web/src/api/queries/search.ts`** — header + move the `SEARCH_PAGE` const and `useSearch`:
```ts
import { keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
import { api } from "../client";
import { HttpError } from "../errors";
import { keys } from "../query-keys";
const SEARCH_PAGE = 20;
```
- [ ] **Step 7: `web/src/api/queries/index.ts`** (barrel):
```ts
export * from "./auth";
export * from "./objects";
export * from "./field-defs";
export * from "./vocab";
export * from "./authorities";
export * from "./search";
export * from "../errors";
export type { ObjectListParams } from "../query-keys";
```
- [ ] **Step 8: Delete the old monolith:** `git rm web/src/api/queries.ts` (every hook has been moved; the barrel + modules now provide the same exports). Confirm no hook/type was dropped: each of the 24 hooks + `ObjectListParams` + the 4 error classes is exported via the barrel.
- [ ] **Step 9: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. The `../api/queries` import path now resolves to `api/queries/index.ts`, so all ~30 consumers + the query-layer test suites resolve unchanged. If typecheck reports a missing export, a hook landed in the wrong module or an import path is off — fix the module, do NOT edit consumers/tests. Report test totals, largest chunk (gz), and the `check:colors` line.
- [ ] **Step 10: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`); `web/src/api/queries.ts` shows as deleted, the 7 new files added.
- [ ] **Step 11: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/api/queries/ && git rm -q web/src/api/queries.ts 2>/dev/null; git add -A web/src/api
git commit -m "refactor(web): split queries.ts into api/queries/ domain modules behind a barrel (#65)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 errors extracted + error-message repointed + barrel re-export (T1, T3 S7); AC2 directory split + queries.ts deleted + stable path (T3); AC3 key factory used everywhere incl. config-provider (T2 S3/S5); AC4 search invalidation on the 3 object mutations (T2 S4); AC5 existing tests unchanged + gate (T1 S4, T2 S7, T3 S9). ✓
**Placeholder scan:** every new file shown in full or as a precise header + verbatim-move instruction; the move tasks name the exact hook list per module; tests have concrete assertions. No TBD. ✓
**Type/consistency:** `keys` (T2) is the same object consumed in T3's modules; `ObjectListParams` defined in `query-keys.ts` (T2), imported by `objects.ts` (T3 S2) and re-exported by the barrel (T3 S7); error classes from `errors.ts` (T1) imported by `objects/field-defs/vocab/authorities/search` modules (T3) and re-exported by the barrel; `keys.terms`/`keys.authorities` accept `string | null | undefined` to match the enabled-gated query usage. ✓
## Notes
- No new dependency, no new i18n keys, `components/ui/*` untouched. `check:size` should be unchanged (pure reorg + one invalidate call). Barrel keeps `../api/queries` stable → zero consumer churn.
- The error classes are intentionally importable from both `../api/errors` (canonical) and `../api/queries` (compat re-export). Repointing the 4 component importers to `../api/errors` is a deferred cosmetic follow-up.
- `auth.ts` needs no `errors.ts` import (its throws are plain `Error`); every other module imports the error classes it throws.
@@ -0,0 +1,726 @@
# Unify Vocabulary + Authority CRUD — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Collapse the duplicated Vocabulary-terms + Authorities CRUD (~280 lines across 4 files) into three shared components, with the rows and pages reduced to thin adapters — behavior-preserving.
**Architecture:** Build `LabelledRecordRow`, `LabelledRecordCreateForm`, and `FilteredRecordList<T>` in `src/components/` (Tasks 1-3, additive — existing app untouched, all existing tests stay green). Then rewire `term-row`/`authority-row` and `authorities-page`/`vocabulary-terms` onto them (Task 4) and run the full gate. Variance (mutation hooks, arg shapes, i18n keys, page chrome) lives entirely in the adapters.
**Tech Stack:** React 19 + TS + pnpm, TanStack Query v5, react-i18next, Base UI, Vitest 4 (jsdom) + RTL.
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; double-quote+semicolon; token classes only; `components/ui/*` untouched. Run a single test pass per task.
**Spec:** `docs/superpowers/specs/2026-06-08-unify-record-crud-design.md`
**Key facts:**
- Types: `type LabelView = components["schemas"]["LabelView"]`, `type LabelInput = components["schemas"]["LabelInput"]`. `labelText(labels: LabelView[], lang)` (`lib/labels`), `byLabel(lang)` returns a comparator over `{ labels: LabelView[] }` (`lib/sort`).
- Shared building blocks (in `src/components/`): `label-editor` (`LabelEditor`, uses `useId` since #62, needs `useConfig` which defaults to `DEFAULTS` — works under `renderApp`), `delete-confirm-dialog` (`DeleteConfirmDialog` — props `description`, `onConfirm: () => Promise<void>`), `mutation-error` (`MutationError` — prop `error: unknown`), `external-uri-link` (`ExternalUriLink` — prop `uri`).
- UI kit: `Button`, `Input`, `Label` from `@/components/ui/*`; `ListSkeleton` from `@/components/ui/skeletons` (props `className`, `rows`).
- Test harness: `renderApp(ui, { route })` from `../test/render` (wraps QueryClient + memory router + i18n; NO ConfigProvider, but `useConfig` falls back to defaults). `HttpError` is exported from `../api/queries`.
- Existing tests that MUST stay green unchanged: `vocab/term-row.test.tsx`, `authorities/authorities.test.tsx`, `vocab/vocabularies.test.tsx`.
- Current `term-row.tsx`/`authority-row.tsx` are twins; `authorities-page.tsx` has a kind `<nav>` + `PageTitle` + `Navigate` guard + breadcrumb; `vocabulary-terms.tsx` has a `vocab.terms` caption + breadcrumb. Both pages render the filter input ALWAYS, then `isLoading ? <ListSkeleton/> : <ul>…</ul>`.
---
# Task 1: `LabelledRecordRow`
**Files:** Create `web/src/components/labelled-record-row.tsx`, `web/src/components/labelled-record-row.test.tsx`.
- [ ] **Step 1: Create `web/src/components/labelled-record-row.tsx`:**
```tsx
import { useId, useState } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { LabelEditor } from "./label-editor";
import { DeleteConfirmDialog } from "./delete-confirm-dialog";
import { MutationError } from "./mutation-error";
import { ExternalUriLink } from "./external-uri-link";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { labelText } from "../lib/labels";
type LabelView = components["schemas"]["LabelView"];
type LabelInput = components["schemas"]["LabelInput"];
export type RecordLike = { id: string; labels: LabelView[]; external_uri: string | null };
/** One labelled record (term/authority): a display row with edit + delete, or an
* inline editor. All variance (mutation hooks, arg shapes, delete-confirm key) is
* supplied by the caller via callbacks/state — see term-row.tsx / authority-row.tsx. */
export function LabelledRecordRow({
record,
lang,
deleteConfirmKey,
savePending,
saveError,
onEditOpen,
onSave,
onDelete,
}: {
record: RecordLike;
lang: string;
deleteConfirmKey: string;
savePending: boolean;
saveError: unknown;
onEditOpen: () => void;
onSave: (labels: LabelInput[], uri: string | null, done: () => void) => void;
onDelete: () => Promise<void>;
}) {
const { t } = useTranslation();
const uriId = useId();
const [editing, setEditing] = useState(false);
const [labels, setLabels] = useState<LabelInput[]>(record.labels as LabelInput[]);
const [uri, setUri] = useState(record.external_uri ?? "");
if (editing) {
return (
<li className="space-y-2 border-b py-2">
<LabelEditor value={labels} onChange={setLabels} />
<div className="space-y-1">
<Label htmlFor={uriId}>{t("labels.externalUri")}</Label>
<Input
id={uriId}
type="url"
placeholder={t("labels.uriPlaceholder")}
value={uri}
onChange={(e) => setUri(e.target.value)}
/>
</div>
<div className="flex gap-2">
<Button
type="button"
size="sm"
disabled={savePending}
onClick={() => onSave(labels, uri.trim() || null, () => setEditing(false))}
>
{t("actions.save")}
</Button>
<Button type="button" variant="ghost" size="sm" onClick={() => setEditing(false)}>
{t("form.cancel")}
</Button>
</div>
<MutationError error={saveError} />
</li>
);
}
return (
<li className="flex items-center gap-2 border-b py-1 text-sm">
<div className="flex-1">
<div>{labelText(record.labels, lang)}</div>
{record.external_uri && <ExternalUriLink uri={record.external_uri} />}
</div>
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => {
onEditOpen();
setLabels(record.labels as LabelInput[]);
setUri(record.external_uri ?? "");
setEditing(true);
}}
>
{t("actions.edit")}
</Button>
<DeleteConfirmDialog description={t(deleteConfirmKey)} onConfirm={onDelete} />
</li>
);
}
```
- [ ] **Step 2: Create `web/src/components/labelled-record-row.test.tsx`** (write + run). Type the test record as `RecordLike` (import it) — no `any`/`never`:
```tsx
import { expect, test, vi } from "vitest";
import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { LabelledRecordRow, type RecordLike } from "./labelled-record-row";
import { HttpError } from "../api/queries";
const record: RecordLike = { id: "r1", external_uri: null, labels: [{ lang: "en", label: "Bronze" }] };
test("edit → save calls onSave and closes via done()", async () => {
const onSave = vi.fn((_labels: unknown, _uri: unknown, done: () => void) => done());
renderApp(
<ul>
<LabelledRecordRow
record={record}
lang="en"
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={false}
saveError={null}
onEditOpen={() => {}}
onSave={onSave}
onDelete={async () => {}}
/>
</ul>,
);
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(onSave).toHaveBeenCalled();
expect(screen.queryByRole("button", { name: /save/i })).toBeNull();
});
test("a save error renders inline and the row stays editable", async () => {
renderApp(
<ul>
<LabelledRecordRow
record={record}
lang="en"
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={false}
saveError={new HttpError(403)}
onEditOpen={() => {}}
onSave={() => {}}
onDelete={async () => {}}
/>
</ul>,
);
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
expect(screen.getByRole("alert")).toHaveTextContent(/permission/i);
expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();
});
test("confirming delete invokes onDelete", async () => {
const onDelete = vi.fn(async () => {});
renderApp(
<ul>
<LabelledRecordRow
record={record}
lang="en"
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={false}
saveError={null}
onEditOpen={() => {}}
onSave={() => {}}
onDelete={onDelete}
/>
</ul>,
);
// open the confirm dialog (trigger button is labelled "Delete")
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
// the dialog renders in a portal on document.body with a confirm "Delete" action
const dialog = within(document.body);
const confirmButtons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(confirmButtons[confirmButtons.length - 1]);
expect(onDelete).toHaveBeenCalled();
});
```
Run: `cd web && pnpm vitest run src/components/labelled-record-row.test.tsx`. (If the delete-dialog DOM differs, mirror the portal/confirm pattern used in `web/src/shell/user-menu.test.tsx` / the delete-confirm-dialog story — the key assertion is that confirming calls `onDelete`. Don't weaken the save/error assertions.)
- [ ] **Step 3: Verify + lint:** `cd web && pnpm vitest run src/components/labelled-record-row.test.tsx && pnpm typecheck && pnpm lint` — all green.
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/components/labelled-record-row.tsx web/src/components/labelled-record-row.test.tsx
git commit -m "feat(web): shared LabelledRecordRow component (#64)"
```
---
# Task 2: `LabelledRecordCreateForm`
**Files:** Create `web/src/components/labelled-record-create-form.tsx`, `web/src/components/labelled-record-create-form.test.tsx`.
- [ ] **Step 1: Create `web/src/components/labelled-record-create-form.tsx`:**
```tsx
import { useId, useState, type FormEvent, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { LabelEditor } from "./label-editor";
import { MutationError } from "./mutation-error";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
type LabelInput = components["schemas"]["LabelInput"];
/** Create form for a labelled record (term/authority): single-language label +
* optional external URI, with required-label validation and a status-aware error.
* `onCreate` performs the mutation and is handed a `reset` to clear the inputs on success. */
export function LabelledRecordCreateForm({
heading,
submitLabel,
pending,
error,
onCreate,
}: {
heading: ReactNode;
submitLabel: string;
pending: boolean;
error: unknown;
onCreate: (labels: LabelInput[], uri: string | null, reset: () => void) => void;
}) {
const { t } = useTranslation();
const uriId = useId();
const [labels, setLabels] = useState<LabelInput[]>([]);
const [uri, setUri] = useState("");
const [requiredError, setRequiredError] = useState(false);
const onSubmit = (event: FormEvent) => {
event.preventDefault();
if (!labels.some((l) => l.label)) {
setRequiredError(true);
return;
}
setRequiredError(false);
onCreate(labels, uri.trim() || null, () => {
setLabels([]);
setUri("");
});
};
return (
<form onSubmit={onSubmit} className="space-y-2 border-t pt-3">
<div className="text-sm font-medium">{heading}</div>
<LabelEditor value={labels} onChange={setLabels} />
<div className="space-y-1">
<Label htmlFor={uriId}>{t("labels.externalUri")}</Label>
<Input
id={uriId}
type="url"
placeholder={t("labels.uriPlaceholder")}
value={uri}
onChange={(e) => setUri(e.target.value)}
/>
</div>
{requiredError && (
<p role="alert" className="text-xs text-destructive">
{t("form.required")}
</p>
)}
<MutationError error={error} />
<Button type="submit" size="sm" disabled={pending}>
{submitLabel}
</Button>
</form>
);
}
```
- [ ] **Step 2: Create `web/src/components/labelled-record-create-form.test.tsx`** (write + run):
```tsx
import { expect, test, vi } from "vitest";
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { LabelledRecordCreateForm } from "./labelled-record-create-form";
test("submitting with empty labels shows the required error and does not call onCreate", async () => {
const onCreate = vi.fn();
renderApp(
<LabelledRecordCreateForm heading="New" submitLabel="Create" pending={false} error={null} onCreate={onCreate} />,
);
await userEvent.click(screen.getByRole("button", { name: /create/i }));
expect(screen.getByRole("alert")).toBeInTheDocument(); // form.required (MutationError is null → no alert)
expect(onCreate).not.toHaveBeenCalled();
});
test("a valid submit calls onCreate and the reset clears the inputs", async () => {
const onCreate = vi.fn((_labels: unknown, _uri: unknown, reset: () => void) => reset());
renderApp(
<LabelledRecordCreateForm heading="New" submitLabel="Create" pending={false} error={null} onCreate={onCreate} />,
);
const labelInput = screen.getByLabelText(/^label$/i) as HTMLInputElement;
await userEvent.type(labelInput, "Bronze");
await userEvent.click(screen.getByRole("button", { name: /create/i }));
expect(onCreate).toHaveBeenCalled();
expect((screen.getByLabelText(/^label$/i) as HTMLInputElement).value).toBe("");
});
```
Run: `cd web && pnpm vitest run src/components/labelled-record-create-form.test.tsx`. (The required-error `<p role="alert">` is the only alert when `error={null}`; `LabelEditor`'s label is "Label".)
- [ ] **Step 3: Verify + lint:** `cd web && pnpm vitest run src/components/labelled-record-create-form.test.tsx && pnpm typecheck && pnpm lint`.
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/components/labelled-record-create-form.tsx web/src/components/labelled-record-create-form.test.tsx
git commit -m "feat(web): shared LabelledRecordCreateForm component (#64)"
```
---
# Task 3: `FilteredRecordList<T>`
**Files:** Create `web/src/components/filtered-record-list.tsx`, `web/src/components/filtered-record-list.test.tsx`.
- [ ] **Step 1: Create `web/src/components/filtered-record-list.tsx`:**
```tsx
import { Fragment, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { byLabel } from "../lib/sort";
import { labelText } from "../lib/labels";
import { Input } from "@/components/ui/input";
import { ListSkeleton } from "@/components/ui/skeletons";
type LabelView = components["schemas"]["LabelView"];
/** Filterable, alphabetically-sorted list of labelled records with the standard
* loading / error / empty / no-matches states. The filter input stays visible
* during load (matching the prior page behaviour). */
export function FilteredRecordList<T extends { id: string; labels: LabelView[] }>({
records,
lang,
isLoading,
isError,
loadErrorText,
emptyText,
renderRow,
}: {
records: T[] | undefined;
lang: string;
isLoading: boolean;
isError: boolean;
loadErrorText: string;
emptyText: string;
renderRow: (record: T) => ReactNode;
}) {
const { t } = useTranslation();
const [filter, setFilter] = useState("");
const q = filter.trim().toLowerCase();
const rows = [...(records ?? [])]
.filter((r) => !q || labelText(r.labels, lang).toLowerCase().includes(q))
.sort(byLabel(lang));
return (
<>
<div className="mb-3">
<Input
aria-label={t("common.filter")}
placeholder={t("common.filter")}
value={filter}
onChange={(e) => setFilter(e.target.value)}
/>
</div>
{isLoading ? (
<ListSkeleton className="mb-4" rows={5} />
) : (
<ul className="mb-4">
{isError && <li className="text-sm text-destructive">{loadErrorText}</li>}
{!isError && records?.length === 0 && (
<li className="text-sm text-muted-foreground">{emptyText}</li>
)}
{!isError && records && records.length > 0 && rows.length === 0 && (
<li className="text-sm text-muted-foreground">{t("common.noMatches")}</li>
)}
{rows.map((r) => (
<Fragment key={r.id}>{renderRow(r)}</Fragment>
))}
</ul>
)}
</>
);
}
```
- [ ] **Step 2: Create `web/src/components/filtered-record-list.test.tsx`** (write + run):
```tsx
import { expect, test } from "vitest";
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { FilteredRecordList } from "./filtered-record-list";
import { labelText } from "../lib/labels";
type Rec = { id: string; labels: { lang: string; label: string }[] };
const recs: Rec[] = [
{ id: "a", labels: [{ lang: "en", label: "Alpha" }] },
{ id: "b", labels: [{ lang: "en", label: "Beta" }] },
];
const row = (r: Rec) => <li>{labelText(r.labels, "en")}</li>;
test("filtering narrows the rendered rows", async () => {
renderApp(
<FilteredRecordList records={recs} lang="en" isLoading={false} isError={false}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
expect(screen.getByText("Alpha")).toBeInTheDocument();
expect(screen.getByText("Beta")).toBeInTheDocument();
await userEvent.type(screen.getByLabelText(/filter/i), "alph");
expect(screen.getByText("Alpha")).toBeInTheDocument();
expect(screen.queryByText("Beta")).toBeNull();
});
test("empty records show the empty text", () => {
renderApp(
<FilteredRecordList records={[]} lang="en" isLoading={false} isError={false}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
expect(screen.getByText("EmptyMsg")).toBeInTheDocument();
});
test("non-empty records with a non-matching filter show no-matches", async () => {
renderApp(
<FilteredRecordList records={recs} lang="en" isLoading={false} isError={false}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
await userEvent.type(screen.getByLabelText(/filter/i), "zzz");
expect(screen.getByText(/no matches/i)).toBeInTheDocument();
});
test("an error shows the load-error text", () => {
renderApp(
<FilteredRecordList records={undefined} lang="en" isLoading={false} isError={true}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
expect(screen.getByText("LoadErr")).toBeInTheDocument();
});
```
Run: `cd web && pnpm vitest run src/components/filtered-record-list.test.tsx`.
- [ ] **Step 3: Verify + lint:** `cd web && pnpm vitest run src/components/filtered-record-list.test.tsx && pnpm typecheck && pnpm lint`.
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/components/filtered-record-list.tsx web/src/components/filtered-record-list.test.tsx
git commit -m "feat(web): shared FilteredRecordList component (#64)"
```
---
# Task 4: Wire the adapters + pages, then full gate
**Files:** Modify `web/src/vocab/term-row.tsx`, `web/src/authorities/authority-row.tsx`, `web/src/authorities/authorities-page.tsx`, `web/src/vocab/vocabulary-terms.tsx`.
- [ ] **Step 1: Rewrite `web/src/vocab/term-row.tsx`** (keep the `<TermRow vocabularyId term lang />` API):
```tsx
import type { components } from "../api/schema";
import { useUpdateTerm, useDeleteTerm } from "../api/queries";
import { LabelledRecordRow } from "../components/labelled-record-row";
type TermView = components["schemas"]["TermView"];
export function TermRow({ vocabularyId, term, lang }: { vocabularyId: string; term: TermView; lang: string }) {
const update = useUpdateTerm();
const del = useDeleteTerm();
return (
<LabelledRecordRow
record={term}
lang={lang}
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={update.isPending}
saveError={update.error}
onEditOpen={() => update.reset()}
onSave={(labels, uri, done) =>
update.mutate({ vocabularyId, termId: term.id, external_uri: uri, labels }, { onSuccess: done })}
onDelete={() => del.mutateAsync({ vocabularyId, termId: term.id })}
/>
);
}
```
- [ ] **Step 2: Rewrite `web/src/authorities/authority-row.tsx`** (keep `<AuthorityRow authority kind lang />`):
```tsx
import type { components } from "../api/schema";
import { useUpdateAuthority, useDeleteAuthority } from "../api/queries";
import { LabelledRecordRow } from "../components/labelled-record-row";
type AuthorityView = components["schemas"]["AuthorityView"];
export function AuthorityRow({ authority, kind, lang }: { authority: AuthorityView; kind: string; lang: string }) {
const update = useUpdateAuthority();
const del = useDeleteAuthority();
return (
<LabelledRecordRow
record={authority}
lang={lang}
deleteConfirmKey="actions.confirmDeleteAuthority"
savePending={update.isPending}
saveError={update.error}
onEditOpen={() => update.reset()}
onSave={(labels, uri, done) =>
update.mutate({ id: authority.id, kind, external_uri: uri, labels }, { onSuccess: done })}
onDelete={() => del.mutateAsync({ id: authority.id, kind })}
/>
);
}
```
- [ ] **Step 3: Rewrite `web/src/authorities/authorities-page.tsx`** to use the shared list + form. Keep the `PageTitle`, kind `<nav>`, `Navigate` guard, `useDocumentTitle`, `useBreadcrumb`. Remove the local `labels`/`uri`/`error`/`filter` state, the `onCreate` handler, and now-unused imports (`useState`, `FormEvent`, `LabelEditor`, `MutationError`, `Input`, `Label`, `ListSkeleton`, `byLabel`, `labelText`). New file:
```tsx
import { NavLink, Navigate, useParams } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useAuthorities, useCreateAuthority } from "../api/queries";
import { FilteredRecordList } from "../components/filtered-record-list";
import { LabelledRecordCreateForm } from "../components/labelled-record-create-form";
import { PageTitle } from "@/components/ui/page-title";
import { AuthorityRow } from "./authority-row";
import { focusRing } from "../lib/focus-ring";
import { useDocumentTitle } from "../lib/use-document-title";
import { useBreadcrumb } from "../shell/use-breadcrumb";
import { cn } from "@/lib/utils";
const KINDS = ["person", "organisation", "place"] as const;
export function AuthoritiesPage() {
const { t, i18n } = useTranslation();
const { kind } = useParams();
const lang = i18n.language.startsWith("sv") ? "sv" : "en";
const isValidKind = (KINDS as readonly string[]).includes(kind ?? "");
const currentKind = isValidKind ? (kind as string) : "person";
const { data: authorities, isLoading, isError } = useAuthorities(currentKind);
const create = useCreateAuthority();
useDocumentTitle(t("nav.authorities"));
useBreadcrumb([{ label: t("nav.authorities") }]);
if (!isValidKind) return <Navigate to="/authorities/person" replace />;
return (
<div className="overflow-auto p-4">
<PageTitle className="mb-3">{t("nav.authorities")}</PageTitle>
<nav aria-label={t("nav.authorities")} className="mb-3 flex gap-2">
{KINDS.map((k) => (
<NavLink
key={k}
to={`/authorities/${k}`}
className={({ isActive }) =>
cn("rounded-md px-3 py-1 text-sm", focusRing, isActive ? "bg-primary text-primary-foreground" : "border")
}
>
{t(`authorities.${k}`)}
</NavLink>
))}
</nav>
<FilteredRecordList
records={authorities}
lang={lang}
isLoading={isLoading}
isError={isError}
loadErrorText={t("authorities.loadError")}
emptyText={t("authorities.empty")}
renderRow={(a) => <AuthorityRow authority={a} kind={currentKind} lang={lang} />}
/>
<LabelledRecordCreateForm
heading={`${t("authorities.new")} · ${t(`authorities.${currentKind}`)}`}
submitLabel={t("authorities.create")}
pending={create.isPending}
error={create.error}
onCreate={(labels, uri, reset) =>
create.mutate({ kind: currentKind, external_uri: uri, labels }, { onSuccess: reset })}
/>
</div>
);
}
```
(Note: the original passed `kind: kind as string` to `create.mutate`; `currentKind` is equivalent here since the `isValidKind` guard already returned otherwise — use `currentKind`.)
- [ ] **Step 4: Rewrite `web/src/vocab/vocabulary-terms.tsx`** to use the shared list + form. Keep the `vocab.terms` caption + breadcrumb + the `useVocabularies` lookup. Remove the local form/list state + now-unused imports:
```tsx
import { useParams } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useTerms, useAddTerm, useVocabularies } from "../api/queries";
import { useBreadcrumb } from "../shell/use-breadcrumb";
import { FilteredRecordList } from "../components/filtered-record-list";
import { LabelledRecordCreateForm } from "../components/labelled-record-create-form";
import { TermRow } from "./term-row";
export function VocabularyTerms() {
const { t, i18n } = useTranslation();
const { id } = useParams();
const lang = i18n.language.startsWith("sv") ? "sv" : "en";
const { data: terms, isLoading, isError } = useTerms(id);
const addTerm = useAddTerm();
const { data: vocabularies } = useVocabularies();
const vocabKey = vocabularies?.find((v) => v.id === id)?.key;
useBreadcrumb(
vocabKey
? [{ label: t("nav.vocabularies"), to: "/vocabularies" }, { label: vocabKey }]
: [{ label: t("nav.vocabularies"), to: "/vocabularies" }],
);
if (!id) return null;
return (
<div className="overflow-auto p-4">
<div className="mb-2 label-caption">{t("vocab.terms")}</div>
<FilteredRecordList
records={terms}
lang={lang}
isLoading={isLoading}
isError={isError}
loadErrorText={t("vocab.loadError")}
emptyText={t("vocab.noTerms")}
renderRow={(term) => <TermRow vocabularyId={id} term={term} lang={lang} />}
/>
<LabelledRecordCreateForm
heading={t("vocab.addTerm")}
submitLabel={t("vocab.addTerm")}
pending={addTerm.isPending}
error={addTerm.error}
onCreate={(labels, uri, reset) =>
addTerm.mutate({ vocabularyId: id, external_uri: uri, labels }, { onSuccess: reset })}
/>
</div>
);
}
```
(Note: `useTerms(id)` and `if (!id) return null``id` is `string | undefined`; the hooks accept it, and the `!id` guard runs after the hooks, matching the original order. `renderRow`/`onCreate` use the narrowed `id` inside JSX where `!id` already returned — but to satisfy TS, `id` is `string` after the guard since the guard is before the `return` JSX. Confirm typecheck is clean; if TS still widens, the original already used `id` directly in the same spot, so it resolves.)
- [ ] **Step 5: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. The existing `term-row.test.tsx`, `authorities.test.tsx`, `vocabularies.test.tsx` MUST pass unchanged (behavior-preserving). Report test totals, largest chunk (gz), and the `check:colors` line. If an existing test fails, the refactor changed behavior — fix the adapter/page to match, do NOT edit the test.
- [ ] **Step 6: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 7: Manual smoke (recommended).** `pnpm dev`: on Authorities and Vocabulary-terms — filter narrows the list; create with an empty label shows the required error; create/edit/delete work; a failed save shows the inline message and keeps the row editable; the authorities kind-tabs + breadcrumbs are unchanged.
- [ ] **Step 8: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/vocab/term-row.tsx web/src/authorities/authority-row.tsx web/src/authorities/authorities-page.tsx web/src/vocab/vocabulary-terms.tsx
git commit -m "refactor(web): term/authority rows + pages adopt shared CRUD components (#64)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 three components with the prop shapes (T1-T3); AC2 rows + pages de-duplicated (T4 S1-S4); AC3 existing tests green + 3 new component tests (T1-T3 tests, T4 S5); AC4 behavior preserved — edit/save/delete/create/validation/filter/4-states/kind-nav/breadcrumb (T4 + the existing-test guard); AC5 gate/check:size/no-new-keys/codename (T4 S5-S6). ✓
**Placeholder scan:** every component + adapter shown in full; tests have concrete assertions; the two soft spots (delete-dialog portal DOM in T1; `id` narrowing in T4) name the exact mitigation/precedent. No TBD. ✓
**Type/consistency:** `RecordLike = { id; labels: LabelView[]; external_uri }` (T1) is the row's `record`; `TermView`/`AuthorityView` structurally satisfy it (both have those fields). `onSave(labels: LabelInput[], uri: string | null, done)` (T1) matches the adapters' `update.mutate({…, external_uri: uri, labels}, { onSuccess: done })` (T4). `LabelledRecordCreateForm.onCreate(labels, uri, reset)` (T2) matches `create.mutate({…}, { onSuccess: reset })` (T4). `FilteredRecordList<T extends { id; labels: LabelView[] }>` (T3) consumed with `authorities`/`terms` (T4). ✓
## Notes
- No new dependency, no new i18n keys, `components/ui/*` untouched. Net code reduction → `check:size` should not grow.
- `TermRow`/`AuthorityRow` keep their public props so `term-row.test.tsx` stays valid unchanged.
- `vocabulary-list.tsx` (key-based vocabularies) is deliberately NOT touched.
@@ -0,0 +1,338 @@
# Bundle Vendor-Split + Test-Gap Fills — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Split framework deps into cache-stable vendor chunks, and fill the audit's unit-test (prune-fields, delete-confirm-dialog in-use, labels, format-date) + combobox-story gaps.
**Architecture:** Task 1 adds `build.rollupOptions.output.manualChunks` to `vite.config.ts` (production build only). Task 2 adds 4 jsdom unit tests for pure/critical units. Task 3 adds a Storybook story for the combobox primitive and runs the full gate. All additive/behavior-preserving.
**Tech Stack:** Vite 6 (Rollup), React 19 + TS + pnpm, Vitest 4 (jsdom + storybook-browser projects), Storybook, RTL + MSW.
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; app/test source double-quote+semicolon; **stories use single-quote + no-semicolon** (mirror existing `ui/*.stories.tsx`). Run a single test pass per task.
**Spec:** `docs/superpowers/specs/2026-06-09-bundle-and-tests-design.md`
**Key facts:**
- `scripts/check-bundle-size.mjs` fails if the **largest** `dist/assets/*.js` gzip exceeds 250 KB; today ~216.5 KB (one chunk).
- `vite.config.ts` exports `defineConfig({ plugins, resolve, server, test })` — no `build` block. Deps: `react`, `react-dom`, `react-router-dom` (v7, re-exports `react-router`), `@tanstack/react-query`, `@base-ui/react`, `i18next`, `react-i18next`.
- `objects/prune-fields.ts`: `pruneFields(fields: Record<string, unknown>, localizedTextKeys: Set<string>, defaultLang: string): Record<string, unknown>`.
- `lib/labels.ts`: `labelText(labels: LabelView[], lang: string): string` (lang match → `"en"` → first → `""`).
- `lib/format-date.ts`: `formatDate(value: unknown, lang: string): string` (non-string null→`"—"`, non-string→`String(value)`; invalid→returns value; valid `YYYY-MM-DD``Intl.DateTimeFormat(lang,{dateStyle:"medium"})`, parsed at local midnight).
- `components/delete-confirm-dialog.tsx`: `DeleteConfirmDialog({ description, onConfirm: () => Promise<void>, triggerLabel? })`. Trigger is a `Button` labelled `t("actions.delete")` ("Delete"); the confirm action is also labelled `t("actions.delete")`. On a thrown error it sets a message via `errorMessageKey(err)` and **returns without closing**; on success it closes. `actions.inUse` (en) = "Can't delete — used by {{count}} object(s). Clear those fields first." `InUseError` is exported from `../api/errors` (and re-exported by `../api/queries`).
- `components/ui/combobox.tsx` exports `ComboboxRoot<Value>`, `ComboboxInputGroup`, `ComboboxInput`, `ComboboxClear`, `ComboboxTrigger`, `ComboboxPopup`, `ComboboxList`, `ComboboxItem`, `ComboboxItemIndicator`, `ComboboxEmpty`. See `objects/options-combobox.tsx` for the composition (ComboboxRoot props: `items`, `value`, `onValueChange`, `itemToStringLabel`, `isItemEqualToValue`; ComboboxList takes a render-function child).
- Story format (from `components/ui/badge.stories.tsx`): `import type { Meta, StoryObj } from '@storybook/react-vite'`; `import { expect } from 'storybook/test'`; `const meta = { component, tags: ['ai-generated'] } satisfies Meta<typeof X>`; `export default meta`; `type Story = StoryObj<typeof meta>`; `export const Default: Story = { play: async ({ canvas }) => {...} }`.
---
# Task 1: Vendor split (`vite.config.ts`)
**Files:** Modify `web/vite.config.ts`.
- [ ] **Step 1: Add the `build` block.** In `web/vite.config.ts`, add a top-level `build` key to the `defineConfig({...})` object (sibling of `plugins`/`resolve`/`server`/`test`):
```ts
build: {
rollupOptions: {
output: {
manualChunks: {
react: ["react", "react-dom", "react-router", "react-router-dom"],
"base-ui": ["@base-ui/react"],
query: ["@tanstack/react-query"],
i18n: ["i18next", "react-i18next"],
},
},
},
},
```
- [ ] **Step 2: Build + verify chunk split + budget:**
```bash
cd web && pnpm build && pnpm check:size && ls -1 dist/assets/*.js
```
Expected: build succeeds; `check:size` prints `largest JS chunk: <name> = <kb> KB gz (budget 250 KB)` and passes; `dist/assets/` now contains separate `react-*.js`, `base-ui-*.js`, `query-*.js`, `i18n-*.js` chunks plus the app entry. Report the largest chunk size (should be a vendor chunk, well under 250). If the build warns about React being in multiple chunks or a chunk is oversized, confirm `react`+`react-dom`+`react-router`+`react-router-dom` are all in the one `react` group and adjust.
- [ ] **Step 3: typecheck + lint** (config change shouldn't affect them, but confirm):
```bash
cd web && pnpm typecheck && pnpm lint
```
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/vite.config.ts
git commit -m "build(web): split framework deps into cache-stable vendor chunks (#67)"
```
---
# Task 2: Unit-test gaps (prune-fields, labels, format-date, delete-confirm-dialog)
**Files:** Create `web/src/objects/prune-fields.test.ts`, `web/src/lib/labels.test.ts`, `web/src/lib/format-date.test.ts`, `web/src/components/delete-confirm-dialog.test.tsx`.
- [ ] **Step 1: `web/src/objects/prune-fields.test.ts`:**
```ts
import { expect, test } from "vitest";
import { pruneFields } from "./prune-fields";
test("drops empty/null/undefined scalars, keeps real scalars", () => {
const out = pruneFields(
{ a: "x", b: "", c: null, d: undefined, e: 0, f: false },
new Set(),
"en",
);
expect(out).toEqual({ a: "x", e: 0, f: false });
});
test("a localized_text key keeps only the default-language entry", () => {
const out = pruneFields(
{ title: { en: "Bowl", sv: "Skål" } },
new Set(["title"]),
"sv",
);
expect(out).toEqual({ title: { sv: "Skål" } });
});
test("a non-localized object value keeps all non-empty entries", () => {
const out = pruneFields(
{ dims: { w: "10", h: "", d: "5" } },
new Set(),
"en",
);
expect(out).toEqual({ dims: { w: "10", d: "5" } });
});
test("an object value left with no entries is dropped entirely", () => {
const out = pruneFields(
{ title: { en: "Bowl" }, empty: { en: "", sv: "" } },
new Set(["title", "empty"]),
"sv",
);
// title has no `sv` entry → empty after filtering → dropped; empty → dropped
expect(out).toEqual({});
});
```
Run: `cd web && pnpm vitest run src/objects/prune-fields.test.ts`. (If a case's expectation mismatches the real semantics, re-read `prune-fields.ts` and correct the EXPECTATION to the true behavior — do not change the source.)
- [ ] **Step 2: `web/src/lib/labels.test.ts`:**
```ts
import { expect, test } from "vitest";
import { labelText } from "./labels";
const labels = [
{ lang: "en", label: "Bowl" },
{ lang: "sv", label: "Skål" },
];
test("returns the exact-language label when present", () => {
expect(labelText(labels, "sv")).toBe("Skål");
});
test("falls back to the English label when the requested language is missing", () => {
expect(labelText(labels, "de")).toBe("Bowl");
});
test("falls back to the first label when neither the language nor English is present", () => {
expect(labelText([{ lang: "fr", label: "Bol" }], "de")).toBe("Bol");
});
test("returns an empty string for no labels", () => {
expect(labelText([], "en")).toBe("");
});
```
Run: `cd web && pnpm vitest run src/lib/labels.test.ts`.
- [ ] **Step 3: `web/src/lib/format-date.test.ts`:**
```ts
import { expect, test } from "vitest";
import { formatDate } from "./format-date";
test("formats a date-only string in the locale without a timezone day-shift", () => {
// local-midnight parse must keep the calendar day (3), not shift to the 2nd
expect(formatDate("1962-04-03", "en")).toBe("Apr 3, 1962");
});
test("returns the em-dash placeholder for null", () => {
expect(formatDate(null, "en")).toBe("—");
});
test("returns an unparseable string unchanged", () => {
expect(formatDate("not-a-date", "en")).toBe("not-a-date");
});
test("stringifies a non-string, non-null value", () => {
expect(formatDate(42, "en")).toBe("42");
});
```
Run: `cd web && pnpm vitest run src/lib/format-date.test.ts`. (The CI/runtime is full-ICU Node 22, so en `dateStyle:"medium"``"Apr 3, 1962"`. If your local ICU formats slightly differently, assert the equivalent string but keep the day as `3` — the day-shift guard is the point.)
- [ ] **Step 4: `web/src/components/delete-confirm-dialog.test.tsx`:**
```tsx
import { expect, test, vi } from "vitest";
import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { DeleteConfirmDialog } from "./delete-confirm-dialog";
import { InUseError } from "../api/errors";
test("delete-in-use shows the in-use count and keeps the dialog open", async () => {
const onConfirm = vi.fn<() => Promise<void>>().mockRejectedValue(new InUseError(3));
renderApp(<DeleteConfirmDialog description="Delete this term?" onConfirm={onConfirm} />);
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
const dialog = within(document.body);
const buttons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(buttons[buttons.length - 1]); // the confirm action
expect(await dialog.findByText(/used by 3/i)).toBeInTheDocument();
// dialog stays open: its description is still shown
expect(dialog.getByText("Delete this term?")).toBeInTheDocument();
});
test("a clean confirm closes the dialog", async () => {
const onConfirm = vi.fn<() => Promise<void>>().mockResolvedValue(undefined);
renderApp(<DeleteConfirmDialog description="Delete this term?" onConfirm={onConfirm} />);
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
const dialog = within(document.body);
const buttons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(buttons[buttons.length - 1]);
await waitFor(() => expect(dialog.queryByText("Delete this term?")).toBeNull());
expect(onConfirm).toHaveBeenCalledTimes(1);
});
```
Run: `cd web && pnpm vitest run src/components/delete-confirm-dialog.test.tsx`. (Before opening, only the trigger "Delete" button exists. After opening, the portal adds the action "Delete"; `findAllByRole` returns both, click the last. `actions.inUse` en contains "used by 3". If `vi.fn<() => Promise<void>>()` generic syntax trips the TS/vitest version, use `vi.fn(() => Promise.reject(new InUseError(3)))` / `vi.fn(() => Promise.resolve())`.)
- [ ] **Step 5: Verify all four (vitest ONCE) + typecheck + lint:**
```bash
cd web && pnpm vitest run src/objects/prune-fields.test.ts src/lib/labels.test.ts src/lib/format-date.test.ts src/components/delete-confirm-dialog.test.tsx && pnpm typecheck && pnpm lint
```
All green.
- [ ] **Step 6: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/objects/prune-fields.test.ts web/src/lib/labels.test.ts web/src/lib/format-date.test.ts web/src/components/delete-confirm-dialog.test.tsx
git commit -m "test(web): cover prune-fields, labels, format-date, delete-in-use dialog (#67)"
```
---
# Task 3: Combobox story + full gate
**Files:** Create `web/src/components/ui/combobox.stories.tsx`.
- [ ] **Step 1: Read `web/src/objects/options-combobox.tsx`** to mirror the exact `ComboboxRoot` composition (generic, props, the `ComboboxList` render-function child), then create `web/src/components/ui/combobox.stories.tsx` (single-quote + no-semicolon, matching `badge.stories.tsx`):
```tsx
import type { Meta, StoryObj } from '@storybook/react-vite'
import { useState } from 'react'
import { expect } from 'storybook/test'
import {
ComboboxRoot,
ComboboxInputGroup,
ComboboxInput,
ComboboxClear,
ComboboxTrigger,
ComboboxPopup,
ComboboxList,
ComboboxItem,
ComboboxItemIndicator,
ComboboxEmpty,
} from './combobox'
const fruits = ['Apple', 'Apricot', 'Banana', 'Cherry']
function ComboboxDemo() {
const [value, setValue] = useState<string | null>(null)
return (
<ComboboxRoot<string | null>
items={fruits}
value={value}
onValueChange={setValue}
itemToStringLabel={(item) => item ?? ''}
isItemEqualToValue={(a, b) => a === b}
>
<ComboboxInputGroup>
<ComboboxInput placeholder='Pick a fruit' />
<ComboboxClear aria-label='Clear' />
<ComboboxTrigger aria-label='Open' />
</ComboboxInputGroup>
<ComboboxPopup>
<ComboboxEmpty>No matches</ComboboxEmpty>
<ComboboxList>
{(item: string) => (
<ComboboxItem key={item} value={item}>
<ComboboxItemIndicator className='text-primary'></ComboboxItemIndicator>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxPopup>
</ComboboxRoot>
)
}
const meta = {
component: ComboboxDemo,
tags: ['ai-generated'],
} satisfies Meta<typeof ComboboxDemo>
export default meta
type Story = StoryObj<typeof meta>
export const Default: Story = {
play: async ({ canvas }) => {
await expect(canvas.getByPlaceholderText('Pick a fruit')).toBeInTheDocument()
},
}
```
(Adjust the `ComboboxRoot` generic/props to match `options-combobox.tsx` exactly if they differ — the goal is a rendering `Default` story; keep the `play` minimal to stay stable in the browser project.)
- [ ] **Step 2: Run the storybook project for this story** (browser test):
```bash
cd web && pnpm vitest run src/components/ui/combobox.stories.tsx
```
Expected: the `Default` story passes (input renders). If the Base UI combobox API needs a different prop shape, fix the story (mirror `options-combobox.tsx`); do not weaken the assertion below a render check.
- [ ] **Step 3: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report total test count (should be prior 268 + 4 unit + 1 story ≈ 273), the largest chunk (gz) from check:size (a vendor chunk, < 250), and the check:colors line.
- [ ] **Step 4: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 5: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/components/ui/combobox.stories.tsx
git commit -m "test(web): add a Storybook story for the combobox primitive (#67)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 vendor split + verify (T1); AC2 the 4 unit tests (T2 S1-S4); AC3 combobox story (T3 S1-S2); AC4 full gate + codename (T3 S3-S4). ✓
**Placeholder scan:** every test/story is complete code with concrete assertions; manualChunks is exact; the format-date ICU note and the vi.fn generic note give concrete fallbacks, not vague TODOs. ✓
**Type/consistency:** `pruneFields(fields, Set, lang)`, `labelText(labels, lang)`, `formatDate(value, lang)`, `DeleteConfirmDialog({description,onConfirm})`, `InUseError(count)` from `../api/errors` — all match the spec's documented signatures; the combobox story imports the exact exports from `./combobox`. ✓
## Notes
- No new dependency, no new i18n keys. `build.rollupOptions` only affects `pnpm build`; the test projects are unaffected.
- The combobox story is the one item with a (small) CI cost (a browser test on the slow runner); it's isolated in its own task/commit so it can be dropped cleanly if undesired.
- `check:size` should report a smaller largest chunk after the split (a vendor chunk, not the combined app+vendor chunk).
@@ -0,0 +1,425 @@
# Responsive Master/Detail (vocab/search/fields) — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Bring the Vocabularies, Search, and Fields master/detail screens to the responsive behavior the Objects screen already has — preserving wide side-by-side, collapsing to single-column + a slide-in Drawer (vocab/search) or a stack (fields) on narrow — via one shared `DetailDrawer`.
**Architecture:** Generalize the objects-specific drawer into a reusable `components/detail-drawer.tsx` and retrofit Objects onto it (Task 1). Make vocabularies + search responsive with `useMediaQuery("(min-width: 1024px)")` + `useMatch` + the shared drawer (Tasks 2-3). Make fields a pure-CSS responsive stack (Task 4) + full gate. Behavior-preserving on wide; only narrow changes.
**Tech Stack:** React 19 + TS + pnpm, React Router 7, Base UI Drawer, Tailwind v4, Vitest 4 (jsdom) + RTL + MSW.
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; double-quote+semicolon; token classes only. Breakpoint **1024px** (`useMediaQuery("(min-width: 1024px)")` / Tailwind `lg:`), matching Objects. Run a single test pass per task.
**Spec:** `docs/superpowers/specs/2026-06-09-responsive-master-detail-design.md`
**Key facts:**
- `lib/use-media-query.ts`: `useMediaQuery(query): boolean` (SSR-safe matchMedia).
- `objects/object-detail-drawer.tsx` (to be generalized + deleted) is: `<Drawer open onOpenChange={(n)=>{if(!n)onClose()}} swipeDirection="right"><DrawerContent aria-label={t("objects.detailTitle")}><div className="flex justify-end border-b p-2"><DrawerClose aria-label={t("actions.closeDetail")} render={<Button variant="ghost" size="icon-sm" />}><X className="size-4" aria-hidden="true" /></DrawerClose></div><div className="flex-1 overflow-auto"><Outlet/></div></DrawerContent></Drawer>`. `Drawer`/`DrawerClose`/`DrawerContent` from `@/components/ui/drawer`.
- `objects/objects-page.tsx` currently `lazy()`-loads `ObjectDetailDrawer` + wraps in `<Suspense fallback={null}>`; narrow branch renders `{open && <Suspense><ObjectDetailDrawer open={open} onClose={closeDetail} /></Suspense>}`. The WIDE pane has its own close `<Button … aria-label={t("actions.closeDetail")}><X/></Button>` — keep it (`X` import stays).
- Existing i18n (no new keys): `objects.detailTitle` ("Object detail"), `vocab.terms` ("Terms"), `actions.closeDetail` ("Close detail").
- `vocabularies-page.tsx`: `<div flex h-full flex-col><PageTitle>…</PageTitle><div grid grid-cols-[20rem_1fr]><div border-r><VocabularyList/></div><div><Outlet/></div></div></div>`. Routes: `/vocabularies` (index `SelectVocabularyPrompt`) + `/vocabularies/:id` (`VocabularyTerms`).
- `search-page.tsx`: same shape, `grid-cols-[24rem_1fr]`, `<SearchPanel/>`, routes `/search` (index `SelectSearchPrompt`) + `/search/:id` (`ObjectDetail`).
- `fields-page.tsx`: `useState`-driven; `<div grid grid-cols-[20rem_1fr]><div border-r><FieldList selectedKey onSelect/></div><div><FieldForm key editing onDone/></div></div>`. No routes.
- Test harness: `objects-page.test.tsx` has a `setViewport(wide: boolean)` helper that overrides `window.matchMedia` to match `(min-width: 1024px)` only when `wide`; default test setup is narrow (`matches:false`); `afterEach(() => vi.restoreAllMocks())`. Narrow-drawer assertion pattern: deep-link to `:id`, then `within(document.body).findByRole(...)` for the portaled drawer + assert the `/close detail/i` button. No `vocabularies-page`/`search-page`/`fields-page` test files exist yet.
---
# Task 1: Shared `DetailDrawer` + retrofit Objects
**Files:** Create `web/src/components/detail-drawer.tsx`, `web/src/components/detail-drawer.test.tsx`; Modify `web/src/objects/objects-page.tsx`; Delete `web/src/objects/object-detail-drawer.tsx`.
- [ ] **Step 1: Create `web/src/components/detail-drawer.tsx`:**
```tsx
import type { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { X } from "lucide-react";
import { Drawer, DrawerClose, DrawerContent } from "@/components/ui/drawer";
import { Button } from "@/components/ui/button";
/** A right-sliding Base UI Drawer for a master/detail "detail" on narrow viewports.
* Provides the close affordance + an accessible dialog name; the caller supplies the content. */
export function DetailDrawer({
open,
onClose,
ariaLabel,
children,
}: {
open: boolean;
onClose: () => void;
ariaLabel: string;
children: ReactNode;
}) {
const { t } = useTranslation();
return (
<Drawer
open={open}
onOpenChange={(next) => {
if (!next) onClose();
}}
swipeDirection="right"
>
<DrawerContent aria-label={ariaLabel}>
<div className="flex justify-end border-b p-2">
<DrawerClose
aria-label={t("actions.closeDetail")}
render={<Button variant="ghost" size="icon-sm" />}
>
<X className="size-4" aria-hidden="true" />
</DrawerClose>
</div>
<div className="flex-1 overflow-auto">{children}</div>
</DrawerContent>
</Drawer>
);
}
```
- [ ] **Step 2: Create `web/src/components/detail-drawer.test.tsx`** (write + run):
```tsx
import { expect, test, vi } from "vitest";
import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { DetailDrawer } from "./detail-drawer";
test("renders children in a named drawer and closes via the close button", async () => {
const onClose = vi.fn();
renderApp(
<DetailDrawer open onClose={onClose} ariaLabel="Object detail">
<p>detail body</p>
</DetailDrawer>,
);
const body = within(document.body);
expect(await body.findByText("detail body")).toBeInTheDocument();
await userEvent.click(body.getByRole("button", { name: /close detail/i }));
expect(onClose).toHaveBeenCalled();
});
```
Run: `cd web && pnpm vitest run src/components/detail-drawer.test.tsx`. (If Base UI requires `open` to mount the portal, it's set; the content is portaled to `document.body`.)
- [ ] **Step 3: Retrofit `web/src/objects/objects-page.tsx`.** Remove `import { lazy, Suspense } from "react";` and the `const ObjectDetailDrawer = lazy(...)` block; add `import { DetailDrawer } from "../components/detail-drawer";`. Replace the narrow `return` block's drawer with:
```tsx
return (
<div className="h-full">
{table}
{open && (
<DetailDrawer open={open} onClose={closeDetail} ariaLabel={t("objects.detailTitle")}>
<Outlet />
</DetailDrawer>
)}
</div>
);
```
(Keep everything else — the wide grid branch with its own close `<Button><X/></Button>` is unchanged, so the `X` and `Button` imports stay.)
- [ ] **Step 4: Delete the old drawer:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git rm web/src/objects/object-detail-drawer.tsx
```
(Confirm no other importer: `git grep -n object-detail-drawer web/src` → only objects-page, now changed.)
- [ ] **Step 5: Verify (vitest ONCE) + typecheck + lint:**
```bash
cd web && pnpm vitest run src/components/detail-drawer.test.tsx src/objects/objects-page.test.tsx && pnpm typecheck && pnpm lint
```
Expected: green. The objects-page narrow + wide tests must pass unchanged (the shared `DetailDrawer` renders the same drawer + `/close detail/i` button).
- [ ] **Step 6: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/components/detail-drawer.tsx web/src/components/detail-drawer.test.tsx web/src/objects/objects-page.tsx
git rm -q web/src/objects/object-detail-drawer.tsx 2>/dev/null; git add -A web/src/objects
git commit -m "refactor(web): shared DetailDrawer; objects-page uses it (#58)"
```
---
# Task 2: Responsive Vocabularies page
**Files:** Modify `web/src/vocab/vocabularies-page.tsx`; Create `web/src/vocab/vocabularies-page.test.tsx`.
- [ ] **Step 1: Rewrite `web/src/vocab/vocabularies-page.tsx`:**
```tsx
import { Outlet, useMatch, useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { VocabularyList } from "./vocabulary-list";
import { DetailDrawer } from "../components/detail-drawer";
import { useMediaQuery } from "../lib/use-media-query";
import { useDocumentTitle } from "../lib/use-document-title";
import { useBreadcrumb } from "../shell/use-breadcrumb";
import { PageTitle } from "@/components/ui/page-title";
export function VocabulariesPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const detailMatch = useMatch("/vocabularies/:id");
const open = Boolean(detailMatch);
const isWide = useMediaQuery("(min-width: 1024px)");
useDocumentTitle(t("nav.vocabularies"));
useBreadcrumb([{ label: t("nav.vocabularies") }]);
const close = () => navigate("/vocabularies");
return (
<div className="flex h-full flex-col">
<PageTitle className="px-4 pt-4 pb-2">{t("nav.vocabularies")}</PageTitle>
{isWide ? (
<div className="grid flex-1 grid-cols-[20rem_1fr] overflow-hidden">
<div className="overflow-hidden border-r">
<VocabularyList />
</div>
<div className="overflow-hidden">
<Outlet />
</div>
</div>
) : (
<div className="flex-1 overflow-hidden">
<VocabularyList />
</div>
)}
{!isWide && open && (
<DetailDrawer open={open} onClose={close} ariaLabel={t("vocab.terms")}>
<Outlet />
</DetailDrawer>
)}
</div>
);
}
```
(The `<Outlet/>` is rendered in exactly one place: the wide grid, OR the narrow drawer when `open`. On narrow with no `:id`, neither renders the Outlet — just the list.)
- [ ] **Step 2: Create `web/src/vocab/vocabularies-page.test.tsx`.** Read `web/src/test/fixtures.ts` + `web/src/test/handlers.ts` for the vocabularies list + terms handlers and a real vocabulary id. Mirror the `objects-page.test.tsx` `setViewport` harness:
```tsx
import { afterEach, expect, test, vi } from "vitest";
import { screen, within } from "@testing-library/react";
import { Route, Routes } from "react-router-dom";
import { renderApp } from "../test/render";
import { VocabulariesPage } from "./vocabularies-page";
import { VocabularyTerms } from "./vocabulary-terms";
import { SelectVocabularyPrompt } from "./select-vocabulary-prompt";
function setViewport(wide: boolean) {
Object.defineProperty(window, "matchMedia", {
value: (query: string): MediaQueryList =>
({
matches: wide && query === "(min-width: 1024px)",
media: query,
onchange: null,
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
}) as MediaQueryList,
writable: true,
});
}
afterEach(() => vi.restoreAllMocks());
function tree() {
return (
<Routes>
<Route path="/vocabularies" element={<VocabulariesPage />}>
<Route index element={<SelectVocabularyPrompt />} />
<Route path=":id" element={<VocabularyTerms />} />
</Route>
</Routes>
);
}
test("narrow: a selected vocabulary's detail renders in a portaled drawer", async () => {
setViewport(false);
renderApp(tree(), { route: `/vocabularies/<VOCAB_ID>` });
const body = within(document.body);
expect(
await body.findByRole("button", { name: /close detail/i }, { timeout: 5000 }),
).toBeInTheDocument();
});
test("wide: a selected vocabulary renders inline (no detail drawer)", async () => {
setViewport(true);
renderApp(tree(), { route: `/vocabularies/<VOCAB_ID>` });
// the list (master) is present and there is NO close-detail button (inline pane, not a drawer)
expect(await screen.findByRole("button", { name: /close detail/i }).catch(() => null)).toBeNull();
});
```
Replace `<VOCAB_ID>` with a real id from `fixtures.ts`. The narrow test asserts the drawer is present (the `/close detail/i` button only exists inside `DetailDrawer`). For the wide test, prefer a positive assertion that the master + inline detail both render (e.g. `await screen.findByText(<a stable vocab list item or the vocab.terms caption>)`) AND `screen.queryByRole("button", { name: /close detail/i })` is null. Adjust the queries to the fixtures' actual rendered text; the load-bearing checks are: **narrow → close-detail button present (drawer); wide → close-detail button absent (inline)**. Reuse the default MSW handlers (don't add new ones unless a handler is missing).
- [ ] **Step 3: Verify (vitest ONCE) + typecheck + lint:**
```bash
cd web && pnpm vitest run src/vocab/vocabularies-page.test.tsx src/vocab && pnpm typecheck && pnpm lint
```
Green. (Existing vocab tests stay green.)
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/vocab/vocabularies-page.tsx web/src/vocab/vocabularies-page.test.tsx
git commit -m "feat(web): responsive Vocabularies master/detail (drawer on narrow) (#58)"
```
---
# Task 3: Responsive Search page
**Files:** Modify `web/src/search/search-page.tsx`; Create `web/src/search/search-page.test.tsx`.
- [ ] **Step 1: Rewrite `web/src/search/search-page.tsx`** (same pattern as vocab; `24rem` master, `SearchPanel`, route `"/search/:id"`, close `"/search"`, drawer ariaLabel `t("objects.detailTitle")` since the search detail is an object):
```tsx
import { Outlet, useMatch, useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { SearchPanel } from "./search-panel";
import { DetailDrawer } from "../components/detail-drawer";
import { useMediaQuery } from "../lib/use-media-query";
import { useDocumentTitle } from "../lib/use-document-title";
import { useBreadcrumb } from "../shell/use-breadcrumb";
import { PageTitle } from "@/components/ui/page-title";
export function SearchPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const detailMatch = useMatch("/search/:id");
const open = Boolean(detailMatch);
const isWide = useMediaQuery("(min-width: 1024px)");
useDocumentTitle(t("nav.search"));
useBreadcrumb([{ label: t("nav.search") }]);
const close = () => navigate("/search");
return (
<div className="flex h-full flex-col">
<PageTitle className="px-4 pt-4 pb-2">{t("nav.search")}</PageTitle>
{isWide ? (
<div className="grid flex-1 grid-cols-[24rem_1fr] overflow-hidden">
<div className="overflow-hidden border-r">
<SearchPanel />
</div>
<div className="overflow-hidden">
<Outlet />
</div>
</div>
) : (
<div className="flex-1 overflow-hidden">
<SearchPanel />
</div>
)}
{!isWide && open && (
<DetailDrawer open={open} onClose={close} ariaLabel={t("objects.detailTitle")}>
<Outlet />
</DetailDrawer>
)}
</div>
);
}
```
- [ ] **Step 2: Create `web/src/search/search-page.test.tsx`** mirroring the vocab test (the `setViewport` helper, the same narrow→close-detail-present / wide→absent discriminator). Tree: `<Route path="/search" element={<SearchPage/>}><Route index element={<SelectSearchPrompt/>}/><Route path=":id" element={<ObjectDetail/>}/></Route>`. Deep-link `/search/<OBJECT_ID>` (use a real object id from `fixtures.ts`; the search detail loads the object via the same `/api/admin/objects/{id}` handler the objects tests use). Narrow → `within(document.body).findByRole("button", { name: /close detail/i })` present; wide → absent + the object detail renders inline. Reuse the default MSW handlers.
- [ ] **Step 3: Verify (vitest ONCE) + typecheck + lint:**
```bash
cd web && pnpm vitest run src/search/search-page.test.tsx src/search && pnpm typecheck && pnpm lint
```
Green.
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/search/search-page.tsx web/src/search/search-page.test.tsx
git commit -m "feat(web): responsive Search master/detail (drawer on narrow) (#58)"
```
---
# Task 4: Responsive Fields page (CSS stack) + full gate
**Files:** Modify `web/src/fields/fields-page.tsx`; Create `web/src/fields/fields-page.test.tsx`.
- [ ] **Step 1: Make `fields-page.tsx` a responsive stack.** Change the grid container + the list pane's border so it stacks on narrow and is side-by-side on `lg`:
```tsx
<div className="grid flex-1 grid-cols-1 overflow-auto lg:grid-cols-[20rem_1fr] lg:overflow-hidden">
<div className="overflow-hidden border-b lg:border-r lg:border-b-0">
<FieldList selectedKey={selected?.key ?? null} onSelect={setSelected} />
</div>
<div className="overflow-hidden">
<FieldForm
key={selected?.key ?? "create"}
editing={selected}
onDone={() => setSelected(null)}
/>
</div>
</div>
```
(On narrow: single column — list then form, the grid container scrolls (`overflow-auto`), the list gets a bottom divider. On `lg`: the two-column grid with the list's right border, clipped overflow as before. If the stacked panes still clip awkwardly in a manual smoke, adjust the narrow pane `overflow` — keep `lg:` behavior identical to today.)
- [ ] **Step 2: Create `web/src/fields/fields-page.test.tsx`:**
```tsx
import { expect, test } from "vitest";
import { screen } from "@testing-library/react";
import { renderApp } from "../test/render";
import { FieldsPage } from "./fields-page";
test("renders the field list and the field form, in a responsive grid", async () => {
const { container } = renderApp(<FieldsPage />);
// both panes present (master + detail)
expect(await screen.findByText(/fields/i)).toBeInTheDocument();
// the responsive grid: single-column by default, two-column at lg
const grid = container.querySelector("div.grid");
expect(grid?.className).toContain("grid-cols-1");
expect(grid?.className).toContain("lg:grid-cols-[20rem_1fr]");
});
```
Run: `cd web && pnpm vitest run src/fields/fields-page.test.tsx`. (Adjust the `findByText` to a stable rendered string — the `fields.title` PageTitle, the field-list, or the field-form's "Key" label. jsdom can't measure layout, so the class assertion is the responsive guard.)
- [ ] **Step 3: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. Report total test count, largest chunk (gz), the check:colors line. (`check:size` should be unchanged-or-smaller — the objects drawer's separate lazy chunk folds into `base-ui`.)
- [ ] **Step 4: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 5: Manual smoke (recommended).** `pnpm dev`, narrow the window (<1024px): the sidebar is an icon rail; Vocabularies/Search show the list/panel full-width and selecting an item slides in the detail Drawer (close returns to the index); Fields stacks the list above the form (both scrollable). Widen (≥1024): all three return to side-by-side; Objects unchanged.
- [ ] **Step 6: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/fields/fields-page.tsx web/src/fields/fields-page.test.tsx
git commit -m "feat(web): responsive Fields page (stacks on narrow) (#58)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 `DetailDrawer` + objects retrofit + delete old (T1); AC2 vocab + search responsive drawer (T2-T3); AC3 fields responsive grid (T4 S1); AC4 new tests for drawer/vocab/search/fields + existing green (T1-T4 tests); AC5 gate/codename/no-new-keys (T4 S3-S4). ✓
**Placeholder scan:** full code for `DetailDrawer` + all three pages; tests give the exact `setViewport` harness + the narrow/wide discriminator; the `<VOCAB_ID>`/`<OBJECT_ID>` and `findByText` adjustments are explicit "read fixtures" instructions with a stated load-bearing assertion, not vague TODOs. ✓
**Type/consistency:** `DetailDrawer({ open, onClose, ariaLabel, children })` (T1) is consumed with those exact props in objects/vocab/search (T1-T3); `useMediaQuery("(min-width: 1024px)")` + `useMatch("/<x>/:id")` + `navigate("/<x>")` consistent across vocab/search; ariaLabels use existing keys (`objects.detailTitle`, `vocab.terms`). ✓
## Notes
- No new dependency; no new i18n keys (`objects.detailTitle`, `vocab.terms`, `actions.closeDetail` all exist). `components/ui/*` untouched (drawer/button wrappers unchanged; only a new app-level `components/detail-drawer.tsx`).
- The `<Outlet/>` per page is rendered in exactly one place per `isWide` branch — no double-mount.
- Fields stays `useState`-driven + stacked (no routing change, no "New field" trigger needed); the resizable splitter is deferred.
- Breakpoint 1024px is consistent with the existing Objects screen.
@@ -0,0 +1,154 @@
# Instance-Timezone Timestamp Formatter — Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Add a shared `formatTimestamp(value, timeZone, locale)` helper (date+time in the instance timezone, with an invalid-IANA → UTC fallback) and route the objects-table "Updated" column through it.
**Architecture:** Task 1 adds `lib/format-timestamp.ts` (mirrors `lib/format-date.ts`) + its unit test. Task 2 swaps objects-table's inline `dateFmt`/`formatUpdated` for the helper and runs the full gate. Display-only; UTC stays in storage/transmission.
**Tech Stack:** React 19 + TS + pnpm, `Intl.DateTimeFormat`, Vitest 4 (jsdom).
**Conventions:** pnpm; **no `any`/`eslint-disable`/`@ts-ignore`**; no codename; double-quote+semicolon. No new dependency, no new i18n keys, no backend change.
**Spec:** `docs/superpowers/specs/2026-06-09-timestamp-tz-design.md`
**Key facts:**
- `lib/format-date.ts` is the sibling pattern (date-only, no tz): `if (typeof value !== "string") return value == null ? "—" : String(value); const date = new Date(\`${value}T00:00:00\`); if (Number.isNaN(date.getTime())) return value; return new Intl.DateTimeFormat(lang, { dateStyle: "medium" }).format(date);`.
- `objects/objects-table.tsx`: `const { t, i18n } = useTranslation();` (line ~31), `const { default_timezone } = useConfig();` (~32). Lines ~123-131 are the inline `const dateFmt = new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium", timeZone: default_timezone });` + `const formatUpdated = (iso: string) => { const parsed = new Date(iso); return Number.isNaN(parsed.getTime()) ? iso : dateFmt.format(parsed); };`. The Updated cell (~line 270): `<td className="px-3 py-2 text-muted-foreground">{formatUpdated(object.updated_at)}</td>`.
- After removing `dateFmt`/`formatUpdated`, both `i18n` and `default_timezone` remain used (passed to `formatTimestamp`), and `t` is used elsewhere — no unused-locals.
- `objects-table.test.tsx` does NOT assert the rendered Updated value → no test edit needed there. Fixture `amphora.updated_at = "2026-01-05T14:30:00Z"`.
---
# Task 1: `lib/format-timestamp.ts` + test
**Files:** Create `web/src/lib/format-timestamp.ts`, `web/src/lib/format-timestamp.test.ts`.
- [ ] **Step 1: Create `web/src/lib/format-timestamp.ts`:**
```ts
/** Formats a UTC ISO timestamp for display in the instance timezone + active locale.
* Storage/transmission stay UTC — this is display-only. Falls back to UTC formatting on an
* invalid IANA zone (a misconfigured instance) rather than throwing. */
export function formatTimestamp(value: unknown, timeZone: string, locale: string): string {
if (typeof value !== "string") return value == null ? "—" : String(value);
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
const opts = { dateStyle: "medium", timeStyle: "short" } as const;
try {
return new Intl.DateTimeFormat(locale, { ...opts, timeZone }).format(date);
} catch {
// Invalid IANA timeZone (misconfigured instance) — fall back to UTC rather than crash.
return new Intl.DateTimeFormat(locale, { ...opts, timeZone: "UTC" }).format(date);
}
}
```
- [ ] **Step 2: Create `web/src/lib/format-timestamp.test.ts`** (write + run):
```ts
import { expect, test } from "vitest";
import { formatTimestamp } from "./format-timestamp";
test("formats a UTC timestamp with date and time in the given locale", () => {
const out = formatTimestamp("2026-06-08T12:30:00Z", "UTC", "en");
expect(out).toContain("2026");
expect(out).toContain("12:30");
});
test("applies the timezone — a near-midnight UTC instant shifts the calendar day", () => {
// 02:00 UTC on Jun 8 is 22:00 on Jun 7 in New York (EDT, UTC-4)
const ny = formatTimestamp("2026-06-08T02:00:00Z", "America/New_York", "en");
const utc = formatTimestamp("2026-06-08T02:00:00Z", "UTC", "en");
expect(ny).toContain("Jun 7");
expect(utc).toContain("Jun 8");
});
test("an invalid IANA zone does not throw and falls back to UTC", () => {
const out = formatTimestamp("2026-06-08T12:30:00Z", "Not/AZone", "en");
expect(out).toContain("2026");
});
test("null renders the em-dash placeholder; an unparseable string is returned unchanged", () => {
expect(formatTimestamp(null, "UTC", "en")).toBe("—");
expect(formatTimestamp("not-a-date", "UTC", "en")).toBe("not-a-date");
});
```
Run: `cd web && pnpm vitest run src/lib/format-timestamp.test.ts` → 4 passing. (Full-ICU Node renders en medium+short as e.g. `"Jun 8, 2026, 12:30 PM"`; the assertions check substrings — `2026`, `12:30`, `Jun 7`/`Jun 8` — to stay robust across ICU punctuation. If the local ICU renders the time without a leading-zero/`12:30`, assert the day-shift `Jun 7` vs `Jun 8` which is the load-bearing tz check.)
- [ ] **Step 3: Verify + lint:**
```bash
cd web && pnpm vitest run src/lib/format-timestamp.test.ts && pnpm typecheck && pnpm lint
```
All green.
- [ ] **Step 4: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/lib/format-timestamp.ts web/src/lib/format-timestamp.test.ts
git commit -m "feat(web): formatTimestamp helper (instance tz + locale, UTC fallback) (#42)"
```
---
# Task 2: Route objects-table "Updated" through `formatTimestamp` + full gate
**Files:** Modify `web/src/objects/objects-table.tsx`.
- [ ] **Step 1: Add the import** to `web/src/objects/objects-table.tsx` (alongside the other `../lib/*` imports):
```ts
import { formatTimestamp } from "../lib/format-timestamp";
```
- [ ] **Step 2: Remove the inline formatter.** Delete the `const dateFmt = new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium", timeZone: default_timezone });` block AND the `const formatUpdated = (iso: string) => { … };` function (the ~9 lines, currently around lines 123-131). (`i18n` and `default_timezone` stay declared — they're now passed to `formatTimestamp` at the call site; `t` remains used elsewhere.)
- [ ] **Step 3: Update the Updated cell.** Change:
```tsx
<td className="px-3 py-2 text-muted-foreground">{formatUpdated(object.updated_at)}</td>
```
to:
```tsx
<td className="px-3 py-2 text-muted-foreground">
{formatTimestamp(object.updated_at, default_timezone, i18n.language)}
</td>
```
- [ ] **Step 4: FULL FRONTEND GATE (run tests EXACTLY ONCE):**
```bash
cd web && pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm check:size && pnpm check:colors
```
All green. The objects-table tests stay green (they don't assert the Updated cell's text). Report total test count, largest chunk (gz), the check:colors line. If typecheck flags `i18n`/`default_timezone` as unused, the call site in Step 3 must reference them (it does) — re-check the edit.
- [ ] **Step 5: Codename + status:**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git grep -in 'biggus\|dickus' -- web/src; echo "codename-exit=$?"
git status --short
```
Expected: no matches (`codename-exit=1`).
- [ ] **Step 6: Manual smoke (recommended).** `pnpm dev`: the objects list "Updated" column now shows date + time in the instance timezone (e.g. for an instance in `Europe/Stockholm`, a `…T14:30:00Z` value renders ~`Jan 5, 2026, 3:30 PM`); switching the UI language reformats it.
- [ ] **Step 7: Commit**
```bash
cd /Users/olsson/Laboratory/biggus-dickus
git add web/src/objects/objects-table.tsx
git commit -m "feat(web): render objects 'Updated' as a tz-aware timestamp via formatTimestamp (#42)"
```
---
## Self-Review (completed)
**Spec coverage:** AC1 `formatTimestamp` helper + unit tests incl. day-shift + invalid-zone (T1); AC2 objects-table adoption, inline formatter removed, date+time (T2 S1-S3); AC3 gate/codename/no-new-dep-or-keys (T2 S4-S5). ✓
**Placeholder scan:** full helper + test code; the ICU-substring note gives a concrete robustness fallback (assert `Jun 7`/`Jun 8`); exact lines/strings for the objects-table edit. No TBD. ✓
**Type/consistency:** `formatTimestamp(value: unknown, timeZone: string, locale: string)` (T1) called as `formatTimestamp(object.updated_at, default_timezone, i18n.language)` (T2) — `updated_at: string`, `default_timezone: string` (useConfig), `i18n.language: string`. ✓
## Notes
- No new dependency, no new i18n keys, no backend change. `format-date.ts` (plain DATE) is untouched.
- The helper constructs an `Intl.DateTimeFormat` per call (vs the prior once-per-render memo); negligible for the ≤200-row page.
- Only the one timestamp display exists today; future displays (object-detail created/updated, audit history) route through the same helper when they land.
@@ -0,0 +1,163 @@
# Accessibility Defect Bundle — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #62 (label-id collision, invalid table row semantics, unnamed drawer, unannounced table states, last untranslated strings).
## Context
A frontend deep audit (post the #52 a11y pass, which is verified correct) found five remaining
accessibility gaps. They are independent, low-risk fixes; no new dependency. The #52 work (focus ring,
skip link, route focus, authority nav links, lang group, `<html lang>` sync) stays untouched.
## Components
### 1. `components/label-editor.tsx` — id collision → `useId()`
`LabelEditor` hardcodes `<Label htmlFor="label">` + `<Input id="label">` (`:35-36`). It renders
simultaneously in the create form **and** any inline-edit row (authorities + vocab), so two `id="label"`
inputs coexist and every `<label for="label">` resolves to the first — the second editor's label points
at the wrong field (WCAG 1.3.1 / 4.1.1 / 4.1.2). Fix with React's `useId()` (zero call-site changes):
```tsx
import { useId } from "react";
// …
const inputId = useId();
<Label htmlFor={inputId}>{t("labels.label")}</Label>
<Input id={inputId} value={current} onChange={(e) => set(e.target.value)} />
```
### 2. `objects/objects-table.tsx` — rows: real link + `aria-current`
The data rows are `<tr role="link" tabIndex={0} aria-selected={selected} onClick onKeyDown>` (`:247-259`).
`aria-selected` is invalid on `role="link"` (AT ignores it). Decision (brainstorming): make the
object-**number** cell a real React Router `<Link>` and keep the whole row clickable. The `<tr>` becomes a
plain row:
```tsx
<tr
key={object.id}
onClick={() => navigate(`/objects/${object.id}?${params}`)}
className={`cursor-pointer border-b text-sm ${selected ? "bg-primary/10" : "hover:bg-muted"}`}
>
<td className="px-3 py-2 text-muted-foreground">
<Link
to={`/objects/${object.id}?${params}`}
aria-current={selected ? "page" : undefined}
onClick={(event) => event.stopPropagation()}
className={`${focusRing} rounded-sm hover:underline`}
>
{object.object_number}
</Link>
</td>
{/* …other cells unchanged… */}
</tr>
```
- Native anchor ⇒ keyboard focus + Enter activation + SR "link" + middle-click / open-in-new-tab, all free.
- `event.stopPropagation()` on the link prevents the row `onClick` from double-navigating when the link
itself is clicked.
- The whole-row `onClick` stays, so clicking any non-link cell still opens the object (preserves current
UX; the existing "clicking a row …" test clicks the object-**name** cell and still passes).
- Selection is conveyed by `aria-current="page"` on the link (announced when focused); the visual
highlight stays on the `<tr>` via the className.
- Drop `role="link"`, `tabIndex`, `onKeyDown`, and `aria-selected` from the `<tr>`.
### 2b. `objects/objects-table.tsx` — filter pills: restore `focusRing`
The visibility pills (`:168-177`) lack the keyboard focus ring their siblings (`search-panel`,
`authorities-page`) have. Import `focusRing` from `../lib/focus-ring` and append it to the pill className:
```tsx
className={`${focusRing} rounded-md px-2 py-1 ${active ? "bg-primary text-primary-foreground" : "border"}`}
```
### 3. `objects/objects-table.tsx` — announce loading / error
Loading renders bare `<Skeleton>` rows and the error a plain `<td>` — neither is announced (WCAG 4.1.3).
- Add `aria-busy={isLoading || undefined}` to the `<table>`.
- Add an `sr-only` live `<caption>` that announces loading and settles to the table name (also gives the
table an accessible name):
```tsx
<table className="w-full border-collapse" aria-busy={isLoading || undefined}>
<caption className="sr-only" aria-live="polite">
{isLoading ? t("common.loading") : t("objects.tableLabel")}
</caption>
{columns}
{body}
</table>
```
- Add `role="alert"` to the error `<td>` (`:223`) so a load failure is announced assertively:
```tsx
<td colSpan={6} role="alert" className="px-3 py-6 text-center text-sm text-destructive">
{t("objects.loadError")}
</td>
```
### 4. `objects/object-detail-drawer.tsx` — name the drawer dialog
The Base UI drawer (a modal dialog) has no accessible name. `DrawerContent` spreads props onto the Popup,
so pass an `aria-label`:
```tsx
<DrawerContent aria-label={t("objects.detailTitle")}>
```
(No change to `components/ui/drawer.tsx`.)
### 5. Last untranslated strings
- `objects/options-combobox.tsx` (`:47-52`): add `import { useTranslation } from "react-i18next";` +
`const { t } = useTranslation();`, then `aria-label={t("common.clear")}` on `ComboboxClear`,
`aria-label={t("common.open")}` on `ComboboxTrigger`, and `<ComboboxEmpty>{t("common.noMatches")}</ComboboxEmpty>`
(`common.noMatches` already exists).
- `shell/breadcrumb.tsx` (`:10`): add `useTranslation`; `<nav aria-label={t("nav.breadcrumb")}>`.
### i18n (en + sv parity — 5 new keys)
`common.noMatches` and `common.loading` already exist. New keys:
| key | en | sv |
|-----|----|----|
| `common.clear` | Clear | Rensa |
| `common.open` | Open | Öppna |
| `nav.breadcrumb` | Breadcrumb | Brödsmulor |
| `objects.detailTitle` | Object detail | Objektdetalj |
| `objects.tableLabel` | Objects | Objekt |
## Data flow / accessibility
No data-flow changes. Tab order in the objects table becomes: filter input → visibility pills (now with
ring) → New button → each row's object-number link → pagination. The selected row's link carries
`aria-current="page"`. Loading politely announces via the caption; a load error asserts via the alert
cell. The drawer and breadcrumb gain accessible names; every combobox control is named in the active
locale.
## Error handling / edges
- `aria-busy={isLoading || undefined}` omits the attribute when not loading (no `aria-busy="false"` noise).
- `event.stopPropagation()` on the row link means a plain click on the number navigates once (link), not
twice; modifier/middle clicks open a new tab natively (React Router `<Link>` respects them).
- `aria-current` is omitted (not `"false"`) when the row isn't selected.
- The `<caption>` is `sr-only` — no visual change to the table.
- `useId()` ids are SSR/StrictMode stable and unique per instance.
## Testing
- **`label-editor`**: render two `LabelEditor`s together; assert their inputs have **different** ids and
each `<label>` is associated with its own input (e.g. `getAllByLabelText` returns two distinct nodes).
- **`objects-table`**: (a) a data row exposes a `link` named by the object number
(`getByRole("link", { name: <object_number> })`); (b) the row matching the selected `:id` has the link
with `aria-current="page"`; (c) clicking a row still deep-links (existing test stays green); (d) the
loading state sets `aria-busy` on the table; (e) an errored fetch renders `role="alert"`. Update any
existing assertion that referenced `aria-selected`/the row as a link.
- **`options-combobox`**: the clear/open controls are named via `t()` and the empty text is translated
(assert the English defaults render; the parity test guards sv).
- **`breadcrumb`**: the `<nav>` accessible name comes from `t("nav.breadcrumb")`.
- **`object-detail-drawer`**: the open drawer dialog has an accessible name (`getByRole("dialog", { name })`
or equivalent for the Base UI popup).
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; en/sv parity (5 new keys,
guarded by the #60 parity test); no codename; no new dependency. `focusRing` is token-based so
`check:colors` stays green.
## Acceptance criteria
1. `LabelEditor` uses `useId()`; two instances never share an input id.
2. Objects-table data rows expose a real `<Link>` (object-number cell) with `aria-current="page"` on the
selected row; no `role="link"`/`aria-selected` on the `<tr>`; whole-row click still navigates; the
filter pills show the keyboard focus ring.
3. The table sets `aria-busy` while loading and exposes a polite live caption; a load error renders
`role="alert"`.
4. The object-detail drawer dialog and the breadcrumb `<nav>` have accessible names; the combobox
clear/open/empty strings are translated.
5. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported; en/sv parity (5 new
keys); no codename; no new dependency.
## Out of scope → follow-ups
- The combobox wrapper's raw palette utilities (`text-neutral-*`, `bg-indigo-50`, `bg-white`) inside
`components/ui/combobox.tsx`, and the segmented-control extraction → design-kit issue **#66**.
- A full ARIA grid for the objects table (sortable/selectable grid semantics) — the navigation-table +
real-link pattern is the correct, simpler choice here.
- Automated axe/CI a11y scanning.
@@ -0,0 +1,135 @@
# Accessibility — Focus, Route Management, Honest Semantics — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #52.
## Context
Keyboard-driven fast data entry is a stated goal and this is a public-institution tool, but several
custom controls drop the `ui/*` kit's focus ring, route changes leave focus stranded, there's no skip
link, the authority "tabs" announce tab semantics they don't fulfill, the lang switch lacks a group
label, and `<html lang>` never updates.
**Current state (post #49/#51/#54/#59):** the four raw `<select>`s are now `ui/Select` (have rings);
sidebar NavLinks + collapse button have rings; reference-data Edit/Delete are `ui/Button` (rings). The
**remaining bare controls without a focus ring**: `lang-switch.tsx` buttons, `theme-switch.tsx`
buttons, `search-panel.tsx` visibility facet chips, `field-list.tsx` row button, and the
`authorities-page.tsx` tab NavLinks. `ui/Button`'s ring = `focus-visible:border-ring
focus-visible:ring-3 focus-visible:ring-ring/50`. `app-shell.tsx`: `<main className="flex-1
overflow-hidden">` (no id/tabIndex), no skip link, no route-focus effect. Authority tabs are router
`NavLink`s with `role="tablist"`/`role="tab"`/`aria-selected` but no roving tabindex/arrow/aria-controls.
Lang switch: two `aria-pressed` buttons, no `role="group"`/`aria-label`. `index.html` hardcodes
`lang="en"`; no `documentElement.lang` sync (i18n init in `i18n/index.ts`, changes via
`use-locale.ts` `setLocale` + `config-provider` default-language effect).
### Decisions (from brainstorming)
1. Shared `focusRing` class on the 5 bare controls.
2. Authority tabs → honest navigation (`NavLink` + `aria-current`, drop tab roles); lang switch →
`role="group"` + `aria-label`.
3. Skip link + focus `<main>` on route change.
4. Sync `document.documentElement.lang` on every language change.
## Components
### `web/src/lib/focus-ring.ts` (new)
`export const focusRing = "outline-none focus-visible:ring-3 focus-visible:ring-ring/50";` — applied
via `cn(...)`/template to bare controls (they already have `rounded-md` so the ring shape is correct).
### A. Focus rings (5 controls)
- `lang-switch.tsx`: each `<button>` gets `type="button"` + `focusRing` (+ `rounded-sm px-1` so the
ring has shape).
- `theme-switch.tsx`: each `<button>` className gains `focusRing` (already `rounded-md`).
- `search-panel.tsx`: each facet `<button>` className gains `focusRing` (already `rounded-md`).
- `field-list.tsx`: the row `<button className="flex flex-1 …">` gains `focusRing` + `rounded-sm`.
- `authorities-page.tsx`: the tab `NavLink`s gain `focusRing` (with the semantics change below).
### B. Honest semantics
- **Authority tabs** (`authorities-page.tsx`): replace the `<div role="tablist">` + `role="tab"` +
`aria-selected` with a `<nav aria-label={t("nav.authorities")}>` containing the `NavLink`s; rely on
`NavLink`'s native **`aria-current="page"`** for the active state (drop `role`/`aria-selected`). Keep
the segmented styling (active `bg-primary text-primary-foreground`, else `border`) + add `focusRing`.
- **Lang switch** (`lang-switch.tsx`): wrap the buttons in `<div role="group" aria-label={t("common.language")}>`;
keep `aria-pressed`; inactive stays `text-muted-foreground` (token) — the ring + `font-bold` active +
group label make state/affordance clear.
### C. Route focus + skip link (`app-shell.tsx`)
- **Skip link** as the FIRST focusable element (before `<Sidebar/>`): a visually-hidden-until-focused
anchor —
`<a href="#main-content" className="sr-only focus:not-sr-only focus:absolute focus:left-2 focus:top-2 focus:z-50 focus:rounded-md focus:bg-background focus:px-3 focus:py-2 focus:ring-3 focus:ring-ring/50">{t("common.skipToContent")}</a>`.
(If `sr-only`/`not-sr-only` utilities aren't available in this Tailwind v4 setup, use an explicit
visually-hidden pattern; verify by running.)
- `<main id="main-content" tabIndex={-1} className="flex-1 overflow-hidden …">`.
- A focus effect: `const location = useLocation();` + `const mainRef = useRef<HTMLElement>(null);` +
`useEffect(() => { mainRef.current?.focus(); }, [location.pathname]);` — but **skip the initial
mount** (so a deep-link load doesn't yank focus): track a `didMount` ref, focus only on subsequent
`pathname` changes. `<main ref={mainRef} …>`. (`tabIndex={-1}` makes `<main>` programmatically
focusable without adding it to the tab order; `outline-none` to avoid an outline on the container.)
### D. `<html lang>` sync (`i18n/index.ts`)
After `i18n.init`, register once:
```ts
function syncHtmlLang(lng: string) {
if (typeof document !== "undefined") {
document.documentElement.lang = lng.startsWith("sv") ? "sv" : "en";
}
}
i18n.on("languageChanged", syncHtmlLang);
syncHtmlLang(i18n.language);
```
This covers the switcher (`setLocale``changeLanguage`), the config default-language effect, and
startup — a single source of truth.
### i18n (en + sv parity)
- `common.language` = "Language" / "Språk"
- `common.skipToContent` = "Skip to content" / "Hoppa till innehåll"
## Data flow / accessibility
Tab order: skip link → sidebar → header → main content. Activating the skip link (or any route change)
moves focus into `<main>` (which contains the route's `<h1>` from #57). `aria-current="page"` marks the
active authority kind + nav route. `documentElement.lang` reflects the active language for SR
pronunciation / browser translation.
## Error handling / edges
- `focusRing` uses only `focus-visible:` (keyboard focus), not `:focus`, so mouse clicks don't show the
ring.
- Route-focus effect skips the initial mount; only fires on `pathname` change (covers nav + master-detail
open; query-param-only changes like the objects filter/pagination don't change `pathname`, so no
refocus there).
- `<main tabIndex={-1}>` is not in the tab order (negative) — only programmatically focusable.
- `documentElement.lang` guarded for non-DOM (tests/SSR).
- The authority tab role change: `aria-current="page"` is the honest active indicator; tests that
queried `role="tab"`/`aria-selected` are updated to `role="link"` + `aria-current`.
## Testing
- **lang-switch:** the buttons are within a `role="group"` named by `common.language`; each is a
keyboard-focusable button with `aria-pressed`.
- **authority tabs:** the kind links are `role="link"` (not `tab`); the active one has
`aria-current="page"`; navigating still works. Update the existing `authorities.test.tsx` assertions
(tab → link, aria-selected → aria-current) — don't weaken (still assert the active kind + href).
- **skip link:** an anchor to `#main-content` is the first focusable element; `<main>` has
`id="main-content"` + `tabIndex={-1}`.
- **route focus:** navigating to a new route focuses `<main>` (assert `document.activeElement` is the
main element after a nav, or that main received focus). Mirror the app-shell test harness.
- **html lang sync:** switching the locale sets `document.documentElement.lang` to "sv"/"en" (drive via
the lang switch or `i18n.changeLanguage`; assert `document.documentElement.lang`).
- The i18n **parity test** (#60) covers the 2 new keys automatically.
- Gate: `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors`; en/sv parity; no codename; no new
dependency. **`check:colors`:** `focusRing` uses token utilities (`ring-ring`), not raw palette — OK.
## Acceptance criteria
1. All five bare controls (lang-switch, theme-switch, search facet chips, field-list row, authority
kind links) show a keyboard `focus-visible` ring matching the kit.
2. A skip-to-content link is the first focusable element; `<main id="main-content" tabIndex={-1}>`
receives focus on route change (not on initial mount).
3. Authority kind links no longer claim `role="tab"`/`aria-selected`; they use `aria-current="page"` in
a labelled `<nav>`. The lang switch is a labelled `role="group"`.
4. `document.documentElement.lang` updates to the active language on every language change.
5. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported; en/sv parity (2 new
keys); no codename; no new dependency.
## Out of scope → follow-ups
- A full ARIA tab-widget (roving tabindex/arrows/tabpanel) for authorities — they're navigation, not a
tab panel, so honest links are correct.
- Automated axe/a11y scanning in CI; a broader sweep beyond the listed controls.
- An `aria-live` route-announcement region (focusing `<main>`/the `<h1>` covers the core need).
@@ -0,0 +1,122 @@
# Design-Kit Consistency — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #66 (dead Card, duplicated segmented-control + selected-row recipes, `useLang`/`focusRing` drift, misc kit one-offs).
## Context
A frontend deep audit found subtler design-system inconsistencies the `check:colors` guard doesn't catch
(the app is already hex-free + token-based + dark-mode-clean). These are duplicated class recipes and
non-adoption of the `ui/*` kit. All fixes are behavior-preserving; `check:colors`/`check:size`/the existing
component tests are the guards. State re-verified against the current code (post #62/#64).
## Components
### New shared helpers
**`lib/use-lang.ts`** — `useLang(): "sv" | "en"`
```ts
import { useTranslation } from "react-i18next";
/** The instance's active UI language, narrowed to the two supported locales. */
export function useLang(): "sv" | "en" {
const { i18n } = useTranslation();
return i18n.language.startsWith("sv") ? "sv" : "en";
}
```
Replaces the inline `const lang = i18n.language.startsWith("sv") ? "sv" : "en";` in **6** components:
`objects/object-detail.tsx`, `objects/field-input.tsx`, `vocab/vocabulary-terms.tsx`,
`vocab/vocabulary-list.tsx`, `fields/field-list.tsx`, `authorities/authorities-page.tsx`. Each switches to
`const lang = useLang();` and drops the now-unused `i18n` from its `useTranslation()` destructure where
`i18n` is otherwise unused. (Left untouched: `shell/lang-switch.tsx` — derives from a different `locale`
var; `i18n/index.ts` — the infra `languageChanged` handler.)
**`lib/class-recipes.ts`** — two shared class helpers
```ts
import { cn } from "@/lib/utils";
import { focusRing } from "./focus-ring";
/** Segmented-control / filter-pill item. Unifies the active/inactive token recipe +
* focus ring; callers pass their contextual padding/size via `className`. */
export function segmentClass(active: boolean, className?: string): string {
return cn("rounded-md", focusRing, active ? "bg-primary text-primary-foreground" : "border", className);
}
/** Selected vs idle row background for master-detail / list rows. */
export function rowStateClass(active: boolean): string {
return active ? "bg-primary/10" : "hover:bg-muted";
}
```
- **`segmentClass`** is adopted at the 3 segmented sites, each keeping its contextual padding:
- `objects/objects-table.tsx:174``segmentClass(active, "px-2 py-1")`
- `search/search-panel.tsx:76``segmentClass(active, "px-2 py-0.5")`
- `authorities/authorities-page.tsx:41` (NavLink) → `segmentClass(isActive, "px-3 py-1 text-sm")`
This DRYs the bug-prone recipe (the active/inactive token pair + `focusRing` — the part that drifted and
caused the #62 missing-ring bug); contextual sizing is intentionally preserved per site.
- **`rowStateClass`** is adopted at the 4 selected-row sites:
- `objects/objects-table.tsx:252``rowStateClass(selected)`
- `vocab/vocabulary-list.tsx:113``rowStateClass(isActive)`
- `search/search-result-row.tsx:15``rowStateClass(isActive)`
- `fields/field-list.tsx:86``rowStateClass(def.key === selectedKey)`**fixes** this site, which
currently uses `… ? "bg-primary/10" : ""` (dropping the `hover:bg-muted` idle hover the others have).
### One-off cleanups
- **Delete `components/ui/card.tsx`** — zero importers (no app/test/story references; no `card.stories`).
- **`shell/sidebar.tsx:46,88`** — replace the raw `focus-visible:ring-3 focus-visible:ring-ring/50`
string (inside the existing `cn(...)`) with the imported `focusRing` constant (adds `outline-none`,
matching every other call site).
- **`auth/login-page.tsx:49`** — `<h1 className="text-2xl font-semibold">{app_name}</h1>`
`<PageTitle>{app_name}</PageTitle>` (`PageTitle` is `text-2xl font-semibold tracking-tight`, restoring
the missing `tracking-tight`). Import `PageTitle` from `@/components/ui/page-title`.
- **`fields/field-list.tsx:97`** — the hand-rolled type-tag `<span className="rounded-md bg-muted px-1.5
py-0.5 text-xs text-muted-foreground">` → `<Badge variant="secondary">` (from `@/components/ui/badge`).
- **Icon sizing** — `h-4 w-4` → `size-4` in the 3 **app-source** sites: `shell/theme-switch.tsx:39`,
`shell/user-menu.tsx:27`, `shell/header-search.tsx:23`. (Leave `components/ui/select.tsx` — kit-internal.)
- **Icon dismiss buttons** → kit `Button variant="ghost" size="icon-sm"`:
- `objects/objects-page.tsx:54` (plain `<button onClick={closeDetail} aria-label={…}>`) → `<Button
variant="ghost" size="icon-sm" onClick={closeDetail} aria-label={t("actions.closeDetail")}><X
className="size-4" aria-hidden="true" /></Button>` (import `Button`).
- `objects/object-detail-drawer.tsx:33` (Base UI `<DrawerClose>`) → keep `DrawerClose` for its
close-on-click semantics, render it AS the kit Button via the render prop:
`<DrawerClose aria-label={t("actions.closeDetail")} render={<Button variant="ghost" size="icon-sm" />}><X
className="size-4" aria-hidden="true" /></DrawerClose>` (mirrors the `AlertDialogTrigger render={<Button/>}`
pattern in `delete-confirm-dialog.tsx`; import `Button`).
## Error handling / edges
- `segmentClass`/`rowStateClass` are pure string builders — no runtime concerns. `cn()` (tailwind-merge)
resolves any padding/utility overlap predictably.
- `<Badge variant="secondary">` shifts the type-tag from `bg-muted`/`text-muted-foreground` to the
`secondary` token pair — a deliberate, minor visual adoption of the kit; still token-based (check:colors
clean).
- The drawer `DrawerClose render={<Button/>}` keeps Base UI's close behaviour (Base UI merges its props
onto the rendered Button); the `aria-label` stays on `DrawerClose`.
- `useLang` returns the same `"sv" | "en"` the inline code produced — no behaviour change.
## Testing
- **`lib/class-recipes.test.ts`** (new): `segmentClass(true)` contains `bg-primary` + `text-primary-foreground`
and the focus-ring utility; `segmentClass(false)` contains `border` (not `bg-primary`); both contain a
passed `className`; `rowStateClass(true)` === `"bg-primary/10"`, `rowStateClass(false)` === `"hover:bg-muted"`.
- **Behavior guard:** the existing component tests must stay green unchanged — `objects-table.test.tsx`,
`authorities.test.tsx`, `vocabularies.test.tsx`, `field-list` / `search` tests, `login-page.test.tsx`,
`breadcrumb`/sidebar, `object-detail`/drawer, `user-menu.test.tsx`. (The login `PageTitle` still renders an
`<h1>`; the icon buttons keep their `aria-label`s; the drawer close still closes.)
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; no new dependency; no new
i18n keys; no codename. `check:size` unchanged-or-smaller (Card deletion removes dead code).
## Acceptance criteria
1. `useLang()` exists and replaces the inline lang derivation in the 6 listed components; `lang-switch` and
`i18n/index.ts` are untouched.
2. `segmentClass`/`rowStateClass` exist (unit-tested) and are adopted at the 3 segmented + 4 selected-row
sites; `field-list`'s selected row gains the `hover:bg-muted` idle hover.
3. `components/ui/card.tsx` is deleted (no remaining references).
4. The one-offs are applied: sidebar uses `focusRing`; login uses `PageTitle`; field-list type-tag uses
`Badge`; the 3 app-source icons use `size-4`; both icon dismiss buttons use `Button variant="ghost"
size="icon-sm"`.
5. All existing tests pass unchanged; `typecheck`/`lint`/`build`/`check:colors` green; `check:size`
unchanged-or-smaller; no new dependency; no new i18n keys; no codename.
## Out of scope → follow-ups
- A full `<SegmentedControl>`/`<ToggleGroup>` component (the button-vs-NavLink interaction split makes a
class helper the better fit); the form `space-y-*` scale (too subjective — churn risk).
- Standardizing icon sizing inside `components/ui/*` (kit-internal style, separate from app-source).
@@ -0,0 +1,136 @@
# Object-Form Flexible-Field Grouping — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #45 (deferred half — the object **form**'s flexible-field grouping; the detail view shipped in `e2ae093`).
## Context
#45 fixed the object **detail** view: flexible fields are now grouped by `FieldDefinitionView.group`
in definition order with a trailing "Other" bucket (`object-detail.tsx`). The issue's problem #2
explicitly says "detail **and** form", but the **form** still renders flexible-field inputs flat — one
`<fieldset>` with a single "Catalogue fields" legend and `definitions.map(...)` (definition order, no
group subheadings). This milestone brings the form to parity by reusing the *same* grouping the detail
view uses (extracted into a shared helper so they can't drift).
**Facts:** `object-detail.tsx:71-94` builds `{ group, defs }[]` inline: iterate the definitions
(filtered to those with a value), bucket by `def.group?.trim()` else `t("fields.other")`, sort so the
"Other" group is last, plus orphan-key handling. `object-form.tsx` renders the flexible block as
`<fieldset className="space-y-3 border-t pt-3"><legend className="label-caption">{form.flexibleHeading}</legend>{definitions.map((def) => <div key={def.key}><FieldInput .../>{error}</div>)}</fieldset>`.
Field-definition fixtures carry `group` (e.g. `"Description"` / `null`). `field-list.tsx` also groups,
but AZ (a management list, from #50) — out of scope to unify with it.
### Decisions (from brainstorming)
1. Extract the definition-grouping into a shared `lib/group-fields.ts` and use it in **both** detail
and form (one source of truth).
2. The form keeps its `<fieldset>` for a11y but with an `sr-only` legend; visible per-group
`label-caption` subheadings (matching the detail view) — no redundant double-heading.
3. Definition order within groups (no re-sort); ungrouped → trailing "Other".
## Components
### `web/src/lib/group-fields.ts` (new)
```ts
import type { components } from "../api/schema";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
export type FieldGroup = { group: string; defs: FieldDefinitionView[] };
/** Group field definitions by `def.group` (trimmed), preserving definition order
* within and across groups; ungrouped defs fall into a trailing `otherLabel` bucket. */
export function groupDefinitions(
definitions: FieldDefinitionView[],
otherLabel: string,
): FieldGroup[] {
const groups: FieldGroup[] = [];
for (const def of definitions) {
const group = def.group?.trim() ? def.group : otherLabel;
let bucket = groups.find((g) => g.group === group);
if (!bucket) {
bucket = { group, defs: [] };
groups.push(bucket);
}
bucket.defs.push(def);
}
// Keep definition order among real groups; the "Other" bucket sorts last.
groups.sort((a, b) => Number(a.group === otherLabel) - Number(b.group === otherLabel));
return groups;
}
```
A unit test asserts: definition order preserved; two defs in the same group bucket together; ungrouped
defs land in the `otherLabel` bucket which is **last** even when defined before a grouped def.
### `object-detail.tsx` (refactor to the shared helper)
Replace the inline `for (const def of present) { … }` group-building + final `groups.sort(...)` with
`const groups = groupDefinitions(present, other);`. Keep the existing orphan handling appended after
(`if (orphans.length > 0 && !groups.some(g => g.group === other)) groups.push({ group: other, defs: [] });`
— appending the Other bucket still leaves it last). Render is unchanged. Net: identical output, now via
the shared helper. (Verify the existing object-detail tests stay green.)
### `object-form.tsx` (group the flexible inputs)
Replace the flat flexible block with grouped rendering, reusing the FieldInput + per-field error markup
inside each group:
```tsx
{definitions && definitions.length > 0 && (
<fieldset className="space-y-3 border-t pt-3">
<legend className="sr-only">{t("form.flexibleHeading")}</legend>
{groupDefinitions(definitions, t("fields.other")).map((g) => (
<div key={g.group} className="space-y-3">
<div className="label-caption">{g.group}</div>
{g.defs.map((def) => (
<div key={def.key}>
<FieldInput definition={def} form={form} />
{errors.fields?.[def.key] && (
<p role="alert" className="text-xs text-destructive">
{errors.fields[def.key]?.message ?? t("form.required")}
</p>
)}
</div>
))}
</div>
))}
</fieldset>
)}
```
- The `<legend>` becomes `sr-only` (a11y-correct fieldset name) while the visible structure is the
per-group `label-caption` subheadings — mirroring detail, no double-heading.
- Field inputs and their error rendering are unchanged (just moved under group wrappers); the submit
payload / validation / pruning are untouched (the same `definitions` drive the same inputs).
- `import { groupDefinitions } from "../lib/group-fields";` (and `fields.other` is an existing key).
## Data flow
`useFieldDefinitions()``groupDefinitions(defs, t("fields.other"))` → grouped subheadings + the same
`FieldInput`s. No change to form state, RHF registration, pruning, or submission.
## Error handling / edges
- Definition order preserved; "Other" last (even if an ungrouped def precedes a grouped one).
- All-ungrouped case → a single "Other" group with a visible "Other" subheading (consistent with the
detail view's behavior).
- The shared helper takes the caller's def list: detail passes `present` (defs with values); the form
passes all `definitions`. Orphan handling stays in detail only.
- `sr-only` legend keeps the fieldset accessible without a visible redundant heading.
## Testing
- **`group-fields.test.ts`** (unit): order preserved; same-group bucketing; ungrouped → trailing Other.
- **object-form test:** with the field-definition fixtures (which include `group` values + ungrouped),
rendering the form shows the grouped subheadings (e.g. "Description" then "Other") and the fields
under them in definition order. (Extend `object-form.test.tsx`; reuse the existing MSW field-def
handler / fixtures — add a `group` to one fixture def if needed to exercise a real named group.)
- **object-detail tests** stay green (the refactor is output-preserving).
- Gate: `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors`; no new i18n keys (reuse
`fields.other`, `form.flexibleHeading`); en/sv parity unaffected; no codename; no new dependency.
## Acceptance criteria
1. `groupDefinitions` exists (shared, unit-tested) and is used by **both** object-detail and object-form.
2. The object form renders flexible-field inputs grouped by `def.group` (definition order, "Other" last)
with `label-caption` subheadings; the fieldset keeps an `sr-only` legend.
3. The detail view's grouping is unchanged in output (now via the shared helper).
4. Form behavior is otherwise unchanged (inputs, validation, errors, submission, pruning).
5. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported; en/sv parity; no
codename; no new dependency.
## Out of scope → follow-ups
- Export/PDF (#39); backend label resolution.
- Reordering fields within a group beyond definition order; a field-definition "position"/sort concept.
- Unifying `field-list.tsx`'s (AZ) grouping with this (different purpose — a management list).
@@ -0,0 +1,213 @@
# Consistent, Status-Aware Mutation Error Feedback — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #63 (silent update failures + dead/unreachable mutation error strings).
## Context
A frontend deep audit flagged the TanStack Query mutation layer for inconsistent error feedback.
Grounding in the code refined the picture:
- **The update mutations are not silent — they're inconsistent.** `useUpdateTerm` (`queries.ts:444`) and
`useUpdateAuthority` (`:521`) are the **only 2 of 18 mutations** without `meta.suppressErrorToast`, so on
failure they fire the global `MutationCache` error toast while the row stays in edit mode. The matching
**create** and **delete** actions on the same screens suppress the toast and show an **inline** message
instead. So within one screen, create-failure → inline, delete-failure → inline, edit-failure → a
disconnected global toast.
- **Once the 2 update mutations also suppress (the consistency fix), all 18 mutations suppress** → the
global error toast becomes vestigial. Therefore status differentiation only delivers value at the
**inline** error sites, which today all render a generic `t("form.rejected")` ("Could not be saved").
- **The generic error strings are dead.** 16 mutations `throw new Error("update failed" | …)`, but
`mutationErrorMessage` (`query-client.ts:11-23`) never reads `.message` — it falls back to
`t("toast.error")`. The bespoke strings are unreachable.
- **`object-edit-form` mislabels a fetch failure.** `objects/object-edit-form.tsx:17` destructures only
`{ data, isLoading }` from `useObject`; a non-404 fetch error (network/500) falls through to the
"object not found" branch. (The **submit** path already handles `FieldRejection` and a generic else
correctly.)
### Decisions (from brainstorming)
1. Differentiate errors **by HTTP status**, surfaced via one shared mapping.
2. Because all mutations suppress the toast, apply status-aware messages at **all inline error sites**
(via one shared `<MutationError>` component), not the (now-vestigial) toast. Half-applying would
create a new inline inconsistency.
3. Make `useUpdateTerm`/`useUpdateAuthority` consistent with their create/delete siblings (suppress +
inline).
4. Fix the `object-edit-form` fetch mislabel.
## Components
### `web/src/api/error-message.ts` (new) — single source of truth
```ts
import { HttpError, InUseError } from "./queries";
/** Maps a caught mutation error to an i18n key (+ interpolation opts). Used by
* both the global toast fallback and every inline error display. */
export function errorMessageKey(error: unknown): { key: string; opts?: Record<string, unknown> } {
if (error instanceof InUseError) return { key: "actions.inUse", opts: { count: error.count } };
if (error instanceof HttpError) return { key: statusKey(error.status) };
return { key: "toast.error" };
}
function statusKey(status: number): string {
if (status === 403) return "errors.forbidden";
if (status === 404) return "errors.notFound";
if (status === 409) return "errors.conflict";
if (status === 422) return "errors.validation";
if (status >= 500) return "errors.server";
return "toast.error";
}
```
Returns a key+opts (not a resolved string) so callers render with their own `t` (reactive to language).
`InUseError` is checked first so 409-with-count keeps its richer message. (No circular import:
`queries.ts` does not import this module; `query-client.ts` imports both.)
### `web/src/components/mutation-error.tsx` (new) — shared inline display
```tsx
import { useTranslation } from "react-i18next";
import { errorMessageKey } from "../api/error-message";
/** Renders a caught mutation error as an inline alert, or nothing when error is falsy.
* Replaces the duplicated `<p role="alert" className="text-xs text-destructive">` markup. */
export function MutationError({ error }: { error: unknown }) {
const { t } = useTranslation();
if (!error) return null;
const { key, opts } = errorMessageKey(error);
return (
<p role="alert" className="text-xs text-destructive">
{t(key, opts)}
</p>
);
}
```
### `web/src/api/query-client.ts` (rewire)
`mutationErrorMessage` becomes:
```ts
function mutationErrorMessage(error: unknown, meta: MutationMeta | undefined): string {
if (meta?.errorMessage) return i18n.t(meta.errorMessage);
const { key, opts } = errorMessageKey(error);
return i18n.t(key, opts);
}
```
This keeps the toast path as a one-source-of-truth fallback (for any future non-suppressed mutation) and
drops the old special-cases (`InUseError` and `503 → search.unavailable` now flow through
`errorMessageKey`; no mutation throws 503, and the search **query**'s own inline 503 handling in
`search-panel.tsx` is untouched). Import moves from `{ HttpError, InUseError }` to `{ errorMessageKey }`.
### `web/src/api/queries.ts` (load-bearing errors)
Replace the 16 **mutation** `throw new Error("…")` with `throw new HttpError(response.status)`, preserving
the existing `InUseError` (409) and `FieldRejection` (422 on `setFields`) branches. Two POSTs
(`useCreateObject` `:184`, `useCreateVocabulary` `:279`) destructure only `{ data, error }` — add
`response` so the status is available. **Query** fetch errors (`:38,73,90,105,152,169,264`) and the login
`"invalid"/"network"` mapping (`:121`) are NOT changed (they're consumed by component `isError`/login
mapping, not the mutation toast). `useUpdateObject`'s 422 maps to the generic `errors.validation`;
surfacing a *structured* core-field rejection there is uncertain backend behavior → out of scope.
### Inline-site adoption — replace generic `form.rejected` with `<MutationError>`
- `web/src/components/delete-confirm-dialog.tsx:33-42` — the `confirm` catch currently sets
`err instanceof InUseError ? t("actions.inUse", {count}) : t("form.rejected")`. Replace with
`const { key, opts } = errorMessageKey(err); setMessage(t(key, opts));``errorMessageKey` already
handles `InUseError`, so the dialog simplifies and a 403/404 delete now reads a specific message. (The
dialog keeps its own `message` state because it must stay open on failure; it does not render
`<MutationError>` directly.)
- `web/src/authorities/authorities-page.tsx:144-148` — replace the `{create.isError && <p…>form.rejected}`
with `<MutationError error={create.error} />`.
- `web/src/vocab/vocabulary-terms.tsx:119-123``<MutationError error={addTerm.error} />`.
- `web/src/vocab/vocabulary-list.tsx:57-61` (create) and `:109-113` (rename) — `<MutationError error={create.error} />`
and `<MutationError error={renameVocabulary.error} />`.
- `web/src/fields/field-form.tsx:202-204` — replace the `{failed && <p…>form.rejected}` with
`<MutationError error={isEdit ? update.error : create.error} />`.
- **Object create/edit:** `object-new-page.tsx` and `object-edit-form.tsx` keep the `FieldRejection`
field-specific branch; in the non-`FieldRejection` else, replace `setError(t("form.rejected"))` with
`const { key, opts } = errorMessageKey(e); setError(t(key, opts));` (these set a string passed to
`ObjectForm`'s `formError` prop, so they use `errorMessageKey` directly, not `<MutationError>`).
### Edit-row consistency — `term-row.tsx`, `authority-row.tsx`, `queries.ts`
- Add `suppressErrorToast: true` to `useUpdateTerm` (`:444`) and `useUpdateAuthority` (`:521`) meta.
- In each row's edit view, render `<MutationError error={updateTerm.error} />` (resp. `updateAuthority.error`)
below the save/cancel buttons.
- Call `updateTerm.reset()` (resp. `updateAuthority.reset()`) inside the Edit button's `onClick` (alongside
the existing `setLabels`/`setUri`/`setEditing`) so a stale error from a prior failed save doesn't linger
when re-entering edit mode. On a failed save the row already stays editable (the `onSuccess`
`setEditing(false)` doesn't fire), preserving the user's input.
### `web/src/objects/object-edit-form.tsx` (fetch fix)
```tsx
const { data: object, isLoading, isError } = useObject(id!);
if (isLoading) return <FormSkeleton />;
if (isError) return <p className="p-4 text-sm text-destructive">{t("objects.loadError")}</p>;
if (!object) return <p className="p-4 text-sm text-muted-foreground">{t("objects.notFound")}</p>;
```
`objects.loadError` ("Could not load objects") already exists in both locales.
### i18n (en + sv parity — 5 new keys under `errors`)
| key | en | sv |
|-----|----|----|
| `errors.forbidden` | You don't have permission to do that. | Du har inte behörighet att göra det. |
| `errors.notFound` | That item no longer exists. | Objektet finns inte längre. |
| `errors.conflict` | That conflicts with existing data. | Det står i konflikt med befintliga data. |
| `errors.validation` | Some values weren't accepted. | Vissa värden godtogs inte. |
| `errors.server` | The server had a problem. Please try again. | Servern hade ett problem. Försök igen. |
## Data flow
mutation rejects → `HttpError(status)` (or `InUseError`/`FieldRejection`) → caught by the component (or
the `MutationCache` fallback) → `errorMessageKey(error)``{key, opts}``t(key, opts)` rendered inline
via `<MutationError>` (or as a `formError` string / dialog message). One mapping, one component, every
surface.
## Error handling / edges
- **All mutations suppress** after this change → the `MutationCache.onError` toast path is a dormant
fallback; kept (not deleted) so a future non-suppressed mutation still gets a sensible message.
- **401** isn't in the status map → falls to `toast.error`; the `client.ts` middleware still redirects to
login (unchanged). A brief generic toast before redirect is pre-existing, not worsened.
- **Network error with no response:** openapi-fetch may reject before a `response` exists; those paths
keep throwing a generic `Error``errorMessageKey` returns `toast.error`. (Only status-bearing
failures get differentiated.)
- **Language reactivity:** `<MutationError>` and the object-form `formError` re-render with the active
locale because they call `t` at render (the `formError` string is recomputed on the next failed submit;
acceptable — error strings are transient).
- **Stale row error:** cleared via `mutation.reset()` on re-edit.
## Testing
- **`web/src/api/error-message.test.ts`** (unit): each status → expected key (403/404/409/422/500/502);
`InUseError(3)``{ key: "actions.inUse", opts: { count: 3 } }`; a bare `Error`/unknown →
`{ key: "toast.error" }`.
- **`web/src/components/mutation-error.test.tsx`**: renders the mapped text for an `HttpError(403)`;
renders nothing for `null`/`undefined`; renders the in-use count for `InUseError`.
- **`web/src/api/mutation-feedback.test.tsx`** (rework): the "non-suppressed → catch-all toast" test is
obsolete (all mutations now suppress). Replace it with a test that a suppressed mutation failing adds
**no** toast (keep the existing delete case), plus assert the success-toast case still works. The
status→message behavior is covered by `error-message.test.ts`.
- **`term-row` / `authority-row`** (extend existing or via `mutation-feedback`): a failed update renders
the inline `MutationError` text and the row stays editable; a successful update closes the editor;
re-entering edit after a failure shows no stale error.
- **`object-edit-form`**: a `useObject` fetch error (mock `/api/admin/objects/{id}` → 500) renders
`objects.loadError`, not `objects.notFound`.
- **Inline adoption**: at least one create-form test (e.g. authorities) asserts a failed create shows the
status-aware message via `MutationError` (e.g. 403 → `errors.forbidden`).
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; en/sv parity (the #60
parity test guards the 5 new keys); no codename; no new dependency.
## Acceptance criteria
1. A shared `errorMessageKey(error)` maps `InUseError` and `HttpError` (by status: 403/404/409/422/≥500)
to i18n keys, with a `toast.error` fallback; unit-tested.
2. A shared `<MutationError error>` component renders the mapped inline alert (or nothing) and replaces
the duplicated `form.rejected` markup at every inline mutation-error site (delete dialog via the helper
directly, create/rename forms, edit rows, object-form `formError` via the helper).
3. The 16 mutation throws use `HttpError(status)` (queries unchanged); `query-client.ts` routes the toast
fallback through `errorMessageKey`.
4. `useUpdateTerm`/`useUpdateAuthority` suppress the toast and show an inline error at the row, staying
editable on failure and clearing stale errors on re-edit.
5. `object-edit-form` distinguishes a fetch error (`objects.loadError`) from "not found."
6. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported; en/sv parity (5 new
keys); no codename; no new dependency.
## Out of scope → follow-ups
- Structured field-level rejection on **core-object** update 422 (uncertain backend shape).
- The broader `queries.ts` split + relocating the error classes to `api/errors.ts` (tracked in **#65** —
`error-message.ts` imports the classes from `queries.ts` for now).
- Toast-based error surfacing (deliberately superseded by inline; the toast path remains only as a
fallback).
- Retry affordances / optimistic updates.
@@ -0,0 +1,100 @@
# Reference-Data Scannability + Parity — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #50 (scoped: scannability + parity; layout/edit-modality unification + API-backed counts are follow-ups).
## Context
The three reference-data screens (vocabularies, authorities, fields) render lists in API
**creation order** with **no client sort and no filter** — finding "Bronze" in a 200-term vocabulary
is effectively impossible. Read-mode rows show **only the label**; `external_uri` (the point of
authority control — distinguishing two "Mercury"s) is visible only in edit mode (`term-row.tsx`,
`authority-row.tsx`). There are **no counts** anywhere. And there are small parity/validation gaps:
authority **create** has no `external_uri` field (hardcoded `null`) though edit does; vocab **rename**
lacks the empty-guard the create form has; URI inputs have no `type="url"`/placeholder.
This milestone fixes the daily-pain "can't find / can't disambiguate" problems + the parity gaps,
**without** changing layout/edit modality (a separate redesign) and **without** backend changes.
**Facts:** `labelText(labels, lang)` exists (`lib/labels.ts`). No `Intl.Collator` anywhere. List
shapes: term/authority = `{ id, labels, external_uri?: string|null }` (authority also `kind`);
vocabulary = `{ id, key }` (no labels); field-definition = `{ key, labels, data_type, group?, required, … }`.
No count field on any view (so per-vocab term counts + per-kind authority counts need backend →
deferred; **field-group counts are client-side** and in scope). `Badge`/`Input` exist. Mutations:
`useCreateAuthority` accepts `external_uri` but the page passes `null`; `useRenameVocabulary` has no
empty-guard; `useAddTerm`/`useUpdateTerm`/`useUpdateAuthority` already accept `external_uri`.
### Decisions (from brainstorming)
1. Sort every list by label/key with a locale-aware `Intl.Collator`; add a client-side filter `Input`.
2. Show `external_uri` (linkified, muted) in term/authority read rows; field-group count `Badge`s.
3. Close the parity/validation gaps (authority-create URI, rename empty-guard, `type="url"` + placeholder).
## Components
### `web/src/lib/sort.ts` (new)
- A memoized collator per lang: `Intl.Collator(lang, { sensitivity: "base", numeric: true })` (cache in a `Map<string, Intl.Collator>`).
- `export function byLabel(lang: string)``(a: { labels: LabelView[] }, b) => number` comparing `labelText(a.labels, lang)` vs `labelText(b.labels, lang)` via the collator.
- `export function byKey(lang: string)``(a: { key: string }, b) => number` comparing `a.key` vs `b.key`.
- (Comparators take the minimal structural type so they work for terms/authorities/fields/vocabs.)
### `web/src/components/external-uri-link.tsx` (new)
A tiny shared read-mode link: `function ExternalUriLink({ uri }: { uri: string })`
`<a href={uri} target="_blank" rel="noopener noreferrer" className="block truncate text-xs text-muted-foreground hover:text-foreground">{uri}</a>`. Used in term-row + authority-row read mode (render only when `external_uri` is truthy).
### i18n (en + sv parity)
- `common.filter` = "Filter…" / "Filtrera…"
- `common.noMatches` = "No matches" / "Inga träffar"
- `labels.uriPlaceholder` = "https://…" (same in both)
### Per-screen changes
**Vocabularies**
- `vocabulary-list.tsx`: a filter `<Input>` (placeholder `common.filter`) above the list; filter vocabularies by `key` (case-insensitive `includes`) then **sort by `byKey(lang)`**; if filtered-empty but data exists, show muted `common.noMatches`. Add the **rename empty-guard**: `if (!draftKey.trim()) return;` before the rename mutate.
- `vocabulary-terms.tsx`: a filter `<Input>` above the terms list; filter terms by `labelText` then **sort by `byLabel(lang)`**; filtered-empty → `common.noMatches`. The add-term `external_uri` `<Input>` gets `type="url"` + `placeholder={t("labels.uriPlaceholder")}`.
- `term-row.tsx`: read mode renders `<ExternalUriLink uri={term.external_uri} />` under the label when present; the edit-mode `external_uri` `<Input>` gets `type="url"` + the placeholder.
**Authorities**
- `authorities-page.tsx`: a filter `<Input>` above the list; filter by `labelText` then **sort by `byLabel(lang)`**; filtered-empty → `common.noMatches`. The **create form gains an `external_uri` `<Input>`** (`type="url"`, placeholder) mirroring the edit row, stored in a `useState`, sent via `useCreateAuthority` (replace the hardcoded `external_uri: null`).
- `authority-row.tsx`: read mode renders `<ExternalUriLink uri={authority.external_uri} />` under the label when present; edit `external_uri` `<Input>` gets `type="url"` + placeholder.
**Fields**
- `field-list.tsx`: a filter `<Input>` above the list; filter by `labelText`/`key`; **sort groups alphabetically with "Other"/ungrouped last, and sort fields within each group by `byLabel(lang)`**; each group header shows a `<Badge variant="secondary">{count}</Badge>` (count of fields in that group, after filtering); filtered-empty → `common.noMatches`.
## Data flow
Loaded list → client filter (by label/key substring) → collator sort → render. `external_uri` read
from the existing view field. Field-group counts from the grouped array length. No new queries.
## Error handling / edges
- Filter is case-insensitive substring on the localized label (or key). Empty filter = show all.
- Collator memoized per lang; lang from `i18n.language` (sv/en) as elsewhere.
- `ExternalUriLink` only renders for truthy `external_uri`; `rel="noopener noreferrer"` + `target="_blank"`.
- `type="url"` gives the browser's basic URL hint (not strict validation — full validation is a follow-up); it must not block submitting an empty optional URI (the field stays optional → send `null`/omit when blank, exactly as today).
- Sorting must not mutate the query cache array — sort a copy (`[...list].sort(...)`).
- Field group ordering: a stable rule (named groups AZ by collator, then the ungrouped "Other" bucket last).
## Testing
- **`sort.test.ts`** (unit): `byLabel`/`byKey` order case-insensitively + locale-aware (e.g. "ä" sorts sensibly in sv; "bronze" before "Iron").
- **Vocabularies** (`vocabularies.test.tsx`): vocabularies render sorted by key; typing in the filter narrows the list; rename with an empty key does not fire the mutation.
- **Authorities** (`authorities.test.tsx`): list sorted by label; filter narrows; the create form has an `external_uri` field and a created authority posts the entered URI; a read row shows the `external_uri` link.
- **Vocabulary terms / term-row:** terms sorted by label; a term read row shows its `external_uri` link.
- **Fields** (`fields.test.tsx`): fields sorted within group; a group header shows a count badge; filter narrows. Keep the existing create/reveal-picker assertions green.
- Gate: `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors`; en/sv parity (3 new keys); no codename; no new dependency.
## Acceptance criteria
1. Every reference list is sorted by label (terms/authorities/fields) or key (vocabularies) via a
locale-aware `Intl.Collator`, and has a client-side filter `Input` (with a `common.noMatches`
empty state).
2. `external_uri` is shown (linkified, muted, truncated) in term + authority read rows when present.
3. Field-group headers show a count `Badge`.
4. Parity/validation gaps closed: authority **create** has an `external_uri` field and sends it; vocab
**rename** has an empty-guard; all `external_uri` inputs use `type="url"` + a placeholder.
5. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported; en/sv parity; no
codename; no new dependency; no layout/edit-modality change; no backend change.
## Out of scope → follow-ups
- The layout + edit-modality **unification** (authorities → two-pane pane-edit; vocab-rename/term/
authority → pane-edit; one create location) — a separate redesign.
- **API-backed counts**: per-vocabulary term counts and per-kind authority-tab counts (need view/count
fields or endpoints).
- Strict URL validation (beyond `type="url"`); linkifying `external_uri` elsewhere (e.g. object detail).
@@ -0,0 +1,128 @@
# Search-Result Date Meta + Estimated-Count Copy — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #61 (scoped: add a date to result rows + soften the estimated count; object type/facet is
declined — no such field exists in the domain).
## Context
Search result rows show name + object_number + visibility badge + snippet, but no date — power users
disambiguate hits by period. The count copy reads "{{count}} results" though it's Meilisearch's
`estimated_total`. **There is no object type/category/classification field anywhere in the domain**
(`CatalogueObject` = object_number/object_name/number_of_objects/brief_description/current_location/
current_owner/recorder/**recording_date**/visibility + flexible `fields` + timestamps), so a "type"
meta/facet is impossible without a new domain concept (out of scope). The only disambiguating date
available is `recording_date` (`YYYY-MM-DD`) — but it is **not indexed** into the search document or
returned in the hit, so surfacing it needs a backend change.
**Facts:** `SearchDocument` (`crates/search/src/lib.rs:30`) and `SearchHit` (`:52`) carry
`id/object_number/object_name/brief_description/[current_owner/recorder]/visibility/[snippet]` — no
date. `build_document` (`:302`) projects a `CatalogueObject``SearchDocument` (does not copy
`recording_date`). `search_objects` (`:185`) maps Meili results → `SearchHit`. `SearchHitView`
(`crates/api/src/admin_search.rs:26`) mirrors `SearchHit`. The frontend type is
`components["schemas"]["SearchHitView"]`; `schema.d.ts` is generated by `pnpm gen:api`. `AdminObjectView`
already serializes `recording_date` as `Option<String>` (`YYYY-MM-DD`, via `format_date`). On-write
`sync_object` re-projects an object after each catalogue write; `reindex_all` is the full rebuild path.
### Decisions (from brainstorming)
1. Thread `recording_date` (`YYYY-MM-DD`) through `SearchDocument``SearchHit``SearchHitView`;
show it on the row when present.
2. Soften the count to `~{{count}}` (it's an estimate).
3. Decline object type/facet (no domain field).
## Backend (`crates/search`, `crates/api`)
### `SearchDocument` + `build_document` (`crates/search/src/lib.rs`)
- Add field: `pub recording_date: Option<String>,` to `SearchDocument`.
- In `build_document`'s returned struct: `recording_date: object.recording_date.map(|d| d.to_string()),`
(`domain::Date`'s `Display` is ISO `YYYY-MM-DD`, matching `AdminObjectView`). Index it as a plain
string (Meili stores/returns it; it need not be filterable).
### `SearchHit` + `search_objects` mapping (`crates/search/src/lib.rs`)
- Add `pub recording_date: Option<String>,` to `SearchHit`.
- In `search_objects`'s hit map: `recording_date: doc.recording_date,` (the executed
`SearchDocument` deserialization carries it; old index docs missing the field deserialize to `None`
because it's `Option` — graceful).
### `SearchHitView` (`crates/api/src/admin_search.rs`)
- Add `pub recording_date: Option<String>,` to `SearchHitView`.
- In the map closure (`:100`): `recording_date: h.recording_date,`.
### Indexing / backfill
- New & edited objects get `recording_date` automatically via the existing on-write `sync_object`.
- Already-indexed objects return `recording_date: None` until a `reindex_all` (the existing rebuild
path) runs — a graceful, opt-in backfill; **a `reindex` CLI command is a follow-up**, not in scope.
### Tests (Rust — need the docker stack: Postgres :5442, Meilisearch :7700)
- Update any direct `SearchDocument`/`SearchHit` struct literals in `crates/search/tests/*` /
`crates/api/tests/*` to include `recording_date: None` (compile fix). (Object fixtures construct
`CatalogueObject`, which already has `recording_date` — unaffected.)
- Extend `crates/search/tests/search.rs` (the `search_objects_returns_hits_…` test): give one seeded
object a `recording_date` and assert the returned hit's `recording_date` is the `YYYY-MM-DD` string
(proves it flows index → hit).
## Frontend (`web`)
### `web/src/api/schema.d.ts` (generated)
After the backend change, regenerate via `pnpm gen:api` (stack + server up) **or** hand-add
`recording_date?: string | null;` to the `SearchHitView` block (mirroring `brief_description?: string |
null` / `snippet?: string | null`) — a later `gen:api` reproduces it identically. Either is acceptable;
the manual edit avoids running the server purely for codegen.
### `search-result-row.tsx`
Add `recording_date` to the meta line (the `flex items-center gap-2 text-xs text-muted-foreground`
row), after `object_number`, when present:
```tsx
<span>{hit.object_number}</span>
{hit.recording_date && <span>· {hit.recording_date}</span>}
<VisibilityBadge visibility={hit.visibility} />
```
(Render the `YYYY-MM-DD` string as-is — it's a plain recording date, not a UTC timestamp, so no
timezone formatting. A separator dot before it keeps the row scannable.)
### `search-panel.tsx` + i18n — soften the count
The count uses `t("search.resultCount", { count: total })` with `resultCount_one`/`resultCount_other`.
Change the i18n values to flag the estimate (en + sv, parity preserved):
- en: `"resultCount_one": "~{{count}} result"`, `"resultCount_other": "~{{count}} results"`
- sv: `"resultCount_one": "~{{count}} träff"`, `"resultCount_other": "~{{count}} träffar"`
No code change in `search-panel.tsx` (the key already interpolates `count`).
### Fixtures + tests
- `web/src/test/fixtures.ts` `searchHits`: add `recording_date` to the first hit (e.g. `"1962-04-03"`);
the rest can stay `recording_date: null` (or omit — it's optional).
- `web/src/search/search.test.tsx`: assert the first result row shows the date (`getByText("1962-04-03")`
or within the row); assert the count copy includes the `~` (`getByText(/~\s*25 results/i)` — note the
existing `/25 results/i` assertion still matches the new copy; tighten it to require the `~`).
## Data flow
DB object → `build_document` (now copies `recording_date`) → Meili doc → `search_objects` hit → API
`SearchHitView``useSearch``SearchResultRow` renders the date. The count is the same
`estimated_total`, now labelled `~`.
## Error handling / edges
- `recording_date` is `Option` end-to-end → absent on objects without a recording date and on
not-yet-reindexed docs; the row simply omits it.
- `Date::to_string()` is ISO `YYYY-MM-DD` (matches `AdminObjectView`); no locale/timezone formatting.
- The `~` is cosmetic; pluralization (`_one`/`_other`) is unchanged.
## Testing
- **Rust:** `cargo build --workspace`; `cargo nextest run -p search -p api` (stack up) green, incl. the
new recording_date assertion + any literal compile-fixes.
- **Frontend:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; the new row-date +
count tests pass; en/sv parity (the #60 parity test guards the changed values); no codename.
- No new dependency.
## Acceptance criteria
1. `recording_date` is projected into the search index (`SearchDocument`/`build_document`) and returned
through `SearchHit``SearchHitView``schema.d.ts` (`SearchHitView.recording_date?: string|null`).
2. The search result row shows the recording date (when present) on its meta line.
3. The result-count copy reads `~{{count}} …` to flag the Meilisearch estimate (en + sv).
4. Rust (`cargo build` + search/api tests) green; frontend gate green; en/sv parity; no codename; no new
dependency.
## Out of scope → follow-ups
- An object **type/classification** domain concept + a **type facet** (no such field exists today).
- A `reindex` CLI command to backfill `recording_date` onto already-indexed objects (new/edited objects
index it automatically; `reindex_all` is the existing rebuild path).
- Richer faceting; making `recording_date` filterable/sortable in search.
@@ -0,0 +1,205 @@
# Session-Expiry Soft Redirect + Auth Feedback — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #48 (Frontend UX: session-expiry handling loses in-progress work; auth feedback gaps).
## Context
The session-expiry path is the most damaging daily failure mode for a long-session data-entry tool.
Today a 401 from any API call runs `redirectToLogin()` (`web/src/api/auth-redirect.ts`) →
`window.location.assign("/login")`, a **full page reload**: it tears down the React app and query
cache, gives the login page no reason ("session expired"), and after re-login drops the user on
`/objects` rather than where they were. Login also hardcodes `navigate("/objects")` and ignores the
attempted destination, and the Sign out control has no pending state.
**Already fixed since the audit (verified):** `require-auth.tsx` now renders `<AppShellSkeleton />`
during the `useMe` fetch (not the blank `<div role="status">` the audit cited), so the "blank screen on
load" problem is gone and is **out of scope**. Logout **error** feedback also already exists: `useLogout`
carries no `meta.suppressErrorToast`, so the global `MutationCache.onError` (`api/query-client.ts`)
already shows an error toast on logout failure — the only remaining logout gap is a pending/disabled
state.
**The crux:** the 401 handler lives in an openapi-fetch `Middleware` (`api/client.ts`), which runs
**outside** React and therefore cannot call `useNavigate`. The app uses a React Router 7 **data router**
(`createBrowserRouter`, `app.tsx`). The test harness `renderApp` (`src/test/render.tsx`) mounts the
component under test at `path:"*"` in a **fresh `createMemoryRouter`** — it does not use the app's real
`router` singleton.
### Decisions (from brainstorming)
1. **Soft redirect + return** (chosen over an in-place re-auth modal): router-navigate (no full reload)
to `/login?reason=expired&from=<path>`; login shows the reason and returns the user to `from`. The
React app + query cache survive; an unsaved edit form's typed values are still lost, but the user
lands back on the same record. (Full field-level preservation via an in-place modal is a deferred
follow-up.)
2. **Navigate-bridge module** (chosen over exporting the `router` singleton or a custom DOM event): a
module holds a settable `navigate` ref that a one-line component registers. This is testable with the
memory-router harness (tests call `setNavigate(mock)` directly); importing the singleton `router`
would be untestable because `renderApp` never mounts it.
## Components
### `api/auth-redirect.ts` (rewrite)
```ts
type NavigateFn = (to: string, opts?: { replace?: boolean }) => void;
let navigateFn: NavigateFn | null = null;
/** Register/unregister the router's navigate fn (called by NavigationBridge). */
export function setNavigate(fn: NavigateFn | null): void {
navigateFn = fn;
}
/** Soft-redirect to login on a 401, preserving SPA state and the return path.
* Falls back to a hard navigation when no router navigate is registered yet
* (e.g. a 401 during the very first load). No-op when already on /login. */
export function redirectToLogin(): void {
const { pathname, search } = window.location;
if (pathname === "/login") return;
const from = encodeURIComponent(pathname + search);
const target = `/login?reason=expired&from=${from}`;
if (navigateFn) {
navigateFn(target, { replace: true });
} else {
window.location.assign(target);
}
}
```
`api/client.ts` is unchanged — the middleware still calls `redirectToLogin()`; only its behaviour changes.
### `shell/navigation-bridge.tsx` (new)
```tsx
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { setNavigate } from "../api/auth-redirect";
/** Bridges React Router's navigate to the non-React 401 handler. Renders nothing. */
export function NavigationBridge() {
const navigate = useNavigate();
useEffect(() => {
setNavigate(navigate);
return () => setNavigate(null);
}, [navigate]);
return null;
}
```
### `app.tsx` (wrap routes in a pathless layout)
Add a pathless layout route around **all** existing routes whose element mounts the bridge plus an
`<Outlet/>`, so the bridge is always present (covers `/login` and the authed area):
```tsx
function RootLayout() {
return (
<>
<NavigationBridge />
<Outlet />
</>
);
}
```
```tsx
createRoutesFromElements(
<Route element={<RootLayout />}>
<Route path="/login" element={<LoginPage />} />
{/* …existing RequireAuth / AppShell subtree unchanged… */}
<Route path="*" element={<Navigate to="/objects" replace />} />
</Route>,
)
```
### `auth/login-page.tsx` (reason banner + return-to + empty-field guard)
- `const [params] = useSearchParams();`
- Reason banner when `params.get("reason") === "expired"`: render `t("auth.sessionExpired")` (a
non-error info note, distinct from the existing `role="alert"` invalid-credentials message).
- On success, navigate to `safeFrom(params.get("from"))` instead of the hardcoded `/objects`:
```ts
function safeFrom(raw: string | null): string {
if (!raw) return "/objects";
// single leading slash only — reject protocol-relative ("//host") / absolute URLs (open redirect)
return /^\/(?!\/)/.test(raw) ? raw : "/objects";
}
```
(`raw` from `useSearchParams` is already percent-decoded.)
- Disable submit on empty fields: `disabled={login.isPending || !email.trim() || !password}`.
### `auth/require-auth.tsx` (capture the attempted location)
```tsx
const location = useLocation();
// …
if (!user) {
const from = encodeURIComponent(location.pathname + location.search);
return <Navigate to={`/login?from=${from}`} replace />;
}
```
No `reason` here — an unauthenticated deep-link is not an expiry; login simply returns the user to `from`.
### `shell/user-menu.tsx` (logout pending state)
Keep the menu open during logout and show a pending state on the item (Base UI `MenuItem` supports both
`closeOnClick` and `disabled`):
```tsx
<MenuItem closeOnClick={false} disabled={logout.isPending} onClick={onSignOut}>
{logout.isPending ? t("auth.signingOut") : t("auth.signOut")}
</MenuItem>
```
On success the mutation sets `me` to null and navigates to `/login`, so `UserMenu` unmounts (it returns
`null` when `!me`). Logout **error** already surfaces via the global toast (`useLogout` has no
`suppressErrorToast`). `closeOnClick={false}` also prevents a double-submit (the item is disabled while
pending).
### i18n (en + sv parity — 2 new keys)
- `auth.sessionExpired` = "Your session expired — please sign in again." /
"Din session har gått ut — logga in igen."
- `auth.signingOut` = "Signing out…" / "Loggar ut…"
## Data flow
API 401 → `client.ts` middleware → `redirectToLogin()` → registered `navigate("/login?reason=expired&
from=<path>", {replace})` (no reload) → `LoginPage` reads `reason`/`from`, shows the banner → on success
`navigate(safeFrom(from), {replace})`. Deep-link-while-unauthed → `RequireAuth``/login?from=<path>`
same return-to. The `NavigationBridge` keeps `setNavigate` current for the lifetime of the app.
## Error handling / edges
- **Already on `/login`:** `redirectToLogin()` is a no-op (no redirect loop).
- **401 before the bridge mounts** (first paint): `navigateFn` is null → hard `window.location.assign`
to `/login?reason=expired&from=…` (graceful fallback; reason/return still preserved).
- **Open redirect:** `safeFrom` accepts only a single-leading-slash local path; `//evil.com`,
`https://…`, and empty → `/objects`.
- **Login success with no `from`:** defaults to `/objects` (unchanged behaviour).
- **StrictMode double-invoke:** the bridge effect is idempotent (sets the same fn; cleanup nulls it) —
safe.
- **Reason banner is informational**, not `role="alert"` (it is not an error and should not preempt the
credential-error alert region).
## Testing
- **`api/auth-redirect.test.ts`** (unit, jsdom): with `setNavigate(mock)` and a stubbed
`window.location` (pathname `/objects/abc`), `redirectToLogin()` calls `mock` with
`"/login?reason=expired&from=%2Fobjects%2Fabc"` and `{replace:true}`; with `setNavigate(null)` it calls
`window.location.assign` with the same target; when `pathname === "/login"` it does nothing.
- **`auth/login-page` test:** rendering at `/login?reason=expired` shows the `auth.sessionExpired` text;
a successful login at `/login?from=%2Fobjects%2F123` navigates to `/objects/123`; a bad `from`
(`//evil.com`) falls back to `/objects`; the submit button is disabled until both fields are non-empty.
- **`auth/require-auth` test:** when `useMe` resolves to no user, it renders a redirect to
`/login?from=…` carrying the attempted path.
- **`shell/user-menu` test:** with a delayed logout handler, after clicking Sign out the item shows the
`auth.signingOut` pending text (the menu stays open via `closeOnClick={false}`).
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; en/sv parity (the #60
parity test guards the 2 new keys); no codename; no new dependency.
## Acceptance criteria
1. A 401 from an API call performs a **router** navigation (no full page reload) to
`/login?reason=expired&from=<attempted path>`; the React app/query cache are not torn down.
2. The login page shows a "session expired" message when `reason=expired`, and on success returns the
user to a **validated** `from` path (defaulting to `/objects`); protocol-relative/absolute `from`
values are rejected.
3. `RequireAuth` redirects unauthenticated users to `/login?from=<attempted path>` so deep links return
after login.
4. Login submit is disabled while either field is empty; the Sign out item shows a pending state
(`auth.signingOut`, disabled) while logging out.
5. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported; en/sv parity (2 new
keys); no codename; no new dependency.
## Out of scope → follow-ups
- **In-place re-auth modal** that keeps the edit form mounted for full field-level preservation of
in-progress work (deferred by decision; soft redirect ships first).
- Idle/expiry **pre-warning** (e.g. "your session will expire soon") and token/silent refresh.
- Broader auth-event audit surfacing on the client.
@@ -0,0 +1,146 @@
# Split queries.ts: Errors + Query-Key Factory + Domain Modules — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #65 (split the 584-line `queries.ts`, extract the error classes, add a query-key factory, decide the search-invalidation gap).
## Context
`web/src/api/queries.ts` is 584 lines spanning 8 domains (error classes, auth, objects, fields-on-object,
field-definitions, vocab, terms, authorities, search). No hook crosses a domain boundary, so a split is
low-risk. Three concrete problems:
- **Toast path drags in the hook module.** After #63, `query-client.ts` imports `errorMessageKey` from
`error-message.ts`, which imports `{ HttpError, InUseError }` from `./queries` — so the toast wiring
transitively loads all the hooks. Extracting the error classes breaks that chain.
- **No query-key factory.** ~20 key literals (`["objects", params]`, `["object", id]`,
`["terms", vocabularyId]`, …) plus `config-provider.tsx`'s `["config"]` are hand-written and
typo-prone.
- **Object writes don't invalidate `["search"]`.** `useUpdateObject`/`useDeleteObject`/`useSetVisibility`
invalidate `["objects"]`/`["object", id]` but never the search index, so the search panel keeps stale
hits until the user re-searches. **Decision (brainstorming): invalidate** — conventional
eventually-consistent behaviour; the query is `enabled` only with a term, so it's a no-op when not
searching.
**Constraint:** ~30 files import from `../api/queries`. The public import path must stay stable (a
barrel), so the refactor touches the data layer only — feature files don't change.
## Components
### `api/errors.ts` (new) — canonical home for the 4 error classes
Move `HttpError`, `FieldRejection`, `InUseError`, `VisibilityError` here verbatim (no behaviour change).
`error-message.ts` changes its import from `./queries` to `./errors` — this is the decoupling: the toast
path (`query-client → error-message → errors`) no longer loads the hook module. The barrel (below)
**re-exports** the error classes (`export * from "../errors"`), so the existing importers that pull them
from `../api/queries``object-edit-form.tsx`, `object-new-page.tsx`, `publish-control.tsx`,
`search-panel.tsx`, and the tests `mutation-error.test.tsx` / `labelled-record-row.test.tsx` /
`error-message.test.ts` — keep working unchanged.
### `api/query-keys.ts` (new) — one key factory
```ts
export type ObjectListParams = {
limit: number;
offset: number;
sort?: string;
order?: "asc" | "desc";
visibility?: string;
q?: string;
};
export const keys = {
me: () => ["me"] as const,
config: () => ["config"] as const,
objects: () => ["objects"] as const, // family prefix (invalidation)
objectsPage: (p: ObjectListParams) => ["objects", p] as const,
object: (id: string) => ["object", id] as const,
fieldDefinitions: () => ["field-definitions"] as const,
vocabularies: () => ["vocabularies"] as const,
terms: (vocabularyId: string) => ["terms", vocabularyId] as const,
authorities: (kind: string) => ["authorities", kind] as const,
search: () => ["search"] as const, // family prefix (invalidation)
searchResults: (term: string, visibility: string | null) => ["search", term, visibility] as const,
};
```
- Every `queryKey:` / `invalidateQueries` / `setQueryData` site in the query modules **and**
`config-provider.tsx`'s `["config"]` routes through `keys.*` — producing the identical arrays, so
cache behaviour is unchanged. (`keys.objects()` prefix-matches `keys.objectsPage(p)` exactly as
`["objects"]` matches `["objects", params]` today.)
- `ObjectListParams` lives here (it *is* part of the key); `objects.ts` imports it from here — one
direction, no import cycle. (It moves out of `queries.ts`'s objects section.)
### `api/queries/` (new directory) — split by domain + barrel
```
api/
client.ts (unchanged)
errors.ts (new)
query-keys.ts (new)
query-client.ts (unchanged — already imports only error-message)
error-message.ts (one-line change: import errors from ./errors)
queries/
index.ts barrel: `export * from "./auth"; … ; export * from "../errors";`
auth.ts useMe, useLogin, useLogout
objects.ts useObjectsPage, useObject, useCreateObject, useUpdateObject, useSetFields, useDeleteObject, useSetVisibility
field-defs.ts useFieldDefinitions, useCreateFieldDefinition, useUpdateFieldDefinition, useDeleteFieldDefinition
vocab.ts useVocabularies, useCreateVocabulary, useRenameVocabulary, useDeleteVocabulary, useTerms, useAddTerm, useUpdateTerm, useDeleteTerm
authorities.ts useAuthorities, useCreateAuthority, useUpdateAuthority, useDeleteAuthority
search.ts useSearch, SEARCH_PAGE
```
`api/queries.ts` is deleted; `import { … } from "../api/queries"` resolves to `api/queries/index.ts`.
Each module imports `api` from `../client`, types from `../schema`, error classes from `../errors`, and
`keys` from `../query-keys`. The hook bodies are moved verbatim (only their key literals → `keys.*`).
### `api/query-client.ts`
No change needed — after #63 it imports only `errorMessageKey`. (Once `error-message.ts` points at
`./errors`, the `query-client → error-message → queries` hook dependency is gone.)
### Search invalidation (`objects.ts`)
`useUpdateObject`, `useDeleteObject`, `useSetVisibility` add
`void qc.invalidateQueries({ queryKey: keys.search() })` alongside their existing object invalidations.
## Data flow / behaviour
Identical to today except: after an object update/delete/visibility-change, an active search query
refetches (eventually-consistent with the Meilisearch reindex). All keys, hooks, error classes, and
endpoints are otherwise unchanged.
## Error handling / edges
- The barrel re-exporting errors means the classes are importable from both `../api/errors` (canonical)
and `../api/queries` (compat) — intentional, to avoid churning ~6 importers + tests.
- `as const` keys are readonly tuples; TanStack Query accepts readonly arrays as keys — no type issue.
- No import cycle: `query-keys.ts` exports `ObjectListParams` + `keys` and imports nothing from
`queries/`; `objects.ts` imports both from `query-keys.ts`.
- The search query stays `enabled: term.length > 0`, so invalidating `["search"]` is a no-op when no
search is active.
## Testing
- **Behavior guard (must pass unchanged):** `queries.test.ts`, `queries.authoring.test.tsx`,
`queries.fields.test.tsx`, `queries.search.test.tsx`, `queries.visibility.test.tsx`,
`queries.vocab.test.tsx`, `mutation-feedback.test.tsx`, `error-message.test.ts`,
`mutation-error.test.tsx`, `labelled-record-row.test.tsx` — all import from `../api/queries` (the
barrel) and from the re-exported error classes; they must not need edits. The full app suite is the
integration guard.
- **New `query-keys.test.ts`:** assert the factory arrays — e.g. `keys.objects()``["objects"]`,
`keys.objectsPage(p)``["objects", p]`, `keys.object("x")``["object", "x"]`,
`keys.searchResults("q", null)``["search", "q", null]`.
- **Search-invalidation assertion:** extend `queries.visibility.test.tsx` (or `.authoring`) to seed an
active `["search", …]` query in the cache, run an object update/delete/set-visibility, and assert the
search query is invalidated (e.g. its `isInvalidated`/refetch fires).
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; no new dependency; no new
i18n keys; no codename; `check:size` unchanged (pure reorg + one invalidate call).
## Acceptance criteria
1. `api/errors.ts` holds the 4 error classes; `error-message.ts` imports them from `./errors`; the barrel
re-exports them so every existing importer (components + tests) still resolves.
2. `queries.ts` is split into `api/queries/{auth,objects,field-defs,vocab,authorities,search}.ts` +
`index.ts`; `api/queries.ts` is deleted; the `../api/queries` import path is unchanged for all ~30
consumers.
3. A `keys` factory in `api/query-keys.ts` is used by every `queryKey`/invalidate/setQueryData site,
including `config-provider.tsx`.
4. `useUpdateObject`/`useDeleteObject`/`useSetVisibility` invalidate `keys.search()`.
5. All existing tests pass unchanged; `typecheck`/`lint`/`build`/`check:colors` green; `check:size`
unchanged; no new dependency; no new i18n keys; no codename.
## Out of scope → follow-ups
- No hook signature/behaviour changes beyond the search invalidation; no endpoint changes.
- `staleTime` / `keepPreviousData` choices stay as-is (intentional per #63's note).
- Repointing the 4 component error-importers from the barrel to `../api/errors` (the re-export keeps them
working; a later cosmetic cleanup could do it).
@@ -0,0 +1,182 @@
# Unify Vocabulary + Authority CRUD — Design
**Date:** 2026-06-08
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #64 (the duplicated Vocabulary-terms + Authorities CRUD surfaces).
## Context
The Vocabulary-terms and Authorities admin surfaces are two copies of one feature ("a labelled record
with an optional external URI: filterable list + inline-edit rows + create form"). The duplication spans
four files and ~280 lines:
- `vocab/term-row.tsx` and `authorities/authority-row.tsx` are byte-for-byte twins except: the mutation
hooks (`useUpdateTerm`/`useDeleteTerm` vs `useUpdateAuthority`/`useDeleteAuthority`), the record type
(`TermView` vs `AuthorityView`), the URI-input id prefix (`term-uri-` / `auth-uri-`), the mutate-arg
shape (`{ vocabularyId, termId, … }` vs `{ id, kind, … }`), and the delete-confirm i18n key.
- `authorities/authorities-page.tsx` and `vocab/vocabulary-terms.tsx` share the filter input, the
4-state list (loading→skeleton / error / empty / no-matches / rows), and the create form
(`LabelEditor` + URI input + `labels.some()` validation + `MutationError` + submit). They differ in the
i18n keys, the create-form heading, and (authorities only) the kind-tabs `<nav>` + `PageTitle` +
`Navigate` guard.
A fix to inline-edit behaviour must currently be made in two places and silently drifts. (These files
already share `LabelEditor`, `ExternalUriLink`, `DeleteConfirmDialog`, and `MutationError`.)
`vocabulary-list.tsx` (vocabularies are `key`-based, not labelled records) and the `objects` RHF surface
are intentionally **not** unified — different shapes.
### Decisions (from brainstorming)
Full unification — three shared components in `src/components/`, with `TermRow`/`AuthorityRow` and the
two pages reduced to thin adapters. All variance is pushed into props; no generics on the row (the adapter
owns the mutate-arg shape). Behavior-preserving — existing tests are the guard.
## Components
### `components/labelled-record-row.tsx` (new) — `LabelledRecordRow`
Owns `editing` / `labels` / `uri` state. Renders the display row (`labelText` + `ExternalUriLink` + Edit
button + `DeleteConfirmDialog`) or the edit view (`LabelEditor` + URI `<Input>` + Save/Cancel +
`MutationError`).
```ts
type RecordLike = { id: string; labels: LabelView[]; external_uri: string | null };
function LabelledRecordRow(props: {
record: RecordLike;
lang: string;
deleteConfirmKey: string; // i18n key for the confirm prompt
savePending: boolean; // update.isPending
saveError: unknown; // update.error
onEditOpen: () => void; // adapter calls update.reset()
onSave: (labels: LabelInput[], uri: string | null, done: () => void) => void;
onDelete: () => Promise<void>;
}): JSX.Element;
```
- Edit button onClick: `onEditOpen(); setLabels(record.labels as LabelInput[]); setUri(record.external_uri ?? ""); setEditing(true);` (preserves the #63 reset-on-edit-open behaviour).
- Save: `onSave(labels, uri.trim() || null, () => setEditing(false))`; Save disabled while `savePending`;
`<MutationError error={saveError} />` below the buttons.
- The URI input id uses `useId()` (replaces the `term-uri-${id}` / `auth-uri-${id}` scheme).
- `record.labels` is `LabelView[]`; cast to `LabelInput[]` for `LabelEditor` (same cast the current rows do).
### `components/labelled-record-create-form.tsx` (new) — `LabelledRecordCreateForm`
Owns its own `labels` / `uri` / `requiredError` state. Renders `heading` + `LabelEditor` + URI `<Input>`
(id via `useId()`) + the `form.required` validation alert + `<MutationError error={error} />` + submit.
```ts
function LabelledRecordCreateForm(props: {
heading: ReactNode;
submitLabel: string;
pending: boolean; // create.isPending
error: unknown; // create.error
onCreate: (labels: LabelInput[], uri: string | null, reset: () => void) => void;
}): JSX.Element;
```
- Submit: `if (!labels.some((l) => l.label)) { setRequiredError(true); return; } setRequiredError(false);
onCreate(labels, uri.trim() || null, () => { setLabels([]); setUri(""); });`
- Submit button disabled while `pending`.
### `components/filtered-record-list.tsx` (new) — `FilteredRecordList<T>`
Owns the `filter` state. Renders the filter `<Input>` **always**, then (loading → `<ListSkeleton>` else the
`<ul>`), so the filter stays visible during load (matches current behaviour).
```ts
function FilteredRecordList<T extends { id: string; labels: LabelView[] }>(props: {
records: T[] | undefined;
lang: string;
isLoading: boolean;
isError: boolean;
loadErrorText: string;
emptyText: string;
renderRow: (record: T) => ReactNode;
}): JSX.Element;
```
- `const q = filter.trim().toLowerCase(); const rows = [...(records ?? [])].filter((r) => !q ||
labelText(r.labels, lang).toLowerCase().includes(q)).sort(byLabel(lang));`
- List states (preserving the current logic exactly): `isError` → `loadErrorText`; `!isError &&
records?.length === 0` → `emptyText`; `!isError && records && records.length > 0 && rows.length === 0`
→ `t("common.noMatches")`; else `rows.map(renderRow)`.
- Filter input uses `aria-label={t("common.filter")}` + `placeholder={t("common.filter")}` (unchanged).
### Adapters
**`vocab/term-row.tsx`** keeps its public API `<TermRow vocabularyId term lang />` (its #63 test stays
valid). Body:
```tsx
const update = useUpdateTerm();
const del = useDeleteTerm();
return (
<LabelledRecordRow
record={term}
lang={lang}
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={update.isPending}
saveError={update.error}
onEditOpen={() => update.reset()}
onSave={(labels, uri, done) =>
update.mutate({ vocabularyId, termId: term.id, external_uri: uri, labels }, { onSuccess: done })}
onDelete={() => del.mutateAsync({ vocabularyId, termId: term.id })}
/>
);
```
**`authorities/authority-row.tsx`** keeps `<AuthorityRow authority kind lang />`; analogous with
`useUpdateAuthority`/`useDeleteAuthority`, `deleteConfirmKey="actions.confirmDeleteAuthority"`, save arg
`{ id: authority.id, kind, external_uri: uri, labels }`, delete `{ id: authority.id, kind }`.
**`authorities/authorities-page.tsx`** keeps the `PageTitle`, kind `<nav>`, `Navigate` guard, breadcrumb,
and `useDocumentTitle`. Replaces the inline filter/list with `<FilteredRecordList records={authorities}
lang={lang} isLoading={isLoading} isError={isError} loadErrorText={t("authorities.loadError")}
emptyText={t("authorities.empty")} renderRow={(a) => <AuthorityRow authority={a} kind={currentKind}
lang={lang} />} />`, and the inline form with `<LabelledRecordCreateForm heading={`${t("authorities.new")} ·
${t(\`authorities.${currentKind}\`)}`} submitLabel={t("authorities.create")} pending={create.isPending}
error={create.error} onCreate={(labels, uri, reset) => create.mutate({ kind, external_uri: uri, labels },
{ onSuccess: reset })} />`. (Drops the now-unused local `labels`/`uri`/`error`/`filter` state and the
`onCreate` handler.)
**`vocab/vocabulary-terms.tsx`** keeps its `vocab.terms` caption + breadcrumb. Uses `<FilteredRecordList
records={terms} … loadErrorText={t("vocab.loadError")} emptyText={t("vocab.noTerms")} renderRow={(term) =>
<TermRow vocabularyId={id} term={term} lang={lang} />} />` and `<LabelledRecordCreateForm
heading={t("vocab.addTerm")} submitLabel={t("vocab.addTerm")} pending={addTerm.isPending}
error={addTerm.error} onCreate={(labels, uri, reset) => addTerm.mutate({ vocabularyId: id, external_uri:
uri, labels }, { onSuccess: reset })} />`.
## Data flow
No change to the query/mutation layer. Each page owns its data hooks + the page-specific chrome (nav,
headings, breadcrumb) and hands records + render callbacks to the shared list, mutate callbacks to the
shared create form, and per-row mutate callbacks (via the row adapters) to the shared row.
## Error handling / edges
- Inline save errors and create errors still render via `<MutationError>` (status-aware, from #63),
unchanged.
- The `form.required` validation (empty labels) stays in the create form.
- Reset-on-edit-open (`update.reset()`) preserved so a stale save error doesn't linger.
- The empty-vs-no-matches distinction is computed from the **raw** `records` length (empty) vs the
**filtered** `rows` length (no matches), exactly as today.
- `useId()` keeps URI-input ids unique across simultaneously-mounted rows/forms.
## Testing
- **Behavior guard:** existing tests stay green unchanged — `vocab/term-row.test.tsx`,
`authorities/authorities.test.tsx` (page filter/create/list), `vocab/vocabularies.test.tsx`
(out-of-scope list, must not break). Same public component APIs + DOM.
- **New focused tests** for the three components:
- `labelled-record-row.test.tsx`: display → click Edit → Save calls `onSave` and closes on `done`; a
failed save (passing `saveError`) shows the inline error and the row stays editable; Delete invokes
`onDelete`.
- `labelled-record-create-form.test.tsx`: submit with empty labels shows `form.required` and does NOT
call `onCreate`; a valid submit calls `onCreate` and the `reset` clears the inputs.
- `filtered-record-list.test.tsx`: typing in the filter narrows the rendered rows; `records=[]`
`emptyText`; non-empty records + non-matching filter → `common.noMatches`; `isError``loadErrorText`;
`isLoading` → skeleton.
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; no new i18n keys; en/sv
parity unaffected; no codename; no new dependency. `check:size` should not grow (net code reduction).
## Acceptance criteria
1. `LabelledRecordRow`, `LabelledRecordCreateForm`, `FilteredRecordList<T>` exist in `src/components/` with
the prop shapes above; `TermRow`/`AuthorityRow` and the two pages are adapters over them.
2. `term-row.tsx` + `authority-row.tsx` no longer duplicate the row body; `authorities-page.tsx` +
`vocabulary-terms.tsx` no longer duplicate the filter/list/create-form body.
3. All existing tests pass unchanged; the three new components have focused tests.
4. No behavior change: inline edit/save/delete, create + validation, filtering, the 4 list states, the
authorities kind-nav, and breadcrumbs all work as before.
5. `typecheck`/`lint`/`test`/`build`/`check:colors` green; `check:size` reported (not increased); no new
i18n keys; no codename; no new dependency.
## Out of scope → follow-ups
- `vocabulary-list.tsx` (key-based vocabularies) and the `objects` table/detail/RHF surface.
- A field-definition "position"/ordering concept; server-side filtering for large vocabularies (#43).
@@ -0,0 +1,111 @@
# Bundle Vendor-Split + Test-Gap Fills — Design
**Date:** 2026-06-09
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #67 (vendor-split the bundle for cache stability/headroom; fill the audit's unit-test + story gaps).
## Context
The production build emits a single ~216 KB-gz entry chunk (the largest, measured by
`scripts/check-bundle-size.mjs`, budget 250 KB) that bundles the framework deps (react-dom, react-router 7,
@tanstack/react-query, @base-ui/react, i18next) together with app code — so every app edit invalidates the
whole chunk's cache. `vite.config.ts` has no `build.rollupOptions`. Separately, four well-isolated units
have no direct tests, and the composed combobox primitive has no story.
All changes are additive/low-risk; `check:size`/`check:colors`/the existing tests are the guards.
## Components
### 1. Vendor split — `vite.config.ts`
Add a top-level `build` block (sibling of `plugins`/`resolve`/`server`/`test`) with `manualChunks`:
```ts
build: {
rollupOptions: {
output: {
manualChunks: {
react: ["react", "react-dom", "react-router", "react-router-dom"],
"base-ui": ["@base-ui/react"],
query: ["@tanstack/react-query"],
i18n: ["i18next", "react-i18next"],
},
},
},
},
```
- The app entry chunk then carries only app code; the listed deps land in cache-stable vendor chunks
(`react-*.js`, `base-ui-*.js`, `query-*.js`, `i18n-*.js`). `react-router` **and** `react-router-dom` are
both listed so router code (v7 re-exports `react-router` internally) stays in one chunk and React isn't
duplicated.
- Only affects `pnpm build` (production); the Storybook/vitest browser builder is independent, so the
test suite is unaffected.
- `check:size` measures the largest emitted chunk; after the split the largest is a vendor chunk well under
the 250 KB budget (verified in implementation by running `pnpm build` + `pnpm check:size` and reporting
the chunk sizes). If a chunk is unexpectedly oversized or React duplicates, adjust the groups.
### 2. Test-gap fills (4 unit tests)
**`objects/prune-fields.test.ts`** (new) — `pruneFields(fields, localizedTextKeys: Set<string>, defaultLang)`:
- scalars pass through; `undefined`/`null`/`""` top-level values are dropped.
- a non-array object value whose key is in `localizedTextKeys` keeps **only** the `defaultLang` entry;
other-language entries are dropped.
- a non-array object value whose key is **not** in `localizedTextKeys` keeps all (non-empty) language
entries.
- empty inner entries (`""`/`null`/`undefined`) are filtered, and a map left with zero entries is dropped
entirely.
**`components/delete-confirm-dialog.test.tsx`** (new) — the delete-in-use flow:
- render `<DeleteConfirmDialog description=… onConfirm=… />`, open it (click the trigger), click the
confirm action.
- when `onConfirm` rejects with `InUseError(3)`, the dialog shows the `actions.inUse` message (containing
the count `3`) and **stays open** (the confirm action is still present / the dialog isn't dismissed).
- when `onConfirm` resolves, the dialog closes.
(`DeleteConfirmDialog` routes its catch through `errorMessageKey`, which maps `InUseError`
`actions.inUse` with the count — so this also exercises the shared error mapping.)
**`lib/labels.test.ts`** (new) — `labelText(labels, lang)`:
- exact `lang` match wins; else falls back to the `"en"` label; else the first label; else `""` for an
empty array.
**`lib/format-date.test.ts`** (new) — `formatDate(value, lang)`:
- a valid `"YYYY-MM-DD"` formats via `Intl.DateTimeFormat(lang, { dateStyle: "medium" })` and is **not**
shifted off its calendar day (assert the rendered string contains the expected day number for a fixed
date + a fixed `lang` like `"en"`).
- `null``"—"`; a non-date string (e.g. `"not-a-date"`) is returned unchanged; a non-string non-null
(e.g. a number) is `String()`-ified.
### 3. Combobox story — `components/ui/combobox.stories.tsx` (new)
A visual/interactive story for the composed combobox primitive, mirroring the existing `ui/*` story format
(`@storybook/react-vite` `Meta`/`StoryObj`, single-quote + no-semicolon, `tags: ['ai-generated']`, a `play`
using `canvas` + `storybook/test`). A small controlled wrapper renders `ComboboxRoot` with a handful of
string options (`ComboboxInput`/`ComboboxClear`/`ComboboxTrigger`/`ComboboxPopup`/`ComboboxList`/
`ComboboxItem`/`ComboboxEmpty`), matching how `OptionsCombobox` composes them. A `Default` story; the
`play` asserts the input renders and (optionally) typing filters the list. (Runs as a browser test in the
Storybook vitest project — a few seconds in CI.)
## Error handling / edges
- `manualChunks`: Rollup orders chunks by the import graph, so vendor chunks load before the app chunk that
imports them — no load-order/duplicate-React risk when react+react-dom+router share one named chunk.
- `formatDate` parses `\`${value}T00:00:00\`` (local midnight) to avoid a UTC day-shift; the test pins a
fixed `lang` to keep `Intl` output deterministic (assert a substring like the day number, not the full
locale string, to stay robust across ICU versions).
- `delete-confirm-dialog` test drives the Base UI AlertDialog in its portal (`within(document.body)` for the
confirm action), mirroring existing portal-aware tests.
## Testing
- The 4 unit tests run in the jsdom project; the story adds one Storybook browser test.
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; `check:size` reports the
new largest (vendor) chunk under budget; no new dependency; no new i18n keys; no codename. en/sv parity
unaffected.
## Acceptance criteria
1. `vite.config.ts` has `build.rollupOptions.output.manualChunks` splitting react/base-ui/query/i18n into
their own chunks; `pnpm build` succeeds and `check:size` passes (largest chunk reported, < 250 KB gz, no
React duplication).
2. New tests exist and pass: `prune-fields.test.ts`, `delete-confirm-dialog.test.tsx` (InUseError →
`actions.inUse`, stays open), `labels.test.ts`, `format-date.test.ts`.
3. `components/ui/combobox.stories.tsx` exists with a working `Default` story (browser-test green).
4. Full gate green; no new dependency; no new i18n keys; no codename; existing tests unchanged.
## Out of scope → follow-ups
- The buildkit/Dockerfile CI migration (the robust fix for the slow native runner; overlaps #25).
- Deeper treeshaking/bundle analysis; route-level code-split changes beyond the existing `lazy()` boundaries.
@@ -0,0 +1,137 @@
# Responsive Master/Detail for Vocabularies, Search, Fields — Design
**Date:** 2026-06-09
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #58 (master/detail + sidebar layout has no responsive/small-screen handling — remaining half).
## Context
#58 is partially done (commit `0a88a86`, #44): the sidebar collapses to an icon rail, the **Objects** master/detail
is responsive (wide right-pane / narrow Base UI `Drawer`), and a reusable `lib/use-media-query.ts` exists. The
**remaining** master/detail screens still use fixed `grid-cols-[20rem_1fr]` / `[24rem_1fr]` with no breakpoints —
`vocabularies-page.tsx`, `search-page.tsx`, `fields-page.tsx` — so on a small laptop / tablet / split window the
fixed list + sidebar leave a cramped detail pane, and below ~640px the panes can't coexist.
**Decision (brainstorming): keep the wide side-by-side layout (it's useful for curators); fix only narrow.** Reuse
one shared drawer + the existing `useMediaQuery` hook. Breakpoint **1024px (`lg`)**, matching Objects. Authorities
is single-pane → no change. The "resizable splitter" the issue *suggests considering* is out of scope.
The three pages differ: vocabularies + search are **route-driven** (`<Outlet/>`, an index-prompt route + `:id`);
fields is **`useState`-driven** (FieldList → FieldForm, with the form always present for "create").
## Components
### 1. `components/detail-drawer.tsx` (new) — generalize the Objects drawer
Today `objects/object-detail-drawer.tsx` is objects-specific (Base UI `Drawer` + close button + a hardcoded
`<Outlet/>`). Generalize to a reusable component taking `children` + `ariaLabel`:
```tsx
import type { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { X } from "lucide-react";
import { Drawer, DrawerClose, DrawerContent } from "@/components/ui/drawer";
import { Button } from "@/components/ui/button";
/** A right-sliding Base UI Drawer for a master/detail "detail" on narrow viewports. */
export function DetailDrawer({
open,
onClose,
ariaLabel,
children,
}: {
open: boolean;
onClose: () => void;
ariaLabel: string;
children: ReactNode;
}) {
const { t } = useTranslation();
return (
<Drawer open={open} onOpenChange={(next) => { if (!next) onClose(); }} swipeDirection="right">
<DrawerContent aria-label={ariaLabel}>
<div className="flex justify-end border-b p-2">
<DrawerClose aria-label={t("actions.closeDetail")} render={<Button variant="ghost" size="icon-sm" />}>
<X className="size-4" aria-hidden="true" />
</DrawerClose>
</div>
<div className="flex-1 overflow-auto">{children}</div>
</DrawerContent>
</Drawer>
);
}
```
- Delete `objects/object-detail-drawer.tsx`.
- **Drop the `lazy()`/`Suspense`** that objects-page used to wrap the drawer: it kept Base UI's drawer code out of
the entry chunk, but #67 already split `@base-ui/react` into its own `base-ui` vendor chunk (loaded app-wide via
the menu/select/etc.), so the lazy boundary no longer saves anything. `DetailDrawer` is a normal import.
- The close-button label keeps the existing generic `actions.closeDetail` key.
### 2. `vocab/vocabularies-page.tsx` + `search/search-page.tsx` — `useMediaQuery` + route drawer
Mirror the Objects pattern. Add `useMediaQuery("(min-width: 1024px)")` (`isWide`) and a `useMatch` for the detail
route (`"/vocabularies/:id"` / `"/search/:id"`), `open = Boolean(match)`, `close = () => navigate("/vocabularies")`
(resp. `"/search"`).
- **Wide:** the current side-by-side grid with `<Outlet/>` inline (prompt when no `:id`, detail when selected) —
unchanged.
- **Narrow:** the master full-width (`VocabularyList` / `SearchPanel` under the existing `PageTitle`), plus
`{open && <DetailDrawer open={open} onClose={close} ariaLabel={…}><Outlet/></DetailDrawer>}`. With no `:id`, just
the master (the index-prompt is a wide-only affordance).
- Drawer `ariaLabel`: vocabularies → `t("vocab.terms")` ("Terms"); search → `t("objects.detailTitle")` ("Object
detail", since a search result's detail IS an object). **No new i18n keys.**
- The `<Outlet/>` is rendered in exactly one place per branch (the `isWide` ternary), so no double-mount.
### 3. `fields/fields-page.tsx` — pure-CSS responsive stack
`fields-page` is `useState`-driven and its right pane is *always* a form (create when nothing selected), so a
drawer would need a new "New field" trigger. Instead make it a responsive **stack** (the issue comment explicitly
allows "or stack"): change the grid container from `grid grid-cols-[20rem_1fr]` to
`grid grid-cols-1 lg:grid-cols-[20rem_1fr]`, and give the form pane a top border that only shows when stacked
(`border-t lg:border-t-0`) while the list keeps its `border-r` (which reads as a bottom divider when stacked — or
add `border-b lg:border-b-0 lg:border-r` for a clean stacked divider). No JS, no drawer, no new trigger, no
element duplication — the same `FieldForm` reflows from below the list (narrow) to beside it (wide).
### 4. `objects/objects-page.tsx` — retrofit onto `DetailDrawer`
Replace the `lazy(ObjectDetailDrawer)` import + `<Suspense>` with a direct `DetailDrawer` import; in the narrow
branch render `{open && <DetailDrawer open={open} onClose={closeDetail} ariaLabel={t("objects.detailTitle")}><Outlet/></DetailDrawer>}`.
Behavior-preserving — its existing "narrow: detail renders inside a portaled drawer" test stays green.
## Data flow / behaviour
No data/routing changes. Each page picks layout via `useMediaQuery("(min-width:1024px)")` (vocab/search/objects) or
pure CSS (fields). The detail content is identical to today; only its container (inline pane vs Drawer) changes by
width. The detail route/state, breadcrumbs, and titles are unchanged.
## Error handling / edges
- `useMediaQuery` is SSR-safe (returns `false` server-side / pre-mount → narrow-first, then corrects on mount).
- Drawer `open` is derived from the route match (`:id`) / nothing on the index, so the Outlet only has content when
open; rendering `{open && <DetailDrawer …>}` mounts it only when active (matches the current objects behaviour).
- Fields stack: `FieldList` is `overflow-hidden` in a `grid-cols-1` row — ensure the stacked list has a sensible
height (it's in a flex/grid row that can scroll); the form below scrolls in its own pane. Keep each pane's
`overflow` as today.
- Drawer accessible name comes from `ariaLabel` (required prop) so every detail drawer is a named dialog (the #62
a11y fix, preserved + generalized).
## Testing
- **`components/detail-drawer.test.tsx`** (new): with `open`, the children render inside a dialog whose accessible
name is the `ariaLabel`; clicking the close button (labelled `actions.closeDetail`) calls `onClose`.
- **`vocab/vocabularies-page` + `search/search-page` tests** (new or extended): reuse the `setViewport(wide)`
matchMedia mock from `objects-page.test.tsx`. Narrow + a `:id` route → the detail renders in a portaled drawer
(`getByRole("dialog", { name })` within `document.body`); wide + `:id` → the detail is the inline pane (no
dialog). Closing the drawer navigates back to the index route.
- **`fields/fields-page` test**: the grid container carries the responsive classes (`grid-cols-1` +
`lg:grid-cols-[20rem_1fr]`); both the list and the form render (jsdom can't measure layout, so assert structure).
- **`objects/objects-page` tests**: stay green unchanged (the drawer is now the shared `DetailDrawer`).
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; no new dependency; no new i18n
keys; no codename; en/sv parity unaffected. `check:size` unchanged-or-smaller (dropping the objects drawer's
separate lazy chunk folds it into base-ui).
## Acceptance criteria
1. `components/detail-drawer.tsx` exists (Base UI drawer + close button + `children`/`ariaLabel`); `object-detail-drawer.tsx`
is deleted; Objects uses the shared `DetailDrawer` (no `lazy`/`Suspense`); its tests stay green.
2. Vocabularies + Search: wide = current side-by-side (unchanged); narrow (<1024) = master full-width + the detail
in a `DetailDrawer` when a `:id` is active; close returns to the index route.
3. Fields: responsive grid (`grid-cols-1 lg:grid-cols-[20rem_1fr]`) — stacked on narrow, side-by-side on wide.
4. New tests for `DetailDrawer`, vocabularies-page, search-page (narrow drawer + wide pane), fields-page (responsive
structure); all existing tests pass unchanged.
5. `typecheck`/`lint`/`build`/`check:colors` green; `check:size` reported (unchanged-or-smaller); no new
dependency; no new i18n keys; no codename.
## Out of scope → follow-ups
- A resizable master/detail splitter (issue "consider"); a per-user pane-width preference.
- Converting `fields` to a route-driven master/detail (it stays `useState`-driven + stacked).
@@ -0,0 +1,104 @@
# Instance-Timezone Timestamp Formatter — Design
**Date:** 2026-06-09
**Status:** Approved (brainstorming) — ready for implementation planning.
**Issue:** #42 (render UTC timestamps in the instance timezone via Intl — now that a display exists).
## Context
#42 was filed conditionally ("wire up the `default_timezone` formatter when the first timestamp display
lands"). That condition is now met: the objects-table **"Updated" column** (`updated_at`, a UTC timestamp)
is rendered — and it's already timezone+locale-aware, but via an **inline** `Intl.DateTimeFormat` in
`objects-table.tsx` (`dateStyle: "medium"`, `timeZone: default_timezone`) that:
- is **not** the shared `formatTimestamp` helper the issue asks for,
- shows **date only** (no time-of-day), and
- has **no invalid-IANA guard** — a misconfigured `default_timezone` would make `Intl.DateTimeFormat`
throw a `RangeError` and crash the table.
`recording_date` (object-detail) is a plain `DATE` formatted by `lib/format-date.ts` (no timezone) — correct
and out of scope. There are no other UTC-timestamp displays. `default_timezone` is exposed via
`useConfig().default_timezone` (IANA name; default `"Europe/Stockholm"`).
This is a display-only change: storage/transmission stay UTC. No backend change, no new dependency, no new
i18n keys.
## Components
### `lib/format-timestamp.ts` (new)
Mirrors `lib/format-date.ts`'s shape (same null/invalid-string edge handling), for UTC **timestamps**:
```ts
/** Formats a UTC ISO timestamp for display in the instance timezone + active locale.
* Storage/transmission stay UTC — this is display-only. Falls back to UTC formatting on an
* invalid IANA zone (a misconfigured instance) rather than throwing. */
export function formatTimestamp(value: unknown, timeZone: string, locale: string): string {
if (typeof value !== "string") return value == null ? "—" : String(value);
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
const opts = { dateStyle: "medium", timeStyle: "short" } as const;
try {
return new Intl.DateTimeFormat(locale, { ...opts, timeZone }).format(date);
} catch {
return new Intl.DateTimeFormat(locale, { ...opts, timeZone: "UTC" }).format(date);
}
}
```
- **date + short time** (the chosen display) in `timeZone` + `locale`.
- **Invalid-IANA guard:** `new Intl.DateTimeFormat(locale, { timeZone })` throws `RangeError` for a bad
zone → the `catch` re-formats with `timeZone: "UTC"` (no crash).
- Edge handling matches `format-date.ts`: non-string `null``"—"`; other non-strings → `String(value)`;
an unparseable string → returned unchanged.
### `objects/objects-table.tsx` (modify)
Remove the inline `const dateFmt = new Intl.DateTimeFormat(...)` + `formatUpdated` helper. Add
`import { formatTimestamp } from "../lib/format-timestamp";`. Keep `default_timezone` (from `useConfig()`)
and `i18n.language`. Render the Updated cell as:
```tsx
<td className="px-3 py-2 text-muted-foreground">
{formatTimestamp(object.updated_at, default_timezone, i18n.language)}
</td>
```
The column changes from date-only to **date + short time**. (The helper constructs an `Intl.DateTimeFormat`
per cell rather than once-per-render; negligible for the ≤200-row page — kept simple over re-memoizing.)
## Data flow / behaviour
`updated_at` (UTC ISO from the API) → `formatTimestamp(value, default_timezone, i18n.language)` → a
locale-formatted date+time in the instance zone. Identical data; only the display string changes (now
includes the time and is crash-guarded).
## Error handling / edges
- Invalid `default_timezone` → UTC-formatted output (guarded), never a thrown render.
- `null`/non-string `updated_at``"—"`/`String(value)` (defensive; in practice `updated_at` is always a
string).
- Unparseable date string → returned verbatim (matches `format-date.ts`).
- Locale comes from `i18n.language` (full-ICU Node in CI / browsers) — deterministic per locale.
## Testing
- **`lib/format-timestamp.test.ts`** (new):
- valid: `formatTimestamp("2026-06-08T12:30:00Z", "UTC", "en")` contains `"2026"` and `"12:30"` (date +
time rendered).
- **timezone applied (day-shift):** `formatTimestamp("2026-06-08T02:00:00Z", "America/New_York", "en")`
shows `Jun 7` (02:00 UTC = 22:00 prev-day EDT), distinct from the same instant in `"UTC"` (`Jun 8`) —
proves the zone is honored.
- **invalid IANA:** `formatTimestamp("2026-06-08T12:30:00Z", "Not/AZone", "en")` does **not** throw and
returns a non-empty string containing `"2026"` (UTC fallback).
- `null``"—"`; `"not-a-date"``"not-a-date"`.
- **`objects-table.test.tsx`:** the suite does not assert the rendered Updated value, so it stays green;
if any assertion is added/affected, assert the new date+time output loosely (don't pin the exact locale
string).
- **Gate:** `typecheck`/`lint`/`test`/`build`/`check:size`/`check:colors` green; no new dependency; no new
i18n keys; no codename; en/sv parity unaffected.
## Acceptance criteria
1. `lib/format-timestamp.ts` exports `formatTimestamp(value, timeZone, locale)` — date+time in the given
zone/locale, with a UTC fallback on an invalid IANA zone and the null/invalid edge handling; unit-tested
(incl. the day-shift + invalid-zone cases).
2. `objects-table.tsx` renders `updated_at` via `formatTimestamp(object.updated_at, default_timezone,
i18n.language)`; the inline `dateFmt`/`formatUpdated` are removed; the column shows date + short time.
3. All existing tests pass (objects-table green); `typecheck`/`lint`/`build`/`check:colors` green;
`check:size` reported; no new dependency; no new i18n keys; no codename.
## Out of scope → follow-ups
- Additional timestamp displays (object-detail `created_at`/`updated_at`, an audit-history view) — none
exist yet; route them through `formatTimestamp` when they land.
- Server-side timestamp formatting for the PDF export (#39) — needs a Rust tz library, separate.
- `recording_date` / `format-date.ts` (plain DATE, no timezone) — unchanged.
+146
View File
@@ -0,0 +1,146 @@
# Frontend Guardrails & Test Harness
The web frontend has a handful of CI guardrails and test-harness quirks that trip
new work in the same predictable ways. This file consolidates them so the lessons
are enforced up front instead of rediscovered one failing run at a time.
The canonical gate is `.gitea/workflows/ci.yaml`, which runs (in order) from `web/`:
```
pnpm typecheck # tsc -b --noEmit
pnpm lint # eslint .
pnpm test # vitest run (jsdom + storybook projects)
pnpm build # tsc -b && vite build
pnpm check:size # scripts/check-bundle-size.mjs
pnpm check:colors # scripts/check-no-raw-colors.mjs
```
Run the whole gate locally before pushing. The two `check:*` scripts only have
meaningful output **after** `pnpm build` has produced `dist/`.
---
## Guardrail: bundle size (`check:size`)
`scripts/check-bundle-size.mjs` fails if the **largest single JS chunk** exceeds
**250 KB gzipped**. It measures every file in `dist/assets/*.js` and reports the
biggest.
- The budget is on the *largest chunk*, not the total. Code-split chunks are
measured independently — a heavy primitive that lands in its own lazy chunk
costs that chunk's size, not the entry bundle's. Past additions (Base UI
Select ≈12.6 KB gz, the Drawer ≈12.7 KB gz) paid their cost in a separate
`*.js` chunk and left the main `index` chunk untouched.
- **When a UI primitive pushes the main chunk over budget**, extract the
component that uses it into its own module and `React.lazy()` it (the codebase
already lazy-loads `ObjectEditForm` / `FieldsPage`). This is the standard fix,
especially for anything rendered conditionally (e.g. only on a narrow
viewport) — the wide path then never pays for it.
- Adding one more Base UI primitive to the always-loaded app shell costs
single-digit KB gz; there is comfortable headroom under 250.
## Guardrail: design tokens / no raw colors (`check:colors`)
`scripts/check-no-raw-colors.mjs` fails if any raw Tailwind palette utility
appears **outside `src/components/ui/`**. The regex matches
`{text|bg|border|ring|fill|stroke|from|to|via|decoration|outline|divide|placeholder}-{palette}-{50..950}`
(palette = `neutral|gray|slate|…|rose`).
- In app / shell code use **token classes only**: `bg-accent`, `text-foreground`,
`text-muted-foreground`, `bg-primary`, etc. Numerics like `gap-2` and token
names are never flagged — only `palette-shade` pairs are.
- `src/components/ui/` (the vendored kit) is **exempt** and may use raw colors.
- New status/semantic colors must be exposed as tokens, not raw palette classes.
In Tailwind v4 a token utility (`bg-success`, `text-warning`) only *exists* if
the token is declared in `@theme inline` as `--color-<name>: var(--<name>)`
defining the raw CSS var in `:root` alone is not enough. Opacity modifiers on
tokens (`bg-primary/10`) do resolve.
- For a sweeping token migration, add/enable the `check:colors` guard **last**
(after the file-by-file swap) — it can only pass once the whole migration is
complete.
> **Contrast caveat:** do not trust a WCAG contrast ratio quoted in a plan or a
> prior review — recompute it (OKLCH → linear sRGB → relative luminance) before
> changing a color token. A near-black foreground on a *lighter* colored
> background has *higher* contrast; a single wrong measurement has propagated
> through spec + plan more than once here.
---
## Test harness: two vitest projects
`vite.config.ts` defines **two** vitest projects, and a test file is claimed by
exactly one:
| Project | Environment | Claims |
|-------------|----------------------|-------------------------|
| (default) | jsdom + `src/test/setup.ts` | `*.test.ts(x)` |
| `storybook` | chromium (Playwright, via `storybookTest`) | `*.stories.tsx` |
- Component tests render through **`renderApp`** (`src/test/render.tsx`), which
wraps `QueryClientProvider` + a `createMemoryRouter` and **side-imports
`../i18n`** — so i18n is auto-initialized and you do **not** add extra
providers. Pass an initial route via `renderApp(ui, { route: "/x" })`.
- Story-as-test runs the story's `play()` fn in a real browser:
`pnpm vitest run <file.stories.tsx>`. A canonical pass reports
"1 Test File, 1 Test passed". A first-cold-cache storybook run can emit a
transient `Cannot read properties of null (reading useEffect)` from
`QueryClientProvider` — it does not reproduce once the cache warms; re-run to
confirm green.
## Test harness: MSW handlers (`onUnhandledRequest: "error"`)
`src/test/setup.ts` starts the MSW server with **`onUnhandledRequest: "error"`**
and `resetHandlers()` after each test. Shared handlers live in
`src/test/handlers.ts`.
- Any request with **no matching handler fails the test** with
`[MSW] Cannot bypass a request when using the error strategy`. If a hook hits
an endpoint the shared handlers don't cover (e.g. `PATCH /…/terms/:id` for an
update-mutation success test), **add a per-test override**
`server.use(http.patch(url, () => new HttpResponse(null, { status: 204 })))`
rather than editing the shared `handlers.ts`. The `afterEach` reset keeps the
override test-local.
## Test harness: React Testing Library accessible-name collisions
`getByRole('textbox', { name: /key/i })` throws on **multiple matches** when two
inputs resolve to the same accessible name — e.g. a create-form input
(`<Label htmlFor>`) and a rename input (`aria-label`) both named "Key" once a
rename form is open.
- Use `getAllByRole(...)` and select by position (the rename form renders after
the create form in the DOM, so take the last), or give the inputs distinct
accessible names.
## Test harness: Storybook routers and portals
- `.storybook/preview.tsx` already wraps every story in
`QueryClientProvider` + `ConfigProvider` + `MemoryRouter`. **Do not add a
`MemoryRouter` decorator** inside a story — nesting throws
*"You cannot render a `<Router>` inside another `<Router>`"*. To exercise
URL-dependent state, drive it through `play()` interactions (click a header
link) rather than seeding `initialEntries` via a nested router.
- Portalled content (Base UI Popup/Portal, toasts) renders outside the canvas.
Assert it with `within(document.body).findByText(...)`, and open triggers from
the `play` context with `userEvent.click(canvas.getByRole('button', { name: 'Open' }))`.
---
## Code-style split (convention, not lint)
ESLint passes either way, so this is enforced by review, not tooling — **check a
neighbor file before writing**:
- `src/components/ui/` (vendored shadcn / base-nova kit): **no semicolons**,
single-quote base style.
- The rest of `src/` (app source, hooks, pages): **double-quote + semicolons**.
- `*.stories.tsx`: single-quote, no semicolons.
A component placed in `ui/` must match the no-semicolon kit style even though the
rest of the app uses semicolons.
> Note: `react-refresh/only-export-components` is configured as `warn`
> (`allowConstantExport: true`), so exporting a component **and** a helper from
> one `.tsx` (e.g. `button.tsx` exporting `buttonVariants`) is an accepted
> tradeoff when a plan needs the helper colocated — `pnpm lint` still exits 0.
+5
View File
@@ -3,6 +3,8 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light dark" />
<meta name="theme-color" content="#ffffff" />
<title>Collection</title>
<script>
try {
@@ -12,6 +14,9 @@
(t === "system" &&
window.matchMedia("(prefers-color-scheme: dark)").matches);
document.documentElement.classList.toggle("dark", dark);
// Keep in sync with THEME_COLORS in src/theme/theme.ts.
var meta = document.querySelector('meta[name="theme-color"]');
if (meta) meta.setAttribute("content", dark ? "#0a0a0a" : "#ffffff");
} catch (e) {}
</script>
</head>
+1 -1
View File
@@ -5,7 +5,7 @@ import { join, relative } from "node:path";
const root = "src";
const excludeDir = join("src", "components", "ui");
const RAW_COLOR =
/(?:text|bg|border|ring|fill|stroke|from|to|via|decoration|outline|divide|placeholder)-(?:neutral|gray|slate|zinc|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(?:50|100|200|300|400|500|600|700|800|900|950)\b/;
/(?:text|bg|border|ring|fill|stroke|from|to|via|decoration|outline|divide|placeholder)-(?:(?:neutral|gray|slate|zinc|stone|red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-(?:50|100|200|300|400|500|600|700|800|900|950)|white|black)\b/;
function walk(dir) {
const files = [];
+46
View File
@@ -0,0 +1,46 @@
import { afterEach, expect, test, vi } from "vitest";
import { redirectToLogin, setNavigate } from "./auth-redirect";
function stubLocation(pathname: string, search = "") {
const assign = vi.fn();
vi.stubGlobal("location", { pathname, search, assign });
return assign;
}
afterEach(() => {
setNavigate(null);
vi.unstubAllGlobals();
});
test("uses the registered navigate to soft-redirect with reason + from", () => {
const assign = stubLocation("/objects/abc", "?x=1");
const navigate = vi.fn();
setNavigate(navigate);
redirectToLogin();
expect(navigate).toHaveBeenCalledWith(
"/login?reason=expired&from=%2Fobjects%2Fabc%3Fx%3D1",
{ replace: true },
);
expect(assign).not.toHaveBeenCalled();
});
test("falls back to a hard navigation when no navigate is registered", () => {
const assign = stubLocation("/objects/abc");
redirectToLogin();
expect(assign).toHaveBeenCalledWith("/login?reason=expired&from=%2Fobjects%2Fabc");
});
test("does nothing when already on /login", () => {
stubLocation("/login");
const navigate = vi.fn();
setNavigate(navigate);
redirectToLogin();
expect(navigate).not.toHaveBeenCalled();
});
+20 -4
View File
@@ -1,7 +1,23 @@
/** Hard-navigate to login. Isolated so it can be spied/mocked in tests and swapped
* for a router navigation if needed. */
type NavigateFn = (to: string, opts?: { replace?: boolean }) => void;
let navigateFn: NavigateFn | null = null;
/** Register (or clear) the router's navigate fn. Called by NavigationBridge. */
export function setNavigate(fn: NavigateFn | null): void {
navigateFn = fn;
}
/** Soft-redirect to login on a 401, preserving SPA state and the return path.
* Falls back to a hard navigation when no router navigate is registered yet
* (e.g. a 401 during the very first load). No-op when already on /login. */
export function redirectToLogin(): void {
if (window.location.pathname !== "/login") {
window.location.assign("/login");
const { pathname, search } = window.location;
if (pathname === "/login") return;
const from = encodeURIComponent(pathname + search);
const target = `/login?reason=expired&from=${from}`;
if (navigateFn) {
navigateFn(target, { replace: true });
} else {
window.location.assign(target);
}
}
+26
View File
@@ -0,0 +1,26 @@
import { expect, test } from "vitest";
import { errorMessageKey } from "./error-message";
import { HttpError, InUseError } from "./queries";
test("maps HTTP statuses to specific keys", () => {
expect(errorMessageKey(new HttpError(403))).toEqual({ key: "errors.forbidden" });
expect(errorMessageKey(new HttpError(404))).toEqual({ key: "errors.notFound" });
expect(errorMessageKey(new HttpError(409))).toEqual({ key: "errors.conflict" });
expect(errorMessageKey(new HttpError(422))).toEqual({ key: "errors.validation" });
expect(errorMessageKey(new HttpError(500))).toEqual({ key: "errors.server" });
expect(errorMessageKey(new HttpError(502))).toEqual({ key: "errors.server" });
});
test("an unmapped status falls back to the generic key", () => {
expect(errorMessageKey(new HttpError(418))).toEqual({ key: "toast.error" });
});
test("InUseError carries the count", () => {
expect(errorMessageKey(new InUseError(3))).toEqual({ key: "actions.inUse", opts: { count: 3 } });
});
test("a bare Error or unknown maps to the generic key", () => {
expect(errorMessageKey(new Error("boom"))).toEqual({ key: "toast.error" });
expect(errorMessageKey(null)).toEqual({ key: "toast.error" });
});
+18
View File
@@ -0,0 +1,18 @@
import { HttpError, InUseError } from "./errors";
/** Maps a caught mutation error to an i18n key (+ interpolation opts). The single
* source of truth shared by the global toast fallback and every inline display. */
export function errorMessageKey(error: unknown): { key: string; opts?: Record<string, unknown> } {
if (error instanceof InUseError) return { key: "actions.inUse", opts: { count: error.count } };
if (error instanceof HttpError) return { key: statusKey(error.status) };
return { key: "toast.error" };
}
function statusKey(status: number): string {
if (status === 403) return "errors.forbidden";
if (status === 404) return "errors.notFound";
if (status === 409) return "errors.conflict";
if (status === 422) return "errors.validation";
if (status >= 500) return "errors.server";
return "toast.error";
}
+28
View File
@@ -0,0 +1,28 @@
export class HttpError extends Error {
constructor(public readonly status: number) {
super(`HTTP ${status}`);
this.name = "HttpError";
}
}
export class FieldRejection extends Error {
constructor(public readonly field: string, public readonly code: string) {
super(`field rejected: ${field}`);
this.name = "FieldRejection";
}
}
export class InUseError extends Error {
constructor(public readonly count: number) {
super(`in use: ${count}`);
this.name = "InUseError";
}
}
/** Error carrying the HTTP status so callers can branch 422-gate vs 409-illegal. */
export class VisibilityError extends Error {
constructor(public status: number) {
super(`visibility change failed (${status})`);
this.name = "VisibilityError";
}
}
+13 -21
View File
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, test } from "vitest";
import { renderHook, waitFor, within } from "@testing-library/react";
import { QueryClientProvider } from "@tanstack/react-query";
import { QueryClientProvider, useMutation } from "@tanstack/react-query";
import { http, HttpResponse } from "msw";
import type { ReactNode } from "react";
@@ -8,7 +8,7 @@ import i18n from "../i18n";
import { ToastRegion } from "../components/ui/toast";
import { server } from "../test/server";
import { makeQueryClient } from "./query-client";
import { useDeleteVocabulary, useUpdateTerm } from "./queries";
import { HttpError, useDeleteVocabulary, useUpdateTerm } from "./queries";
// The toast manager is a module-scope singleton shared across renders, so each
// test mounts a fresh region and tears it down afterwards to keep toasts from
@@ -59,30 +59,22 @@ describe("mutation feedback toasts", () => {
unmount();
});
test("a non-suppressed mutation failing shows the catch-all error toast", async () => {
server.use(
http.patch(
"/api/admin/vocabularies/:id/terms/:term_id",
() => new HttpResponse(null, { status: 500 }),
),
test("a non-suppressed mutation failing shows the status-mapped error toast", async () => {
const { result, unmount } = renderHook(
() =>
useMutation({
mutationFn: async () => {
throw new HttpError(500);
},
}),
{ wrapper: makeWrapper() },
);
const { result, unmount } = renderHook(() => useUpdateTerm(), {
wrapper: makeWrapper(),
});
await expect(
result.current.mutateAsync({
vocabularyId: "v1",
termId: "t1",
external_uri: null,
labels: [{ lang: "en", label: "Bronze" }],
}),
).rejects.toThrow();
await expect(result.current.mutateAsync()).rejects.toThrow();
await waitFor(() => {
expect(
within(document.body).getByText(i18n.t("toast.error")),
within(document.body).getByText(i18n.t("errors.server")),
).toBeInTheDocument();
});
-584
View File
@@ -1,584 +0,0 @@
import { keepPreviousData, useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "./client";
import type { components } from "./schema";
export class HttpError extends Error {
constructor(public readonly status: number) {
super(`HTTP ${status}`);
this.name = "HttpError";
}
}
export class FieldRejection extends Error {
constructor(public readonly field: string, public readonly code: string) {
super(`field rejected: ${field}`);
this.name = "FieldRejection";
}
}
export class InUseError extends Error {
constructor(public readonly count: number) {
super(`in use: ${count}`);
this.name = "InUseError";
}
}
type UserView = components["schemas"]["UserView"];
type LoginRequest = components["schemas"]["LoginRequest"];
export function useMe() {
return useQuery({
queryKey: ["me"],
queryFn: async (): Promise<UserView | null> => {
const { data, response } = await api.GET("/api/admin/me");
if (response.status === 401) return null;
if (!data) throw new Error("failed to load session");
return data;
},
retry: false,
});
}
export type ObjectListParams = {
limit: number;
offset: number;
sort?: string;
order?: "asc" | "desc";
visibility?: string;
q?: string;
};
export function useObjectsPage(params: ObjectListParams) {
return useQuery({
queryKey: ["objects", params],
placeholderData: keepPreviousData,
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/objects", {
params: {
query: {
limit: params.limit,
offset: params.offset,
sort: params.sort,
order: params.order,
visibility: params.visibility,
q: params.q,
},
},
});
if (error || !data) throw new Error("failed to load objects");
return data;
},
});
}
export function useObject(id: string) {
return useQuery({
queryKey: ["object", id],
queryFn: async () => {
const { data, response } = await api.GET("/api/admin/objects/{id}", {
params: { path: { id } },
});
if (response.status === 404) return null;
if (!data) throw new Error("failed to load object");
return data;
},
// A 404 resolves to null rather than erroring, so don't retry it.
retry: false,
});
}
export function useFieldDefinitions() {
return useQuery({
queryKey: ["field-definitions"],
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/field-definitions");
if (error || !data) throw new Error("failed to load field definitions");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useLogin() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: LoginRequest) => {
const { response } = await api.POST("/api/admin/login", { body });
if (response.status !== 204) {
throw new Error(response.status === 401 ? "invalid" : "network");
}
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["me"] }),
meta: { suppressErrorToast: true },
});
}
export function useLogout() {
const qc = useQueryClient();
return useMutation({
mutationFn: async () => {
await api.POST("/api/admin/logout");
},
onSuccess: () => qc.setQueryData(["me"], null),
});
}
type ObjectCreateRequest = components["schemas"]["ObjectCreateRequest"];
type ObjectUpdateRequest = components["schemas"]["ObjectUpdateRequest"];
export function useTerms(vocabularyId: string | null | undefined) {
return useQuery({
queryKey: ["terms", vocabularyId],
enabled: !!vocabularyId,
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/vocabularies/{id}/terms", {
params: { path: { id: vocabularyId! } },
});
if (error || !data) throw new Error("failed to load terms");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useAuthorities(kind: string | null | undefined) {
return useQuery({
queryKey: ["authorities", kind],
enabled: !!kind,
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/authorities", {
params: { query: { kind: kind! } },
});
if (error || !data) throw new Error("failed to load authorities");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useCreateObject() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: ObjectCreateRequest) => {
const { data, error } = await api.POST("/api/admin/objects", { body });
if (error || !data) throw new Error("create failed");
return data;
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["objects"] }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useUpdateObject() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, body }: { id: string; body: ObjectUpdateRequest }) => {
const { response } = await api.PUT("/api/admin/objects/{id}", {
params: { path: { id } },
body,
});
if (response.status !== 204) throw new Error("update failed");
},
onSuccess: (_d, { id }) => {
void qc.invalidateQueries({ queryKey: ["objects"] });
void qc.invalidateQueries({ queryKey: ["object", id] });
},
meta: { successMessage: "toast.saved", suppressErrorToast: true },
});
}
export function useSetFields() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, fields }: { id: string; fields: Record<string, unknown> }) => {
const { response, error } = await api.PUT("/api/admin/objects/{id}/fields", {
params: { path: { id } },
body: fields as Record<string, never>,
});
if (response.status === 204) return;
if (response.status === 422 && error && typeof error === "object" && "field" in error) {
const detail = error as { field: string; code: string };
throw new FieldRejection(detail.field, detail.code);
}
throw new Error("set fields failed");
},
onSuccess: (_d, { id }) => {
void qc.invalidateQueries({ queryKey: ["object", id] });
},
meta: { suppressErrorToast: true },
});
}
export function useDeleteObject() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (id: string) => {
const { response } = await api.DELETE("/api/admin/objects/{id}", {
params: { path: { id } },
});
if (response.status !== 204) throw new Error("delete failed");
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["objects"] }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
type NewVocabularyRequest = components["schemas"]["NewVocabularyRequest"];
type LabelInput = components["schemas"]["LabelInput"];
export function useVocabularies() {
return useQuery({
queryKey: ["vocabularies"],
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/vocabularies");
if (error || !data) throw new Error("failed to load vocabularies");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useCreateVocabulary() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: NewVocabularyRequest) => {
const { data, error } = await api.POST("/api/admin/vocabularies", { body });
if (error || !data) throw new Error("create vocabulary failed");
return data;
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["vocabularies"] }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useAddTerm() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
vocabularyId,
external_uri,
labels,
}: {
vocabularyId: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.POST("/api/admin/vocabularies/{id}/terms", {
params: { path: { id: vocabularyId } },
body: { external_uri, labels },
});
if (response.status !== 201) throw new Error("add term failed");
},
onSuccess: (_result, { vocabularyId }) =>
qc.invalidateQueries({ queryKey: ["terms", vocabularyId] }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useCreateAuthority() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
kind,
external_uri,
labels,
}: {
kind: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.POST("/api/admin/authorities", {
body: { kind, external_uri, labels },
});
if (response.status !== 201) throw new Error("create authority failed");
},
onSuccess: (_result, { kind }) =>
qc.invalidateQueries({ queryKey: ["authorities", kind] }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
const SEARCH_PAGE = 20;
export function useSearch(q: string, visibility: string | null) {
const term = q.trim();
return useInfiniteQuery({
queryKey: ["search", term, visibility],
enabled: term.length > 0,
initialPageParam: 0,
queryFn: async ({ pageParam }) => {
const { data, error, response } = await api.GET("/api/admin/search", {
params: {
query: {
q: term,
...(visibility ? { visibility } : {}),
offset: pageParam,
limit: SEARCH_PAGE,
},
},
});
if (error || !data) throw new HttpError(response.status);
return data;
},
placeholderData: keepPreviousData,
getNextPageParam: (lastPage, allPages) => {
const loaded = allPages.reduce((n, page) => n + page.hits.length, 0);
return loaded < lastPage.estimated_total ? loaded : undefined;
},
});
}
type NewFieldDefinitionRequest = components["schemas"]["NewFieldDefinitionRequest"];
export function useCreateFieldDefinition() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: NewFieldDefinitionRequest) => {
const { data, response } = await api.POST("/api/admin/field-definitions", { body });
if (response.status !== 201 || !data) throw new Error("failed to create field definition");
return data;
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["field-definitions"] }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
type Visibility = "draft" | "internal" | "public";
/** Error carrying the HTTP status so callers can branch 422-gate vs 409-illegal. */
export class VisibilityError extends Error {
constructor(public status: number) {
super(`visibility change failed (${status})`);
this.name = "VisibilityError";
}
}
export function useSetVisibility() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, visibility }: { id: string; visibility: Visibility }) => {
const { response } = await api.POST("/api/admin/objects/{id}/visibility", {
params: { path: { id } },
body: { visibility },
});
if (response.status !== 204) throw new VisibilityError(response.status);
},
onSuccess: (_result, { id }) => {
void qc.invalidateQueries({ queryKey: ["object", id] });
void qc.invalidateQueries({ queryKey: ["objects"] });
},
meta: { successMessage: "toast.published", suppressErrorToast: true },
});
}
export function useUpdateTerm() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
vocabularyId,
termId,
external_uri,
labels,
}: {
vocabularyId: string;
termId: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.PATCH("/api/admin/vocabularies/{id}/terms/{term_id}", {
params: { path: { id: vocabularyId, term_id: termId } },
body: { external_uri, labels },
});
if (response.status !== 204) throw new Error("update term failed");
},
onSuccess: (_d, { vocabularyId }) => qc.invalidateQueries({ queryKey: ["terms", vocabularyId] }),
meta: { successMessage: "toast.saved" },
});
}
export function useDeleteTerm() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ vocabularyId, termId }: { vocabularyId: string; termId: string }) => {
const { error, response } = await api.DELETE("/api/admin/vocabularies/{id}/terms/{term_id}", {
params: { path: { id: vocabularyId, term_id: termId } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new Error("delete term failed");
},
onSuccess: (_d, { vocabularyId }) => qc.invalidateQueries({ queryKey: ["terms", vocabularyId] }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
export function useRenameVocabulary() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, key }: { id: string; key: string }) => {
const { response } = await api.PATCH("/api/admin/vocabularies/{id}", {
params: { path: { id } },
body: { key },
});
if (response.status !== 204) throw new Error("rename failed");
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["vocabularies"] }),
meta: { successMessage: "toast.renamed", suppressErrorToast: true },
});
}
export function useDeleteVocabulary() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (id: string) => {
const { error, response } = await api.DELETE("/api/admin/vocabularies/{id}", {
params: { path: { id } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new Error("delete vocabulary failed");
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["vocabularies"] }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
export function useUpdateAuthority() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
id,
external_uri,
labels,
}: {
id: string;
kind: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.PATCH("/api/admin/authorities/{id}", {
params: { path: { id } },
body: { external_uri, labels },
});
if (response.status !== 204) throw new Error("update authority failed");
},
onSuccess: (_d, { kind }) => qc.invalidateQueries({ queryKey: ["authorities", kind] }),
meta: { successMessage: "toast.saved" },
});
}
export function useDeleteAuthority() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id }: { id: string; kind: string }) => {
const { error, response } = await api.DELETE("/api/admin/authorities/{id}", {
params: { path: { id } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new Error("delete authority failed");
},
onSuccess: (_d, { kind }) => qc.invalidateQueries({ queryKey: ["authorities", kind] }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
export function useUpdateFieldDefinition() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
key,
required,
group,
labels,
}: {
key: string;
required: boolean;
group: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.PATCH("/api/admin/field-definitions/{key}", {
params: { path: { key } },
body: { required, group, labels },
});
if (response.status !== 204) throw new Error("update field failed");
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["field-definitions"] }),
meta: { successMessage: "toast.saved", suppressErrorToast: true },
});
}
export function useDeleteFieldDefinition() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (key: string) => {
const { error, response } = await api.DELETE("/api/admin/field-definitions/{key}", {
params: { path: { key } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new Error("delete field failed");
},
onSuccess: () => qc.invalidateQueries({ queryKey: ["field-definitions"] }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
+51
View File
@@ -0,0 +1,51 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { keys } from "../query-keys";
type UserView = components["schemas"]["UserView"];
type LoginRequest = components["schemas"]["LoginRequest"];
export function useMe() {
return useQuery({
queryKey: keys.me(),
queryFn: async (): Promise<UserView | null> => {
const { data, response } = await api.GET("/api/admin/me");
if (response.status === 401) return null;
if (!data) throw new Error("failed to load session");
return data;
},
retry: false,
});
}
export function useLogin() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: LoginRequest) => {
const { response } = await api.POST("/api/admin/login", { body });
if (response.status !== 204) {
throw new Error(response.status === 401 ? "invalid" : "network");
}
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.me() }),
meta: { suppressErrorToast: true },
});
}
export function useLogout() {
const qc = useQueryClient();
return useMutation({
mutationFn: async () => {
await api.POST("/api/admin/logout");
},
onSuccess: () => qc.setQueryData(keys.me(), null),
});
}
+93
View File
@@ -0,0 +1,93 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, InUseError } from "../errors";
import { keys } from "../query-keys";
type LabelInput = components["schemas"]["LabelInput"];
export function useAuthorities(kind: string | null | undefined) {
return useQuery({
queryKey: keys.authorities(kind),
enabled: !!kind,
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/authorities", {
params: { query: { kind: kind! } },
});
if (error || !data) throw new Error("failed to load authorities");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useCreateAuthority() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
kind,
external_uri,
labels,
}: {
kind: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.POST("/api/admin/authorities", {
body: { kind, external_uri, labels },
});
if (response.status !== 201) throw new HttpError(response.status);
},
onSuccess: (_result, { kind }) =>
qc.invalidateQueries({ queryKey: keys.authorities(kind) }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useUpdateAuthority() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
id,
external_uri,
labels,
}: {
id: string;
kind: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.PATCH("/api/admin/authorities/{id}", {
params: { path: { id } },
body: { external_uri, labels },
});
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: (_d, { kind }) => qc.invalidateQueries({ queryKey: keys.authorities(kind) }),
meta: { successMessage: "toast.saved", suppressErrorToast: true },
});
}
export function useDeleteAuthority() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id }: { id: string; kind: string }) => {
const { error, response } = await api.DELETE("/api/admin/authorities/{id}", {
params: { path: { id } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: (_d, { kind }) => qc.invalidateQueries({ queryKey: keys.authorities(kind) }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
+83
View File
@@ -0,0 +1,83 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, InUseError } from "../errors";
import { keys } from "../query-keys";
type NewFieldDefinitionRequest = components["schemas"]["NewFieldDefinitionRequest"];
type LabelInput = components["schemas"]["LabelInput"];
export function useFieldDefinitions() {
return useQuery({
queryKey: keys.fieldDefinitions(),
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/field-definitions");
if (error || !data) throw new Error("failed to load field definitions");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useCreateFieldDefinition() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: NewFieldDefinitionRequest) => {
const { data, response } = await api.POST("/api/admin/field-definitions", { body });
if (response.status !== 201 || !data) throw new HttpError(response.status);
return data;
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.fieldDefinitions() }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useUpdateFieldDefinition() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
key,
required,
group,
labels,
}: {
key: string;
required: boolean;
group: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.PATCH("/api/admin/field-definitions/{key}", {
params: { path: { key } },
body: { required, group, labels },
});
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.fieldDefinitions() }),
meta: { successMessage: "toast.saved", suppressErrorToast: true },
});
}
export function useDeleteFieldDefinition() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (key: string) => {
const { error, response } = await api.DELETE("/api/admin/field-definitions/{key}", {
params: { path: { key } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.fieldDefinitions() }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
+8
View File
@@ -0,0 +1,8 @@
export * from "./auth";
export * from "./objects";
export * from "./field-defs";
export * from "./vocab";
export * from "./authorities";
export * from "./search";
export * from "../errors";
export type { ObjectListParams } from "../query-keys";
+157
View File
@@ -0,0 +1,157 @@
import { keepPreviousData, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, FieldRejection, VisibilityError } from "../errors";
import { keys, type ObjectListParams } from "../query-keys";
type ObjectCreateRequest = components["schemas"]["ObjectCreateRequest"];
type ObjectUpdateRequest = components["schemas"]["ObjectUpdateRequest"];
type Visibility = "draft" | "internal" | "public";
export function useObjectsPage(params: ObjectListParams) {
return useQuery({
queryKey: keys.objectsPage(params),
placeholderData: keepPreviousData,
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/objects", {
params: {
query: {
limit: params.limit,
offset: params.offset,
sort: params.sort,
order: params.order,
visibility: params.visibility,
q: params.q,
},
},
});
if (error || !data) throw new Error("failed to load objects");
return data;
},
});
}
export function useObject(id: string) {
return useQuery({
queryKey: keys.object(id),
queryFn: async () => {
const { data, response } = await api.GET("/api/admin/objects/{id}", {
params: { path: { id } },
});
if (response.status === 404) return null;
if (!data) throw new Error("failed to load object");
return data;
},
// A 404 resolves to null rather than erroring, so don't retry it.
retry: false,
});
}
export function useCreateObject() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: ObjectCreateRequest) => {
const { data, error, response } = await api.POST("/api/admin/objects", { body });
if (error || !data) throw new HttpError(response.status);
return data;
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.objects() }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useUpdateObject() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, body }: { id: string; body: ObjectUpdateRequest }) => {
const { response } = await api.PUT("/api/admin/objects/{id}", {
params: { path: { id } },
body,
});
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: (_d, { id }) => {
void qc.invalidateQueries({ queryKey: keys.objects() });
void qc.invalidateQueries({ queryKey: keys.object(id) });
void qc.invalidateQueries({ queryKey: keys.search() });
},
meta: { successMessage: "toast.saved", suppressErrorToast: true },
});
}
export function useSetFields() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, fields }: { id: string; fields: Record<string, unknown> }) => {
const { response, error } = await api.PUT("/api/admin/objects/{id}/fields", {
params: { path: { id } },
body: fields as Record<string, never>,
});
if (response.status === 204) return;
if (response.status === 422 && error && typeof error === "object" && "field" in error) {
const detail = error as { field: string; code: string };
throw new FieldRejection(detail.field, detail.code);
}
throw new HttpError(response.status);
},
onSuccess: (_d, { id }) => {
void qc.invalidateQueries({ queryKey: keys.object(id) });
},
meta: { suppressErrorToast: true },
});
}
export function useDeleteObject() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (id: string) => {
const { response } = await api.DELETE("/api/admin/objects/{id}", {
params: { path: { id } },
});
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: () => {
void qc.invalidateQueries({ queryKey: keys.objects() });
void qc.invalidateQueries({ queryKey: keys.search() });
},
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
export function useSetVisibility() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, visibility }: { id: string; visibility: Visibility }) => {
const { response } = await api.POST("/api/admin/objects/{id}/visibility", {
params: { path: { id } },
body: { visibility },
});
if (response.status !== 204) throw new VisibilityError(response.status);
},
onSuccess: (_result, { id }) => {
void qc.invalidateQueries({ queryKey: keys.object(id) });
void qc.invalidateQueries({ queryKey: keys.objects() });
void qc.invalidateQueries({ queryKey: keys.search() });
},
meta: { successMessage: "toast.published", suppressErrorToast: true },
});
}
+39
View File
@@ -0,0 +1,39 @@
import { keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
import { api } from "../client";
import { HttpError } from "../errors";
import { keys } from "../query-keys";
const SEARCH_PAGE = 20;
export function useSearch(q: string, visibility: string | null) {
const term = q.trim();
return useInfiniteQuery({
queryKey: keys.searchResults(term, visibility),
enabled: term.length > 0,
initialPageParam: 0,
queryFn: async ({ pageParam }) => {
const { data, error, response } = await api.GET("/api/admin/search", {
params: {
query: {
q: term,
...(visibility ? { visibility } : {}),
offset: pageParam,
limit: SEARCH_PAGE,
},
},
});
if (error || !data) throw new HttpError(response.status);
return data;
},
placeholderData: keepPreviousData,
getNextPageParam: (lastPage, allPages) => {
const loaded = allPages.reduce((n, page) => n + page.hits.length, 0);
return loaded < lastPage.estimated_total ? loaded : undefined;
},
});
}
+160
View File
@@ -0,0 +1,160 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "../client";
import type { components } from "../schema";
import { HttpError, InUseError } from "../errors";
import { keys } from "../query-keys";
type NewVocabularyRequest = components["schemas"]["NewVocabularyRequest"];
type LabelInput = components["schemas"]["LabelInput"];
export function useVocabularies() {
return useQuery({
queryKey: keys.vocabularies(),
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/vocabularies");
if (error || !data) throw new Error("failed to load vocabularies");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useCreateVocabulary() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (body: NewVocabularyRequest) => {
const { data, error, response } = await api.POST("/api/admin/vocabularies", { body });
if (error || !data) throw new HttpError(response.status);
return data;
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.vocabularies() }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useRenameVocabulary() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ id, key }: { id: string; key: string }) => {
const { response } = await api.PATCH("/api/admin/vocabularies/{id}", {
params: { path: { id } },
body: { key },
});
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.vocabularies() }),
meta: { successMessage: "toast.renamed", suppressErrorToast: true },
});
}
export function useDeleteVocabulary() {
const qc = useQueryClient();
return useMutation({
mutationFn: async (id: string) => {
const { error, response } = await api.DELETE("/api/admin/vocabularies/{id}", {
params: { path: { id } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: () => qc.invalidateQueries({ queryKey: keys.vocabularies() }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
export function useTerms(vocabularyId: string | null | undefined) {
return useQuery({
queryKey: keys.terms(vocabularyId),
enabled: !!vocabularyId,
queryFn: async () => {
const { data, error } = await api.GET("/api/admin/vocabularies/{id}/terms", {
params: { path: { id: vocabularyId! } },
});
if (error || !data) throw new Error("failed to load terms");
return data;
},
staleTime: 5 * 60 * 1000,
});
}
export function useAddTerm() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
vocabularyId,
external_uri,
labels,
}: {
vocabularyId: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.POST("/api/admin/vocabularies/{id}/terms", {
params: { path: { id: vocabularyId } },
body: { external_uri, labels },
});
if (response.status !== 201) throw new HttpError(response.status);
},
onSuccess: (_result, { vocabularyId }) =>
qc.invalidateQueries({ queryKey: keys.terms(vocabularyId) }),
meta: { successMessage: "toast.created", suppressErrorToast: true },
});
}
export function useUpdateTerm() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
vocabularyId,
termId,
external_uri,
labels,
}: {
vocabularyId: string;
termId: string;
external_uri: string | null;
labels: LabelInput[];
}) => {
const { response } = await api.PATCH("/api/admin/vocabularies/{id}/terms/{term_id}", {
params: { path: { id: vocabularyId, term_id: termId } },
body: { external_uri, labels },
});
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: (_d, { vocabularyId }) => qc.invalidateQueries({ queryKey: keys.terms(vocabularyId) }),
meta: { successMessage: "toast.saved", suppressErrorToast: true },
});
}
export function useDeleteTerm() {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({ vocabularyId, termId }: { vocabularyId: string; termId: string }) => {
const { error, response } = await api.DELETE("/api/admin/vocabularies/{id}/terms/{term_id}", {
params: { path: { id: vocabularyId, term_id: termId } },
});
if (response.status === 409) throw new InUseError((error as { count?: number })?.count ?? 0);
if (response.status !== 204) throw new HttpError(response.status);
},
onSuccess: (_d, { vocabularyId }) => qc.invalidateQueries({ queryKey: keys.terms(vocabularyId) }),
meta: { successMessage: "toast.deleted", suppressErrorToast: true },
});
}
+3 -8
View File
@@ -6,20 +6,15 @@ import {
import i18n from "../i18n";
import { toastManager } from "../toast/toast-manager";
import { HttpError, InUseError } from "./queries";
import { errorMessageKey } from "./error-message";
function mutationErrorMessage(
error: unknown,
meta: MutationMeta | undefined,
): string {
if (meta?.errorMessage) return i18n.t(meta.errorMessage);
if (error instanceof InUseError) {
return i18n.t("actions.inUse", { count: error.count });
}
if (error instanceof HttpError && error.status === 503) {
return i18n.t("search.unavailable");
}
return i18n.t("toast.error");
const { key, opts } = errorMessageKey(error);
return i18n.t(key, opts);
}
/** Builds the app's QueryClient, including the MutationCache that bridges every
+24
View File
@@ -0,0 +1,24 @@
import { expect, test } from "vitest";
import { keys } from "./query-keys";
test("the key factory produces the expected arrays", () => {
expect(keys.me()).toEqual(["me"]);
expect(keys.config()).toEqual(["config"]);
expect(keys.objects()).toEqual(["objects"]);
const p = { limit: 50, offset: 0 };
expect(keys.objectsPage(p)).toEqual(["objects", p]);
expect(keys.object("x")).toEqual(["object", "x"]);
expect(keys.fieldDefinitions()).toEqual(["field-definitions"]);
expect(keys.vocabularies()).toEqual(["vocabularies"]);
expect(keys.terms("v1")).toEqual(["terms", "v1"]);
expect(keys.authorities("person")).toEqual(["authorities", "person"]);
expect(keys.search()).toEqual(["search"]);
expect(keys.searchResults("q", null)).toEqual(["search", "q", null]);
});
test("objects() is a prefix of objectsPage() so invalidation matches", () => {
const prefix = keys.objects();
const full = keys.objectsPage({ limit: 50, offset: 0 });
expect(full.slice(0, prefix.length)).toEqual(prefix);
});
+24
View File
@@ -0,0 +1,24 @@
export type ObjectListParams = {
limit: number;
offset: number;
sort?: string;
order?: "asc" | "desc";
visibility?: string;
q?: string;
};
/** Central query-key factory the single source of truth for cache keys, so
* query/invalidate/setQueryData sites can't drift. */
export const keys = {
me: () => ["me"] as const,
config: () => ["config"] as const,
objects: () => ["objects"] as const,
objectsPage: (params: ObjectListParams) => ["objects", params] as const,
object: (id: string) => ["object", id] as const,
fieldDefinitions: () => ["field-definitions"] as const,
vocabularies: () => ["vocabularies"] as const,
terms: (vocabularyId: string | null | undefined) => ["terms", vocabularyId] as const,
authorities: (kind: string | null | undefined) => ["authorities", kind] as const,
search: () => ["search"] as const,
searchResults: (term: string, visibility: string | null) => ["search", term, visibility] as const,
};
+1
View File
@@ -603,6 +603,7 @@ export interface components {
id: string;
object_name: string;
object_number: string;
recording_date?: string | null;
snippet?: string | null;
visibility: components["schemas"]["Visibility"];
};
+28
View File
@@ -0,0 +1,28 @@
import { expect, test } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { http, HttpResponse } from "msw";
import type { ReactNode } from "react";
import { server } from "../test/server";
import { useSetVisibility } from "./queries";
import { keys } from "./query-keys";
test("changing an object's visibility invalidates the active search query", async () => {
server.use(
http.post("/api/admin/objects/:id/visibility", () => new HttpResponse(null, { status: 204 })),
);
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
qc.setQueryData(keys.searchResults("amphora", null), { pages: [], pageParams: [] });
const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={qc}>{children}</QueryClientProvider>
);
const { result } = renderHook(() => useSetVisibility(), { wrapper });
await result.current.mutateAsync({ id: "o1", visibility: "public" });
await waitFor(() =>
expect(qc.getQueryState(keys.searchResults("amphora", null))?.isInvalidated).toBe(true),
);
});
+14 -4
View File
@@ -1,6 +1,7 @@
import { lazy, Suspense } from "react";
import { createBrowserRouter, createRoutesFromElements, Navigate, Route, RouterProvider } from "react-router-dom";
import { createBrowserRouter, createRoutesFromElements, Navigate, Outlet, Route, RouterProvider } from "react-router-dom";
import { NavigationBridge } from "./shell/navigation-bridge";
import { RequireAuth } from "./auth/require-auth";
import { LoginPage } from "./auth/login-page";
import { AppShell } from "./shell/app-shell";
@@ -26,9 +27,18 @@ const FieldsPage = lazy(() =>
import("./fields/fields-page").then((m) => ({ default: m.FieldsPage })),
);
function RootLayout() {
return (
<>
<NavigationBridge />
<Outlet />
</>
);
}
const router = createBrowserRouter(
createRoutesFromElements(
<>
<Route element={<RootLayout />}>
<Route path="/login" element={<LoginPage />} />
<Route element={<RequireAuth />}>
<Route element={<AppShell />}>
@@ -62,7 +72,7 @@ const router = createBrowserRouter(
<Route path="/authorities" element={<Navigate to="/authorities/person" replace />} />
<Route path="/authorities/:kind" element={<AuthoritiesPage />} />
<Route
path="/fields"
path="/fields/:key?"
element={
<Suspense fallback={<ListSkeleton />}>
<FieldsPage />
@@ -73,7 +83,7 @@ const router = createBrowserRouter(
</Route>
</Route>
<Route path="*" element={<Navigate to="/objects" replace />} />
</>,
</Route>,
),
);
+57
View File
@@ -12,6 +12,7 @@ function tree() {
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route path="/objects" element={<div>objects landing</div>} />
<Route path="/objects/:id" element={<div>object detail</div>} />
</Routes>
);
}
@@ -34,3 +35,59 @@ test("invalid credentials show an inline error", async () => {
expect(screen.getByText(/invalid email or password/i)).toBeInTheDocument(),
);
});
test("shows the session-expired notice when reason=expired", async () => {
renderApp(tree(), { route: "/login?reason=expired" });
expect(await screen.findByText(/session expired/i)).toBeInTheDocument();
});
test("returns to the from path on success", async () => {
renderApp(tree(), { route: "/login?from=%2Fobjects%2F123" });
await userEvent.type(screen.getByLabelText(/email/i), "editor@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "pw-editor-123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
expect(await screen.findByText("object detail")).toBeInTheDocument();
});
test("rejects an off-site from and falls back to /objects", async () => {
renderApp(tree(), { route: "/login?from=%2F%2Fevil.com" });
await userEvent.type(screen.getByLabelText(/email/i), "editor@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "pw-editor-123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
expect(await screen.findByText("objects landing")).toBeInTheDocument();
});
test("shows Signing in… and disables the button while pending", async () => {
let release!: () => void;
const gate = new Promise<void>((r) => {
release = r;
});
server.use(
http.post("/api/admin/login", async () => {
await gate;
return new HttpResponse(null, { status: 204 });
}),
);
renderApp(tree(), { route: "/login" });
await userEvent.type(screen.getByLabelText(/email/i), "editor@example.com");
await userEvent.type(screen.getByLabelText(/password/i), "pw-editor-123");
await userEvent.click(screen.getByRole("button", { name: /sign in/i }));
const pending = await screen.findByRole("button", { name: /signing in/i });
expect(pending).toBeDisabled();
release();
expect(await screen.findByText("objects landing")).toBeInTheDocument();
});
test("disables submit until both fields are filled", async () => {
renderApp(tree(), { route: "/login" });
const button = screen.getByRole("button", { name: /sign in/i });
expect(button).toBeDisabled();
await userEvent.type(screen.getByLabelText(/email/i), "a@b.se");
expect(button).toBeDisabled();
await userEvent.type(screen.getByLabelText(/password/i), "pw");
expect(button).toBeEnabled();
});
+18 -5
View File
@@ -1,5 +1,5 @@
import { useEffect, useState, type FormEvent } from "react";
import { useNavigate } from "react-router-dom";
import { useNavigate, useSearchParams } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useLogin } from "../api/queries";
@@ -7,11 +7,21 @@ import { useConfig } from "../config/config-context";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { PageTitle } from "@/components/ui/page-title";
/** Accept only a single-leading-slash local path; reject protocol-relative
* ("//host") and absolute URLs to avoid an open redirect. */
function safeFrom(raw: string | null): string {
if (!raw) return "/objects";
return /^\/(?!\/)/.test(raw) ? raw : "/objects";
}
export function LoginPage() {
const { t } = useTranslation();
const { app_name } = useConfig();
const navigate = useNavigate();
const [params] = useSearchParams();
const sessionExpired = params.get("reason") === "expired";
const login = useLogin();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
@@ -24,7 +34,7 @@ export function LoginPage() {
event.preventDefault();
login.mutate(
{ email, password },
{ onSuccess: () => navigate("/objects", { replace: true }) },
{ onSuccess: () => navigate(safeFrom(params.get("from")), { replace: true }) },
);
};
@@ -37,7 +47,10 @@ export function LoginPage() {
return (
<div className="flex min-h-screen items-center justify-center p-4">
<form onSubmit={onSubmit} className="w-full max-w-sm space-y-4">
<h1 className="text-2xl font-semibold">{app_name}</h1>
<PageTitle>{app_name}</PageTitle>
{sessionExpired && (
<p className="text-sm text-muted-foreground">{t("auth.sessionExpired")}</p>
)}
<div className="space-y-2">
<Label htmlFor="email">{t("auth.email")}</Label>
<Input
@@ -63,8 +76,8 @@ export function LoginPage() {
{t(errorKey)}
</p>
)}
<Button type="submit" className="w-full" disabled={login.isPending}>
{t("auth.signIn")}
<Button type="submit" className="w-full" disabled={login.isPending || !email.trim() || !password}>
{login.isPending ? t("auth.signingIn") : t("auth.signIn")}
</Button>
</form>
</div>
+9 -4
View File
@@ -1,16 +1,21 @@
import { screen, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw";
import { expect, test } from "vitest";
import { Route, Routes } from "react-router-dom";
import { Route, Routes, useLocation } from "react-router-dom";
import { server } from "../test/server";
import { renderApp } from "../test/render";
import { RequireAuth } from "./require-auth";
function LoginStub() {
const location = useLocation();
return <div>login page {location.search}</div>;
}
function tree() {
return (
<Routes>
<Route path="/login" element={<div>login page</div>} />
<Route path="/login" element={<LoginStub />} />
<Route element={<RequireAuth />}>
<Route path="/objects" element={<div>secret objects</div>} />
</Route>
@@ -23,8 +28,8 @@ test("renders children when authenticated", async () => {
expect(await screen.findByText("secret objects")).toBeInTheDocument();
});
test("redirects to /login when unauthenticated", async () => {
test("redirects unauthenticated users to /login carrying the attempted path", async () => {
server.use(http.get("/api/admin/me", () => new HttpResponse(null, { status: 401 })));
renderApp(tree(), { route: "/objects" });
await waitFor(() => expect(screen.getByText("login page")).toBeInTheDocument());
await waitFor(() => expect(screen.getByText(/from=%2Fobjects/)).toBeInTheDocument());
});
+6 -2
View File
@@ -1,14 +1,18 @@
import { Navigate, Outlet } from "react-router-dom";
import { Navigate, Outlet, useLocation } from "react-router-dom";
import { useMe } from "../api/queries";
import { AppShellSkeleton } from "@/components/ui/skeletons";
export function RequireAuth() {
const { data: user, isLoading } = useMe();
const location = useLocation();
if (isLoading) return <AppShellSkeleton />;
if (!user) return <Navigate to="/login" replace />;
if (!user) {
const from = encodeURIComponent(location.pathname + location.search);
return <Navigate to={`/login?from=${from}`} replace />;
}
return <Outlet />;
}
+26 -72
View File
@@ -1,25 +1,22 @@
import { useState, type FormEvent } from "react";
import { NavLink, Navigate, useParams } from "react-router-dom";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { useAuthorities, useCreateAuthority } from "../api/queries";
import { LabelEditor } from "../components/label-editor";
import { Button } from "@/components/ui/button";
import { FilteredRecordList } from "../components/filtered-record-list";
import { LabelledRecordCreateForm } from "../components/labelled-record-create-form";
import { PageTitle } from "@/components/ui/page-title";
import { ListSkeleton } from "@/components/ui/skeletons";
import { AuthorityRow } from "./authority-row";
import { useLang } from "../lib/use-lang";
import { segmentClass } from "../lib/class-recipes";
import { useDocumentTitle } from "../lib/use-document-title";
import { useBreadcrumb } from "../shell/use-breadcrumb";
type LabelInput = components["schemas"]["LabelInput"];
const KINDS = ["person", "organisation", "place"] as const;
export function AuthoritiesPage() {
const { t, i18n } = useTranslation();
const { t } = useTranslation();
const { kind } = useParams();
const lang = i18n.language.startsWith("sv") ? "sv" : "en";
const lang = useLang();
const isValidKind = (KINDS as readonly string[]).includes(kind ?? "");
const currentKind = isValidKind ? (kind as string) : "person";
@@ -27,87 +24,44 @@ export function AuthoritiesPage() {
const { data: authorities, isLoading, isError } = useAuthorities(currentKind);
const create = useCreateAuthority();
const [labels, setLabels] = useState<LabelInput[]>([]);
const [error, setError] = useState(false);
useDocumentTitle(t("nav.authorities"));
useBreadcrumb([{ label: t("nav.authorities") }]);
if (!isValidKind) return <Navigate to="/authorities/person" replace />;
const onCreate = (event: FormEvent) => {
event.preventDefault();
if (!labels.some((l) => l.label)) {
setError(true);
return;
}
setError(false);
create.mutate(
{ kind: kind as string, external_uri: null, labels },
{ onSuccess: () => setLabels([]) },
);
};
return (
<div className="overflow-auto p-4">
<PageTitle className="mb-3">{t("nav.authorities")}</PageTitle>
<div role="tablist" className="mb-3 flex gap-2">
<nav aria-label={t("nav.authorities")} className="mb-3 flex gap-2">
{KINDS.map((k) => (
<NavLink
key={k}
to={`/authorities/${k}`}
role="tab"
aria-selected={k === currentKind}
className={({ isActive }) =>
`rounded-md px-3 py-1 text-sm ${isActive ? "bg-primary text-primary-foreground" : "border"}`
}
className={({ isActive }) => segmentClass(isActive, "px-3 py-1 text-sm")}
>
{t(`authorities.${k}`)}
</NavLink>
))}
</div>
</nav>
{isLoading ? (
<ListSkeleton className="mb-4" rows={5} />
) : (
<ul className="mb-4">
{isError && (
<li className="text-sm text-destructive">{t("authorities.loadError")}</li>
)}
{!isError && authorities?.length === 0 && (
<li className="text-sm text-muted-foreground">{t("authorities.empty")}</li>
)}
{authorities?.map((a) => (
<AuthorityRow key={a.id} authority={a} kind={currentKind} lang={lang} />
))}
</ul>
)}
<FilteredRecordList
records={authorities}
lang={lang}
isLoading={isLoading}
isError={isError}
loadErrorText={t("authorities.loadError")}
emptyText={t("authorities.empty")}
renderRow={(a) => <AuthorityRow authority={a} kind={currentKind} lang={lang} />}
/>
<form onSubmit={onCreate} className="space-y-2 border-t pt-3">
<div className="text-sm font-medium">
{t("authorities.new")} · {t(`authorities.${currentKind}`)}
</div>
<LabelEditor value={labels} onChange={setLabels} />
{error && (
<p role="alert" className="text-xs text-destructive">
{t("form.required")}
</p>
)}
{create.isError && (
<p role="alert" className="text-xs text-destructive">
{t("form.rejected")}
</p>
)}
<Button type="submit" size="sm" disabled={create.isPending}>
{t("authorities.create")}
</Button>
</form>
<LabelledRecordCreateForm
heading={`${t("authorities.new")} · ${t(`authorities.${currentKind}`)}`}
submitLabel={t("authorities.create")}
pending={create.isPending}
error={create.error}
onCreate={(labels, uri, reset) =>
create.mutate({ kind: currentKind, external_uri: uri, labels }, { onSuccess: reset })}
/>
</div>
);
}
+70 -4
View File
@@ -33,13 +33,13 @@ test("lists authorities for the kind and creates one", async () => {
test("kind tabs link to the other kinds", async () => {
renderApp(tree(), { route: "/authorities/person" });
expect(await screen.findByRole("tab", { name: /place/i })).toHaveAttribute("href", "/authorities/place");
expect(await screen.findByRole("link", { name: /place/i })).toHaveAttribute("href", "/authorities/place");
});
test("aria-selected is on the tab element and reflects the active kind", async () => {
test("aria-current marks the active kind link", async () => {
renderApp(tree(), { route: "/authorities/person" });
expect(await screen.findByRole("tab", { name: /^person$/i })).toHaveAttribute("aria-selected", "true");
expect(screen.getByRole("tab", { name: /^place$/i })).toHaveAttribute("aria-selected", "false");
expect(await screen.findByRole("link", { name: /^person$/i })).toHaveAttribute("aria-current", "page");
expect(screen.getByRole("link", { name: /^place$/i })).not.toHaveAttribute("aria-current");
});
test("create without EN label shows required alert and does not POST", async () => {
@@ -69,3 +69,69 @@ test("unknown kind redirects to person list", async () => {
renderApp(tree(), { route: "/authorities/banana" });
expect(await screen.findByText("Ada Lovelace")).toBeInTheDocument();
});
test("authorities render sorted by label", async () => {
server.use(
http.get("/api/admin/authorities", () =>
HttpResponse.json([
{ id: "a-zoe", kind: "person", external_uri: null, labels: [{ lang: "en", label: "Zoe" }] },
{ id: "a-adam", kind: "person", external_uri: null, labels: [{ lang: "en", label: "Adam" }] },
]),
),
);
renderApp(tree(), { route: "/authorities/person" });
expect(await screen.findByText("Adam")).toBeInTheDocument();
const items = screen.getAllByRole("listitem");
const texts = items.map((item) => item.textContent ?? "");
const adam = texts.findIndex((text) => text.includes("Adam"));
const zoe = texts.findIndex((text) => text.includes("Zoe"));
expect(adam).toBeLessThan(zoe);
});
test("filter narrows the authority list", async () => {
server.use(
http.get("/api/admin/authorities", () =>
HttpResponse.json([
{ id: "a-ada", kind: "person", external_uri: null, labels: [{ lang: "en", label: "Ada Lovelace" }] },
{ id: "a-grace", kind: "person", external_uri: null, labels: [{ lang: "en", label: "Grace Hopper" }] },
]),
),
);
renderApp(tree(), { route: "/authorities/person" });
expect(await screen.findByText("Ada Lovelace")).toBeInTheDocument();
expect(screen.getByText("Grace Hopper")).toBeInTheDocument();
await userEvent.type(screen.getByRole("textbox", { name: /filter/i }), "grace");
expect(screen.getByText("Grace Hopper")).toBeInTheDocument();
expect(screen.queryByText("Ada Lovelace")).not.toBeInTheDocument();
});
test("create posts the entered external_uri", async () => {
let body: unknown;
server.use(
http.post("/api/admin/authorities", async ({ request }) => {
body = await request.json();
return HttpResponse.json({ id: "a-c" }, { status: 201 });
}),
);
renderApp(tree(), { route: "/authorities/person" });
expect(await screen.findByText("Ada Lovelace")).toBeInTheDocument();
await userEvent.type(screen.getByLabelText(/^label$/i), "Carl von Linné");
await userEvent.type(screen.getByLabelText(/external uri/i), "https://viaf.org/456");
await userEvent.click(screen.getByRole("button", { name: /create/i }));
await waitFor(() =>
expect((body as { external_uri: string })?.external_uri).toBe("https://viaf.org/456"),
);
});
test("read row shows its external_uri as a link", async () => {
server.use(
http.get("/api/admin/authorities", () =>
HttpResponse.json([
{ id: "a-ada", kind: "person", external_uri: "https://viaf.org/123", labels: [{ lang: "en", label: "Ada Lovelace" }] },
]),
),
);
renderApp(tree(), { route: "/authorities/person" });
expect(await screen.findByText("Ada Lovelace")).toBeInTheDocument();
expect(screen.getByRole("link", { name: /viaf\.org/ })).toBeInTheDocument();
});
+13 -66
View File
@@ -1,77 +1,24 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { useUpdateAuthority, useDeleteAuthority } from "../api/queries";
import { LabelEditor } from "../components/label-editor";
import { DeleteConfirmDialog } from "../components/delete-confirm-dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { labelText } from "../lib/labels";
import { LabelledRecordRow } from "../components/labelled-record-row";
type AuthorityView = components["schemas"]["AuthorityView"];
type LabelInput = components["schemas"]["LabelInput"];
export function AuthorityRow({ authority, kind, lang }: { authority: AuthorityView; kind: string; lang: string }) {
const { t } = useTranslation();
const updateAuthority = useUpdateAuthority();
const deleteAuthority = useDeleteAuthority();
const [editing, setEditing] = useState(false);
const [labels, setLabels] = useState<LabelInput[]>(authority.labels as LabelInput[]);
const [uri, setUri] = useState(authority.external_uri ?? "");
if (editing) {
return (
<li className="space-y-2 border-b py-2">
<LabelEditor value={labels} onChange={setLabels} />
<div className="space-y-1">
<Label htmlFor={`auth-uri-${authority.id}`}>{t("labels.externalUri")}</Label>
<Input id={`auth-uri-${authority.id}`} value={uri} onChange={(e) => setUri(e.target.value)} />
</div>
<div className="flex gap-2">
<Button
type="button"
size="sm"
disabled={updateAuthority.isPending}
onClick={() =>
updateAuthority.mutate(
{ id: authority.id, kind, external_uri: uri.trim() || null, labels },
{ onSuccess: () => setEditing(false) },
)
}
>
{t("actions.save")}
</Button>
<Button type="button" variant="ghost" size="sm" onClick={() => setEditing(false)}>
{t("form.cancel")}
</Button>
</div>
</li>
);
}
const update = useUpdateAuthority();
const del = useDeleteAuthority();
return (
<li className="flex items-center gap-2 border-b py-1 text-sm">
<span className="flex-1">{labelText(authority.labels, lang)}</span>
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => {
setLabels(authority.labels as LabelInput[]);
setUri(authority.external_uri ?? "");
setEditing(true);
}}
>
{t("actions.edit")}
</Button>
<DeleteConfirmDialog
description={t("actions.confirmDeleteAuthority")}
onConfirm={() => deleteAuthority.mutateAsync({ id: authority.id, kind })}
<LabelledRecordRow
record={{ ...authority, external_uri: authority.external_uri ?? null }}
lang={lang}
deleteConfirmKey="actions.confirmDeleteAuthority"
savePending={update.isPending}
saveError={update.error}
onEditOpen={() => update.reset()}
onSave={(labels, uri, done) =>
update.mutate({ id: authority.id, kind, external_uri: uri, labels }, { onSuccess: done })}
onDelete={() => del.mutateAsync({ id: authority.id, kind })}
/>
</li>
);
}
@@ -0,0 +1,60 @@
import { expect, test, vi } from "vitest";
import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { DeleteConfirmDialog } from "./delete-confirm-dialog";
import { InUseError } from "../api/errors";
test("delete-in-use shows the in-use count and keeps the dialog open", async () => {
const onConfirm = vi.fn(() => Promise.reject(new InUseError(3)));
renderApp(<DeleteConfirmDialog description="Delete this term?" onConfirm={onConfirm} />);
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
const dialog = within(document.body);
const buttons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(buttons[buttons.length - 1]);
expect(await dialog.findByText(/used by 3/i)).toBeInTheDocument();
expect(dialog.getByText("Delete this term?")).toBeInTheDocument();
});
test("confirm is disabled and labelled Deleting… while pending", async () => {
let resolve!: () => void;
const onConfirm = vi.fn(
() =>
new Promise<void>((r) => {
resolve = r;
}),
);
renderApp(<DeleteConfirmDialog description="Delete this term?" onConfirm={onConfirm} />);
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
const dialog = within(document.body);
const buttons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(buttons[buttons.length - 1]);
const pending = await dialog.findByRole("button", { name: /deleting/i });
expect(pending).toBeDisabled();
expect(dialog.getByRole("button", { name: /cancel/i })).toBeDisabled();
expect(onConfirm).toHaveBeenCalledTimes(1);
resolve();
await waitFor(() => expect(dialog.queryByText("Delete this term?")).toBeNull());
});
test("a clean confirm closes the dialog", async () => {
const onConfirm = vi.fn(() => Promise.resolve());
renderApp(<DeleteConfirmDialog description="Delete this term?" onConfirm={onConfirm} />);
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
const dialog = within(document.body);
const buttons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(buttons[buttons.length - 1]);
await waitFor(() => expect(dialog.queryByText("Delete this term?")).toBeNull());
expect(onConfirm).toHaveBeenCalledTimes(1);
});
+11 -4
View File
@@ -1,7 +1,7 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { InUseError } from "../api/queries";
import { errorMessageKey } from "../api/error-message";
import {
AlertDialog,
AlertDialogTrigger,
@@ -28,17 +28,22 @@ export function DeleteConfirmDialog({
}) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [pending, setPending] = useState(false);
const [message, setMessage] = useState<string | null>(null);
const confirm = async () => {
setMessage(null);
setPending(true);
try {
await onConfirm();
} catch (err) {
// Keep the dialog open; show the blocking reason. Never let the rejected
// mutation escape as an unhandled rejection.
setMessage(err instanceof InUseError ? t("actions.inUse", { count: err.count }) : t("form.rejected"));
const { key, opts } = errorMessageKey(err);
setMessage(t(key, opts));
return;
} finally {
setPending(false);
}
setOpen(false);
};
@@ -61,8 +66,10 @@ export function DeleteConfirmDialog({
</p>
)}
<AlertDialogFooter>
<AlertDialogCancel>{t("form.cancel")}</AlertDialogCancel>
<AlertDialogAction onClick={confirm}>{t("actions.delete")}</AlertDialogAction>
<AlertDialogCancel disabled={pending}>{t("form.cancel")}</AlertDialogCancel>
<AlertDialogAction disabled={pending} onClick={confirm}>
{pending ? t("actions.deleting") : t("actions.delete")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
+20
View File
@@ -0,0 +1,20 @@
import { expect, test, vi } from "vitest";
import { within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { DetailDrawer } from "./detail-drawer";
test("renders children in a named drawer and closes via the close button", async () => {
const onClose = vi.fn();
renderApp(
<DetailDrawer open onClose={onClose} ariaLabel="Object detail">
<p>detail body</p>
</DetailDrawer>,
);
const body = within(document.body);
expect(await body.findByText("detail body")).toBeInTheDocument();
await userEvent.click(body.getByRole("button", { name: /close detail/i }));
expect(onClose).toHaveBeenCalled();
});
@@ -1,20 +1,22 @@
import { Outlet } from "react-router-dom";
import type { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { X } from "lucide-react";
import { Drawer, DrawerClose, DrawerContent } from "@/components/ui/drawer";
import { Button } from "@/components/ui/button";
/**
* Narrow-viewport object detail: the nested <Outlet/> inside a Base UI Drawer that
* slides from the right. Lazy-loaded so Base UI's drawer code (swipe/snap machinery)
* splits out of the main entry chunk the wide pane path never pays for it.
*/
export function ObjectDetailDrawer({
/** A right-sliding Base UI Drawer for a master/detail "detail" on narrow viewports.
* Provides the close affordance + an accessible dialog name; the caller supplies the content. */
export function DetailDrawer({
open,
onClose,
ariaLabel,
children,
}: {
open: boolean;
onClose: () => void;
ariaLabel: string;
children: ReactNode;
}) {
const { t } = useTranslation();
@@ -26,18 +28,16 @@ export function ObjectDetailDrawer({
}}
swipeDirection="right"
>
<DrawerContent>
<DrawerContent aria-label={ariaLabel}>
<div className="flex justify-end border-b p-2">
<DrawerClose
aria-label={t("actions.closeDetail")}
className="rounded-md p-1 text-muted-foreground hover:bg-muted hover:text-foreground"
render={<Button variant="ghost" size="icon-sm" />}
>
<X className="size-4" aria-hidden="true" />
</DrawerClose>
</div>
<div className="flex-1 overflow-auto">
<Outlet />
</div>
<div className="flex-1 overflow-auto">{children}</div>
</DrawerContent>
</Drawer>
);
+14
View File
@@ -0,0 +1,14 @@
import { focusRing } from "../lib/focus-ring";
export function ExternalUriLink({ uri }: { uri: string }) {
return (
<a
href={uri}
target="_blank"
rel="noopener noreferrer"
className={`block truncate rounded-sm text-xs text-muted-foreground hover:text-foreground ${focusRing}`}
>
{uri}
</a>
);
}
@@ -0,0 +1,51 @@
import { expect, test } from "vitest";
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { FilteredRecordList } from "./filtered-record-list";
import { labelText } from "../lib/labels";
type Rec = { id: string; labels: { lang: string; label: string }[] };
const recs: Rec[] = [
{ id: "a", labels: [{ lang: "en", label: "Alpha" }] },
{ id: "b", labels: [{ lang: "en", label: "Beta" }] },
];
const row = (r: Rec) => <li>{labelText(r.labels, "en")}</li>;
test("filtering narrows the rendered rows", async () => {
renderApp(
<FilteredRecordList records={recs} lang="en" isLoading={false} isError={false}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
expect(screen.getByText("Alpha")).toBeInTheDocument();
expect(screen.getByText("Beta")).toBeInTheDocument();
await userEvent.type(screen.getByLabelText(/filter/i), "alph");
expect(screen.getByText("Alpha")).toBeInTheDocument();
expect(screen.queryByText("Beta")).toBeNull();
});
test("empty records show the empty text", () => {
renderApp(
<FilteredRecordList records={[]} lang="en" isLoading={false} isError={false}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
expect(screen.getByText("EmptyMsg")).toBeInTheDocument();
});
test("non-empty records with a non-matching filter show no-matches", async () => {
renderApp(
<FilteredRecordList records={recs} lang="en" isLoading={false} isError={false}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
await userEvent.type(screen.getByLabelText(/filter/i), "zzz");
expect(screen.getByText(/no matches/i)).toBeInTheDocument();
});
test("an error shows the load-error text", () => {
renderApp(
<FilteredRecordList records={undefined} lang="en" isLoading={false} isError={true}
loadErrorText="LoadErr" emptyText="EmptyMsg" renderRow={row} />,
);
expect(screen.getByText("LoadErr")).toBeInTheDocument();
});
@@ -0,0 +1,68 @@
import { Fragment, useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { byLabel } from "../lib/sort";
import { labelText } from "../lib/labels";
import { Input } from "@/components/ui/input";
import { ListSkeleton } from "@/components/ui/skeletons";
type LabelView = components["schemas"]["LabelView"];
/** Filterable, alphabetically-sorted list of labelled records with the standard
* loading / error / empty / no-matches states. The filter input stays visible
* during load (matching the prior page behaviour). */
export function FilteredRecordList<T extends { id: string; labels: LabelView[] }>({
records,
lang,
isLoading,
isError,
loadErrorText,
emptyText,
renderRow,
}: {
records: T[] | undefined;
lang: string;
isLoading: boolean;
isError: boolean;
loadErrorText: string;
emptyText: string;
renderRow: (record: T) => ReactNode;
}) {
const { t } = useTranslation();
const [filter, setFilter] = useState("");
const q = filter.trim().toLowerCase();
const rows = [...(records ?? [])]
.filter((r) => !q || labelText(r.labels, lang).toLowerCase().includes(q))
.sort(byLabel(lang));
return (
<>
<div className="mb-3">
<Input
aria-label={t("common.filter")}
placeholder={t("common.filter")}
value={filter}
onChange={(e) => setFilter(e.target.value)}
/>
</div>
{isLoading ? (
<ListSkeleton className="mb-4" rows={5} />
) : (
<ul className="mb-4">
{isError && <li className="text-sm text-destructive">{loadErrorText}</li>}
{!isError && records?.length === 0 && (
<li className="text-sm text-muted-foreground">{emptyText}</li>
)}
{!isError && records && records.length > 0 && rows.length === 0 && (
<li className="text-sm text-muted-foreground">{t("common.noMatches")}</li>
)}
{rows.map((r) => (
<Fragment key={r.id}>{renderRow(r)}</Fragment>
))}
</ul>
)}
</>
);
}
+13
View File
@@ -85,3 +85,16 @@ test("no hint when only the default-language label exists", () => {
renderApp(<Harness initial={[{ lang: "sv", label: "Brons" }]} />);
expect(screen.queryByText(/other languages/i)).not.toBeInTheDocument();
});
test("each LabelEditor instance gets a unique input id", () => {
renderApp(
<>
<LabelEditor value={[]} onChange={() => {}} />
<LabelEditor value={[]} onChange={() => {}} />
</>,
);
const inputs = screen.getAllByLabelText(/label/i);
expect(inputs).toHaveLength(2);
expect(inputs[0].id).not.toBe("");
expect(inputs[0].id).not.toBe(inputs[1].id);
});
+4 -2
View File
@@ -1,3 +1,4 @@
import { useId } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
@@ -19,6 +20,7 @@ export function LabelEditor({
onChange: (labels: LabelInput[]) => void;
}) {
const { t } = useTranslation();
const inputId = useId();
const { default_language } = useConfig();
const current = value.find((l) => l.lang === default_language)?.label ?? "";
@@ -32,8 +34,8 @@ export function LabelEditor({
return (
<div className="space-y-1">
<Label htmlFor="label">{t("labels.label")}</Label>
<Input id="label" value={current} onChange={(e) => set(e.target.value)} />
<Label htmlFor={inputId}>{t("labels.label")}</Label>
<Input id={inputId} value={current} onChange={(e) => set(e.target.value)} />
{hasOtherLanguages && (
<p className="text-xs text-muted-foreground">{t("labels.otherLanguages")}</p>
)}
@@ -0,0 +1,28 @@
import { expect, test, vi } from "vitest";
import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { LabelledRecordCreateForm } from "./labelled-record-create-form";
test("submitting with empty labels shows the required error and does not call onCreate", async () => {
const onCreate = vi.fn();
renderApp(
<LabelledRecordCreateForm heading="New" submitLabel="Create" pending={false} error={null} onCreate={onCreate} />,
);
await userEvent.click(screen.getByRole("button", { name: /create/i }));
expect(screen.getByRole("alert")).toBeInTheDocument();
expect(onCreate).not.toHaveBeenCalled();
});
test("a valid submit calls onCreate and the reset clears the inputs", async () => {
const onCreate = vi.fn((_labels: unknown, _uri: unknown, reset: () => void) => reset());
renderApp(
<LabelledRecordCreateForm heading="New" submitLabel="Create" pending={false} error={null} onCreate={onCreate} />,
);
const labelInput = screen.getByLabelText(/^label$/i) as HTMLInputElement;
await userEvent.type(labelInput, "Bronze");
await userEvent.click(screen.getByRole("button", { name: /create/i }));
expect(onCreate).toHaveBeenCalled();
expect((screen.getByLabelText(/^label$/i) as HTMLInputElement).value).toBe("");
});
@@ -0,0 +1,76 @@
import { useId, useState, type FormEvent, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { LabelEditor } from "./label-editor";
import { MutationError } from "./mutation-error";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
type LabelInput = components["schemas"]["LabelInput"];
/** Create form for a labelled record (term/authority): single-language label +
* optional external URI, with required-label validation and a status-aware error.
* `onCreate` performs the mutation and is handed a `reset` to clear the inputs on success. */
export function LabelledRecordCreateForm({
heading,
submitLabel,
pending,
error,
onCreate,
}: {
heading: ReactNode;
submitLabel: string;
pending: boolean;
error: unknown;
onCreate: (labels: LabelInput[], uri: string | null, reset: () => void) => void;
}) {
const { t } = useTranslation();
const uriId = useId();
const [labels, setLabels] = useState<LabelInput[]>([]);
const [uri, setUri] = useState("");
const [requiredError, setRequiredError] = useState(false);
const onSubmit = (event: FormEvent) => {
event.preventDefault();
if (!labels.some((l) => l.label)) {
setRequiredError(true);
return;
}
setRequiredError(false);
onCreate(labels, uri.trim() || null, () => {
setLabels([]);
setUri("");
});
};
return (
<form onSubmit={onSubmit} className="space-y-2 border-t pt-3">
<div className="text-sm font-medium">{heading}</div>
<LabelEditor value={labels} onChange={setLabels} />
<div className="space-y-1">
<Label htmlFor={uriId}>{t("labels.externalUri")}</Label>
<Input
id={uriId}
type="url"
placeholder={t("labels.uriPlaceholder")}
value={uri}
onChange={(e) => setUri(e.target.value)}
/>
</div>
{requiredError && (
<p role="alert" className="text-xs text-destructive">
{t("form.required")}
</p>
)}
<MutationError error={error} />
<Button type="submit" size="sm" disabled={pending}>
{submitLabel}
</Button>
</form>
);
}
@@ -0,0 +1,74 @@
import { expect, test, vi } from "vitest";
import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderApp } from "../test/render";
import { LabelledRecordRow, type RecordLike } from "./labelled-record-row";
import { HttpError } from "../api/queries";
const record: RecordLike = { id: "r1", external_uri: null, labels: [{ lang: "en", label: "Bronze" }] };
test("edit → save calls onSave and closes via done()", async () => {
const onSave = vi.fn((_labels: unknown, _uri: unknown, done: () => void) => done());
renderApp(
<ul>
<LabelledRecordRow
record={record}
lang="en"
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={false}
saveError={null}
onEditOpen={() => {}}
onSave={onSave}
onDelete={async () => {}}
/>
</ul>,
);
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(onSave).toHaveBeenCalled();
expect(screen.queryByRole("button", { name: /save/i })).toBeNull();
});
test("a save error renders inline and the row stays editable", async () => {
renderApp(
<ul>
<LabelledRecordRow
record={record}
lang="en"
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={false}
saveError={new HttpError(403)}
onEditOpen={() => {}}
onSave={() => {}}
onDelete={async () => {}}
/>
</ul>,
);
await userEvent.click(screen.getByRole("button", { name: /edit/i }));
expect(screen.getByRole("alert")).toHaveTextContent(/permission/i);
expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();
});
test("confirming delete invokes onDelete", async () => {
const onDelete = vi.fn(async () => {});
renderApp(
<ul>
<LabelledRecordRow
record={record}
lang="en"
deleteConfirmKey="actions.confirmDeleteTerm"
savePending={false}
saveError={null}
onEditOpen={() => {}}
onSave={() => {}}
onDelete={onDelete}
/>
</ul>,
);
await userEvent.click(screen.getByRole("button", { name: /delete/i }));
const dialog = within(document.body);
const confirmButtons = await dialog.findAllByRole("button", { name: /delete/i });
await userEvent.click(confirmButtons[confirmButtons.length - 1]);
expect(onDelete).toHaveBeenCalled();
});
+102
View File
@@ -0,0 +1,102 @@
import { useId, useState } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { LabelEditor } from "./label-editor";
import { DeleteConfirmDialog } from "./delete-confirm-dialog";
import { MutationError } from "./mutation-error";
import { ExternalUriLink } from "./external-uri-link";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { labelText } from "../lib/labels";
type LabelView = components["schemas"]["LabelView"];
type LabelInput = components["schemas"]["LabelInput"];
export type RecordLike = { id: string; labels: LabelView[]; external_uri: string | null };
/** One labelled record (term/authority): a display row with edit + delete, or an
* inline editor. All variance (mutation hooks, arg shapes, delete-confirm key) is
* supplied by the caller via callbacks/state see term-row.tsx / authority-row.tsx. */
export function LabelledRecordRow({
record,
lang,
deleteConfirmKey,
savePending,
saveError,
onEditOpen,
onSave,
onDelete,
}: {
record: RecordLike;
lang: string;
deleteConfirmKey: string;
savePending: boolean;
saveError: unknown;
onEditOpen: () => void;
onSave: (labels: LabelInput[], uri: string | null, done: () => void) => void;
onDelete: () => Promise<void>;
}) {
const { t } = useTranslation();
const uriId = useId();
const [editing, setEditing] = useState(false);
const [labels, setLabels] = useState<LabelInput[]>(record.labels as LabelInput[]);
const [uri, setUri] = useState(record.external_uri ?? "");
if (editing) {
return (
<li className="space-y-2 border-b py-2">
<LabelEditor value={labels} onChange={setLabels} />
<div className="space-y-1">
<Label htmlFor={uriId}>{t("labels.externalUri")}</Label>
<Input
id={uriId}
type="url"
placeholder={t("labels.uriPlaceholder")}
value={uri}
onChange={(e) => setUri(e.target.value)}
/>
</div>
<div className="flex gap-2">
<Button
type="button"
size="sm"
disabled={savePending}
onClick={() => onSave(labels, uri.trim() || null, () => setEditing(false))}
>
{t("actions.save")}
</Button>
<Button type="button" variant="ghost" size="sm" onClick={() => setEditing(false)}>
{t("form.cancel")}
</Button>
</div>
<MutationError error={saveError} />
</li>
);
}
return (
<li className="flex items-center gap-2 border-b py-1 text-sm">
<div className="flex-1">
<div>{labelText(record.labels, lang)}</div>
{record.external_uri && <ExternalUriLink uri={record.external_uri} />}
</div>
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => {
onEditOpen();
setLabels(record.labels as LabelInput[]);
setUri(record.external_uri ?? "");
setEditing(true);
}}
>
{t("actions.edit")}
</Button>
<DeleteConfirmDialog description={t(deleteConfirmKey)} onConfirm={onDelete} />
</li>
);
}
@@ -0,0 +1,21 @@
import { expect, test } from "vitest";
import { render, screen } from "@testing-library/react";
import "../i18n";
import { MutationError } from "./mutation-error";
import { HttpError, InUseError } from "../api/queries";
test("renders the status-specific message for an HttpError", () => {
render(<MutationError error={new HttpError(403)} />);
expect(screen.getByRole("alert")).toHaveTextContent(/permission/i);
});
test("renders the in-use count for an InUseError", () => {
render(<MutationError error={new InUseError(2)} />);
expect(screen.getByRole("alert")).toHaveTextContent(/2/);
});
test("renders nothing when there is no error", () => {
const { container } = render(<MutationError error={null} />);
expect(container).toBeEmptyDOMElement();
});
+16
View File
@@ -0,0 +1,16 @@
import { useTranslation } from "react-i18next";
import { errorMessageKey } from "../api/error-message";
/** Renders a caught mutation error as an inline alert, or nothing when error is falsy.
* Replaces the duplicated `<p role="alert" className="text-xs text-destructive">` markup. */
export function MutationError({ error }: { error: unknown }) {
const { t } = useTranslation();
if (!error) return null;
const { key, opts } = errorMessageKey(error);
return (
<p role="alert" className="text-xs text-destructive">
{t(key, opts)}
</p>
);
}
+1 -1
View File
@@ -50,7 +50,7 @@ function AlertDialogContent({
data-slot="alert-dialog-content"
data-size={size}
className={cn(
"group/alert-dialog-content fixed top-1/2 left-1/2 z-50 grid w-full -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none data-[size=default]:max-w-xs data-[size=sm]:max-w-xs data-[size=default]:sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
"group/alert-dialog-content fixed top-1/2 left-1/2 z-50 grid w-full -translate-x-1/2 -translate-y-1/2 gap-4 overscroll-y-contain rounded-xl bg-popover p-4 text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none data-[size=default]:max-w-xs data-[size=sm]:max-w-xs data-[size=default]:sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
className
)}
{...props}
-103
View File
@@ -1,103 +0,0 @@
import * as React from "react"
import { cn } from "@/lib/utils"
function Card({
className,
size = "default",
...props
}: React.ComponentProps<"div"> & { size?: "default" | "sm" }) {
return (
<div
data-slot="card"
data-size={size}
className={cn(
"group/card flex flex-col gap-4 overflow-hidden rounded-xl bg-card py-4 text-sm text-card-foreground ring-1 ring-foreground/10 has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:gap-3 data-[size=sm]:py-3 data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
className
)}
{...props}
/>
)
}
function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-header"
className={cn(
"group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-4 group-data-[size=sm]/card:px-3 has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-4 group-data-[size=sm]/card:[.border-b]:pb-3",
className
)}
{...props}
/>
)
}
function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-title"
className={cn(
"text-base leading-snug font-medium group-data-[size=sm]/card:text-sm",
className
)}
{...props}
/>
)
}
function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-description"
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
)
}
function CardAction({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-action"
className={cn(
"col-start-2 row-span-2 row-start-1 self-start justify-self-end",
className
)}
{...props}
/>
)
}
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-content"
className={cn("px-4 group-data-[size=sm]/card:px-3", className)}
{...props}
/>
)
}
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-footer"
className={cn(
"flex items-center rounded-b-xl border-t bg-muted/50 p-4 group-data-[size=sm]/card:p-3",
className
)}
{...props}
/>
)
}
export {
Card,
CardHeader,
CardFooter,
CardTitle,
CardAction,
CardDescription,
CardContent,
}
@@ -0,0 +1,63 @@
import type { Meta, StoryObj } from '@storybook/react-vite'
import { useState } from 'react'
import { expect } from 'storybook/test'
import {
ComboboxRoot,
ComboboxInputGroup,
ComboboxInput,
ComboboxClear,
ComboboxTrigger,
ComboboxPopup,
ComboboxList,
ComboboxItem,
ComboboxItemIndicator,
ComboboxEmpty,
} from './combobox'
const fruits = ['Apple', 'Apricot', 'Banana', 'Cherry']
function ComboboxDemo() {
const [value, setValue] = useState<string | null>(null)
return (
<ComboboxRoot<string | null>
items={fruits}
value={value}
onValueChange={setValue}
itemToStringLabel={(item) => item ?? ''}
isItemEqualToValue={(a, b) => a === b}
>
<ComboboxInputGroup>
<ComboboxInput placeholder='Pick a fruit' />
<ComboboxClear aria-label='Clear' />
<ComboboxTrigger aria-label='Open' />
</ComboboxInputGroup>
<ComboboxPopup>
<ComboboxEmpty>No matches</ComboboxEmpty>
<ComboboxList>
{(item: string) => (
<ComboboxItem key={item} value={item}>
<ComboboxItemIndicator className='text-primary'></ComboboxItemIndicator>
{item}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxPopup>
</ComboboxRoot>
)
}
const meta = {
component: ComboboxDemo,
tags: ['ai-generated'],
} satisfies Meta<typeof ComboboxDemo>
export default meta
type Story = StoryObj<typeof meta>
export const Default: Story = {
play: async ({ canvas }) => {
await expect(canvas.getByPlaceholderText('Pick a fruit')).toBeInTheDocument()
},
}
+12 -6
View File
@@ -20,7 +20,10 @@ function ComboboxInput({ className, ...props }: ComboboxPrimitive.Input.Props) {
return (
<ComboboxPrimitive.Input
data-slot="combobox-input"
className={cn("w-full rounded border px-2 py-1 pr-12 text-sm", className)}
className={cn(
"w-full rounded border px-2 py-1 pr-12 text-sm transition-colors outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50",
className,
)}
{...props}
/>
);
@@ -31,7 +34,7 @@ function ComboboxClear({ className, ...props }: ComboboxPrimitive.Clear.Props) {
<ComboboxPrimitive.Clear
data-slot="combobox-clear"
className={cn(
"absolute right-6 text-neutral-400 hover:text-neutral-700",
"absolute right-6 rounded-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50",
className,
)}
{...props}
@@ -43,7 +46,10 @@ function ComboboxTrigger({ className, ...props }: ComboboxPrimitive.Trigger.Prop
return (
<ComboboxPrimitive.Trigger
data-slot="combobox-trigger"
className={cn("absolute right-1 text-neutral-500", className)}
className={cn(
"absolute right-1 rounded-sm text-muted-foreground outline-none focus-visible:ring-3 focus-visible:ring-ring/50",
className,
)}
{...props}
/>
);
@@ -56,7 +62,7 @@ function ComboboxPopup({ className, ...props }: ComboboxPrimitive.Popup.Props) {
<ComboboxPrimitive.Popup
data-slot="combobox-popup"
className={cn(
"max-h-64 min-w-48 overflow-auto rounded border bg-white p-1 text-sm shadow-md",
"max-h-64 min-w-48 overflow-auto overscroll-y-contain rounded border bg-popover p-1 text-sm text-popover-foreground shadow-md",
className,
)}
{...props}
@@ -81,7 +87,7 @@ function ComboboxItem({ className, ...props }: ComboboxPrimitive.Item.Props) {
<ComboboxPrimitive.Item
data-slot="combobox-item"
className={cn(
"flex cursor-default items-center gap-2 rounded px-2 py-1 data-[highlighted]:bg-indigo-50",
"flex cursor-default items-center gap-2 rounded px-2 py-1 data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
className,
)}
{...props}
@@ -103,7 +109,7 @@ function ComboboxEmpty({ className, ...props }: ComboboxPrimitive.Empty.Props) {
return (
<ComboboxPrimitive.Empty
data-slot="combobox-empty"
className={cn("px-2 py-1 text-neutral-500", className)}
className={cn("px-2 py-1 text-muted-foreground", className)}
{...props}
/>
);
+1 -1
View File
@@ -35,7 +35,7 @@ function DrawerContent({ className, children, ...props }: DrawerPrimitive.Popup.
<DrawerPrimitive.Popup
data-slot="drawer-content"
className={cn(
"fixed inset-y-0 right-0 flex w-full max-w-md flex-col overflow-y-auto bg-background shadow-xl outline-none duration-200 data-open:animate-in data-open:slide-in-from-right data-closed:animate-out data-closed:slide-out-to-right",
"fixed inset-y-0 right-0 flex w-full max-w-md flex-col overflow-y-auto overscroll-y-contain bg-background shadow-xl outline-none duration-200 data-open:animate-in data-open:slide-in-from-right data-closed:animate-out data-closed:slide-out-to-right",
className,
)}
{...props}
+1 -1
View File
@@ -6,7 +6,7 @@ export function PageTitle({ className, ...props }: ComponentProps<"h1">) {
return (
<h1
data-slot="page-title"
className={cn("text-2xl font-semibold tracking-tight", className)}
className={cn("text-2xl font-semibold tracking-tight text-balance", className)}
{...props}
/>
)
+1 -1
View File
@@ -99,7 +99,7 @@ function SelectContent({
<SelectPrimitive.Popup
data-slot="select-content"
className={cn(
"max-h-[min(24rem,var(--available-height))] min-w-[var(--anchor-width)] overflow-y-auto rounded-md border bg-popover p-1 text-popover-foreground shadow-md outline-none",
"max-h-[min(24rem,var(--available-height))] min-w-[var(--anchor-width)] overflow-y-auto overscroll-y-contain rounded-md border bg-popover p-1 text-popover-foreground shadow-md outline-none",
"data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
className
)}
+7 -6
View File
@@ -1,6 +1,7 @@
import type * as React from "react";
import { useTranslation } from "react-i18next";
import { Toast as ToastPrimitive } from "@base-ui/react/toast";
import { X } from "lucide-react";
import { cn } from "@/lib/utils";
import { toastManager } from "@/toast/toast-manager";
@@ -14,9 +15,9 @@ function ToastList() {
toast={toast}
data-slot="toast"
className={cn(
"flex items-start gap-2 rounded-md border bg-white p-3 text-sm shadow-md",
toast.type === "error" && "border-red-300",
toast.type === "success" && "border-green-300",
"flex items-start gap-2 rounded-md border bg-popover p-3 text-sm text-popover-foreground shadow-md",
toast.type === "error" && "border-destructive",
toast.type === "success" && "border-success",
)}
>
<div className="flex-1">
@@ -28,15 +29,15 @@ function ToastList() {
)}
<ToastPrimitive.Description
data-slot="toast-description"
className="text-neutral-700"
className="text-muted-foreground"
/>
</div>
<ToastPrimitive.Close
data-slot="toast-close"
aria-label={t("common.close")}
className="text-neutral-400 hover:text-neutral-700"
className="text-muted-foreground hover:text-foreground"
>
×
<X className="size-4" aria-hidden="true" />
</ToastPrimitive.Close>
</ToastPrimitive.Root>
));
+1 -1
View File
@@ -22,7 +22,7 @@ function TooltipPopup({ className, ...props }: TooltipPrimitive.Popup.Props) {
<TooltipPrimitive.Popup
data-slot="tooltip-popup"
className={cn(
"rounded border bg-white px-2 py-1 text-sm shadow-md",
"rounded border bg-popover px-2 py-1 text-sm text-popover-foreground shadow-md",
className,
)}
{...props}
+2 -1
View File
@@ -2,12 +2,13 @@ import { useEffect, type ReactNode } from "react";
import { useQuery } from "@tanstack/react-query";
import { api } from "../api/client";
import { keys } from "../api/query-keys";
import i18n, { LOCALE_KEY } from "../i18n";
import { ConfigContext, DEFAULTS, type ConfigView } from "./config-context";
export function ConfigProvider({ children }: { children: ReactNode }) {
const { data } = useQuery({
queryKey: ["config"],
queryKey: keys.config(),
queryFn: async (): Promise<ConfigView> => {
const { data, error } = await api.GET("/api/config");
+4 -6
View File
@@ -8,6 +8,7 @@ import {
useVocabularies,
} from "../api/queries";
import { LabelEditor } from "../components/label-editor";
import { MutationError } from "../components/mutation-error";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
@@ -95,7 +96,6 @@ export function FieldForm({
};
const pending = isEdit ? update.isPending : create.isPending;
const failed = isEdit ? update.isError : create.isError;
return (
<form onSubmit={onSubmit} className="space-y-3 overflow-auto p-4">
@@ -110,6 +110,8 @@ export function FieldForm({
)}
</div>
{isEdit && <p className="text-xs text-muted-foreground">{t("fields.lockedNote")}</p>}
<div className="space-y-1">
<Label htmlFor="field-key">{t("fields.key")}</Label>
<Input
@@ -198,11 +200,7 @@ export function FieldForm({
{t("form.required")}
</p>
)}
{failed && (
<p role="alert" className="text-xs text-destructive">
{t("form.rejected")}
</p>
)}
<MutationError error={isEdit ? update.error : create.error} />
<Button type="submit" size="sm" disabled={pending}>
{isEdit ? t("actions.save") : t("fields.create")}
+57 -18
View File
@@ -1,9 +1,17 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import type { components } from "../api/schema";
import { useFieldDefinitions, useDeleteFieldDefinition } from "../api/queries";
import { useLang } from "../lib/use-lang";
import { rowStateClass } from "../lib/class-recipes";
import { labelText } from "../lib/labels";
import { byLabel, compareStrings } from "../lib/sort";
import { focusRing } from "../lib/focus-ring";
import { DeleteConfirmDialog } from "../components/delete-confirm-dialog";
import { Badge } from "@/components/ui/badge";
import { cn } from "@/lib/utils";
import { Input } from "@/components/ui/input";
import { ListSkeleton } from "@/components/ui/skeletons";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
@@ -15,10 +23,11 @@ export function FieldList({
selectedKey: string | null;
onSelect: (def: FieldDefinitionView) => void;
}) {
const { t, i18n } = useTranslation();
const { t } = useTranslation();
const { data, isLoading, isError } = useFieldDefinitions();
const deleteField = useDeleteFieldDefinition();
const lang = i18n.language.startsWith("sv") ? "sv" : "en";
const lang = useLang();
const [filter, setFilter] = useState("");
if (isLoading) return <ListSkeleton rows={6} />;
@@ -26,9 +35,17 @@ export function FieldList({
if (!data || data.length === 0)
return <p className="p-4 text-sm text-muted-foreground">{t("fields.empty")}</p>;
const q = filter.trim().toLowerCase();
const filtered = (data ?? []).filter(
(d) =>
!q ||
labelText(d.labels, lang).toLowerCase().includes(q) ||
d.key.toLowerCase().includes(q),
);
const groups = new Map<string, FieldDefinitionView[]>();
for (const def of data) {
for (const def of filtered) {
const key = def.group?.trim() ? def.group : t("fields.other");
const bucket = groups.get(key) ?? [];
@@ -37,39 +54,59 @@ export function FieldList({
}
const otherLabel = t("fields.other");
const entries = [...groups.entries()].sort((a, b) =>
a[0] === otherLabel ? 1 : b[0] === otherLabel ? -1 : 0,
);
const entries = [...groups.entries()].sort((a, b) => {
if (a[0] === otherLabel) return 1;
if (b[0] === otherLabel) return -1;
return compareStrings(lang, a[0], b[0]);
});
return (
<div className="flex h-full flex-col">
<div className="border-b p-2">
<Input
aria-label={t("common.filter")}
placeholder={t("common.filter")}
value={filter}
onChange={(e) => setFilter(e.target.value)}
/>
</div>
{filtered.length === 0 ? (
<p className="p-3 text-sm text-muted-foreground">{t("common.noMatches")}</p>
) : (
<ul className="overflow-auto">
{entries.map(([group, defs]) => (
<li key={group}>
<div className="border-b bg-muted px-3 py-1 label-caption">
{group}
<div className="flex items-center justify-between border-b bg-muted px-3 py-1 label-caption">
<span>{group}</span>
<Badge variant="secondary">{defs.length}</Badge>
</div>
<ul>
{defs.map((def) => (
{[...defs].sort(byLabel(lang)).map((def) => (
<li
key={def.key}
className={`flex items-center gap-2 border-b px-3 py-2 text-sm ${
def.key === selectedKey ? "bg-primary/10" : ""
}`}
className={`flex items-center gap-2 border-b px-3 py-2 text-sm ${rowStateClass(
def.key === selectedKey,
)}`}
>
<button
type="button"
className="flex flex-1 items-center gap-2 text-left"
className={cn(
"flex min-w-0 flex-1 items-center gap-2 rounded-sm text-left",
focusRing,
)}
aria-pressed={def.key === selectedKey}
onClick={() => onSelect(def)}
>
<span className="font-medium">{labelText(def.labels, lang)}</span>
<span className="text-xs text-muted-foreground">{def.key}</span>
<span className="rounded-md bg-muted px-1.5 py-0.5 text-xs text-muted-foreground">
{t(`fields.types.${def.data_type}`)}
<span className="min-w-0 truncate font-medium">
{labelText(def.labels, lang)}
</span>
<span className="text-xs text-muted-foreground">{def.key}</span>
<Badge variant="secondary" className="shrink-0">
{t(`fields.types.${def.data_type}`)}
</Badge>
{def.required && (
<span
className="text-xs text-destructive"
className="shrink-0 text-xs text-destructive"
title={t("fields.required")}
aria-label={t("fields.required")}
>
@@ -87,5 +124,7 @@ export function FieldList({
</li>
))}
</ul>
)}
</div>
);
}
+17
View File
@@ -0,0 +1,17 @@
import { expect, test } from "vitest";
import { screen } from "@testing-library/react";
import { renderApp } from "../test/render";
import { FieldsPage } from "./fields-page";
test("renders the field list and form in a responsive grid", async () => {
const { container } = renderApp(<FieldsPage />);
// both panes are present (master list + detail form)
expect(await screen.findByText(/fields/i)).toBeInTheDocument();
// responsive: single-column by default, two-column at lg
const grid = container.querySelector("div.grid");
expect(grid?.className).toContain("grid-cols-1");
expect(grid?.className).toContain("lg:grid-cols-[20rem_1fr]");
});
+17 -9
View File
@@ -1,18 +1,23 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate, useParams } from "react-router-dom";
import type { components } from "../api/schema";
import { useFieldDefinitions } from "../api/queries";
import { FieldList } from "./field-list";
import { FieldForm } from "./field-form";
import { useDocumentTitle } from "../lib/use-document-title";
import { useBreadcrumb } from "../shell/use-breadcrumb";
import { PageTitle } from "@/components/ui/page-title";
type FieldDefinitionView = components["schemas"]["FieldDefinitionView"];
export function FieldsPage() {
const { t } = useTranslation();
const [selected, setSelected] = useState<FieldDefinitionView | null>(null);
const navigate = useNavigate();
const { key } = useParams();
const { data } = useFieldDefinitions();
// Selection lives in the URL (/fields/:key) so it survives reload and can be
// shared, matching /vocabularies/:id. An unknown or absent key falls back to
// the create form. Same cached query as FieldList, so no extra fetch.
const selected = (key && data?.find((def) => def.key === key)) || null;
useDocumentTitle(t("fields.title"));
useBreadcrumb([{ label: t("nav.fields") }]);
@@ -20,15 +25,18 @@ export function FieldsPage() {
return (
<div className="flex h-full flex-col">
<PageTitle className="px-4 pt-4 pb-2">{t("fields.title")}</PageTitle>
<div className="grid flex-1 grid-cols-[20rem_1fr] overflow-hidden">
<div className="overflow-hidden border-r">
<FieldList selectedKey={selected?.key ?? null} onSelect={setSelected} />
<div className="grid flex-1 grid-cols-1 overflow-auto lg:grid-cols-[20rem_1fr] lg:overflow-hidden">
<div className="overflow-hidden border-b lg:border-r lg:border-b-0">
<FieldList
selectedKey={selected?.key ?? null}
onSelect={(def) => navigate(`/fields/${encodeURIComponent(def.key)}`)}
/>
</div>
<div className="overflow-hidden">
<FieldForm
key={selected?.key ?? "create"}
editing={selected}
onDone={() => setSelected(null)}
onDone={() => navigate("/fields")}
/>
</div>
</div>
+94 -1
View File
@@ -11,7 +11,7 @@ import { FieldsPage } from "./fields-page";
function tree() {
return (
<Routes>
<Route path="/fields" element={<FieldsPage />} />
<Route path="/fields/:key?" element={<FieldsPage />} />
</Routes>
);
}
@@ -28,6 +28,99 @@ test("lists field definitions grouped, with an Other heading for ungrouped", asy
expect(screen.getByText(/^Other$/i)).toBeInTheDocument();
});
test("sorts fields within a group alphabetically by label", async () => {
server.use(
http.get("/api/admin/field-definitions", () =>
HttpResponse.json([
{
key: "weight",
data_type: "text",
vocabulary_id: null,
authority_kind: null,
required: false,
group: "Description",
labels: [{ lang: "en", label: "Weight" }],
},
{
key: "color",
data_type: "text",
vocabulary_id: null,
authority_kind: null,
required: false,
group: "Description",
labels: [{ lang: "en", label: "Color" }],
},
]),
),
);
renderApp(tree(), { route: "/fields" });
await screen.findByText("Color");
const labels = screen.getAllByText(/^(Color|Weight)$/).map((el) => el.textContent);
expect(labels).toEqual(["Color", "Weight"]);
});
test("shows a count badge in each group header", async () => {
renderApp(tree(), { route: "/fields" });
const otherHeading = await screen.findByText(/^Other$/i);
const header = otherHeading.closest("div") as HTMLElement;
// 6 ungrouped fields fall under "Other" in the fixture.
expect(within(header).getByText("6")).toBeInTheDocument();
});
test("filter narrows the visible fields", async () => {
renderApp(tree(), { route: "/fields" });
await screen.findByText("Inscription");
await userEvent.type(screen.getByRole("textbox", { name: /filter/i }), "inscription");
expect(screen.getByText("Inscription")).toBeInTheDocument();
await waitFor(() => expect(screen.queryByText("Material")).not.toBeInTheDocument());
await userEvent.clear(screen.getByRole("textbox", { name: /filter/i }));
await userEvent.type(screen.getByRole("textbox", { name: /filter/i }), "zzzznomatch");
expect(await screen.findByText(/no matches/i)).toBeInTheDocument();
});
test("deep link /fields/:key opens the edit form for that field", async () => {
renderApp(tree(), { route: "/fields/inscription" });
// edit mode: the key input is locked and prefilled from the URL. The form
// remounts when the defs query resolves, so re-query inside waitFor.
await waitFor(() => expect(screen.getByLabelText(/^key$/i)).toHaveValue("inscription"));
expect(screen.getByLabelText(/^key$/i)).toBeDisabled();
expect(screen.getByRole("button", { name: /cancel/i })).toBeInTheDocument();
});
test("selecting a field switches to its edit form; cancel returns to create", async () => {
renderApp(tree(), { route: "/fields" });
await userEvent.click(await screen.findByRole("button", { name: /inscription/i }));
await waitFor(() => expect(screen.getByLabelText(/^key$/i)).toHaveValue("inscription"));
expect(screen.getByLabelText(/^key$/i)).toBeDisabled();
await userEvent.click(screen.getByRole("button", { name: /cancel/i }));
await waitFor(() => expect(screen.getByLabelText(/^key$/i)).toHaveValue(""));
expect(screen.getByLabelText(/^key$/i)).toBeEnabled();
});
test("an unknown key falls back to the create form", async () => {
renderApp(tree(), { route: "/fields/zzz-does-not-exist" });
await screen.findByText("Inscription");
const key = screen.getByLabelText(/^key$/i);
expect(key).toHaveValue("");
expect(key).toBeEnabled();
});
test("creates a text field — posts the body and clears the key input", async () => {
let body: { key: string; data_type: string } | undefined;
+17 -9
View File
@@ -1,13 +1,13 @@
{
"common": { "yes": "Yes", "no": "No", "close": "Close", "loading": "Loading" },
"nav": { "objects": "Objects", "vocabularies": "Vocabularies", "authorities": "Authorities", "fields": "Fields", "search": "Search", "collapseSidebar": "Collapse sidebar", "expandSidebar": "Expand sidebar" },
"auth": { "email": "Email", "password": "Password", "signIn": "Sign in", "signOut": "Sign out", "invalid": "Invalid email or password", "networkError": "Could not reach the server" },
"objects": { "title": "Objects", "empty": "No objects yet", "loadError": "Could not load objects", "notFound": "Object not found", "prev": "Previous", "next": "Next", "of": "of", "new": "New object", "filter": "Filter objects…", "pageSize": "Per page", "columns": { "number": "Object №", "name": "Name", "visibility": "Visibility", "location": "Location", "count": "#", "updated": "Updated" }, "unknownRef": "(unknown)" },
"common": { "yes": "Yes", "no": "No", "close": "Close", "loading": "Loading", "filter": "Filter…", "noMatches": "No matches", "language": "Language", "skipToContent": "Skip to content", "clear": "Clear", "open": "Open" },
"nav": { "objects": "Objects", "vocabularies": "Vocabularies", "authorities": "Authorities", "fields": "Fields", "search": "Search", "collapseSidebar": "Collapse sidebar", "expandSidebar": "Expand sidebar", "breadcrumb": "Breadcrumb" },
"auth": { "email": "Email", "password": "Password", "signIn": "Sign in", "signingIn": "Signing in…", "signOut": "Sign out", "invalid": "Invalid email or password", "networkError": "Could not reach the server", "sessionExpired": "Your session expired — please sign in again.", "signingOut": "Signing out…" },
"objects": { "title": "Objects", "empty": "No objects yet", "loadError": "Could not load objects", "notFound": "Object not found", "prev": "Previous", "next": "Next", "of": "of", "new": "New object", "filter": "Filter objects…", "pageSize": "Per page", "columns": { "number": "Object №", "name": "Name", "visibility": "Visibility", "location": "Location", "count": "#", "updated": "Updated" }, "unknownRef": "(unknown)", "detailTitle": "Object detail", "tableLabel": "Objects" },
"fieldsLabels": { "objectNumber": "Object number", "objectName": "Name", "count": "Number of objects", "briefDescription": "Brief description", "currentLocation": "Current location", "currentOwner": "Current owner", "recorder": "Recorder", "recordingDate": "Recording date", "visibility": "Visibility" },
"visibility": { "draft": "Draft", "internal": "Internal", "public": "Public" },
"form": { "selectPlaceholder": "— select", "create": "Create object", "save": "Save", "cancel": "Cancel", "visibility": "Visibility", "draft": "Draft", "internal": "Internal", "required": "This field is required", "rejected": "The server rejected the changes — check required and referenced fields", "fieldRejected": "The field \"{{field}}\" was rejected — check its value", "createdButFieldRejected": "Object created, but a field was rejected — fix it below.", "flexibleHeading": "Catalogue fields", "saving": "Saving…", "createAnother": "Save & create another", "minCount": "Must be at least 1", "fieldError": { "type_mismatch": "Wrong type for this field", "unresolved": "Referenced value not found", "unknown": "Unknown field" }, "unsaved": { "title": "Discard unsaved changes?", "body": "You have unsaved changes that will be lost.", "stay": "Keep editing", "leave": "Discard" } },
"actions": { "edit": "Edit", "delete": "Delete", "rename": "Rename", "save": "Save", "closeDetail": "Close detail", "confirmDelete": "Delete this object? This cannot be undone.", "confirmDeleteTerm": "Delete this term? This cannot be undone.", "confirmDeleteAuthority": "Delete this authority? This cannot be undone.", "confirmDeleteField": "Delete this field definition? This cannot be undone.", "confirmDeleteVocabulary": "Delete this vocabulary? This cannot be undone.", "inUse": "Can't delete — used by {{count}} object(s). Clear those fields first." },
"labels": { "label": "Label", "externalUri": "External URI (optional)", "otherLanguages": "This entry also has labels in other languages, which are kept." },
"form": { "selectPlaceholder": "Select", "create": "Create object", "save": "Save", "cancel": "Cancel", "visibility": "Visibility", "draft": "Draft", "internal": "Internal", "required": "This field is required", "rejected": "The server rejected the changes — check required and referenced fields", "fieldRejected": "The field \"{{field}}\" was rejected — check its value", "createdButFieldRejected": "Object created, but a field was rejected — fix it below.", "flexibleHeading": "Catalogue fields", "saving": "Saving…", "createAnother": "Save & create another", "minCount": "Must be at least 1", "fieldError": { "type_mismatch": "Wrong type for this field", "unresolved": "Referenced value not found", "unknown": "Unknown field" }, "unsaved": { "title": "Discard unsaved changes?", "body": "You have unsaved changes that will be lost.", "stay": "Keep editing", "leave": "Discard" } },
"actions": { "deleting": "Deleting…", "edit": "Edit", "delete": "Delete", "rename": "Rename", "save": "Save", "closeDetail": "Close detail", "confirmDelete": "Delete this object? This cannot be undone.", "confirmDeleteTerm": "Delete this term? This cannot be undone.", "confirmDeleteAuthority": "Delete this authority? This cannot be undone.", "confirmDeleteField": "Delete this field definition? This cannot be undone.", "confirmDeleteVocabulary": "Delete this vocabulary? This cannot be undone.", "inUse": "Can't delete — used by {{count}} object(s). Clear those fields first." },
"labels": { "label": "Label", "externalUri": "External URI (optional)", "otherLanguages": "This entry also has labels in other languages, which are kept.", "uriPlaceholder": "https://…" },
"theme": { "light": "Light", "dark": "Dark", "system": "System" },
"vocab": {
"newVocabulary": "New vocabulary", "key": "Key",
@@ -28,8 +28,8 @@
"loadError": "Search is unavailable",
"unavailable": "Search is not available on this server",
"loadMore": "Load more",
"resultCount_one": "{{count}} result",
"resultCount_other": "{{count}} results",
"resultCount_one": "~{{count}} result",
"resultCount_other": "~{{count}} results",
"selectPrompt": "Select a result to see the full record"
},
"fields": {
@@ -41,6 +41,7 @@
"authorityKind": "Authority kind",
"anyKind": "Any",
"group": "Group",
"lockedNote": "Key and type can't be changed after creation.",
"required": "Required",
"create": "Create field",
"empty": "No field definitions yet",
@@ -61,6 +62,13 @@
"editLink": "Edit the record",
"illegalError": "That visibility change isn't allowed."
},
"errors": {
"forbidden": "You don't have permission to do that.",
"notFound": "That item no longer exists.",
"conflict": "That conflicts with existing data.",
"validation": "Some values weren't accepted.",
"server": "The server had a problem. Please try again."
},
"toast": {
"created": "Created",
"saved": "Saved",
+7
View File
@@ -33,3 +33,10 @@ test("switches language at runtime", async () => {
});
expect(screen.getByTestId("title")).toHaveTextContent("Föremål");
});
test("syncs document.documentElement.lang on language change", async () => {
await i18n.changeLanguage("sv");
expect(document.documentElement.lang).toBe("sv");
await i18n.changeLanguage("en");
expect(document.documentElement.lang).toBe("en");
});
+9
View File
@@ -15,4 +15,13 @@ void i18n.use(initReactI18next).init({
interpolation: { escapeValue: false },
});
function syncHtmlLang(lng: string) {
if (typeof document !== "undefined") {
document.documentElement.lang = lng.startsWith("sv") ? "sv" : "en";
}
}
i18n.on("languageChanged", syncHtmlLang);
syncHtmlLang(i18n.language);
export default i18n;
+46
View File
@@ -0,0 +1,46 @@
import { expect, test } from "vitest";
import en from "./en.json";
import sv from "./sv.json";
// Flatten a nested translation object into a map of dotted leaf keys → string
// values, e.g. { objects: { columns: { number: "…" } } } → "objects.columns.number".
function flatten(obj: Record<string, unknown>, prefix = ""): Map<string, unknown> {
const out = new Map<string, unknown>();
for (const [key, value] of Object.entries(obj)) {
const path = prefix ? `${prefix}.${key}` : key;
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
for (const [childPath, childValue] of flatten(value as Record<string, unknown>, path)) {
out.set(childPath, childValue);
}
} else {
out.set(path, value);
}
}
return out;
}
const enFlat = flatten(en as Record<string, unknown>);
const svFlat = flatten(sv as Record<string, unknown>);
test("en and sv have identical translation key sets", () => {
const missingInSv = [...enFlat.keys()].filter((k) => !svFlat.has(k)).sort();
const missingInEn = [...svFlat.keys()].filter((k) => !enFlat.has(k)).sort();
expect(missingInSv, `keys present in en.json but missing from sv.json:\n${missingInSv.join("\n")}`).toEqual([]);
expect(missingInEn, `keys present in sv.json but missing from en.json:\n${missingInEn.join("\n")}`).toEqual([]);
});
test("every translation value is a non-empty string in both locales", () => {
for (const [name, flat] of [["en", enFlat], ["sv", svFlat]] as const) {
const bad = [...flat.entries()]
.filter(([, v]) => typeof v !== "string" || v.trim() === "")
.map(([k]) => k)
.sort();
expect(bad, `${name}.json has empty or non-string values at:\n${bad.join("\n")}`).toEqual([]);
}
});
+17 -9
View File
@@ -1,13 +1,13 @@
{
"common": { "yes": "Ja", "no": "Nej", "close": "Stäng", "loading": "Laddar" },
"nav": { "objects": "Föremål", "vocabularies": "Vokabulär", "authorities": "Auktoriteter", "fields": "Fält", "search": "Sök", "collapseSidebar": "Fäll ihop sidofältet", "expandSidebar": "Fäll ut sidofältet" },
"auth": { "email": "E-post", "password": "Lösenord", "signIn": "Logga in", "signOut": "Logga ut", "invalid": "Fel e-post eller lösenord", "networkError": "Kunde inte nå servern" },
"objects": { "title": "Föremål", "empty": "Inga föremål ännu", "loadError": "Kunde inte ladda föremål", "notFound": "Föremålet hittades inte", "prev": "Föregående", "next": "Nästa", "of": "av", "new": "Nytt föremål", "filter": "Filtrera föremål…", "pageSize": "Per sida", "columns": { "number": "Föremålsnr", "name": "Namn", "visibility": "Synlighet", "location": "Plats", "count": "Antal", "updated": "Uppdaterad" }, "unknownRef": "(okänd)" },
"common": { "yes": "Ja", "no": "Nej", "close": "Stäng", "loading": "Laddar", "filter": "Filtrera…", "noMatches": "Inga träffar", "language": "Språk", "skipToContent": "Hoppa till innehåll", "clear": "Rensa", "open": "Öppna" },
"nav": { "objects": "Föremål", "vocabularies": "Vokabulär", "authorities": "Auktoriteter", "fields": "Fält", "search": "Sök", "collapseSidebar": "Fäll ihop sidofältet", "expandSidebar": "Fäll ut sidofältet", "breadcrumb": "Brödsmulor" },
"auth": { "email": "E-post", "password": "Lösenord", "signIn": "Logga in", "signingIn": "Loggar in…", "signOut": "Logga ut", "invalid": "Fel e-post eller lösenord", "networkError": "Kunde inte nå servern", "sessionExpired": "Din session har gått ut — logga in igen.", "signingOut": "Loggar ut…" },
"objects": { "title": "Föremål", "empty": "Inga föremål ännu", "loadError": "Kunde inte ladda föremål", "notFound": "Föremålet hittades inte", "prev": "Föregående", "next": "Nästa", "of": "av", "new": "Nytt föremål", "filter": "Filtrera föremål…", "pageSize": "Per sida", "columns": { "number": "Föremålsnr", "name": "Namn", "visibility": "Synlighet", "location": "Plats", "count": "Antal", "updated": "Uppdaterad" }, "unknownRef": "(okänd)", "detailTitle": "Objektdetalj", "tableLabel": "Objekt" },
"fieldsLabels": { "objectNumber": "Föremålsnummer", "objectName": "Namn", "count": "Antal föremål", "briefDescription": "Kort beskrivning", "currentLocation": "Nuvarande plats", "currentOwner": "Nuvarande ägare", "recorder": "Registrerad av", "recordingDate": "Registreringsdatum", "visibility": "Synlighet" },
"visibility": { "draft": "Utkast", "internal": "Intern", "public": "Publik" },
"form": { "selectPlaceholder": "— välj", "create": "Skapa föremål", "save": "Spara", "cancel": "Avbryt", "visibility": "Synlighet", "draft": "Utkast", "internal": "Intern", "required": "Fältet är obligatoriskt", "rejected": "Servern avvisade ändringarna — kontrollera obligatoriska och refererade fält", "fieldRejected": "Fältet \"{{field}}\" avvisades — kontrollera värdet", "createdButFieldRejected": "Föremålet skapades, men ett fält avvisades — åtgärda nedan.", "flexibleHeading": "Katalogfält", "saving": "Sparar…", "createAnother": "Spara & skapa ny", "minCount": "Måste vara minst 1", "fieldError": { "type_mismatch": "Fel typ för detta fält", "unresolved": "Refererat värde hittades inte", "unknown": "Okänt fält" }, "unsaved": { "title": "Kasta osparade ändringar?", "body": "Du har osparade ändringar som går förlorade.", "stay": "Fortsätt redigera", "leave": "Kasta" } },
"actions": { "edit": "Redigera", "delete": "Ta bort", "rename": "Byt namn", "save": "Spara", "closeDetail": "Stäng detalj", "confirmDelete": "Ta bort detta föremål? Detta kan inte ångras.", "confirmDeleteTerm": "Ta bort denna term? Detta kan inte ångras.", "confirmDeleteAuthority": "Ta bort denna auktoritet? Detta kan inte ångras.", "confirmDeleteField": "Ta bort denna fältdefinition? Detta kan inte ångras.", "confirmDeleteVocabulary": "Ta bort denna vokabulär? Detta kan inte ångras.", "inUse": "Kan inte ta bort — används av {{count}} föremål. Rensa de fälten först." },
"labels": { "label": "Etikett", "externalUri": "Extern URI (valfritt)", "otherLanguages": "Denna post har även etiketter på andra språk, som behålls." },
"form": { "selectPlaceholder": "Välj", "create": "Skapa föremål", "save": "Spara", "cancel": "Avbryt", "visibility": "Synlighet", "draft": "Utkast", "internal": "Intern", "required": "Fältet är obligatoriskt", "rejected": "Servern avvisade ändringarna — kontrollera obligatoriska och refererade fält", "fieldRejected": "Fältet \"{{field}}\" avvisades — kontrollera värdet", "createdButFieldRejected": "Föremålet skapades, men ett fält avvisades — åtgärda nedan.", "flexibleHeading": "Katalogfält", "saving": "Sparar…", "createAnother": "Spara & skapa ny", "minCount": "Måste vara minst 1", "fieldError": { "type_mismatch": "Fel typ för detta fält", "unresolved": "Refererat värde hittades inte", "unknown": "Okänt fält" }, "unsaved": { "title": "Kasta osparade ändringar?", "body": "Du har osparade ändringar som går förlorade.", "stay": "Fortsätt redigera", "leave": "Kasta" } },
"actions": { "deleting": "Tar bort…", "edit": "Redigera", "delete": "Ta bort", "rename": "Byt namn", "save": "Spara", "closeDetail": "Stäng detalj", "confirmDelete": "Ta bort detta föremål? Detta kan inte ångras.", "confirmDeleteTerm": "Ta bort denna term? Detta kan inte ångras.", "confirmDeleteAuthority": "Ta bort denna auktoritet? Detta kan inte ångras.", "confirmDeleteField": "Ta bort denna fältdefinition? Detta kan inte ångras.", "confirmDeleteVocabulary": "Ta bort denna vokabulär? Detta kan inte ångras.", "inUse": "Kan inte ta bort — används av {{count}} föremål. Rensa de fälten först." },
"labels": { "label": "Etikett", "externalUri": "Extern URI (valfritt)", "otherLanguages": "Denna post har även etiketter på andra språk, som behålls.", "uriPlaceholder": "https://…" },
"theme": { "light": "Ljust", "dark": "Mörkt", "system": "System" },
"vocab": {
"newVocabulary": "Ny vokabulär", "key": "Nyckel",
@@ -28,8 +28,8 @@
"loadError": "Sök är inte tillgängligt",
"unavailable": "Sök är inte tillgängligt på den här servern",
"loadMore": "Visa fler",
"resultCount_one": "{{count}} träff",
"resultCount_other": "{{count}} träffar",
"resultCount_one": "~{{count}} träff",
"resultCount_other": "~{{count}} träffar",
"selectPrompt": "Välj en träff för att se hela posten"
},
"fields": {
@@ -41,6 +41,7 @@
"authorityKind": "Auktoritetstyp",
"anyKind": "Alla",
"group": "Grupp",
"lockedNote": "Nyckel och typ kan inte ändras efter att fältet skapats.",
"required": "Obligatoriskt",
"create": "Skapa fält",
"empty": "Inga fältdefinitioner ännu",
@@ -61,6 +62,13 @@
"editLink": "Redigera posten",
"illegalError": "Den synlighetsändringen är inte tillåten."
},
"errors": {
"forbidden": "Du har inte behörighet att göra det.",
"notFound": "Objektet finns inte längre.",
"conflict": "Det står i konflikt med befintliga data.",
"validation": "Vissa värden godtogs inte.",
"server": "Servern hade ett problem. Försök igen."
},
"toast": {
"created": "Skapat",
"saved": "Sparat",
+15
View File
@@ -35,6 +35,7 @@
}
:root {
color-scheme: light;
--background: oklch(1 0 0);
--foreground: oklch(0.145 0 0);
--card: oklch(1 0 0);
@@ -63,6 +64,7 @@
}
.dark {
color-scheme: dark;
--background: oklch(0.145 0 0);
--foreground: oklch(0.985 0 0);
--card: oklch(0.205 0 0);
@@ -97,6 +99,19 @@
body {
@apply bg-background text-foreground font-sans;
}
/* Collapse all animation/transition to a single frame for users who ask the
OS for reduced motion. Covers the kit's data-open/closed animations, the
skeleton pulse, and the sidebar width transition in one place. */
@media (prefers-reduced-motion: reduce) {
*,
::before,
::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
}
@layer components {
+22
View File
@@ -0,0 +1,22 @@
import { expect, test } from "vitest";
import { rowStateClass, segmentClass } from "./class-recipes";
test("segmentClass active uses the primary tokens + focus ring", () => {
const cls = segmentClass(true, "px-2 py-1");
expect(cls).toContain("bg-primary");
expect(cls).toContain("text-primary-foreground");
expect(cls).toContain("focus-visible:ring-ring/50");
expect(cls).toContain("px-2");
});
test("segmentClass inactive uses border, not the primary fill", () => {
const cls = segmentClass(false);
expect(cls).toContain("border");
expect(cls).not.toContain("bg-primary");
});
test("rowStateClass toggles selected vs idle-hover", () => {
expect(rowStateClass(true)).toBe("bg-primary/10");
expect(rowStateClass(false)).toBe("hover:bg-muted");
});
+14
View File
@@ -0,0 +1,14 @@
import { cn } from "@/lib/utils";
import { focusRing } from "./focus-ring";
/** Segmented-control / filter-pill item. Unifies the active/inactive token recipe +
* focus ring; callers pass their contextual padding/size via `className`. */
export function segmentClass(active: boolean, className?: string): string {
return cn("rounded-md", focusRing, active ? "bg-primary text-primary-foreground" : "border", className);
}
/** Selected vs idle row background for master-detail / list rows. */
export function rowStateClass(active: boolean): string {
return active ? "bg-primary/10" : "hover:bg-muted";
}
+1
View File
@@ -0,0 +1 @@
export const focusRing = "outline-none focus-visible:ring-3 focus-visible:ring-ring/50";

Some files were not shown because too many files have changed in this diff Show More