usePromptCheck.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {useCallback, useEffect, useState} from 'react';
  2. import {promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  3. import type {Organization} from 'sentry/types';
  4. import {promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  5. import useApi from 'sentry/utils/useApi';
  6. type Opts = {
  7. feature: string;
  8. organization: Organization;
  9. projectId: string;
  10. };
  11. function usePromptCheck({feature, organization, projectId}: Opts) {
  12. const api = useApi();
  13. const [shouldShowPrompt, setShouldShow] = useState<boolean | null>(null);
  14. useEffect(() => {
  15. promptsCheck(api, {
  16. organization,
  17. projectId,
  18. feature,
  19. }).then(data => {
  20. setShouldShow(!promptIsDismissed(data ?? {}, 30));
  21. });
  22. }, [api, feature, organization, projectId]);
  23. const snoozePrompt = useCallback(async () => {
  24. const data = {
  25. projectId,
  26. organization,
  27. feature,
  28. status: 'snoozed' as const,
  29. };
  30. await promptsUpdate(api, data);
  31. setShouldShow(false);
  32. }, [api, feature, organization, projectId]);
  33. return {
  34. shouldShowPrompt,
  35. snoozePrompt,
  36. };
  37. }
  38. export default usePromptCheck;