Skip to content

Commit

Permalink
Update to latest ScopesContext
Browse files Browse the repository at this point in the history
  • Loading branch information
bfmatei committed Dec 9, 2024
1 parent 26f1979 commit c3a4b47
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions packages/scenes/src/core/ScopesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,47 @@ import { Observable } from 'rxjs';

import { Scope } from '@grafana/data';

export interface ScopesContextValueState {
drawerOpened: boolean;
enabled: boolean;
loading: boolean;
readOnly: boolean;
value: Scope[];
}

export interface ScopesContextValue {
state: {
isEnabled: boolean;
isLoading: boolean;
isReadOnly: boolean;
value: Scope[];
};
/**
* Current state.
*/
state: ScopesContextValueState;

/**
* Observable that emits the current state.
*/
stateObservable: Observable<ScopesContextValue['state']>;
changeScopes: (scopeNames: string[]) => void;
enterReadOnly: () => void;
exitReadOnly: () => void;
enable: () => void;
disable: () => void;

/**
* Change the selected scopes. The service takes care about loading them and propagating the changes.
* @param scopeNames
*/
changeScopes(scopeNames: string[]): void;

/**
* Set read-only mode.
* If `readOnly` is `true`, the selector will be set to read-only and the dashboards panel will be closed.
*/
setReadOnly(readOnly: boolean): void;

/**
* Enable or disable the usage of scopes.
* This will hide the selector and the dashboards panel, and it will stop propagating the scopes to the query object.
*/
setEnabled(enabled: boolean): void;
}

export const ScopesContext = createContext<ScopesContextValue | undefined>(undefined);

export function useScopes() {
export function useScopes(): ScopesContextValue | undefined {
const context = useContext(ScopesContext);

useObservable(context?.stateObservable ?? new Observable(), context?.state);
Expand All @@ -31,10 +54,8 @@ export function useScopes() {
state: context.state,
stateObservable: context.stateObservable,
changeScopes: context.changeScopes,
enterReadOnly: context.enterReadOnly,
exitReadOnly: context.exitReadOnly,
enable: context.enable,
disable: context.disable,
setReadOnly: context.setReadOnly,
setEnabled: context.setEnabled,
}
: undefined;
}

0 comments on commit c3a4b47

Please sign in to comment.