Repository navigation
refactor(ui): render results from props instead of the form store - #565
Conversation
WilhelmZA
left a comment
There was a problem hiding this comment.
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.
|
Rebased onto
Frontend: 34 files, 240 tests pass, and Biome and tsc are clean. |
6e052bf to
be9639b
Compare
WilhelmZA
left a comment
There was a problem hiding this comment.
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 gatepassed on a run that merged this head intomainatf89ab22a. #564 has merged since and changesresults/individual.tsxandindividual.test.tsx, which this PR also changes. The merge is textually clean and the merged file keeps theformatTimestampcache label, but no CI run has tested the two together. Please rebase ontomainsoCI gateruns 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.
be9639b to
6ed2c68
Compare
|
Rebased onto
|
WilhelmZA
left a comment
There was a problem hiding this comment.
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".
Base:
mainat31ee3026(after #574; not stacked). Two commits. Piece R1 of the hyperglass-ng port (tracker #550). Not a port of fork code: the fork'sSnapshotResultspassed asnapshotprop into aResultthat 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:
components/results/individual.tsxuseFormState(s => s.form),addResponse;useLGQuery, which POSTs/api/queryand polls/api/query/{id}/enrichmentwhile enrichment is pendingcomponents/results/group.tsxqueryLocationcomponents/results/tags.tsxform,getDirectivecomponents/path/path.tsxtarget.display,response(device),addResponse; on open may POST/api/aspath/enrichcomponents/output/*andhooks/use-table-to-string.tsdon't use the form store;useTableToStringonly 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:Resultis split in place._Resultkeeps its place and its hooks up touseLGQuery: theonSuccess/onErrorhandlers, theerrorLevelstate,addResponse, the device lookup and the "device not found" toast. It ends inreturn <ResultPanel />, which gets the AS path, copy and requery buttons in the same order as before._ResultPanelfollows it and holds the render half. It takes these props:location: { id, name }queryTargetquery: the query stateuseLGQueryreturns (data,error,isLoading,isFetching,isFetchedAfterMount).errorisResponse | QueryResponse | Error | null, andnullfor a stored result.errorLevelbuttons, a render function that gets{ data, copyValue, isQuerying, showPath }.queryreplacinguseLGQuery(...),queryTargetreplacingform.queryTarget, andlocationreplacingdevice.ResultPaneldoesn'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:Pathtakesresponse,displayTargetand an optionalonEnriched(response). WithoutonEnrichedit returns before the/api/aspath/enrichlookup.Resultrenders it through a smallFormPaththat readstarget.displayandresponse(device)from the store and passesaddResponseasonEnriched, asPathdid before.tags.tsx:TagstakesqueryLocation,queryType(the directive name) andqueryTarget.group.tsx: the layout becomesResultsView, which takestagsand the result list as children.Resultsreads the form andgetDirective(moved fromTags) and passes them in.results/index.tsalso exportsResultPanel,Tagsand 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.
ResultPanelnow readsdata,error,isLoading,isFetchingandisFetchedAfterMountfrom the same tracked result object, andResultreadsrefetch, 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:ResultPanelwith an empty form store and afetchthat 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 titlePath to 2001:db8::/32. Each test asserts thatfetchwas never called and that the store'sresponsesstay empty.ResultsViewrenders the tags and results it is given. A liveResulttest opens the AS path with enrichment on and checks that the store receives the enriched response, which covers theFormPathwiring.components/path/path-props.test.tsx:Pathfrom props with an empty store andweb.aspathEnrichment: truemakes no request withoutonEnriched. WithonEnrichedit POSTs the ASNs and passes the enriched response to the callback.components/results/tags.test.tsx:Tagsfrom 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.tsxrenders the liveResult, so it now goes throughResultPanel, which builds the label fromquery.data.timestampwithformatTimestamp(viewer's locale and time zone, as onmain). It passes unchanged. WithformatTimestampremoved fromResultPanel's label it fails (Tests 1 failed | 22 passed (23)).With
origin/main's components (at31ee3026) and the new tests, 9 of the 11 fail (the props don't exist onmain). The two that pass are the liveResultAS path test and "shows nothing without a location", which is whatmainalready does:Mutation checks, each reverted afterwards:
if (!onEnriched) return;guard inPath: "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 (fetchcalled once).onEnrichedfromFormPath: "looks up ASN organizations for the live AS path and stores the enriched response" fails.TagsignorequeryLocation: "shows nothing without a location" fails.ResultPanellook 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,typecheckandtestpass,Tests 280 passed (280)(269 onmain, plus 11 new).next buildwith the UI test fixture ashyperglass.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-treeof this branch against each, then the resolved merge run throughtsc,biome lintandvitest):fix/533-icon-button-namesmainbefore this branch.fix/511-abort-controlleruse-lg-query.tsis untouched here. 290 tests pass.fix/509-error-without-bodysetErrorLevel,isErroranderrorMsgchanges apply to the lines left in place. 282 tests pass.fix/541-a11y-textmainbefore this branch; resolved here.path.tsxkeeps #544'suseStrflabel and drops the twouseFormStatelines, and itspath.test.tsxrendersPathfrom props (see Tests).fix/546-query-retryuse-lg-query.tsis untouched here. 296 tests pass.feat/port-t1-timestampsmainbefore this branch; the rebase applied without conflicts. ItsformatTimestampcache label moved with the render half intoResultPanel(see Tests).Merge order doesn't matter for #536, #542 and #563. #537, #545 and #574 are also on
mainand touch none of this PR's files.Pathchanges signature, from{ device }to{ response, displayTarget, onEnriched? }. Its only caller in the app isResult, which this PR updates.ResultPaneltakes the device and the header buttons. Here the device islocation: { id, name }, not the configured device object. The buttons are abuttonsrender 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.ResultPanelmust be inside anAccordion(it uses the accordion context to open itself once settled). It uses the liveuseConfig()only forweb.textlabels,cache.showText/cache.timeoutand the fallback messages, and the theme for colours. The buttons a caller passes may use more.buttonsruns on every render, so it must not call hooks. The callback can render components that use hooks, asResultdoes withFormPath. I kept a render function rather than aButtonscomponent prop. The live buttons close overrefetchand 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.Resultreturns beforeResultPanelmounts, so a valid result opens instead. The form only offers configured locations, so this needs a hand-edited form state.snapshotprop andreadOnlyflag aren't ported. A stored result is rendered by passingResultPanelthe stored response and the buttons that apply (copy, andPathwithoutonEnriched), soResultPanelhas no mode switch./?share=<id>#560, which render stored results:isFetchedAfterMountanddata.cachedthe caller passes.Countdownwithcache.timeout) restarts whenever the panel mounts.now - age, infields.tsx(Age) and inuse-table-to-string.ts(formatTime, used by the copy text). A stored view needs a reference time, such asdata.timestamp, or the ages drift.next buildprints atopLevelAwaitwarning about the target environment. It also appears onmain, so it doesn't come from this change.Closes #555