-
Notifications
You must be signed in to change notification settings - Fork 15
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
GroupByVariable: Sync label to URL #705
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f3ac536
GroupByVariable: Sync label to URL
bfmatei d22490a
Unescape delimiters
bfmatei 906b6a0
Fix typechecking
bfmatei e284000
Fix typechecking
bfmatei d2b74a9
Add tests
bfmatei 91edd05
Remove urlOptions
bfmatei c861f37
Fix typechecking
bfmatei File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/scenes/src/variables/groupby/GroupByVariableUrlSyncHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { SceneObjectUrlSyncHandler, SceneObjectUrlValues } from '../../core/types'; | ||
import { GroupByVariable } from './GroupByVariable'; | ||
import { toUrlCommaDelimitedString, unescapeUrlDelimiters } from '../utils'; | ||
import { VariableValue } from '../types'; | ||
|
||
export class GroupByVariableUrlSyncHandler implements SceneObjectUrlSyncHandler { | ||
public constructor(private _sceneObject: GroupByVariable) {} | ||
|
||
private getKey(): string { | ||
return `var-${this._sceneObject.state.name}`; | ||
} | ||
|
||
public getKeys(): string[] { | ||
if (this._sceneObject.state.skipUrlSync) { | ||
return []; | ||
} | ||
|
||
return [this.getKey()]; | ||
} | ||
|
||
public getUrlState(): SceneObjectUrlValues { | ||
if (this._sceneObject.state.skipUrlSync) { | ||
return {}; | ||
} | ||
|
||
return { [this.getKey()]: toUrlValues(this._sceneObject.state.value, this._sceneObject.state.text) }; | ||
} | ||
|
||
public updateFromUrl(values: SceneObjectUrlValues): void { | ||
let urlValue = values[this.getKey()]; | ||
|
||
if (urlValue != null) { | ||
/** | ||
* Initial URL Sync happens before scene objects are activated. | ||
* We need to skip validation in this case to make sure values set via URL are maintained. | ||
*/ | ||
if (!this._sceneObject.isActive) { | ||
this._sceneObject.skipNextValidation = true; | ||
} | ||
|
||
const { values, texts } = fromUrlValues(urlValue); | ||
|
||
this._sceneObject.changeValueTo(values, texts); | ||
} | ||
} | ||
} | ||
|
||
function toUrlValues(values: VariableValue, texts: VariableValue): string[] { | ||
values = Array.isArray(values) ? values : [values]; | ||
texts = Array.isArray(texts) ? texts : [texts]; | ||
|
||
return values.map((value, idx) => { | ||
if (value === undefined || value === null) { | ||
return ''; | ||
} | ||
|
||
value = String(value); | ||
|
||
let text = texts[idx]; | ||
text = text === undefined || text === null ? value : String(text); | ||
|
||
return toUrlCommaDelimitedString(value, text); | ||
}); | ||
} | ||
|
||
function fromUrlValues(urlValues: string | string[]): { values: string[]; texts: string[] } { | ||
urlValues = Array.isArray(urlValues) ? urlValues : [urlValues]; | ||
|
||
return urlValues.reduce<{ values: string[]; texts: string[] }>( | ||
(acc, urlValue) => { | ||
const [value, label] = (urlValue ?? '').split(','); | ||
|
||
acc.values.push(unescapeUrlDelimiters(value)); | ||
acc.texts.push(unescapeUrlDelimiters(label ?? value)); | ||
|
||
return acc; | ||
}, | ||
{ | ||
values: [], | ||
texts: [], | ||
} | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can reuse MultiValueUrlSyncHandler but with an option to sync label? since they are so close to identical
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I can merge them into one. I'll implement it in a subsequent commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started working on this but to be honest it kinda makes it weird to bring
urlOptions
to multi-value variable. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bfmatei I think the whole concept of urlOptions is strange, need to figure out a way we can avoid it.
the URL state should only be concerned with setting the current value/text not the options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushing an update without it now.