useGroupEvent.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import type {Event} from 'sentry/types/event';
  2. import {useApiQuery} from 'sentry/utils/queryClient';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import {
  6. getGroupEventQueryKey,
  7. useDefaultIssueEvent,
  8. } from 'sentry/views/issueDetails/utils';
  9. interface UseGroupEventOptions {
  10. environments: string[];
  11. eventId: string | undefined;
  12. groupId: string;
  13. }
  14. export function useGroupEvent({groupId, eventId, environments}: UseGroupEventOptions) {
  15. const organization = useOrganization();
  16. const location = useLocation<{query?: string}>();
  17. const defaultIssueEvent = useDefaultIssueEvent();
  18. const eventIdUrl = eventId ?? defaultIssueEvent;
  19. const recommendedEventQuery =
  20. typeof location.query.query === 'string' ? location.query.query : undefined;
  21. const isLatestOrRecommendedEvent =
  22. eventIdUrl === 'latest' || eventIdUrl === 'recommended';
  23. const queryKey = getGroupEventQueryKey({
  24. orgSlug: organization.slug,
  25. groupId,
  26. eventId: eventIdUrl,
  27. environments,
  28. recommendedEventQuery: isLatestOrRecommendedEvent ? recommendedEventQuery : undefined,
  29. });
  30. const eventQuery = useApiQuery<Event>(queryKey, {
  31. // Latest/recommended event will change over time, so only cache for 30 seconds
  32. // Oldest/specific events will never change
  33. staleTime: isLatestOrRecommendedEvent ? 30000 : Infinity,
  34. retry: false,
  35. });
  36. return eventQuery;
  37. }