useSentryEvent.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {useEffect, useState} from 'react';
  2. import {Client} from 'sentry/api';
  3. import {Event, RequestState} from 'sentry/types';
  4. import useApi from 'sentry/utils/useApi';
  5. function fetchSentryEvent<T extends Event>(
  6. api: Client,
  7. organizationSlug: string,
  8. projectSlug: string,
  9. eventId: string
  10. ): Promise<T> {
  11. return api.requestPromise(
  12. `/projects/${organizationSlug}/${projectSlug}/events/${eventId}/`
  13. );
  14. }
  15. export function useSentryEvent<T extends Event>(
  16. organizationSlug: string,
  17. projectSlug: string,
  18. eventId: string | null
  19. ): RequestState<T> {
  20. const api = useApi();
  21. const [requestState, setRequestState] = useState<RequestState<T>>({
  22. type: 'initial',
  23. });
  24. useEffect(() => {
  25. if (eventId === null) {
  26. return undefined;
  27. }
  28. fetchSentryEvent<T>(api, organizationSlug, projectSlug, eventId)
  29. .then(event => {
  30. setRequestState({
  31. type: 'resolved',
  32. data: event,
  33. });
  34. })
  35. .catch(err => {
  36. setRequestState({type: 'errored', error: err});
  37. });
  38. return () => {
  39. api.clear();
  40. };
  41. }, [api, organizationSlug, projectSlug, eventId]);
  42. return requestState;
  43. }