@grafana/scenes 6.15.0 → 6.16.0--canary.1134.15292037236.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,9 +60,11 @@ class ScopesVariable extends SceneObjectBase {
60
60
  const loading = state.value.length === 0 ? false : state.loading;
61
61
  const oldScopes = this.state.scopes.map((scope) => scope.metadata.name);
62
62
  const newScopes = state.value.map((scope) => scope.metadata.name);
63
- this.setState({ scopes: state.value, loading });
64
63
  if (!loading && !isEqual(oldScopes, newScopes)) {
64
+ this.setState({ scopes: state.value, loading });
65
65
  this.publishEvent(new SceneVariableValueChangedEvent(this), true);
66
+ } else {
67
+ this.setState({ loading });
66
68
  }
67
69
  }
68
70
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ScopesVariable.js","sources":["../../../../src/variables/variants/ScopesVariable.tsx"],"sourcesContent":["import { SceneObjectBase } from '../../core/SceneObjectBase';\nimport {\n CustomVariableValue,\n SceneVariable,\n SceneVariableState,\n SceneVariableValueChangedEvent,\n VariableValue,\n} from '../types';\nimport { Scope } from '@grafana/data';\nimport { SceneComponentProps } from '../../core/types';\nimport { ScopesContext, ScopesContextValue } from '@grafana/runtime';\nimport { useContext, useEffect } from 'react';\nimport { VariableFormatID, VariableHide } from '@grafana/schema';\nimport { SCOPES_VARIABLE_NAME } from '../constants';\nimport { isEqual } from 'lodash';\n\nexport interface ScopesVariableState extends SceneVariableState {\n /**\n * Last captured state from ScopesContext\n */\n scopes: Scope[];\n /**\n * Set to true if you want to the variable to enable / disable scopes when activated / deactivated\n */\n enable?: boolean;\n}\n\nexport class ScopesVariable extends SceneObjectBase<ScopesVariableState> implements SceneVariable<ScopesVariableState> {\n protected _renderBeforeActivation = true;\n protected _context: ScopesContextValue | undefined;\n\n // Special options that enables variables to be hidden but still render to access react contexts\n public UNSAFE_renderAsHidden = true;\n public static Component = ScopesVariableRenderer;\n\n public constructor(state: Partial<ScopesVariableState>) {\n super({\n skipUrlSync: true,\n loading: true,\n scopes: [],\n ...state,\n type: 'system',\n name: SCOPES_VARIABLE_NAME,\n hide: VariableHide.hideVariable,\n });\n }\n\n /**\n * Temporary simple implementation to stringify the scopes.\n */\n public getValue(): VariableValue {\n const scopes = this.state.scopes ?? [];\n return new ScopesVariableFormatter(scopes.map((scope) => scope.metadata.name));\n }\n\n public getScopes(): Scope[] | undefined {\n return this.state.scopes;\n }\n\n /**\n * This method is used to keep the context up to date with the scopes context received from React\n * 1) Subscribes to ScopesContext state changes and synchronizes it with the variable state\n * 2) Handles enable / disabling of scopes based on variable enable option.\n */\n public setContext(context: ScopesContextValue | undefined) {\n if (!context) {\n return;\n }\n\n this._context = context;\n\n const oldState = context.state;\n\n // Update scopes enable state if state.enable is defined\n if (this.state.enable != null) {\n context.setEnabled(this.state.enable);\n }\n\n // Subscribe to context state changes\n const sub = context.stateObservable.subscribe((state) => {\n this.updateStateFromContext(state);\n });\n\n return () => {\n sub.unsubscribe();\n\n if (this.state.enable != null) {\n context.setEnabled(oldState.enabled);\n }\n };\n }\n\n public updateStateFromContext(state: { loading: boolean; value: Scope[] }) {\n // There was logic in SceneQueryRunner that said if there are no scopes then loading state should not block query execution\n const loading = state.value.length === 0 ? false : state.loading;\n const oldScopes = this.state.scopes.map((scope) => scope.metadata.name);\n const newScopes = state.value.map((scope) => scope.metadata.name);\n\n this.setState({ scopes: state.value, loading });\n\n if (!loading && !isEqual(oldScopes, newScopes)) {\n this.publishEvent(new SceneVariableValueChangedEvent(this), true);\n }\n }\n}\n\nfunction ScopesVariableRenderer({ model }: SceneComponentProps<ScopesVariable>) {\n const context = useContext(ScopesContext);\n\n useEffect(() => {\n return model.setContext(context);\n }, [context, model]);\n\n return null;\n}\n\nexport class ScopesVariableFormatter implements CustomVariableValue {\n public constructor(private _value: string[]) {}\n\n public formatter(formatNameOrFn?: string): string {\n if (formatNameOrFn === VariableFormatID.QueryParam) {\n return this._value.map((scope) => `scope=${encodeURIComponent(scope)}`).join('&');\n }\n\n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AAEO,MAAM,uBAAuD,CAAA;AAAA,EAC3D,YAAoB,MAAkB,EAAA;AAAlB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAAA;AAAmB,EAEvC,UAAU,cAAiC,EAAA;AAChD,IAAI,IAAA,cAAA,KAAmB,iBAAiB,UAAY,EAAA;AAClD,MAAA,OAAO,IAAK,CAAA,MAAA,CAAO,GAAI,CAAA,CAAC,KAAU,KAAA,CAAA,MAAA,EAAS,kBAAmB,CAAA,KAAK,CAAC,CAAA,CAAE,CAAE,CAAA,IAAA,CAAK,GAAG,CAAA;AAAA;AAGlF,IAAO,OAAA,IAAA,CAAK,MAAO,CAAA,IAAA,CAAK,IAAI,CAAA;AAAA;AAEhC;;;;"}
1
+ {"version":3,"file":"ScopesVariable.js","sources":["../../../../src/variables/variants/ScopesVariable.tsx"],"sourcesContent":["import { SceneObjectBase } from '../../core/SceneObjectBase';\nimport {\n CustomVariableValue,\n SceneVariable,\n SceneVariableState,\n SceneVariableValueChangedEvent,\n VariableValue,\n} from '../types';\nimport { Scope } from '@grafana/data';\nimport { SceneComponentProps } from '../../core/types';\nimport { ScopesContext, ScopesContextValue } from '@grafana/runtime';\nimport { useContext, useEffect } from 'react';\nimport { VariableFormatID, VariableHide } from '@grafana/schema';\nimport { SCOPES_VARIABLE_NAME } from '../constants';\nimport { isEqual } from 'lodash';\n\nexport interface ScopesVariableState extends SceneVariableState {\n /**\n * Last captured state from ScopesContext\n */\n scopes: Scope[];\n /**\n * Set to true if you want to the variable to enable / disable scopes when activated / deactivated\n */\n enable?: boolean;\n}\n\nexport class ScopesVariable extends SceneObjectBase<ScopesVariableState> implements SceneVariable<ScopesVariableState> {\n protected _renderBeforeActivation = true;\n protected _context: ScopesContextValue | undefined;\n\n // Special options that enables variables to be hidden but still render to access react contexts\n public UNSAFE_renderAsHidden = true;\n public static Component = ScopesVariableRenderer;\n\n public constructor(state: Partial<ScopesVariableState>) {\n super({\n skipUrlSync: true,\n loading: true,\n scopes: [],\n ...state,\n type: 'system',\n name: SCOPES_VARIABLE_NAME,\n hide: VariableHide.hideVariable,\n });\n }\n\n /**\n * Temporary simple implementation to stringify the scopes.\n */\n public getValue(): VariableValue {\n const scopes = this.state.scopes ?? [];\n return new ScopesVariableFormatter(scopes.map((scope) => scope.metadata.name));\n }\n\n public getScopes(): Scope[] | undefined {\n return this.state.scopes;\n }\n\n /**\n * This method is used to keep the context up to date with the scopes context received from React\n * 1) Subscribes to ScopesContext state changes and synchronizes it with the variable state\n * 2) Handles enable / disabling of scopes based on variable enable option.\n */\n public setContext(context: ScopesContextValue | undefined) {\n if (!context) {\n return;\n }\n\n this._context = context;\n\n const oldState = context.state;\n\n // Update scopes enable state if state.enable is defined\n if (this.state.enable != null) {\n context.setEnabled(this.state.enable);\n }\n\n // Subscribe to context state changes\n const sub = context.stateObservable.subscribe((state) => {\n this.updateStateFromContext(state);\n });\n\n return () => {\n sub.unsubscribe();\n\n if (this.state.enable != null) {\n context.setEnabled(oldState.enabled);\n }\n };\n }\n\n public updateStateFromContext(state: { loading: boolean; value: Scope[] }) {\n // There was logic in SceneQueryRunner that said if there are no scopes then loading state should not block query execution\n const loading = state.value.length === 0 ? false : state.loading;\n const oldScopes = this.state.scopes.map((scope) => scope.metadata.name);\n const newScopes = state.value.map((scope) => scope.metadata.name);\n\n // Only update scopes value state when loading is false and the scopes have changed\n if (!loading && !isEqual(oldScopes, newScopes)) {\n this.setState({ scopes: state.value, loading });\n this.publishEvent(new SceneVariableValueChangedEvent(this), true);\n } else {\n this.setState({ loading });\n }\n }\n}\n\nfunction ScopesVariableRenderer({ model }: SceneComponentProps<ScopesVariable>) {\n const context = useContext(ScopesContext);\n\n useEffect(() => {\n return model.setContext(context);\n }, [context, model]);\n\n return null;\n}\n\nexport class ScopesVariableFormatter implements CustomVariableValue {\n public constructor(private _value: string[]) {}\n\n public formatter(formatNameOrFn?: string): string {\n if (formatNameOrFn === VariableFormatID.QueryParam) {\n return this._value.map((scope) => `scope=${encodeURIComponent(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W,OAAO,CAAA;AAAA,GAC9B,EAAA,CAAC,OAAS,EAAA,KAAK,CAAC,CAAA;AAEnB,EAAO,OAAA,IAAA;AACT;AAEO,MAAM,uBAAuD,CAAA;AAAA,EAC3D,YAAoB,MAAkB,EAAA;AAAlB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAAA;AAAmB,EAEvC,UAAU,cAAiC,EAAA;AAChD,IAAI,IAAA,cAAA,KAAmB,iBAAiB,UAAY,EAAA;AAClD,MAAA,OAAO,IAAK,CAAA,MAAA,CAAO,GAAI,CAAA,CAAC,KAAU,KAAA,CAAA,MAAA,EAAS,kBAAmB,CAAA,KAAK,CAAC,CAAA,CAAE,CAAE,CAAA,IAAA,CAAK,GAAG,CAAA;AAAA;AAGlF,IAAO,OAAA,IAAA,CAAK,MAAO,CAAA,IAAA,CAAK,IAAI,CAAA;AAAA;AAEhC;;;;"}
package/dist/index.js CHANGED
@@ -7291,9 +7291,11 @@ class ScopesVariable extends SceneObjectBase {
7291
7291
  const loading = state.value.length === 0 ? false : state.loading;
7292
7292
  const oldScopes = this.state.scopes.map((scope) => scope.metadata.name);
7293
7293
  const newScopes = state.value.map((scope) => scope.metadata.name);
7294
- this.setState({ scopes: state.value, loading });
7295
7294
  if (!loading && !lodash.isEqual(oldScopes, newScopes)) {
7295
+ this.setState({ scopes: state.value, loading });
7296
7296
  this.publishEvent(new SceneVariableValueChangedEvent(this), true);
7297
+ } else {
7298
+ this.setState({ loading });
7297
7299
  }
7298
7300
  }
7299
7301
  }