incidentRedirect.tsx 1.7 KB

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