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

Add query variable in react-scenes #818

Merged
merged 19 commits into from
Aug 20, 2024
Merged

Conversation

mdvictor
Copy link
Collaborator

@mdvictor mdvictor commented Jul 5, 2024

Adds the Query Variable type to react-scenes

Depends on #827

@mdvictor mdvictor requested a review from torkelo July 5, 2024 07:28
@mdvictor mdvictor requested a review from oscarkilhed July 5, 2024 09:06
Base automatically changed from mdvictor/expand-custom-var to main July 5, 2024 09:51
@mdvictor mdvictor marked this pull request as draft July 8, 2024 08:10
<Stack direction="column">
<Stack>
<VariableControl name="job2" />
<Button variant="secondary" onClick={() => setHide(VariableHide.hideLabel)}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a lot of buttons here to toy around with state here but will ultimately remove most of them for the demo.

}

useEffect(() => {
if (variableAdded) {
Copy link
Collaborator Author

@mdvictor mdvictor Jul 8, 2024

Choose a reason for hiding this comment

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

@torkelo I'm not sure how to best tackle this or if it's something that we want. We need a way to refresh the variable when state changes, especially around value changes e.g: sort, regex which should trigger getValueOptions. I didn't find a way to easily do this other than re-adding the variable and letting it run it's activation, but that might not be the best approach. Simply setting the variable state won't work. Any ideas?

Copy link
Member

Choose a reason for hiding this comment

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

@mdvictor yea, we might need some updates to QueryVariable to be able to re-evaluate options when sort / regex changes or re-issue query when query changes

@mdvictor
Copy link
Collaborator Author

mdvictor commented Jul 9, 2024

Also, maybe we should add a prefix to these variables to delimit them from their scenes counterparts.

@torkelo
Copy link
Member

torkelo commented Jul 9, 2024

Also, maybe we should add a prefix to these variables to delimit them from their scenes counterparts.

@mdvictor it's own of the reasons we have separate package to allow some name conflicts, personally I really dislike the name conflicts, tried to get help with naming suggestions (around renaming or prefixing / suffixing) but did not get much feedback on options.

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.

Looks good!

}

useEffect(() => {
if (variableAdded) {
Copy link
Member

Choose a reason for hiding this comment

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

@mdvictor yea, we might need some updates to QueryVariable to be able to re-evaluate options when sort / regex changes or re-issue query when query changes

Comment on lines 55 to 67
variable?.setState({
label,
query,
datasource,
refresh,
sort,
regex,
value: initialValue,
isMulti,
hide,
includeAll,
})
}
Copy link
Member

Choose a reason for hiding this comment

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

better to handle this state update in a separate useEffect that has some deep quality checks as this will not always trigger remove and re-add whenever this re-renders unless parent is very careful to memorize all props.

see useQueryRunner for reference

@mdvictor mdvictor requested a review from torkelo July 10, 2024 10:14
}, [variable, scene, name]);

useEffect(() => {
variable?.setState({
Copy link
Member

Choose a reason for hiding this comment

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

need a check here so this is not run on first mount

Comment on lines 263 to 267
private _handleVariableUpdate(variable: SceneVariable) {
this._variablesToUpdate.add(variable);

this._updateNextBatch();
}
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to do this in a separate PR and not sure we can do it like this, not all state updates should cause a full update like this (would likely cause infinite update loops)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sounds good, I'll move this to another PR. Yeah, it's risky. I think it worked because of this but I might be missing something. In any case, I changed this to be called manually.

Comment on lines 161 to 165
private _updateOptionsOnStateChange = (newState: QueryVariableState, oldState: QueryVariableState) => {
if (!isEqual(pick(newState, this._stateChangePropsToCompare), pick(oldState, this._stateChangePropsToCompare))) {
this.publishEvent(new SceneVariableStateChangeEvent(this), true);
}
};
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to give consumer manual control over which state updates should trigger a re-eval of options

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree. Added a method in MultiValueVariable as it will need to be called in other variables as well (custom/datasource). Will make a new PR with these changes.

@@ -329,6 +330,10 @@ export abstract class MultiValueVariable<TState extends MultiValueVariableState
return options;
}

public refreshOptions() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added this func to update options after the state has changed.

@mdvictor mdvictor changed the base branch from main to mdvictor/variable-refresh-options July 11, 2024 07:48
@mdvictor mdvictor marked this pull request as ready for review July 11, 2024 07:51
@mdvictor mdvictor requested a review from dprokop July 12, 2024 10:29
@mdvictor mdvictor requested a review from torkelo August 14, 2024 09:50
Base automatically changed from mdvictor/variable-refresh-options to main August 14, 2024 09:59
Comment on lines +64 to +75
variable.setState({
label,
query,
datasource,
refresh,
sort,
regex,
hide,
includeAll,
});

variable.refreshOptions();
Copy link
Member

Choose a reason for hiding this comment

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

because query and data source are objects, I would do a deep equals diff here to make sure thing have actually changed

@mdvictor mdvictor requested a review from torkelo August 20, 2024 11:49
@mdvictor
Copy link
Collaborator Author

@torkelo added explicit comparison to check changes both here and in the datasource variable PR

@mdvictor mdvictor merged commit d9d4d4a into main Aug 20, 2024
3 checks passed
@mdvictor mdvictor deleted the mdvictor/add-query-variable branch August 20, 2024 13:25
@grafanabot
Copy link
Contributor

🚀 PR was released in v5.10.1 🚀

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

Successfully merging this pull request may close these issues.

3 participants