Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryVariable: Default to first option when none is set #36521

Merged
merged 3 commits into from
Jul 9, 2021

Conversation

tskarhed
Copy link
Contributor

@tskarhed tskarhed commented Jul 7, 2021

What this PR does / why we need it:
To make sure that query variables do not break when the current dashboard is navigated to via an anchor tag.

Which issue(s) this PR fixes:
Fixes #35471

Special notes for your reviewer:

  1. Create query variable with onDashboardLoad
  2. Create a panel link that leads to /d/${__dashboard.uid}
  3. Select a non default value for the query variable
  4. Click the panel link
  5. The query variables should now be set to default values as opposed to nothing

@tskarhed tskarhed requested review from mckn, a team and thisisobate and removed request for a team July 7, 2021 14:52
@tskarhed tskarhed changed the title TemplateVariables: Default to first option when none is set QueryVariable: Default to first option when none is set Jul 7, 2021
@tskarhed tskarhed added this to the 8.1.0 milestone Jul 7, 2021
@tskarhed tskarhed requested a review from torkelo July 8, 2021 08:35
Comment on lines 126 to 130
instanceState.current = { ...instanceState.options[0], selected: true };
instanceState.options = instanceState.options.map((option, index) =>
index === 0 ? instanceState.current : { ...option, selected: false }
);
return;
Copy link
Member

Choose a reason for hiding this comment

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

what happens if options are an empty array? will this crash?

Since this handler can be written mutably and immer will make it immutable. I think you first can just set all options to selected false except first and set instance.current = instance.options[0] ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we need to add a check to make sure we have options.

Copy link
Contributor

@mckn mckn left a comment

Choose a reason for hiding this comment

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

Looks good! Just need to add the check that @torkelo mentioned in his comment.

// If no value is set, default to the first avilable
if (!current.value && instanceState.options.length) {
instanceState.options.forEach((option, index) => {
option.selected = !Boolean(index);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quite enjoyed this fancy little trick 😎

@tskarhed tskarhed requested review from torkelo and mckn July 9, 2021 09:37
@tskarhed tskarhed enabled auto-merge (squash) July 9, 2021 09:54
@tskarhed tskarhed merged commit 5690358 into grafana:main Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template Variable Defaults Blanked When Using Data Links Back to Same Dashboard
4 participants