-
Notifications
You must be signed in to change notification settings - Fork 28
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
UrlSync: Variable changes adds browser history steps #882
base: main
Are you sure you want to change the base?
Changes from all commits
c86f4ae
7a719df
e7349fd
c8320d0
0e494cf
3366a29
7282073
720b397
6b60643
485e9e1
24bf346
fa16aa0
310eb6a
f019c92
672717b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ export abstract class MultiValueVariable<TState extends MultiValueVariableState | |
extends SceneObjectBase<TState> | ||
implements SceneVariable<TState> | ||
{ | ||
protected _urlSync: SceneObjectUrlSyncHandler = new MultiValueUrlSyncHandler(this); | ||
protected _urlSync: MultiValueUrlSyncHandler<TState> = new MultiValueUrlSyncHandler(this); | ||
|
||
/** | ||
* Set to true to skip next value validation to maintain the current value even it it's not among the options (ie valid values) | ||
|
@@ -260,7 +260,7 @@ export abstract class MultiValueVariable<TState extends MultiValueVariableState | |
/** | ||
* Change the value and publish SceneVariableValueChangedEvent event. | ||
*/ | ||
public changeValueTo(value: VariableValue, text?: VariableValue) { | ||
public changeValueTo(value: VariableValue, text?: VariableValue, isUserAction = false) { | ||
// Ignore if there is no change | ||
if (value === this.state.value && text === this.state.text) { | ||
return; | ||
|
@@ -301,7 +301,18 @@ export abstract class MultiValueVariable<TState extends MultiValueVariableState | |
return; | ||
} | ||
|
||
this.setStateHelper({ value, text, loading: false }); | ||
const stateChangeAction = () => this.setStateHelper({ value, text, loading: false }); | ||
|
||
/** | ||
* Because variable state changes can cause a whole chain of downstream state changes in other variables (that also cause URL update) | ||
* Only some variable changes should add new history items to make sure the browser history contains valid URL states to go back to. | ||
*/ | ||
if (isUserAction) { | ||
this._urlSync.performBrowserHistoryAction(stateChangeAction); | ||
} else { | ||
stateChangeAction(); | ||
} | ||
|
||
this.publishEvent(new SceneVariableValueChangedEvent(this), true); | ||
} | ||
|
||
|
@@ -386,6 +397,8 @@ function findOptionMatchingCurrent( | |
export class MultiValueUrlSyncHandler<TState extends MultiValueVariableState = MultiValueVariableState> | ||
implements SceneObjectUrlSyncHandler | ||
{ | ||
private _nextChangeShouldAddHistoryStep = false; | ||
|
||
public constructor(private _sceneObject: MultiValueVariable<TState>) {} | ||
|
||
private getKey(): string { | ||
|
@@ -446,6 +459,16 @@ export class MultiValueUrlSyncHandler<TState extends MultiValueVariableState = M | |
this._sceneObject.changeValueTo(urlValue); | ||
} | ||
} | ||
|
||
public performBrowserHistoryAction(callback: () => void) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! 🎉 Do you think it could be the default behavior of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea, probably. pushed an update to #878 that adds it to SceneObjectUrlSyncConfig |
||
this._nextChangeShouldAddHistoryStep = true; | ||
callback(); | ||
this._nextChangeShouldAddHistoryStep = false; | ||
} | ||
|
||
public shouldCreateHistoryStep(values: SceneObjectUrlValues): boolean { | ||
return this._nextChangeShouldAddHistoryStep; | ||
} | ||
} | ||
|
||
function handleLegacyUrlAllValue(value: string | string[]) { | ||
|
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.
Would it make sense to add an extra method to avoid repeatedly calling the
changeValueTo
method withundefined
andtrue
?For example:
It's just some syntactic sugar, but will make it easier to read in the plugin code. Alternatively each plugin can also implement their own util to handle this
changeVariablePushToHistory(variable, value)
if we don't want to add it to the library.