useValidateWidget.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {validateWidgetRequest} from 'sentry/actionCreators/dashboards';
  2. import type {Client} from 'sentry/api';
  3. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  4. import {hasOnDemandMetricWidgetFeature} from 'sentry/utils/onDemandMetrics/features';
  5. import {useApiQuery} from 'sentry/utils/queryClient';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import type {ValidateWidgetResponse, Widget} from '../types';
  9. import {cleanWidgetForRequest} from '../widgetCard/genericWidgetQueries';
  10. export function validateWidget(
  11. api: Client,
  12. orgId: string,
  13. widget: Widget
  14. ): Promise<undefined> {
  15. const {selection} = PageFiltersStore.getState();
  16. const widgetQuery = validateWidgetRequest(orgId, widget, selection);
  17. const promise: Promise<undefined> = api.requestPromise(widgetQuery[0], widgetQuery[1]);
  18. return promise;
  19. }
  20. export function useValidateWidgetQuery(_widget: Widget) {
  21. const organization = useOrganization();
  22. const {selection} = usePageFilters();
  23. const data = useApiQuery<ValidateWidgetResponse>(
  24. validateWidgetRequest(organization.slug, cleanWidgetForRequest(_widget), selection),
  25. {
  26. staleTime: 10000,
  27. enabled: hasOnDemandMetricWidgetFeature(organization),
  28. }
  29. );
  30. return data;
  31. }