Skip to content

fix(ui): keep the query form consistent after location changes and resets - #545

Merged
jsenecal merged 7 commits into
mainfrom
fix/540-form-state
Nov 21, 2026
Merged

jsenecal merged 7 commits into
mainfrom
fix/540-form-state

Conversation

@jsenecal

@jsenecal jsenecal commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #537 (#512, #514); review that first. This PR is the commits after it.

Problem

Three form-state issues left out of #537:

  1. A location change that removed the current query type kept it in form.queryType and selections.queryType, so the select still showed it. A query type change clears the target and variables, but a location change kept those too.
  2. When the selected locations had one query type in common, locationChange chose 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.
  3. 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)

locationChange reads the current directive before it replaces filtered.types, then:

  • If the new locations offer it, the query type stays. Under the same ID it keeps its target and variables; under another ID, only what that directive offers (below).
  • Otherwise, if one query type is in common, it is chosen (as before).
  • Otherwise it clears form.queryType, selections.queryType, form.queryTarget, form.queryVariables and target.display. That matches what handleChange clears 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 id first, then by name. Directives are intersected by name and 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 (as Directive.validate_variables checks), 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 locationChange chooses the only query type, it also clears queryTarget and target.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's directive memo 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's reset(values) instead of setValue.

  • reset rather than clearErrors: both pass the new tests. reset also clears react-hook-form's submitted state, which is what SubmitButton.handleClose does when it calls react-hook-form's reset() next to the store's. After a reset the form is a fresh form, not a submitted form with no errors.
  • It is in the mirror rather than at the call site because the header title is outside the FormProvider and has no access to react-hook-form. The mirror is already where the store's changes reach react-hook-form.
  • The mirror now reads the store's form object by reference (useFormState(s => s.form)). The existing form comes 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.
  • Removing every location also brings the values back to empty, now that point 1 clears the query type and target, so it resets react-hook-form too. That clears only errors for fields that are hidden at that point: locationChange already clears the location error.

Tests

New in hooks/use-form-state.test.ts (locationChange):

  • clears the target when it chooses a different query type
  • keeps the target when the only query type in common is the current one
  • clears a query type the new locations do not offer, with its target and variables
  • clears the query type when every location is removed
  • keeps a query type the new locations still offer, with its target and variables
  • moves the query type to the same-named directive of the remaining location

New in components/looking-glass-form.test.tsx:

  • clears a query type the newly selected locations do not offer (the select no longer shows it; a submit reports the missing query type)
  • keeps the query type and target when the remaining location offers it by name (submits an IPv6 target to Router Three with the FRR ID)
  • clears the errors of an empty submit when the form is reset
  • does not show the errors from before a reset on the next query

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:

       × clears the target when it chooses a different query type 12ms
       × clears a query type the new locations do not offer, with its target and variables 1ms
       × clears the query type when every location is removed 1ms
       × moves the query type to the same-named directive of the remaining location 3ms
     × clears a query type the newly selected locations do not offer 333ms
     × keeps the query type and target when the remaining location offers it by name 1594ms
      Tests  6 failed | 51 passed (57)

and the two reset tests:

     × clears the errors of an empty submit when the form is reset 1092ms
     × does not show the errors from before a reset on the next query 435ms
      Tests  2 failed | 27 passed (29)

Reverting one part of the fix at a time on the final code:

== no target clearing on the automatic choice (2)
       × clears the target when it chooses a different query type 22ms
      Tests  1 failed | 216 passed (217)
== no clearing of a query type that isn't offered (1)
       × clears a query type the new locations do not offer, with its target and variables 12ms
       × clears the query type when every location is removed 2ms
     × clears a query type the newly selected locations do not offer 286ms
      Tests  3 failed | 214 passed (217)
== no match by name (1)
       × moves the query type to the same-named directive of the remaining location 12ms
     × keeps the query type and target when the remaining location offers it by name 1497ms
      Tests  2 failed | 215 passed (217)
== reset branch, but the mirror on the deep-compared `form` (3)
     × clears the errors of an empty submit when the form is reset 1097ms
      Tests  1 failed | 28 passed (29)

"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 lint and tsc --noEmit are 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 lint and tsc --noEmit clean; 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 local useState(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 sends remove for 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

@jsenecal jsenecal added bug Confirmed bug or unexpected behavior frontend Next.js UI and the runtime UI build labels Nov 21, 2026
@jsenecal jsenecal self-assigned this Nov 21, 2026
@jsenecal jsenecal added the frontend Next.js UI and the runtime UI build label Nov 21, 2026
@jsenecal
jsenecal requested a review from WilhelmZA November 21, 2026 05:25
@coveralls

coveralls commented Nov 21, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 86.194% (+0.04%) from 86.156% — fix/540-form-state into main

@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 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, queryTarget and queryVariables stay as they are. queryVariables is 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 keep queryVariables only when kept.id === previous.id, or keep only the keys that kept.variables defines. If that configuration is not meant to be supported, please say so in the comment.
  • The base is not main, so Closes #540 does not take effect on this merge. Please retarget this PR to main after #537 merges, or close #540 by hand once the fix is on main.

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.md is not edited. #567 is stacked on this branch and does edit it, so the entries could go there or in the release PR.

@WilhelmZA WilhelmZA added the reviewing Issue is being reviewed by maintainers label Nov 21, 2026
@jsenecal

Copy link
Copy Markdown
Collaborator Author

Thanks, good catch. I reproduced it as a form test before fixing it.

  • Same name, other id: when the kept query type moves to a directive with the same name and another id, locationChange keeps only the variables that directive defines, with a value it offers (as Directive.validate_variables checks). It keeps a select target only if every value is among the new options, and keeps 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 rules. Clearing everything on any id change would undo the multi-vendor case this PR fixes (9292178).
  • Display: the target select now shows the store's value, so a cleared target no longer looks selected, and its clear control now clears the store as well (66d2e16, moved here from feat(ui): fill in the query form from URL parameters #567).
  • Multi-location limit: while both locations stay selected, the same values go to each location's directive of that name. Custom directives that share a name must therefore also share their variables and options. The comment says so (098d733).
  • Reset test: added the form-level test for removing every location (fceeab0).
  • Base: mergepr.sh retargets this PR to main after fix(ui): submit the only available query type and reject an empty target #537 merges, once main plus this PR matches the reviewed diff, so Closes #540 takes effect.
  • CHANGELOG: in the release PR.

@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 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.

@jsenecal

Copy link
Copy Markdown
Collaborator Author

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 use-form-state.ts (#567 is stacked on this branch).

Base automatically changed from fix/512-514-form-validation to main November 21, 2026 14:14
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.

@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 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) builds CliRunner(mix_stderr=False). #535 moved the lock to Click 8.5.1, where CliRunner no longer accepts mix_stderr, so both backend test jobs stop at collection with TypeError: CliRunner.__init__() got an unexpected keyword argument 'mix_stderr'. The #535 branch predated #524, so its own CI never ran that file. main has failed CI since abc5d0d0.

Once main is fixed and this PR is rebased onto it with CI gate passing, I will confirm the approval on that head.

@jsenecal

Copy link
Copy Markdown
Collaborator Author

Thanks. #571 fixes main. One correction on the cause: Typer 0.29 from #535 ships its own typer.testing.CliRunner with the signature (charset, env), and that is what rejects mix_stderr. Once #571 merges I will refresh this PR so CI reruns on the fixed main.

@jsenecal jsenecal closed this Nov 21, 2026
@jsenecal jsenecal reopened this Nov 21, 2026
@jsenecal
jsenecal merged commit 07fb9b5 into main Nov 21, 2026
25 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Confirmed bug or unexpected behavior frontend Next.js UI and the runtime UI build reviewing Issue is being reviewed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Form state: stale query type after a location change, target kept on auto-select, errors kept after reset

3 participants