projectEventRedirect.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {Component} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import DetailedError from 'app/components/errors/detailedError';
  4. import {t} from 'app/locale';
  5. import {PageContent} from 'app/styles/organization';
  6. type Props = RouteComponentProps<{}, {}>;
  7. type State = {
  8. error: string | null;
  9. };
  10. /**
  11. * This component performs a client-side redirect to Event Details given only
  12. * an event ID (which normally additionally requires the event's Issue/Group ID).
  13. * It does this by using an XHR against the identically-named ProjectEventRedirect
  14. * _Django_ view, which responds with a 302 with the Location of the corresponding
  15. * Event Details page (if it exists).
  16. *
  17. * See:
  18. * https://github.com/getsentry/sentry/blob/824c03089907ad22a9282303a5eaca33989ce481/src/sentry/web/urls.py#L578
  19. */
  20. class ProjectEventRedirect extends Component<Props, State> {
  21. state: State = {
  22. error: null,
  23. };
  24. componentDidMount() {
  25. const {router} = this.props;
  26. // This presumes that _this_ React view/route is only reachable at
  27. // /:org/:project/events/:eventId (the same URL which serves the ProjectEventRedirect
  28. // Django view).
  29. const endpoint = router.location.pathname;
  30. // Use XmlHttpRequest directly instead of our client API helper (jQuery),
  31. // because you can't reach the underlying XHR via $.ajax, and we need
  32. // access to `xhr.responseURL`.
  33. const xhr = new XMLHttpRequest();
  34. // Hitting this endpoint will return a 302 with a new location, which
  35. // the XHR will follow and make a _second_ request. Using HEAD instead
  36. // of GET returns an empty response instead of the entire HTML content.
  37. xhr.open('HEAD', endpoint);
  38. xhr.send();
  39. xhr.onload = () => {
  40. if (xhr.status === 404) {
  41. this.setState({error: t('Could not find an issue for the provided event id')});
  42. return;
  43. }
  44. // responseURL is the URL of the document the browser ultimately loaded,
  45. // after following any redirects. It _should_ be the page we're trying
  46. // to reach; use the router to go there.
  47. // Use `replace` so that hitting the browser back button will skip all
  48. // this redirect business.
  49. router.replace(xhr.responseURL);
  50. };
  51. xhr.onerror = () => {
  52. this.setState({error: t('Could not load the requested event')});
  53. };
  54. }
  55. render() {
  56. return this.state.error ? (
  57. <DetailedError
  58. heading={t('Not found')}
  59. message={this.state.error}
  60. hideSupportLinks
  61. />
  62. ) : (
  63. <PageContent />
  64. );
  65. }
  66. }
  67. export default ProjectEventRedirect;