UNPKG

@grafana/runtime

Version:
1 lines 2.29 kB
{"version":3,"file":"ScopesContext.mjs","sources":["../../../src/services/ScopesContext.ts"],"sourcesContent":["import { createContext, useContext } from 'react';\nimport { useObservable } from 'react-use';\nimport { Observable } from 'rxjs';\n\nimport { Scope } from '@grafana/data';\n\nexport interface ScopesContextValueState {\n drawerOpened: boolean;\n enabled: boolean;\n loading: boolean;\n readOnly: boolean;\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 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}\n"],"names":[],"mappings":";;;;AA4Ca,MAAA,aAAA,GAAgB,cAA8C,SAAS;AAE7E,SAAS,SAA4C,GAAA;AA9C5D,EAAA,IAAA,EAAA;AA+CE,EAAM,MAAA,OAAA,GAAU,WAAW,aAAa,CAAA;AAExC,EAAA,aAAA,CAAA,CAAc,2CAAS,eAAT,KAAA,IAAA,GAAA,EAAA,GAA4B,IAAI,UAAW,EAAA,EAAG,sCAAS,KAAK,CAAA;AAE1E,EAAA,OAAO,OACH,GAAA;AAAA,IACE,OAAO,OAAQ,CAAA,KAAA;AAAA,IACf,iBAAiB,OAAQ,CAAA,eAAA;AAAA,IACzB,cAAc,OAAQ,CAAA,YAAA;AAAA,IACtB,aAAa,OAAQ,CAAA,WAAA;AAAA,IACrB,YAAY,OAAQ,CAAA;AAAA,GAEtB,GAAA,SAAA;AACN;;;;"}