Skip to content

Phase 2: recording navigation in the viewer - #258

Merged
neuromechanist merged 9 commits into
feature/issue-256-epic-viewer-workbenchfrom
feature/issue-253-phase2-viewer-nav
Sep 1, 2026
Merged

neuromechanist merged 9 commits into
feature/issue-256-epic-viewer-workbenchfrom
feature/issue-253-phase2-viewer-nav

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements #253. Once the signal viewer is open, moving to the next run, task or subject no longer means closing it, scrolling the tree and clicking another file. The enlarged (dialog) viewer gains subject and task dropdowns plus prev/next in its header row, and swapping a recording remounts in place rather than reopening the page.

Everything is driven off the Zarr index.json the page already fetches: no backend change, and no dependency on which directories the tree happens to have rendered.

New pure module src/lib/eeg-viewer/recording-nav.ts — parses sub/ses/task/acq/run/recording out of store paths (directory recordings like .mefd/.ds/BTi included; nothing looks at extensions), orders the list three ways, enumerates subjects and tasks, resolves a (subject, task) pick, and holds the persisted iteration order behind an injected Storage. Paths that parse to no entities sort after the ones that did, so a dataset the parser cannot read degrades to plain file order.

Gear setting "Next moves through" — runs, then tasks, then subjects (default), subjects first, file order. Persisted in localStorage under nemar:viewer:nav-order, read lazily at each navigation, and tolerant of storage that throws on access. The viewer announces a change on a bubbling event rather than reaching into the dialog chrome. In the gear menu this section sits above Phase 3's Preload section; the two are independent.

State that transfers across a swap — window length, a manually set gain, channel zoom, filters and the display toggles. Window position resets to the start of the new recording, and bad channels and group index do not travel at all (per-recording labels; see the ViewerTransferState doc comment). The notch re-defaults from the new recording's own PowerLineFrequency unless the user picked one, and any cutoff at or above the new recording's Nyquist is carried over as "off" rather than as a setting the gear shows active while designFilters drops it.

Inline mode gets no controls, per the issue. The first navigation inside the dialog collapses the inline row the viewer was opened from and the dialog owns the instance from then on; closing it then destroys the viewer instead of handing it back. The alternative — re-anchoring under the new recording's row — cannot be done honestly, because the target usually sits in a directory the tree never rendered. Written up as ADR 0012.

The swap reuses the existing eegSeq supersession and the mount's isStale predicate, so rapid next/next/next cannot strand a mount or a WebGL context. Note what that does and does not mean: superseding a navigation ignores the superseded mount's result, it does not cancel its network reads — openRecording takes no AbortSignal. True cancellation belongs to #208 and to Phase 3 (#259), which is adding AbortSignal plumbing to store.ts; this PR deliberately does not touch that. Phase 3's per-mount preload controller is stopped by the same teardown a swap already triggers, so background preload does not leak across a navigation.

Neighbours are warmed with the same prefetchZarrStoreMetadata a hovered tree row gets.

Part of epic #256. Rebased onto the epic branch after #257 and #259 merged.

Test plan

  • bun run test — 1192 passing (62 new: recording-nav.test.ts over realistic BIDS paths — sessions, multi-run, HBN-style subject labels, .mefd/.ds/extensionless directory recordings, split files that tie on every tracked entity, non-BIDS paths, storage round-trip and private-mode failures — plus isDirRecordingName cases in bids-tree.test.ts)
  • bun run typecheck, bun run lint, bun run build green
  • Browser pass against on005514 (2885 recordings, 295 subjects) on the dev server, re-run after the rebase onto Phase 1: directory-format recording viewer rows #257 + Phase 3: capped background preload of recordings #259:
    • dialog reports 6 of 2885, 295 subject options, 9 task options, both dropdowns showing the recording on screen
    • Next moves run-1 to run-2 of the same subject and task; position, title and both aria-labels follow
    • HP 1 Hz and the Clock toggle survive the swap; the time window resets to 00:00:00
    • subject jump to a subject without the current task relaxes to that subject's first recording
    • three rapid Next clicks leave exactly one live viewer on the page
    • a mount forced to throw outside mountEegViewer's own try/catch renders "Couldn't open <name>" with the right fallback and puts the title and dropdowns back on the previous recording; the next navigation recovers normally
    • Esc after navigating destroys the viewer (0 canvases left), leaves no expanded tree row, and a row reopens cleanly afterwards
    • header row wraps to two lines at 820px with no horizontal overflow; prev reads disabled at position 1
    • gear shows "Next moves through" and Phase 3's "Preload" side by side; light and dark both correct
  • QA on test.nemar.org once this lands on the epic branch and promotes

Notes

  • Task options are scoped to the subject on screen, so every option resolves to a real recording. A recording whose siblings have a task but which has none itself shows a — placeholder rather than another recording's label.
  • Prev/next stop at the ends rather than wrapping, and focus moves to the opposite button when one goes disabled.
  • A .mefd/.ds/BTi recording reached by navigation has no tree row to read data-dir-recording from, so isDirRecordingName decides from the name and the viewer's "unavailable" message offers Viewer: directory-format recordings (.mefd/.ds/BTi) never get a viewer row #252's browse-the-files fallback instead of a download link onto directory listing JSON.

@neuromechanist
neuromechanist force-pushed the feature/issue-253-phase2-viewer-nav branch from 0c3229e to 6a382bf Compare September 1, 2026 14:46
Pure module behind website#253: parse sub/ses/task/acq/run/recording
entities out of zarr index store paths (files and .mefd/.ds/BTi
directories alike), order the list three ways for the prev/next control,
enumerate subjects and tasks for the dropdowns, and resolve a
(subject, task) pick to a recording. Paths that parse to nothing keep
file order instead of scrambling. Also holds the persisted iteration
order, read through an injected Storage so it is testable.

Tested: 51 new vitest cases (1127 total), typecheck, lint, build green.
The gear popover gains one section, "Next moves through", persisting the
prev/next iteration order and announcing changes on a bubbling event so
the dialog chrome can relabel its controls without polling storage.

`transfer`/`onTransfer` let a caller carry window length, manual gain,
channel zoom, filters and the display toggles into the next recording.
Window position, bad channels and group index deliberately do not
travel; the notch re-defaults from the new recording's
PowerLineFrequency unless the user set it themselves.

Tested: typecheck, lint, build, full vitest suite green.
Subject and task dropdowns plus prev/next in the enlarged viewer's
header row swap the mounted recording in place, reusing the eegSeq
supersession the tree-click path uses so rapid navigation cannot strand
a mount or a WebGL context (website#208). Settings carry over; the
window resets to the start of the new recording. Neighbours are warmed
with the same prefetch a hovered tree row gets.

The first navigation collapses the inline row the viewer came from and
the dialog owns the instance from then on -- the target recording often
sits in a directory the tree never rendered, so re-anchoring is not
available. The inline panel gains no controls.

Tested: typecheck, lint, build, full vitest suite green.
Why navigating inside the dialog collapses the originating inline row
instead of re-anchoring under the new recording, and why the alternatives
lost.
Stepping onto the first or last recording disabled the button under the
user's focus, dropping focus to the body; hand it to the opposite button.

Tested: typecheck and lint green.
Same behaviour, expressed as two candidate lookups picked by `prefer`
instead of a key-dispatched find.

Tested: full vitest suite, typecheck and lint green.
"Subjects first" promises the subject moves fastest, but sorting ses
before sub walked one subject's sessions before reaching the next
subject, which is what runs order already does. Also pins what happens
to recordings that tie on every tracked entity (BIDS split files): they
keep source order, a dropdown pick lands on the first, prev/next reaches
the rest.

Review findings 3a and 3b on PR #258.

Tested: 8 new vitest cases, full suite green.
renderUnavailable always linked "Download the file", which for a
.mefd/.ds/BTi recording (website#252) resolves to raw directory listing
JSON. Thread `dirRecording` through ViewerOptions from both mount sites
and reuse the page's own wording. The nav path has no tree row to read
data-dir-recording from, so `isDirRecordingName` decides from the name.

Also stops the gear from showing a filter that never runs: a cutoff at
or above the new recording's Nyquist, whether transferred or taken from
the declared PowerLineFrequency, now reads as off.

Review findings 4 and 5 on PR #258.

Tested: 3 new vitest cases, full suite, typecheck, lint green.
A throw outside mountEegViewer's own try/catch (DOM build, WebGL setup,
transfer) left the dialog naming the target while the host held a
half-built viewer and no message: the previous instance is torn down as
the mount's first act, so there is nothing to fall back to. Render the
failure with the same fallback the inline path offers, and put the title
and dropdowns back on the recording the user came from.

Prev/next also now update aria-label alongside title, so a screen reader
hears which recording is next rather than the static markup label.

Review findings 1 and 2 on PR #258.

Tested: typecheck, lint, full suite green.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

@neuromechanist
neuromechanist force-pushed the feature/issue-253-phase2-viewer-nav branch from 6a382bf to a5c2384 Compare September 1, 2026 14:58
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review findings addressed, all six. Branch rebased onto the current epic tip ceedd33 (now carrying #257 and #259) and force-pushed.

1. IMPORTANT — navigateEegViewer rendered nothing on failure. Fixed in a5c2384. The catch now renders Couldn't open <name>. <fallback> instead. into the host, using the same preview__error pattern and the same directory-aware fallback the inline path uses, and puts live.path, live.fileName, the dialog title and the dropdowns back on the recording the user came from. The previous instance really is gone by then (the mount tears it down as its first act), so reverting the chrome is the only way the two agree. Verified end-to-end by temporarily injecting a throw after mountEegViewer's internal try/catch: the message names the target, the title and 6 of 2885 revert, aria-busy clears, and the next navigation mounts normally. The hook was removed before committing.

2. IMPORTANT — prev/next accessible names. Fixed in a5c2384. syncEegNavControls now sets aria-label and title from the same string, so a screen reader hears "Next recording: sub-NDARAC589YMB_task-contrastChangeDetection_run-2_eeg.set" rather than the static markup label. The markup keeps a generic pair as the pre-population state, with a comment saying the two must move together. Confirmed in the browser after a navigation.

3. SUGGESTION — test gaps. Both added in 8d3f48a. (a) A describe for recordings that tie on every tracked entity, using BIDS split files: they keep source order under all three orders, a dropdown pick resolves to the first, and prev/next reaches _split-02/_split-03. (b) The sessioned fixture under subjects order — and writing that test changed the implementation: with ses sorted after sub, "subjects first" walked one subject's sessions before reaching the next subject, which is what runs order already does. ses now sorts before sub, so the walk is sub-01/ses-01 → sub-02/ses-01 → … and the setting's name matches its behaviour.

4. SUGGESTION — transferred cutoffs above the new Nyquist. Fixed rather than commented, in da4ba5f. Every cutoff that reaches filters now goes through usableCutoff, which drops anything at or above rate / 2 and syncs the select to off. That also covers the pre-existing case the finding is adjacent to: a declared PowerLineFrequency of 60 Hz on a 100 Hz store used to show an active 60 Hz notch that designFilters silently discarded.

5. SUGGESTION — renderUnavailable download link for directory recordings. Fixed in da4ba5f. dirRecording is threaded through ViewerOptions from both mount sites: openEegViewerInline reads btn.dataset.dirRecording, and the navigation path uses a new isDirRecordingName helper in bids-tree.ts (three unit cases) because the target may have no row in the tree at all. It is the complement of classifyFile().isEEG rather than signalDirExt() !== null, so it also catches the extensionless 4D/BTi case. The message reuses #257's wording verbatim, with a sync comment on both copies.

6. FYI — "abort" means ignore-the-result, not cancel-the-network. No code change, as instructed. The PR body now says so explicitly and points at #208 and #259 as the owners of true cancellation. Worth adding: #259's preload controller is per-mount and its stop() is in the same teardown a swap already runs, so background preload does not survive a navigation.

Rebase. Two rebases, actually — first onto d3617cf (#257), which applied clean, then onto ceedd33 (#257 + #259). The second hit one conflicted commit with three hunks, all in viewer.ts and all additive: the import block (both sides added an import at the same position — kept both), the gear change-handler block (Preload's handlers vs the nav-order handler — kept both, sequentially), and menu.append (both added a section — "Next moves through" now precedes "Preload"). No behaviour was dropped on either side. The browser pass was re-run against the rebased tree; the gear shows both sections and everything else behaves as before.

Gates on the rebased branch: bun run test 1192 passing, typecheck, lint, build green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant