Repository navigation
feat(ui): fill in the query form from URL parameters - #567
Conversation
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at 8eed9655.
I read the diff against fix/540-form-state. URL values reach the store only after a check against the configuration: known device ids, offered directive ids or names, select options and variable options. Free-text targets with control or format characters are dropped, which matches the backend check. Nothing submits the form, so a link cannot run a query without a user action, and the values render only as React text and input values. CI gate passes and all four commits are verified.
Before approval:
- Gallery mode: prefill runs after the form mounts, and
components/location-card.tsxreadsdefaultCheckedinto state only on mount (#549, still open). Open/?location=<id>&type=...&target=...: the store and the form hold the location and submit sends it, but its card shows no selection and the first click on it does nothing. #554 lists #549 as a dependency. Please land the #549 fix first or stack this PR on it, and add a card selection test for a prefilled location. Closes #554claims more than the PR does. #554 asks for a repeatedtargeton directives withmultiple: true, and this PR accepts one target. Please change the keyword toPart of #554, or amend #554 to drop multiple targets. The base is also notmain, so the keyword does not take effect on this merge anyway.
#537 and #545 change the same four form files further down the stack. #564 and #566 also edit CHANGELOG.md.
Nits:
hyperglass/ui/components/query-target.tsx: the select target is now controlled and the shared select is clearable, buthandleSelectChangeignores a null value. Pressing the clear control leaves the option shown and inform.queryTarget. Handling null there, or passingisClearable={false}, would fix it.
8eed965 to
a0cba4d
Compare
|
Thanks.
|
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at a0cba4d4.
The body now says Part of #554, and the clear control on the select target works through the fix in #545. CI gate passes and all four commits are verified. The validation of URL values against the configuration is unchanged from the earlier review and I found no new issue there.
Before approval:
hooks/use-form-state.ts,prefillForm: the target and variables are cleared only when the resolved query type differs from the one already in the form (queryType.id !== get().form.queryType). When they are equal, a link target that is dropped as invalid, or a link with no target, leaves the old target in the form, and the lastsetmerges the link's variables into the old ones ({ ...state.form.queryVariables, ...variables }). Example: the store holdsrouter_one, BGP Route, target192.0.2.1and one variable.prefillFormruns with the same location and type and a target that fails the control character check. The form still shows and submits192.0.2.1with the old variable. The body says a query already in the form has its type, target and variables replaced, and the test for that covers only a different query type. Please clearqueryTarget,target.displayandqueryVariablesonce a query type is resolved, before the link's values are applied, and add a test with the same query type already in the form.- Gallery mode is still open, as you noted:
components/location-card.tsxreadsdefaultCheckedonly on mount, so a prefilled location's card shows no selection and ignores the first click until #549 is fixed. This PR stays blocked on #549, and it needs a card selection test for a prefilled location once it is restacked.
|
Thanks, good catch. New tests cover the same query type already in the form, in the store with an invalid target, no target, and a link variable next to an old one, and in the rendered form, where the input shows an empty value and Submit reports the missing target or variable. The body's steps, tests and mutation table are updated. 09d944b also changes the Still blocked on #549 for gallery mode, as noted. |
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at 09d944b9.
The prefillForm point is fixed. The query type, target, target display and variables are cleared before locationChange, the link's variables replace the old ones, and the new store and form tests cover a link whose query type is already in the form. CI gate passes and all six commits are verified.
Before approval:
- Gallery mode is the one item left.
components/location-card.tsxstill readsdefaultCheckedonly on mount, so a prefilled location's card shows no selection until #549 is fixed. Once #549 lands and this PR is restacked on it, please add the card selection test for a prefilled location and I will review that head.
09d944b to
a3edf4a
Compare
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at a3edf4a2.
This head is the same six commits rebased onto the new #545 head, with no change to the patches reviewed at 09d944b9, and every commit is verified.
Before approval:
CI gatefails on this head. The cause is onmain:hyperglass/cli/tests/test_healthcheck.py:18buildsCliRunner(mix_stderr=False), and Click 8.5.1 from #535 no longer acceptsmix_stderr, so both backend test jobs stop at collection. It needs a fix onmainand a rebase. The details are in my comment on #545.- Gallery mode is unchanged: the PR stays blocked on #549, and it needs the card selection test for a prefilled location once it is restacked.
a3edf4a to
b771b0b
Compare
b771b0b to
663bc54
Compare
|
Restacked on #574, the #549 fix (663bc54). It needs another look.
GitHub keeps |
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at 663bc543.
The prefill commits are the ones reviewed at 09d944b9, restacked on the #549 fix: the range diff shows one context-only difference in a test hunk. The new gallery-mode test applies the prefill after the cards mount, as the page does, and checks the selected card and that one click removes the location and a second restores it. It fails without the card fix. CI gate passes on this head and all eight commits are verified. The body says Part of #554, which matches the single-target scope.
Before approval:
- #564 has merged and
CHANGELOG.mdnow conflicts withmain. - This branch carries #574's commit and has no
Closes #549. Please merge #574 first, then rebase this branch ontomain, so #549 closes through #574 and this diff shrinks to the prefill change. I will approve that head onceCI gatepasses on it.
663bc54 to
68cb475
Compare
|
Rebased onto
|
WilhelmZA
left a comment
There was a problem hiding this comment.
Approving at 68cb4755.
#574 has merged, and this branch is now the seven prefill commits on main. They are the patches reviewed at 663bc543; the range diff shows only CHANGELOG.md context from #564. The branch contains current main, CI gate passes on it and all seven commits are verified. Part of #554 matches the single-target scope.
A link such as /?location=<id>&location=<id>&type=<id or name>&target=<value>&var.<variable>=<value> fills in the form once the router is ready. prefillForm applies each value through the same steps as the user's input (locationChange, query type, target, variables), so values the configuration doesn't offer are left out. The query type matches by ID, then by name. The parameters are then removed from the address through the router. The form is never submitted from a link. The select target now shows the store's target, so a target set without the select shows as chosen.
- An unknown query type stops the prefill also when the locations offer one query type and locationChange chose it, so the link's target isn't applied to it. - The prefill effect runs after the store mirror, so a no-common-query-types error survives a mount with the router already ready. - router.replace gets the page's route and the address separately, so a page served as /index.html stays on the shallow path. - Variables are read as own properties, so IDs such as constructor or toString can be prefilled. - A target with control characters or line separators is left out; the backend rejects it. - The parameters left in the address keep their original text. - Tests mount the form before the router is ready, as on a first load.
prefillForm cleared the target and variables only when the link's query type differed from the one in the form. With the same query type, a target left out (or missing) kept the old one, and the link's variables were merged into the old ones; a link without a resolvable query type kept the old query type when the new locations still offered it. Clear the query type, target, its display and variables once a known location is applied, before locationChange, so the result depends only on the link.
The backend doesn't check a select target against a directive's options, so only the variables and their options must match.
A prefilled location shows as a selected card, one click removes it from the card and the store, and a second click selects it again. The other cards stay unselected.
|
Rebased onto |
68cb475 to
68a96ae
Compare
WilhelmZA
left a comment
There was a problem hiding this comment.
Approving at 68a96aea.
This is the rebase onto main after #565 and #566. The seven commits are the patches approved at 68cb4755; the range diff shows only CHANGELOG.md context, where the entry now follows the forced refresh entry under Added. The branch contains current main, CI gate passes on it and all seven commits are verified.
Builds on #574 (merged), which fixed #549. #545 and #537, which it also builds on, are merged. Rebased onto
main.Problem
There is no way to link to a query with its location, query type and target filled in, for example from a NOC tool, a ticket or documentation. History (H2, #557) and the shared result page (S2, #560) need the same function to fill the form from stored values.
Change
Port of the prefill part of jsenecal#6 and #136, rewritten on top of #537/#545 and extended for several locations, mixed vendors and directive variables.
run=1is not ported: in the fork it set the results view directly, which skipped the required greeting and hostname resolution.URL shape
/?location=<device id>&location=<device id>&type=<directive id or name>&target=<value>&var.<variable id>=<value>locationis repeated for several locations; duplicates are read once.var., because a directive variable may be namedlocationortype(validate_variable_namesonly reservestargetandmask).target. The UI doesn't receive a directive'smultipleflag (Directive.frontend()doesn't export it) and the form only ever submits one target, so a link can't express more than the form can. Fortype,targetand each variable the first value is used. Empty values are skipped.URLSearchParams: IPv6 addresses and prefixes work unencoded (target=2001:db8::/32), a zone needs%25(fe80::1%25eth0), and+reads as a space, as in any query string. The target is taken as written, as typed text is: brackets or a zone are passed on to the backend's validation, which decides as it does for typed input.hooks/use-form-state.tsparsePrefill(search)reads the parameters;withoutPrefill(search)returns the query string without them, keeping the others.prefillForm(prefill, extra)is the one entry point (H2 and S2 will call it too). It goes through the same steps as the user's input, so fix(ui): submit the only available query type and reject an empty target #537/fix(ui): keep the query form consistent after location changes and resets #545's rules apply:target.display) and variables are cleared first, so nothing of the old query is kept when a value from the link is left out or missing. It then setsselections.queryLocation, then callslocationChange, which offers the common query types, chooses the only one if there is one (clearing the target, Form state: stale query type after a location change, target kept on auto-select, errors kept after reset #540), and reports locations without query types in common with the usual message.resolve_directive_by_namedoes on the backend. Atypethat isn't offered stops here, also whenlocationChangechose the only offered query type: the link's target was meant for another query type.type): skipped for a directive without a field (the form sets['null']as before), one of the options for a select, otherwise as written, except that a target with control characters or line separators (Unicode Cc, Cf, Zl, Zp, the classes the backend rejects invalidate_query_target_characters) is dropped, so the form never holds a target the input can't show. It sets bothform.queryTargetandtarget.display(the input showstarget.display).Object.hasOwn), so IDs such asconstructorortoStringwork.The target and variables are independent: an invalid variable doesn't drop a valid target.
components/looking-glass-form.tsxOnce
router.isReady, the form readswindow.location.searchonce, callsprefillForm, then removes the parameters withrouter.replace(${router.pathname}${rest},${pathname}${rest}${hash}, { shallow: true, scroll: false }). Other parameters keep their original text (?q=a%20b&flagstays as written) and the hash stays. The route is the page's (/) and the address is passed asas, so a page served as/index.htmlkeeps its address and stays on the shallow path; with the address as the route,/index.htmlisn't a route and Next.js would load the page again. #537's mirror passes the values to react-hook-form.The prefill effect is declared after the mirror effect. When the router is already ready at mount (a client-side navigation), both run in the same commit, and the mirror's
resetof the still-empty form cleared the "no query types in common" error thatlocationChangehad just set. Nothing is submitted: the user presses Submit, andsubmitHandler's greeting check and hostname resolution apply.Why it waits for the router, and replaces through it: checked in Chromium against a
next buildexport, a first version that calledhistory.replaceStateon mount didn't hold. For an auto-exported page opened with a query string, Next.js replaces the address after hydration with the one it read (that's whenisReadyturns true), which put the parameters back; Back from the results then remounted the form and applied the link again over the user's change. WithisReadyandrouter.replace, Next.js's history state has the new address too. Result, same export, Chromium: opened/?utm=x&location=router_one&location=router_three&type=__hyperglass_juniper_bgp_route__&target=2001:db8::/32#frag→ form filled with BGP Route and2001:db8::/32, address/?utm=x#frag; changed the target to2001:db8::1, submitted, pressed Back → address/?utm=x#frag, target2001:db8::1. The same with/index.html?utm=x&q=a%20b&flag&location=…#frag→ address/index.html?utm=x&q=a%20b&flag#fragthroughout, target2001:db8::1after Back.Why the parameters are removed rather than guarded with a ref or a store flag: the form unmounts while the results show, and once the user changes the form the address would no longer describe it.
components/query-target.tsxThe select target had no
value, so a target set in the store without the select (from a link, and also on return from the results) wasn't shown as chosen. It now shows the option matchingform.queryTarget[0].Invalid values: no message. Unknown or unavailable values are dropped silently. The form shows exactly what was applied, and a message would need new
web.textkeys (types, sample config, docs, fixture) for a case the reader can see on screen. The one existing message that can appear islocationChange's "no query types in common", the same as for a manual selection. H2 plansweb.text.history_location_unavailablefor its own case.Gallery mode (#549).
LocationCardused to read its checked state only on mount, and the form mounts before the router is ready, so prefilled locations were selected in the store but their cards didn't show it, and the first click on such a card did nothing. #574 (merged) made the card controlled (isCheckedfromform.queryLocation); this PR adds a test for a prefilled card (below). The location select (dropdown mode) already showed the prefilled locations.Tests
New in
hooks/use-form-state.test.ts:parsePrefill: null without parameters / only other parameters / only empty values; repeated locations, type, target and a variable; with or without?; encoded IPv6, IPv6 prefix unencoded, zone%25, bracketed;var.locationandvar.typekept apart fromlocationandtype; locations read once, first of other repeated parameters; variables without an ID or value skipped.parsePrefill: variables namedconstructorandtoString.withoutPrefill: removes only its parameters, keeps the others in order and as written (q=a%20b,flag,r=x+y); removes them with encoded names (%6Cocation,var%2Evrf); empty result; unchanged without its parameters.prefillForm: one location; several in order with their common types; unknown locations dropped; nothing changes without a known location; no-common-types error as for a selection; type by ID, by name, by another selected location's ID; unknown type dropped with its target; target without a type dropped; IPv4, IPv6, IPv6 prefix, IPv6 with zone and hostname targets set inform.queryTargetandtarget.display; target for the only query type chosen bylocationChange; select target in / not in the options; target of a field-less directive dropped; only defined variables with allowed values, including one namedlocation; a query already in the form has its type, target and variables replaced; with the same query type already in the form, a target that is left out and a link without a target clear the old target and variables, a link variable replaces the old ones, and a link without a resolvabletypeor with notypeclears the old query type, target and variables, while a link without a known location leaves the old query alone; an unknowntypeon a location with only Ping drops the target and variables (Ping stays chosen bylocationChange); targets with a line feed, carriage return, NUL, zero-width space or line separator are dropped; variablestoStringset, inheritedconstructornot; status staysform.New in
components/looking-glass-form.test.tsx(LookingGlassForm prefill, the address set withhistory.replaceState,next/routerstubbed). As on a first load, the form mounts with the router not ready, which then becomes ready (openAddress); one test mounts with it ready, as after a client-side navigation:data-checked="true", the other cardsfalse); one click removes it from the card and the store (form.queryLocationandselections.queryLocationempty), a second click selects it againform)/index.htmlas the address, on the/routequeryTargetempty, target error on Submit), and no variable when the link has none (required-variable error on Submit)With #545's code (
1a775da1) and the new tests:(That run predates the review fixes below.
parsePrefill,withoutPrefillandprefillFormdon't exist there; the form tests that pass on #545 are its existing ones plus the three where nothing is expected to happen: unavailable values dropped, greeting required, address without parameters.)Reverting one part at a time on the final code (
hooks/use-form-state.test.tsandcomponents/looking-glass-form.test.tsx, 110 tests); every mutation fails at least one test:(The 109-test runs predate the "waits for the router" test.)
After the first review fixes (123 tests in the two files):
The last one survives: an inherited property such as
constructoris a function, which never equals an option's string value, soprefillFormalready ignored it. TheObject.hasOwnthere is defensive and states the intent;parsePrefill's check is the one that matters (within,var.constructorwas skipped).After the second review fix (the query in the form is replaced; 139 tests in the two files):
The "type change keeps target and variables" mutation above no longer applies: the query type step only sets the type now, as the target and variables are already empty.
The gallery card test, with
location-card.tsxandquery-location.tsxfrommain(before #574) plus only thedata-checkedattribute added to the card so the test can read it, fails where the reviewer saw the bug: the prefilled card isn't selected.With the change, rebased onto
main(with #574 and #564):biome format,biome lintandtsc --noEmitare clean;Test Files 33 passed (33),Tests 342 passed (342); the two files above:Tests 144 passed (144).NODE_ENV=production next build(static export, with the UI test fixture ashyperglass.json) succeeded before the restack onto #574:✓ Exporting (3/3),○ / ... (Static); not rerun after it.Changelog
?location=<id>&type=<id or name>&target=<value>&var.<variable>=<value>, withlocationrepeated for several locations. Values the configuration doesn't offer are left out, the parameters are removed from the address once applied, and the query runs only when the user submits it, so a required greeting still applies (Prefill the query form from URL parameters #554).Added to
CHANGELOG.mdunder Unreleased → Added. The user guide has a new section, "Link to a filled-in form", with the parameters, an example, encoding, and what happens to invalid values.Differs from #554
targetrepeated formultiple: truedirectives. The UI doesn't receivemultipleand the form submits one target, so a link reads the firsttargetonly (above).router.replace, nothistory.replaceState.history.replaceStatewas undone by Next.js's own replace after hydration (checked in Chromium, above). The form also waits forrouter.isReady.typethat isn't offered stops the target and variables, but the target and variables are independent: an invalid variable doesn't drop a valid target, and an invalid target doesn't drop valid variables. Their order doesn't change the result.Notes
A dismissed required greeting loses the link's values. The parameters are removed as soon as the form is filled, while the greeting modal is open. If the greeting is required and the user dismisses it (Esc or the overlay) and then submits,
submitHandlerresets the form and reloads the page, as it does today; the reloaded address no longer has the parameters, so the link's values are gone. Acknowledging the greeting keeps them. Keeping the parameters until the greeting is acknowledged would couple the form to the greeting state; I left it as is.Found, not fixed (reported): with fix(ui): keep the query form consistent after location changes and resets #545's
locationChange, a select target is kept when the query type moves to the same-named directive of another vendor, even if that directive's options don't include it. The select (which now shows the store's target) then shows nothing chosen, while the form still holds and submits the old value.Not ported from the fork:
run=1(above), andusePrefillNavigate, which belongs to H2/S2.Not verified: a deployment behind a
basePath(the app has none;router.replacetakes the route fromrouter.pathnameand the address fromwindow.location.pathname).Found but not changed: the select target's missing
valuealso meant a select target restored on return from the results wasn't shown; fixed here because prefill needs it, no separate issue.Part of #554