onDemandThresholdChecker.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {Fragment} from 'react';
  2. import type {Project} from 'sentry/types/project';
  3. import {useApiQuery} from 'sentry/utils/queryClient';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. interface OnDemandThresholdCheckerProps {
  6. children: (props: {isOnDemandLimitReached: boolean | undefined}) => React.ReactNode;
  7. isExtrapolatedChartData: boolean;
  8. projectId: Project['id'];
  9. }
  10. export function OnDemandThresholdChecker({
  11. isExtrapolatedChartData,
  12. children,
  13. projectId,
  14. }: OnDemandThresholdCheckerProps) {
  15. const organization = useOrganization();
  16. const {data} = useApiQuery<{maxAllowed: number; totalOnDemandAlertSpecs: number}>(
  17. [
  18. `/organizations/${organization.slug}/ondemand-rules-stats/`,
  19. {
  20. query: {
  21. project: projectId,
  22. },
  23. },
  24. ],
  25. {
  26. staleTime: 0,
  27. enabled: isExtrapolatedChartData,
  28. }
  29. );
  30. const isOnDemandLimitReached =
  31. data === undefined ? undefined : data.totalOnDemandAlertSpecs >= data.maxAllowed;
  32. return <Fragment>{children({isOnDemandLimitReached})}</Fragment>;
  33. }