open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

refactor(viewer): dedupe shared viewer chrome (pager + back-link)

Open lizhengfeng101 opened this issue 2 months ago • 2 comments

Description

Several list/detail pages now carry their own copy of the same viewer chrome. Two pieces are duplicated on main and should be consolidated into a single source of truth.

1. Client-side pager (Repositories + Sessions)

The Repositories page (#1382 / #1322) and the Sessions list page (#1377 / #1323) each shipped their own copy of the same pager:

  • JS — internal/viewer/static/repos.js (~120 lines, search + pager) and internal/viewer/static/sessions.js (~93 lines, pager only) reimplement the same pageList / render / ellipsis-window / step-disabling / focus-management logic.
  • CSS — internal/viewer/static/style.css carries two identical rule sets differing only in prefix: .repos-page .{pagination,page-step,page-number,page-numbers,page-gap, …} and .sessions-page .{…} (13 selectors each).
  • Template markup — the pager <nav class="pagination">…</nav> block in templates/repos.html and templates/sessions.html is identical apart from its ids.

2. Back-link control (Sessions list + Session detail)

The Sessions list (#1377 / #1323) and the Session detail header (#1384 / #1324) each override .back-link into the same icon-box treatment:

  • internal/viewer/static/style.css has a base .back-link (with a now-dead ::before CSS arrow) plus two near-identical scoped overrides — .sessions-page .back-link and .session-page .back-link (each with :hover, ::before { content: none }, and svg sizing).
  • Both templates already render the same markup: <a class="back-link" …>{{icon "chevron-left"}}</a>.

Scope / approach

Pager

  • CSS: promote the duplicated pager rules to unscoped .pagination, .page-step, .page-number, .page-numbers, .page-gap (drop the .repos-page / .sessions-page prefixes). Keep only genuinely page-specific rules scoped.
  • JS: extract a shared internal/viewer/static/pager.js that exposes a small global initializer (e.g. window.ocrPager({ table, pager, numbers, pageSize, filter })), and add it to the //go:embed directive in server.go. repos.js keeps its search input and calls the shared pager with a filter; sessions.js calls it without one. Stay with plain <script src> (no ES modules) so it remains CSP-safe and consistent with the other viewer scripts; load pager.js before the page script.
  • Template: extract the pager <nav> into a shared partial (like app-header), parameterized by id prefix and aria-label, used by both pages.

Back-link

  • Fold the icon-box treatment into the base .back-link rule (SVG-based): remove the dead ::before CSS arrow, add base .back-link svg sizing, and keep the hover/focus states once. Then delete the two per-page overrides (.sessions-page .back-link*, .session-page .back-link*). The templates already render {{icon "chevron-left"}}, so no template change is needed. Keep any genuinely page-specific bit scoped (there is likely none).

Tests

  • Update the existing pager tests to the deduplicated structure — TestReposJS_PagerContract, TestSessionsJS_PagerContract, TestReposCSS_PagerStaysHiddenUntilScripted, TestSessionsCSS_PagerStaysHiddenUntilScripted (the CSS guard moves to the unscoped .pagination[hidden] selector; the JS contract moves to pager.js).

Out of scope

  • No behavior or visual change — both the pager and the back buttons must look and behave exactly as they do today (10 per page, ellipsis window, hidden-until-scripted, focus handling; back buttons unchanged on both pages).
  • No move to a bundler or ES modules.

Acceptance Criteria

  • [ ] Pager: one shared pager.js, one set of unscoped pager CSS rules, and one pager template partial. repos.js / sessions.js no longer duplicate the pager logic or styles.
  • [ ] Repositories and Sessions pages paginate identically to before (incl. search on the repos page), pager stays hidden until scripted, focus management preserved.
  • [ ] Back-link: a single base .back-link rule drives both pages; the per-page .sessions-page/.session-page back-link overrides and the dead ::before arrow are gone; both back buttons look and behave as they do today.
  • [ ] No CSP regression (external scripts only; no inline <script>).
  • [ ] Tests pass (make test) and code check passes (make check).

Context

Part of #1319. The pager copies landed via #1382 (#1322) and #1377 (#1323); the back-link overrides landed via #1377 (#1323) and #1384 (#1324). Each PR independently mirrored the same chrome — this issue merges them into one source of truth so the patterns aren't copied a fourth time.

AI disclosure: The scope and decisions are my own. An AI assistant (Claude Opus 4.8) was used only to polish the English wording.

lizhengfeng101 avatar Sep 18 '26 03:09 lizhengfeng101

I'd like to work on this.

jimpablo avatar Sep 18 '26 03:09 jimpablo

I'd like to work on this.

ok

lizhengfeng101 avatar Sep 18 '26 04:09 lizhengfeng101