useProjectKeys.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {useEffect, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {Organization, Project, ProjectKey, RequestState} from 'sentry/types';
  4. import useApi from './useApi';
  5. export function useProjectKeys({
  6. organization,
  7. project,
  8. }: {
  9. organization: Organization | null;
  10. project: Project | null;
  11. }) {
  12. const api = useApi();
  13. const [response, setResponse] = useState<RequestState<ProjectKey[]>>({
  14. type: 'initial',
  15. });
  16. useEffect(() => {
  17. if (!organization || !project) {
  18. return () => {};
  19. }
  20. setResponse({type: 'loading'});
  21. const request: Promise<ProjectKey[]> = api.requestPromise(
  22. `/projects/${organization.slug}/${project.slug}/keys/`
  23. );
  24. request
  25. .then(data => {
  26. setResponse({
  27. type: 'resolved',
  28. data,
  29. });
  30. })
  31. .catch(error => {
  32. setResponse({
  33. type: 'errored',
  34. error,
  35. });
  36. Sentry.captureException(error);
  37. });
  38. return () => {
  39. api.clear();
  40. };
  41. }, [organization, project, api]);
  42. return response;
  43. }