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

MultiValueVariable: Fix issue where url update would not take options into account #870

Closed
wants to merge 1 commit into from

Conversation

mdvictor
Copy link
Collaborator

@mdvictor mdvictor commented Aug 14, 2024

This PR fixes an issue where in a variable with key-value pairs(esc), setting the key in the url param would not update to it's value but instead create a new custom option with the key as a value.

Before:

Screen.Recording.2024-08-14.at.11.38.59.mov

After:

Screen.Recording.2024-08-14.at.11.39.41.mov

@mdvictor mdvictor marked this pull request as ready for review August 14, 2024 08:44
@mdvictor mdvictor requested review from torkelo and dprokop and removed request for torkelo August 14, 2024 08:44
query: 'A : 1,B : 2',
});

await variable.urlSync?.updateFromUrl({ ['var-test']: 'B' });
Copy link
Member

Choose a reason for hiding this comment

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

url should not have the text / display value, it has the the value part I think

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the scenario where the issue stemmed from. In the old arch you can set the key/text/display value and it will use it's correct value, but the URL shows the key. So we need something like this for backwards comp.

Copy link
Member

@torkelo torkelo left a comment

Choose a reason for hiding this comment

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

I think this is likely a good solution, hm... wonder how it works in the old arch, maybe it works because URL sync happens so late, after variable has been fully initiated and options fetched/built

@@ -419,6 +419,23 @@ export class MultiValueUrlSyncHandler<TState extends MultiValueVariableState = M
this._sceneObject.skipNextValidation = true;
}

if (!this._sceneObject.state.options || !this._sceneObject.state.options.length) {
const options = await lastValueFrom(this._sceneObject.getValueOptions({}));
Copy link
Member

Choose a reason for hiding this comment

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

could this lead to double queries being issued for QueryVariables ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it will :/ once here and once on activation itself

Copy link
Member

Choose a reason for hiding this comment

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

that will not do :(

Copy link
Member

Choose a reason for hiding this comment

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

the variable will be validated later (when SceneVariableSet calls validateAndUpdate), can't we do this lookup at that point?

@axelavargas axelavargas self-requested a review August 14, 2024 09:12
@mdvictor
Copy link
Collaborator Author

@torkelo As far as I can tell, we also load the options before setting the value from the URL in the old arch here, here

@torkelo
Copy link
Member

torkelo commented Aug 16, 2024

@mdvictor not sure I understand the issue, looked again, I thought it was related to setting the correct key in the select after a full page reload, b

but see now that it's about setting the variable to the display value via URL. Both the new and old variable system sets the variable value via url using the value not the text representation

@torkelo
Copy link
Member

torkelo commented Aug 16, 2024

wonder where/why they have urls with the text representation, grafana only generates URLs with the value

@torkelo
Copy link
Member

torkelo commented Aug 16, 2024

@mdvictor so the problem is this if statement here: https://github.com/grafana/scenes/blob/main/packages/scenes/src/variables/variants/MultiValueVariable.ts#L196

that code there makes sure you can add a custom value (enter anything) in the select, that value get's set in the URL and if you reload the page we need to preserve that custom value even if it's not among the options returned by the query (or specified in the variable csv string).

We might be able to modify that logic a bit and ignore the if statement if current value === stateUpdate.text

like this

if (
      this.skipNextValidation &&
      stateUpdate.value !== this.state.value &&
      stateUpdate.text !== this.state.text &&
      !isAllValueFix
    ) {
      stateUpdate.value = this.state.value;
      stateUpdate.text = this.state.text;
    }

@mdvictor
Copy link
Collaborator Author

Closing this in favor of #874

@mdvictor mdvictor closed this Aug 19, 2024
@mdvictor
Copy link
Collaborator Author

wonder where/why they have urls with the text representation, grafana only generates URLs with the value

I don't know hey that happened either

@mdvictor so the problem is this if statement here: https://github.com/grafana/scenes/blob/main/packages/scenes/src/variables/variants/MultiValueVariable.ts#L196

that code there makes sure you can add a custom value (enter anything) in the select, that value get's set in the URL and if you reload the page we need to preserve that custom value even if it's not among the options returned by the query (or specified in the variable csv string).

We might be able to modify that logic a bit and ignore the if statement if current value === stateUpdate.text

like this

if (
      this.skipNextValidation &&
      stateUpdate.value !== this.state.value &&
      stateUpdate.text !== this.state.text &&
      !isAllValueFix
    ) {
      stateUpdate.value = this.state.value;
      stateUpdate.text = this.state.text;
    }

Thank you! Indeed that fixes the problem, I've opened a new PR with these changes and closed this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants