useCommitters.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {useCallback, useEffect} from 'react';
  2. import {getCommitters} from 'sentry/actionCreators/committers';
  3. import {Client} from 'sentry/api';
  4. import CommitterStore, {getCommitterStoreKey} from 'sentry/stores/committerStore';
  5. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  6. import type {Committer, Organization, ReleaseCommitter} from 'sentry/types';
  7. import useApi from 'sentry/utils/useApi';
  8. import useOrganization from './useOrganization';
  9. interface Props {
  10. eventId: string;
  11. projectSlug: string;
  12. }
  13. interface Result {
  14. committers: Committer[];
  15. fetching: boolean;
  16. // TODO(scttcper): Not optional on GA of release-committer-assignees flag
  17. releaseCommitters?: ReleaseCommitter[];
  18. }
  19. async function fetchCommitters(
  20. api: Client,
  21. organization: Organization,
  22. projectSlug: string,
  23. eventId: string
  24. ) {
  25. const repoData = CommitterStore.get(organization.slug, projectSlug, eventId);
  26. if ((!repoData.committers && !repoData.committersLoading) || repoData.committersError) {
  27. await getCommitters(api, {
  28. orgSlug: organization.slug,
  29. projectSlug,
  30. eventId,
  31. });
  32. }
  33. }
  34. function useCommitters({eventId, projectSlug}: Props): Result {
  35. const api = useApi();
  36. const organization = useOrganization();
  37. const store = useLegacyStore(CommitterStore);
  38. const loadCommitters = useCallback(async () => {
  39. await fetchCommitters(api, organization!, projectSlug, eventId);
  40. }, [api, organization, projectSlug, eventId]);
  41. useEffect(() => {
  42. if (!organization) {
  43. return;
  44. }
  45. loadCommitters();
  46. }, [eventId, loadCommitters, organization]);
  47. const key = getCommitterStoreKey(organization?.slug ?? '', projectSlug, eventId);
  48. return {
  49. committers: store[key]?.committers ?? [],
  50. releaseCommitters: store[key]?.releaseCommitters ?? [],
  51. fetching: store[key]?.committersLoading ?? false,
  52. };
  53. }
  54. export default useCommitters;