-
Notifications
You must be signed in to change notification settings - Fork 27
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
Scopes: Add ScopesBridge
object
#990
Open
bfmatei
wants to merge
11
commits into
main
Choose a base branch
from
bogdan/react-contexts-bindings-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3348dd5
ReactContexts: PoC
bfmatei d9be47d
Remove unused `useContext` hook
bfmatei 4fe7683
Refactor
bfmatei fc6363a
Refactor
bfmatei f5c0b23
Refactor
bfmatei ad7c523
Refactor
bfmatei 26f1979
Remove complexity
bfmatei c3a4b47
Update to latest `ScopesContext`
bfmatei 691e059
Refactors
bfmatei 492764b
Fix tests
bfmatei 6412637
Fix typecheck
bfmatei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,126 @@ | ||
import { isEqual } from 'lodash'; | ||
import { useEffect } from 'react'; | ||
import { BehaviorSubject, filter, map, Observable, pairwise, Unsubscribable } from 'rxjs'; | ||
|
||
import { Scope } from '@grafana/data'; | ||
// @ts-expect-error: TODO: Fix this once new runtime package is released | ||
import { ScopesContextValue, useScopes } from '@grafana/runtime'; | ||
|
||
import { SceneObjectBase } from './SceneObjectBase'; | ||
import { SceneComponentProps, SceneObjectUrlValues, SceneObjectWithUrlSync } from './types'; | ||
import { SceneObjectUrlSyncConfig } from '../services/SceneObjectUrlSyncConfig'; | ||
|
||
export class SceneScopesBridge extends SceneObjectBase implements SceneObjectWithUrlSync { | ||
static Component = SceneScopesBridgeRenderer; | ||
|
||
protected _urlSync = new SceneObjectUrlSyncConfig(this, { keys: ['scopes'] }); | ||
|
||
protected _renderBeforeActivation = true; | ||
|
||
private _contextSubject = new BehaviorSubject<ScopesContextValue | undefined>(undefined); | ||
|
||
private _pendingScopes: string[] | null = null; | ||
|
||
public getUrlState(): SceneObjectUrlValues { | ||
return { | ||
scopes: this._pendingScopes ?? (this.context?.state.value ?? []).map((scope: Scope) => scope.metadata.name), | ||
}; | ||
} | ||
|
||
public updateFromUrl(values: SceneObjectUrlValues) { | ||
let scopes = values['scopes'] ?? []; | ||
scopes = (Array.isArray(scopes) ? scopes : [scopes]).map(String); | ||
|
||
if (!this.context) { | ||
this._pendingScopes = scopes; | ||
return; | ||
} | ||
|
||
this.context?.changeScopes(scopes); | ||
} | ||
|
||
public getValue(): Scope[] { | ||
return this.context?.state.value ?? []; | ||
} | ||
|
||
public subscribeToValue(cb: (newScopes: Scope[], prevScopes: Scope[]) => void): Unsubscribable { | ||
return this.contextObservable | ||
.pipe( | ||
filter((context) => !!context && !context.state.loading), | ||
pairwise(), | ||
map( | ||
([prevContext, newContext]) => | ||
[prevContext?.state.value ?? [], newContext?.state.value ?? []] as [Scope[], Scope[]] | ||
), | ||
filter(([prevScopes, newScopes]) => !isEqual(prevScopes, newScopes)) | ||
) | ||
.subscribe(([prevScopes, newScopes]) => { | ||
cb(newScopes, prevScopes); | ||
}); | ||
} | ||
|
||
public isLoading(): boolean { | ||
return this.context?.state.loading ?? false; | ||
} | ||
|
||
public subscribeToLoading(cb: (loading: boolean) => void): Unsubscribable { | ||
return this.contextObservable | ||
.pipe( | ||
filter((context) => !!context), | ||
pairwise(), | ||
map( | ||
([prevContext, newContext]) => | ||
[prevContext?.state.loading ?? false, newContext?.state.loading ?? false] as [boolean, boolean] | ||
), | ||
filter(([prevLoading, newLoading]) => prevLoading !== newLoading) | ||
) | ||
.subscribe(([_prevLoading, newLoading]) => { | ||
cb(newLoading); | ||
}); | ||
} | ||
|
||
public setEnabled(enabled: boolean) { | ||
this.context?.setEnabled(enabled); | ||
} | ||
|
||
public setReadOnly(readOnly: boolean) { | ||
this.context?.setReadOnly(readOnly); | ||
} | ||
|
||
public updateContext(newContext: ScopesContextValue | undefined) { | ||
if (this._pendingScopes && newContext) { | ||
setTimeout(() => { | ||
newContext?.changeScopes(this._pendingScopes!); | ||
this._pendingScopes = null; | ||
}); | ||
} | ||
|
||
if (this.context !== newContext || this.context?.state !== newContext?.state) { | ||
const shouldUpdate = this.context?.state.value !== newContext?.state.value; | ||
|
||
this._contextSubject.next(newContext); | ||
|
||
if (shouldUpdate) { | ||
this.forceRender(); | ||
} | ||
} | ||
} | ||
|
||
private get context(): ScopesContextValue | undefined { | ||
return this._contextSubject.getValue(); | ||
} | ||
|
||
private get contextObservable(): Observable<ScopesContextValue | undefined> { | ||
return this._contextSubject.asObservable(); | ||
} | ||
} | ||
|
||
function SceneScopesBridgeRenderer({ model }: SceneComponentProps<SceneScopesBridge>) { | ||
const context = useScopes(); | ||
|
||
useEffect(() => { | ||
model.updateContext(context); | ||
}, [context, model]); | ||
|
||
return null; | ||
} |
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
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.
do we really need this as a page level prop?
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'm thinking that some pages might not want scopes to be shown for various reasons 🤔