Repository navigation
fix(ui): keep the query form consistent after location changes and resets - #545
Conversation
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at 1a775da1.
I read the diff against fix/512-514-form-validation. The three #540 fixes are correct for built-in directives, and each has tests. CI gate passes and all three commits are verified.
Before approval:
hooks/use-form-state.ts: when the kept query type moves to a directive with the same name and another id,queryTargetandqueryVariablesstay as they are.queryVariablesis keyed by the old directive's variable ids. Take two devices with custom directives that share a name but have different ids and variables. Select both, choose the query type and set a variable, then remove the first device. The query type moves to the second device's id, the old variable stays in the store and is not shown, and submit returns "Unknown variable". Before this change the old id failed validation and choosing the type again cleared the variables. Please keepqueryVariablesonly whenkept.id === previous.id, or keep only the keys thatkept.variablesdefines. If that configuration is not meant to be supported, please say so in the comment.- The base is not
main, soCloses #540does not take effect on this merge. Please retarget this PR tomainafter #537 merges, or close #540 by hand once the fix is onmain.
Nits:
- No form-level test covers the reset branch when every location is removed.
- The body has a Changelog section with three entries but
CHANGELOG.mdis not edited. #567 is stacked on this branch and does edit it, so the entries could go there or in the release PR.
|
Thanks, good catch. I reproduced it as a form test before fixing it.
|
WilhelmZA
left a comment
There was a problem hiding this comment.
Approving at 098d733e.
I read the four new commits. When the kept query type moves to a directive with the same name and another id, locationChange now keeps only the variables the new directive defines with a value it offers, and keeps the target only when the field type matches and, for a select, every value is offered. The target select is controlled from the store and clearing it removes the target. The new store and form tests check the values the store ends with, and there is now a form test for the reset when every location is removed. CI gate passes and all seven commits are verified.
The base is not main, so Closes #540 takes effect only after this PR is retargeted to main once #537 merges.
Nits:
- The PR body still says the query type "follows the new ID and keeps its target and variables". That no longer holds for custom directives, and the body does not mention the cleared target select or the new tests.
hooks/use-form-state.ts: the new comment says same-named custom directives must share "their variables and options, or the other locations reject the query". The backend rejects unknown variables and variable values, but it does not check a select target against the directive's options, so "variables and variable options" would be exact.
|
Thanks. The body is updated: the same-name/other-id paragraph now describes what is kept and dropped, there is an "Added after review" list of the new tests and the controlled select, and the final test counts are included. On the comment wording, you're right that the backend doesn't check a select target against the options; "variables and variable options" is the exact phrase. I'm leaving that code change out of this head so your approval stands, and will fold it into the next commit that touches |
A location change kept the query type in the form values and the select when the new locations no longer offered it. Clear it with its target and variables, as choosing another query type does. When the new locations offer the same query type under another ID (directives are intersected by name and the first location's IDs are offered), follow it to that ID and keep the target.
When the selected locations have one query type in common and it isn't the current one, locationChange chose it but kept the target, while a manual query type change clears it. Clear the target and its display too. A query type that stays the same keeps its target.
A reset from the header title cleared the store and, through the mirror, react-hook-form's values, but not its errors: an error shown before the reset stayed on screen, and a target error came back with the next query. When the store's values are back to the empty ones, reset react-hook-form instead of setting its values, so its errors and submitted state go too. The mirror reads the store's values object by reference, so it also runs when a reset replaces values that were already empty.
When removing a location moves the query type to a directive with the same name and another ID, the target and variables were kept as they were. Two custom directives can share a name and nothing else, so a variable the new directive doesn't define stayed in the store, unseen, and the query was rejected with "Unknown variable". Keep only the variables (and values) the new directive defines and offers. Keep the target only when the new directive takes the same kind of target and, for a select, offers the selected value. Show the store's value in the target select, so a cleared target isn't still shown as selected.
The target select is now controlled by the form value and the shared select is clearable, but clearing it was ignored: the option stayed shown and in the form. Clear the form value and the displayed target instead, as the query type select does, so the target is required again.
098d733 to
c620a18
Compare
WilhelmZA
left a comment
There was a problem hiding this comment.
Reviewed at c620a18c.
This head is the same seven commits rebased onto main, with no change to the patches I approved at 098d733e, and every commit is verified. CI gate fails on it, and the cause is on main, not in this PR.
hyperglass/cli/tests/test_healthcheck.py:18(from #524) buildsCliRunner(mix_stderr=False). #535 moved the lock to Click 8.5.1, whereCliRunnerno longer acceptsmix_stderr, so both backend test jobs stop at collection withTypeError: CliRunner.__init__() got an unexpected keyword argument 'mix_stderr'. The #535 branch predated #524, so its own CI never ran that file.mainhas failed CI sinceabc5d0d0.
Once main is fixed and this PR is rebased onto it with CI gate passing, I will confirm the approval on that head.
Stacked on #537 (#512, #514); review that first. This PR is the commits after it.
Problem
Three form-state issues left out of #537:
form.queryTypeandselections.queryType, so the select still showed it. A query type change clears the target and variables, but a location change kept those too.locationChangechose it (since fix(ui): submit the only available query type and reject an empty target #537) but kept the target, while a manual query type change clears it. A target entered for BGP Route could carry over to Ping.reset()from the header title cleared the store and, through fix(ui): submit the only available query type and reject an empty target #537's mirror, react-hook-form's values, but not its errors. A test confirms the errors stay: an empty submit followed by a reset leaves "Location must be specified." on screen, and a target error from before the reset comes back on the next query once a query type is selected.Change
Three commits, one per problem.
1. Clear a query type the selected locations don't offer (
hooks/use-form-state.ts)locationChangereads the current directive before it replacesfiltered.types, then:form.queryType,selections.queryType,form.queryTarget,form.queryVariablesandtarget.display. That matches whathandleChangeclears on a manual query type change. Removing every location and a selection with nothing in common go through the same path. fix(ui): submit the only available query type and reject an empty target #537's mirror passes the cleared values to react-hook-form."Offered" matches by
idfirst, then byname. Directives are intersected bynameand the offered IDs are the first location's, so the same query type can come back under another ID. With Router One (Juniper) and Router Three (FRR) selected, BGP Route has the Juniper ID; with Router One removed it has the FRR ID. Matching only by ID would clear a query type the select still offers under the same name, and keeping the old ID fails validation (inside(queryTypes)). The query type follows the new ID. It keeps only what the new directive offers: variables it defines with a value it offers (asDirective.validate_variableschecks), a select target only if every value is among the new options, and a text target only if the field type matches. A text target stays because it is shown and the backend checks it against the new directive's rules (resolve_directive_by_name). For the builtin directives, which differ only by vendor, that keeps the target and variables. Two custom directives can share a name and nothing else, and then values the new one doesn't offer are dropped instead of being submitted unseen. While several locations stay selected, the same values go to each location's directive of that name, so same-named custom directives must also share their variables and variable options.2. Clear the target when the only query type is chosen for you (
hooks/use-form-state.ts)When
locationChangechooses the only query type, it also clearsqueryTargetandtarget.display. That branch only runs when the current query type is not offered (point 1 keeps it otherwise), so the chosen type always differs from the current one. A target is never cleared when the chosen type is the current one, and its variables stay too; before, the variables were cleared even then.A directive without a field type still gets its
['null']target: the form'sdirectivememo sets it after the query type changes, as before.3. Clear validation errors when the form is reset (
components/looking-glass-form.tsx)The mirror effect from #537 now checks for the empty values the store starts with and returns to on
reset(). When it sees them, it calls react-hook-form'sreset(values)instead ofsetValue.resetrather thanclearErrors: both pass the new tests.resetalso clears react-hook-form's submitted state, which is whatSubmitButton.handleClosedoes when it calls react-hook-form'sreset()next to the store's. After a reset the form is a fresh form, not a submitted form with no errors.FormProviderand has no access to react-hook-form. The mirror is already where the store's changes reach react-hook-form.formobject by reference (useFormState(s => s.form)). The existingformcomes from a deep-equal selector, so a reset after an empty submit (values already empty) re-rendered nothing and the effect did not run. That is the most likely way to have errors on screen when clicking the title.locationChangealready clears the location error.Tests
New in
hooks/use-form-state.test.ts(locationChange):New in
components/looking-glass-form.test.tsx:IPv6 targets are used in the store tests and the by-name form test. No existing assertion changed.
With #537's code (the commit before this PR) and the new store and form tests:
and the two reset tests:
Reverting one part of the fix at a time on the final code:
"keeps the target when the only query type in common is the current one" and "keeps a query type the new locations still offer" pass before and after; they guard the cases the fix must not change.
With all three fixes:
biome format,biome lintandtsc --noEmitare clean;Test Files 25 passed (25),Tests 217 passed (217).Added after review:
hooks/use-form-state.test.ts: variables filtered by id and offered value; a select target kept or cleared by option; a field type change clears the target.components/looking-glass-form.test.tsx: the review scenario (two same-named custom directives with different variables, remove the first location, submit carries no stale variable); a select target the remaining location doesn't offer is cleared and no longer shown; clearing the target select removes the target; removing every location clears the previous query's errors.components/query-target.tsx: the target select is controlled from the store, so a target the store clears is no longer shown as selected, and its clear control clears the store.Final run:
biome format,biome lintandtsc --noEmitclean;Test Files 25 passed (25),Tests 225 passed (225).Changelog
Notes
Found but not changed here:
LocationCard(components/location-card.tsx) keeps its checked state in a localuseState(defaultChecked). After a reset from the title while the form is mounted, a card that was selected still looks selected. The next click on it sendsremovefor a location the store no longer has, which does nothing, so the user has to click twice. The second reset test picks another location for that reason.Closes #540