metricsContext.tsx 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {createContext, useContext, useMemo} from 'react';
  2. import {useMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta';
  3. import usePageFilters from 'sentry/utils/usePageFilters';
  4. interface MetricsDashboardContextValue {
  5. isLoading: boolean;
  6. metricsMeta: ReturnType<typeof useMetricsMeta>['data'];
  7. }
  8. export const MetricsDashboardContext = createContext<MetricsDashboardContextValue>({
  9. metricsMeta: [],
  10. isLoading: false,
  11. });
  12. export function useMetricsDashboardContext() {
  13. return useContext(MetricsDashboardContext);
  14. }
  15. export function MetricsDashboardContextProvider({children}: {children: React.ReactNode}) {
  16. const pageFilters = usePageFilters().selection;
  17. const metricsMetaQuery = useMetricsMeta(pageFilters);
  18. const contextValue = useMemo(() => {
  19. return {
  20. metricsMeta: metricsMetaQuery.data,
  21. isLoading: metricsMetaQuery.isLoading,
  22. };
  23. }, [metricsMetaQuery]);
  24. return (
  25. <MetricsDashboardContext.Provider value={contextValue}>
  26. {children}
  27. </MetricsDashboardContext.Provider>
  28. );
  29. }