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

Fix template repeat for custom panels #188

Merged
merged 4 commits into from
May 8, 2023
Merged

Conversation

hwandersman
Copy link
Collaborator

@hwandersman hwandersman commented Apr 27, 2023

What this PR does / why we need it:
When a dashboard has a multi-value template variable in a row that repeats for each value of the variable, each value was not set correctly in a repeated Video Player panel.

The Scene Viewer panel has different types of display options. 3 of these options are for setting the values of certain variables based on a selection in the scene. On load the value of the variables are used to initialize the selection, but it should only be resolved from the URL values. Grafana gives users the ability to save default values which cause issues for the Scene Viewer when deselecting items in the scene.
1 option is for setting the default active camera which can be resolved by the URL or default dashboard value since it cannot be deselected.

Fixed by iwysiu

Which issue(s) this PR fixes:

Fixes #175
Fixes #109

Special notes for your reviewer:

@github-actions
Copy link

github-actions bot commented Apr 27, 2023

Levitate is-compatible report:

🔍 Resolving @grafana/data@latest...
🔍 Resolving @grafana/ui@latest...
🔍 Resolving @grafana/runtime@latest...
🔍 Resolving @grafana/e2e-selectors@latest...

🔬 Checking compatibility between ./src/module.ts and @grafana/data@9.5.1...
✔ Found @grafana/data version 8.4.11 locally

🔬 Checking compatibility between ./src/module.ts and @grafana/ui@9.5.1...
✔ Found @grafana/ui version 8.4.11 locally

🔬 Checking compatibility between ./src/module.ts and @grafana/runtime@9.5.1...
✔ Found @grafana/runtime version 8.4.11 locally

🔬 Checking compatibility between ./src/module.ts and @grafana/e2e-selectors@9.5.1...
✔ Found @grafana/e2e-selectors version 8.4.11 locally

✔️ ./src/module.ts appears to be compatible with @grafana/data,@grafana/ui,@grafana/runtime,@grafana/e2e-selectors

…les for the customInputActiveCamera display option
@github-actions
Copy link

Backend code coverage report for PR #188
No changes

@github-actions
Copy link

Frontend code coverage report for PR #188

Plugin Main PR Difference
src 50.89% 51.21% .32%

Copy link
Contributor

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

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

I ran it and it seems to work! But a question about the logic

const tempVarName = getUrlTempVarName(displayOption || '');
const tempVarVal = checkTempVar(displayOption);
const tempVarURLVal = queryParams[tempVarName];
if (Array.isArray(tempVarURLVal) || tempVarVal !== tempVarURLVal || !(tempVarURLVal as string)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I could be misreading this, but if you're returning tempVarVal when tempVarVal !== tempVarURLVal aren't you functionally always returning tempVarVal?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right I need to reevaluate this

Copy link
Contributor

Choose a reason for hiding this comment

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

!(tempVarURLVal as string) can just be !tempVarURLVal

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated to just use the saved template var value tempVarVal as the default, otherwise check the URL for a value.

const tempVarName = getUrlTempVarName(displayOption || '');
const tempVarVal = checkTempVar(displayOption);
const tempVarURLVal = queryParams[tempVarName];
if (Array.isArray(tempVarURLVal) || tempVarVal !== tempVarURLVal || !(tempVarURLVal as string)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

!(tempVarURLVal as string) can just be !tempVarURLVal

const tempVarName = getUrlTempVarName(displayOption || '');
const tempVarVal = checkTempVar(displayOption);
const tempVarURLVal = queryParams[tempVarName];
if (Array.isArray(tempVarURLVal) || tempVarVal !== tempVarURLVal || !(tempVarURLVal as string)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

!(tempVarURLVal as string) can just be !tempVarURLVal

Comment on lines 93 to 95
entityId: entityId as string,
componentName: componentName as string,
kvsStreamName: kvsStreamName as string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can videoData handle these arguments if they're arrays or will it throw an error? Since getDisplayOptionValue returns a UrlQueryValue, it's possible it can also be one of these types string[] | number[] | boolean[]. But the VideoDataProps type expects only strings.

Some options for removing the type assertions from this file.

  1. Update getDisplayOptionValue to only ever return a string.
  2. Update videoData to support UrlQueryValue, and update the type for the displayOptions state like so (the type can be extracted instead of being defined inline if needed)
const [displayOptions, setDisplayOptions] = useState<{
    entityId: UrlQueryValue;
    componentName: UrlQueryValue;
    kvsStreamName: UrlQueryValue;
    search: UrlQueryValue;
    startTime: UrlQueryValue;
    endTime: UrlQueryValue;
  }>({
    entityId: '',
    componentName: '',
    kvsStreamName: '',
    search: search,
    startTime: '',
    endTime: '',
  });

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 believe UrlQueryValue can only be an array if the variable has a value and the string tempVarVal would be returned. The only way that UrlQueryValue is returned is if some label is in the URL that doesn't match an existing variable in a dashboard. This is possible through the TwinMaker plugin, but we don't support arrays.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated getDisplayOptionValue to return a string. If the type cast gets wonky then there's something wrong with the dashboard setup anyway.

@hwandersman hwandersman merged commit 6f3e6a0 into main May 8, 2023
5 checks passed
@hwandersman hwandersman deleted the fix-template-repeat branch May 8, 2023 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants