incidentRedirect.tsx 1.8 KB

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