incidentRedirect.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {useEffect, useState} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import {Client} from 'sentry/api';
  4. import LoadingError from 'sentry/components/loadingError';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import {Organization} from 'sentry/types';
  7. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  8. import useApi from 'sentry/utils/useApi';
  9. import {alertDetailsLink, fetchIncident} from './utils';
  10. type Props = {
  11. api: Client;
  12. organization: Organization;
  13. } & RouteComponentProps<{alertId: string; orgId: string}, {}>;
  14. function IncidentDetails({organization, params}: Props) {
  15. const api = useApi();
  16. const [hasError, setHasError] = useState(false);
  17. const track = () => {
  18. trackAnalyticsEvent({
  19. eventKey: 'alert_details.viewed',
  20. eventName: 'Alert Details: Viewed',
  21. organization_id: parseInt(organization.id, 10),
  22. alert_id: parseInt(params.alertId, 10),
  23. });
  24. };
  25. const fetchData = 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. };
  37. useEffect(() => {
  38. fetchData();
  39. track();
  40. }, []);
  41. if (hasError) {
  42. return <LoadingError onRetry={fetchData} />;
  43. }
  44. return <LoadingIndicator />;
  45. }
  46. export default IncidentDetails;