@grafana/runtime
Version:
Grafana Runtime Library
1 lines • 2.93 kB
Source Map (JSON)
{"version":3,"file":"ScopesContext.mjs","sources":["../../../src/services/ScopesContext.ts"],"sourcesContent":["import { createContext, useContext, useMemo } from 'react';\nimport { useObservable } from 'react-use';\nimport { Observable } from 'rxjs';\n\nimport { Scope } from '@grafana/data';\n\nexport interface ScopesContextValueState {\n // Whether the drawer with the related dashboards is open\n drawerOpened: boolean;\n enabled: boolean;\n\n // loading state of the scopes\n loading: boolean;\n readOnly: boolean;\n\n // Currently selected scopes\n value: Scope[];\n}\n\nexport interface ScopesContextValue {\n /**\n * Current state.\n */\n state: ScopesContextValueState;\n\n /**\n * Observable that emits the current state.\n */\n stateObservable: Observable<ScopesContextValue['state']>;\n\n /**\n * Change the selected scopes. The service takes care about loading them and propagating the changes.\n * @param scopeNames\n */\n changeScopes(scopeNames: string[]): void;\n\n /**\n * Set read-only mode.\n * If `readOnly` is `true`, the selector will be set to read-only and the dashboards panel will be closed.\n */\n setReadOnly(readOnly: boolean): void;\n\n /**\n * Enable or disable the usage of scopes.\n * This will hide the selector and the dashboards panel, and it will stop propagating the scopes to the query object.\n */\n setEnabled(enabled: boolean): void;\n}\n\nexport const ScopesContext = createContext<ScopesContextValue | undefined>(undefined);\n\nexport function useScopes(): ScopesContextValue | undefined {\n const context = useContext(ScopesContext);\n\n useObservable(context?.stateObservable ?? new Observable(), context?.state);\n\n return useMemo(() => {\n return context\n ? {\n state: context.state,\n stateObservable: context.stateObservable,\n changeScopes: context.changeScopes,\n setReadOnly: context.setReadOnly,\n setEnabled: context.setEnabled,\n }\n : undefined;\n // Not sure why it thinks the context?.state is not required, but we want to recreate this when the state changes.\n // context.stateObservable is readOnly so that is not needed, others are methods which should not change.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [context, context?.state]);\n}\n"],"names":[],"mappings":";;;;AAiDa,MAAA,aAAA,GAAgB,cAA8C,KAAS,CAAA;AAE7E,SAAS,SAA4C,GAAA;AAnD5D,EAAA,IAAA,EAAA;AAoDE,EAAM,MAAA,OAAA,GAAU,WAAW,aAAa,CAAA;AAExC,EAAA,aAAA,CAAA,CAAc,wCAAS,eAAT,KAAA,IAAA,GAAA,EAAA,GAA4B,IAAI,UAAW,EAAA,EAAG,mCAAS,KAAK,CAAA;AAE1E,EAAA,OAAO,QAAQ,MAAM;AACnB,IAAA,OAAO,OACH,GAAA;AAAA,MACE,OAAO,OAAQ,CAAA,KAAA;AAAA,MACf,iBAAiB,OAAQ,CAAA,eAAA;AAAA,MACzB,cAAc,OAAQ,CAAA,YAAA;AAAA,MACtB,aAAa,OAAQ,CAAA,WAAA;AAAA,MACrB,YAAY,OAAQ,CAAA;AAAA,KAEtB,GAAA,KAAA,CAAA;AAAA,GAIH,EAAA,CAAC,OAAS,EAAA,OAAA,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,OAAA,CAAS,KAAK,CAAC,CAAA;AAC9B;;;;"}