useRecentCreatedProject.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import moment from 'moment';
  2. import {Group, OnboardingRecentCreatedProject, Organization, Project} from 'sentry/types';
  3. import {useApiQuery} from 'sentry/utils/queryClient';
  4. // Refetch the data every second
  5. const DEFAULT_POLL_INTERVAL_MS = 1000;
  6. type Props = {
  7. orgSlug: Organization['slug'];
  8. projectSlug?: Project['slug'];
  9. };
  10. // This hook will fetch the project details endpoint until a firstEvent(issue) is received
  11. // When the firstEvent is received it fetches the issues endpoint to find the first issue
  12. export function useRecentCreatedProject({
  13. orgSlug,
  14. projectSlug,
  15. }: Props): undefined | OnboardingRecentCreatedProject {
  16. const {isLoading: isProjectLoading, data: project} = useApiQuery<Project>(
  17. [`/projects/${orgSlug}/${projectSlug}/`],
  18. {
  19. staleTime: 0,
  20. enabled: !!projectSlug,
  21. refetchInterval: data => {
  22. if (!data) {
  23. return false;
  24. }
  25. const [projectData] = data;
  26. return projectData?.firstEvent ? false : DEFAULT_POLL_INTERVAL_MS;
  27. },
  28. }
  29. );
  30. const firstEvent = project?.firstEvent;
  31. const {data: issues} = useApiQuery<Group[]>(
  32. [`/projects/${orgSlug}/${projectSlug}/issues/`],
  33. {
  34. staleTime: Infinity,
  35. enabled: !!firstEvent,
  36. }
  37. );
  38. const firstIssue =
  39. !!firstEvent && issues
  40. ? issues.find((issue: Group) => issue.firstSeen === firstEvent)
  41. : undefined;
  42. const olderThanOneHour = project
  43. ? moment.duration(moment().diff(project.dateCreated)).asHours() > 1
  44. : false;
  45. if (isProjectLoading || !project) {
  46. return undefined;
  47. }
  48. return {
  49. ...project,
  50. firstTransaction: !!project?.firstTransactionEvent,
  51. hasReplays: !!project?.hasReplays,
  52. hasSessions: !!project?.hasSessions,
  53. firstError: !!firstEvent,
  54. firstIssue,
  55. olderThanOneHour,
  56. };
  57. }