incidentRedirect.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {useCallback, useEffect, useState} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import LoadingError from 'sentry/components/loadingError';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import {Organization} from 'sentry/types';
  6. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  7. import useApi from 'sentry/utils/useApi';
  8. import {fetchIncident} from './utils/apiCalls';
  9. import {alertDetailsLink} from './utils';
  10. type Props = {
  11. organization: Organization;
  12. } & RouteComponentProps<{alertId: string; orgId: string}, {}>;
  13. /**
  14. * Reirects from an incident to the incident's metric alert details page
  15. */
  16. function IncidentRedirect({organization, params}: Props) {
  17. const api = useApi();
  18. const [hasError, setHasError] = useState(false);
  19. const track = useCallback(() => {
  20. trackAdvancedAnalyticsEvent('alert_details.viewed', {
  21. organization,
  22. alert_id: parseInt(params.alertId, 10),
  23. });
  24. }, [organization, params.alertId]);
  25. const fetchData = useCallback(async () => {
  26. setHasError(false);
  27. try {
  28. const incident = await fetchIncident(api, params.orgId, params.alertId);
  29. browserHistory.replace({
  30. pathname: alertDetailsLink(organization, incident),
  31. query: {alert: incident.identifier},
  32. });
  33. } catch (err) {
  34. setHasError(true);
  35. }
  36. }, [setHasError, api, params.orgId, params.alertId, organization]);
  37. useEffect(() => {
  38. fetchData();
  39. track();
  40. }, [fetchData, track]);
  41. if (hasError) {
  42. return <LoadingError onRetry={fetchData} />;
  43. }
  44. return <LoadingIndicator />;
  45. }
  46. export default IncidentRedirect;