Skip to content

refactor(ui): render results from props instead of the form store - #565

Merged
jsenecal merged 2 commits into
mainfrom
refactor/port-r1-result-props
Nov 21, 2026
Merged

jsenecal merged 2 commits into
mainfrom
refactor/port-r1-result-props

Conversation

@jsenecal

@jsenecal jsenecal commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Base: main at 31ee3026 (after #574; not stacked). Two commits. Piece R1 of the hyperglass-ng port (tracker #550). Not a port of fork code: the fork's SnapshotResults passed a snapshot prop into a Result that upstream has since coupled to the form store, so this is a refactor of upstream's components.

Problem

Query history "Open" (#556) and the shared result page (#560) need to show a result that is not the current form's query, without querying the device. Upstream's result components can't do that. They read the current query from the form store and run or poll queries themselves:

Component Reads or does
components/results/individual.tsx useFormState(s => s.form), addResponse; useLGQuery, which POSTs /api/query and polls /api/query/{id}/enrichment while enrichment is pending
components/results/group.tsx the form's queryLocation
components/results/tags.tsx form, getDirective
components/path/path.tsx target.display, response(device), addResponse; on open may POST /api/aspath/enrich

components/output/* and hooks/use-table-to-string.ts don't use the form store; useTableToString only takes the target as an argument.

Change

The code moves; it isn't rewritten. Each live component keeps its props and its store reads, and passes values to a component that renders from props.

  • individual.tsx: Result is split in place.
    • _Result keeps its place and its hooks up to useLGQuery: the onSuccess/onError handlers, the errorLevel state, addResponse, the device lookup and the "device not found" toast. It ends in return <ResultPanel />, which gets the AS path, copy and requery buttons in the same order as before.
    • _ResultPanel follows it and holds the render half. It takes these props:
      • location: { id, name }
      • queryTarget
      • query: the query state useLGQuery returns (data, error, isLoading, isFetching, isFetchedAfterMount). error is Response | QueryResponse | Error | null, and null for a stored result.
      • errorLevel
      • buttons, a render function that gets { data, copyValue, isQuerying, showPath }.
    • The render lines are unchanged apart from query replacing useLGQuery(...), queryTarget replacing form.queryTarget, and location replacing device.
    • ResultPanel doesn't read the device configuration. A stored result for a device that has since been removed or renamed still renders, under its stored name.
  • path.tsx: Path takes response, displayTarget and an optional onEnriched(response). Without onEnriched it returns before the /api/aspath/enrich lookup. Result renders it through a small FormPath that reads target.display and response(device) from the store and passes addResponse as onEnriched, as Path did before.
  • tags.tsx: Tags takes queryLocation, queryType (the directive name) and queryTarget.
  • group.tsx: the layout becomes ResultsView, which takes tags and the result list as children. Results reads the form and getDirective (moved from Tags) and passes them in.
  • results/index.ts also exports ResultPanel, Tags and the prop types, for Per-browser query history #556 and Show a shared result at /?share=<id> #560.

Lines that open PRs edit, such as setErrorLevel, stay where they were (see Notes).

The live components make the same requests, in the same order, and store the same responses. React Query v4 tracks which result fields a component reads. ResultPanel now reads data, error, isLoading, isFetching and isFetchedAfterMount from the same tracked result object, and Result reads refetch, so the set of tracked fields, and with it the re-render behaviour, is unchanged.

This PR doesn't add the read-only view itself, or any history or share code.

Tests

New tests, in new files so they don't collide with the open PRs' test edits:

  • components/results/panel.test.tsx: ResultPanel with an empty form store and a fetch that rejects. It renders a stored text response, a stored warning, a stored response for a device id that isn't in the configuration (titled with the stored name, with no "not found" toast), and a stored BGP table whose AS path modal opens with the title Path to 2001:db8::/32. Each test asserts that fetch was never called and that the store's responses stay empty. ResultsView renders the tags and results it is given. A live Result test opens the AS path with enrichment on and checks that the store receives the enriched response, which covers the FormPath wiring.
  • components/path/path-props.test.tsx: Path from props with an empty store and web.aspathEnrichment: true makes no request without onEnriched. With onEnriched it POSTs the ASNs and passes the enriched response to the callback.
  • components/results/tags.test.tsx: Tags from props with an IPv6 target, several targets, and no location.

One existing test changed: components/path/path.test.tsx (added by #544) rendered <Path device="device" /> after putting the target and response in the form store. It now renders <Path response={response} displayTarget={target} /> with no store setup. Its assertions are unchanged.

#564's cache tooltip test in components/results/individual.test.tsx renders the live Result, so it now goes through ResultPanel, which builds the label from query.data.timestamp with formatTimestamp (viewer's locale and time zone, as on main). It passes unchanged. With formatTimestamp removed from ResultPanel's label it fails (Tests 1 failed | 22 passed (23)).

With origin/main's components (at 31ee3026) and the new tests, 9 of the 11 fail (the props don't exist on main). The two that pass are the live Result AS path test and "shows nothing without a location", which is what main already does:

 FAIL  components/path/path-props.test.tsx > Path > renders from props with no form state and makes no request without onEnriched
 FAIL  components/path/path-props.test.tsx > Path > looks up ASN organizations and passes the enriched response to onEnriched
 FAIL  components/results/panel.test.tsx > ResultPanel > renders a stored text response with no form state and no request
 FAIL  components/results/panel.test.tsx > ResultPanel > shows a stored error response at the given level
 FAIL  components/results/panel.test.tsx > ResultPanel > renders a stored response for a device that is no longer configured
 FAIL  components/results/panel.test.tsx > ResultPanel > renders a stored BGP table and opens its AS path with no request
 FAIL  components/results/panel.test.tsx > ResultsView > renders the tags and results it is given
 FAIL  components/results/tags.test.tsx > Tags > shows the query type and an IPv6 target from props
 FAIL  components/results/tags.test.tsx > Tags > joins several targets
      Tests  9 failed | 2 passed (11)

Mutation checks, each reverted afterwards:

  • Remove the if (!onEnriched) return; guard in Path: "renders from props with no form state and makes no request without onEnriched" and "renders a stored BGP table and opens its AS path with no request" fail (fetch called once).
  • Drop onEnriched from FormPath: "looks up ASN organizations for the live AS path and stores the enriched response" fails.
  • Make Tags ignore queryLocation: "shows nothing without a location" fails.
  • Make ResultPanel look its device up in the configuration again and render nothing when it isn't found: "renders a stored response for a device that is no longer configured" fails.

With the change: format:check, lint, typecheck and test pass, Tests 280 passed (280) (269 on main, plus 11 new). next build with the UI test fixture as hyperglass.json (NODE_ENV=production, static export) compiles and exports all 3 pages.

Changelog

None. There's no user-visible change.

Notes

Conflicts with open PRs (git merge-tree --write-tree of this branch against each, then the resolved merge run through tsc, biome lint and vitest):

PR Branch Result
#534 fix/533-icon-button-names Merged into main before this branch.
#536 fix/511-abort-controller Merges cleanly; use-lg-query.ts is untouched here. 290 tests pass.
#542 fix/509-error-without-body Merges cleanly. Its setErrorLevel, isError and errorMsg changes apply to the lines left in place. 282 tests pass.
#544 fix/541-a11y-text Merged into main before this branch; resolved here. path.tsx keeps #544's useStrf label and drops the two useFormState lines, and its path.test.tsx renders Path from props (see Tests).
#563 fix/546-query-retry Merges cleanly; use-lg-query.ts is untouched here. 296 tests pass.
#564 feat/port-t1-timestamps Merged into main before this branch; the rebase applied without conflicts. Its formatTimestamp cache label moved with the render half into ResultPanel (see Tests).

Merge order doesn't matter for #536, #542 and #563. #537, #545 and #574 are also on main and touch none of this PR's files.

  • Path changes signature, from { device } to { response, displayTarget, onEnriched? }. Its only caller in the app is Result, which this PR updates.
  • Deviations from Render result components from props instead of the form store #555's wording: Render result components from props instead of the form store #555 says ResultPanel takes the device and the header buttons. Here the device is location: { id, name }, not the configured device object. The buttons are a buttons render function, because the copy text and whether to show the AS path are computed inside the panel. ResultsView (the layout around tags and results) isn't named in Render result components from props instead of the form store #555. It is there so a stored view can reuse the layout.
  • ResultPanel must be inside an Accordion (it uses the accordion context to open itself once settled). It uses the live useConfig() only for web.text labels, cache.showText/cache.timeout and the fallback messages, and the theme for colours. The buttons a caller passes may use more.
  • buttons runs on every render, so it must not call hooks. The callback can render components that use hooks, as Result does with FormPath. I kept a render function rather than a Buttons component prop. The live buttons close over refetch and the device id, so a component would need either a widened state type or a component created per render, and a component created per render would remount the AS path modal on every re-render.
  • One edge case changes: a location id in the form that isn't in the configuration. Before, the hidden result still ran the effect that opens the first settled result, so it could take the "first open" slot. Now Result returns before ResultPanel mounts, so a valid result opens instead. The form only offers configured locations, so this needs a hand-edited form state.
  • The fork's snapshot prop and readOnly flag aren't ported. A stored result is rendered by passing ResultPanel the stored response and the buttons that apply (copy, and Path without onEnriched), so ResultPanel has no mode switch.
  • For Per-browser query history #556 and Show a shared result at /?share=<id> #560, which render stored results:
    • What a stored result shows for the cache countdown depends on the isFetchedAfterMount and data.cached the caller passes.
    • The countdown (Countdown with cache.timeout) restarts whenever the panel mounts.
    • Route ages are computed as now - age, in fields.tsx (Age) and in use-table-to-string.ts (formatTime, used by the copy text). A stored view needs a reference time, such as data.timestamp, or the ages drift.
    • The effect that opens a settled result opens the panel that settles first while none is open. With stored results, all settle on mount, so which one opens depends on mount order.
  • next build prints a topLevelAwait warning about the target environment. It also appears on main, so it doesn't come from this change.

Closes #555

@jsenecal jsenecal added frontend Next.js UI and the runtime UI build housekeeping Maintenance work: cleanup, hardening, tooling, docs accuracy labels Nov 21, 2026
@jsenecal jsenecal self-assigned this Nov 21, 2026
@jsenecal
jsenecal requested a review from WilhelmZA November 21, 2026 06:10
@coveralls

coveralls commented Nov 21, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 86.449% (+0.2%) from 86.241% — refactor/port-r1-result-props into main

WilhelmZA
WilhelmZA previously approved these changes Nov 21, 2026

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 6e052bfb.

I read the split of Result into Result and ResultPanel, and the prop changes to Path and Tags. The live path keeps the same hooks, requests and button order. Path has one caller and it is updated, and the onEnriched guard stops the AS path enrichment request for stored results. No existing test changes, 11 tests are added, CI gate passes and both commits are verified.

#542 and #564 also change results/individual.tsx, and #544 changes path/path.tsx and adds a path.test.tsx that renders Path with the old props. Whichever of #544 and this PR merges second has to resolve that.

@jsenecal

Copy link
Copy Markdown
Collaborator Author

Rebased onto main after #544 merged (be9639b); this needs another look, sorry for dismissing your approval. The second commit is unchanged. The first commit has two changes:

Frontend: 34 files, 240 tests pass, and Biome and tsc are clean.

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at be9639b4.

The rebase is clean. be9639b4 is the same patch as the second commit I approved at 6e052bfb. The first commit differs only in path/path.tsx and path/path.test.tsx: Path keeps the asPathTitle heading from #544 with the target now passed as a prop, and the three #544 assertions are unchanged. The results files are identical to the approved head. Both commits are verified.

Before approval:

  • CI gate passed on a run that merged this head into main at f89ab22a. #564 has merged since and changes results/individual.tsx and individual.test.tsx, which this PR also changes. The merge is textually clean and the merged file keeps the formatTimestamp cache label, but no CI run has tested the two together. Please rebase onto main so CI gate runs with #564, and I will approve that head.

Nits:

  • The body is out of date after the rebase: the base commit, the test counts, the #544 row in the conflict table and "No existing test changed" (the diff now edits path/path.test.tsx).

Split the result components into parts that render from props and live
parts that read the form state and run the query:

- `ResultPanel` takes the query target, location, query state, error
  level and the header buttons. `Result` runs `useLGQuery`, stores the
  response and passes the copy, requery and AS path buttons.
- `Path` takes the response and display target, and looks up ASN
  organizations only when given an `onEnriched` callback. `Result`
  passes the stored response and a callback that stores the result.
- `Tags` takes its labels. `Results` passes them from the form state,
  through a new `ResultsView` layout that takes the tags and results.

A result, its tags and its AS path can now be rendered from a stored
response with no form state and no request. Nothing does so yet; query
history and share links will. The live components behave as before.

Closes #555
Keep `_Result` where it was, ending in `return <ResultPanel />`, with
`_ResultPanel` after it. Lines that open PRs edit, such as
`setErrorLevel`, stay where they were, so those PRs still apply.

`ResultPanel` now takes the device as `location: { id, name }` instead
of looking it up in the configuration. A stored result for a device that
has since been removed or renamed renders with its stored name. `Result`
looks the device up and shows the "not found" toast, as before.

`ResultQueryState.error` is typed as the hook's error union or `null`,
so a stored result has to pass `null` and is not shown as an error.

Document that `buttons` runs on every render and must not call hooks,
and drop the enrichment polling test, which waited 1.5 s for a code path
`ResultPanel` no longer has.
@jsenecal
jsenecal force-pushed the refactor/port-r1-result-props branch from be9639b to 6ed2c68 Compare November 21, 2026 16:14
@jsenecal

Copy link
Copy Markdown
Collaborator Author

Rebased onto main with #564 and #574 (6ed2c68), so CI gate now runs with #564.

@jsenecal
jsenecal requested a review from WilhelmZA November 21, 2026 16:14

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 6ed2c68f.

Both commits are the same patches reviewed at be9639b4, rebased onto main with #564 and #574. results/individual.tsx keeps the formatTimestamp cache label from #564. The branch contains current main, CI gate passes on it and both commits are verified.

Nits:

  • The body is still out of date after the rebases: the base commit, the test counts, the #544 row in the conflict table and "No existing test changed".

@jsenecal
jsenecal merged commit 2cf550c into main Nov 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Next.js UI and the runtime UI build housekeeping Maintenance work: cleanup, hardening, tooling, docs accuracy reviewing Issue is being reviewed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Render result components from props instead of the form store

3 participants