useOrganizationStats.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {useQuery, UseQueryOptions} from '@tanstack/react-query';
  2. import {ResponseMeta} from 'sentry/api';
  3. import {t} from 'sentry/locale';
  4. import {Organization, SeriesApi} from 'sentry/types';
  5. import handleXhrErrorResponse from 'sentry/utils/handleXhrErrorResponse';
  6. import useApi from 'sentry/utils/useApi';
  7. type Props = {
  8. organizationSlug: Organization['slug'];
  9. /**
  10. * Can be used to configure how the query is fetched, cached, etc
  11. */
  12. queryOptions?: UseQueryOptions<SeriesApi>;
  13. /**
  14. * Query parameters to add to the requested URL
  15. */
  16. queryParameters?: Record<string, any>;
  17. };
  18. // Fetches the organization stats
  19. export function useOrganizationStats({
  20. organizationSlug,
  21. queryOptions,
  22. queryParameters,
  23. }: Props) {
  24. const api = useApi();
  25. const organizationStats = useQuery<SeriesApi>(
  26. ['organizationStats', organizationSlug, queryParameters],
  27. async (): Promise<SeriesApi> =>
  28. await api.requestPromise(`/organizations/${organizationSlug}/stats_v2/`, {
  29. query: queryParameters,
  30. }),
  31. {
  32. // refetchOnMount defaults to false as this hook can be used on different components on the same page and
  33. // we generally don't want to refetch the data in each component mount.
  34. refetchOnMount: false,
  35. onError: error => {
  36. const errorMessage = t('Unable to fetch organization stats');
  37. handleXhrErrorResponse(errorMessage)(error as ResponseMeta);
  38. },
  39. ...queryOptions,
  40. }
  41. );
  42. return organizationStats;
  43. }