useCommitters.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, Group, 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. group?: Group;
  13. }
  14. interface Result {
  15. committers: Committer[];
  16. fetching: boolean;
  17. // TODO(scttcper): Not optional on GA of release-committer-assignees flag
  18. releaseCommitters?: ReleaseCommitter[];
  19. }
  20. async function fetchCommitters(
  21. api: Client,
  22. organization: Organization,
  23. projectSlug: string,
  24. eventId: string
  25. ) {
  26. const repoData = CommitterStore.get(organization.slug, projectSlug, eventId);
  27. if ((!repoData.committers && !repoData.committersLoading) || repoData.committersError) {
  28. await getCommitters(api, {
  29. orgSlug: organization.slug,
  30. projectSlug,
  31. eventId,
  32. });
  33. }
  34. }
  35. function useCommitters({group, eventId, projectSlug}: Props): Result {
  36. const api = useApi();
  37. const organization = useOrganization();
  38. const store = useLegacyStore(CommitterStore);
  39. const loadCommitters = useCallback(async () => {
  40. await fetchCommitters(api, organization!, projectSlug, eventId);
  41. }, [api, organization, projectSlug, eventId]);
  42. useEffect(() => {
  43. // No committers if group doesn't have any releases
  44. if (!group?.firstRelease || !organization) {
  45. return;
  46. }
  47. loadCommitters();
  48. }, [eventId, group?.firstRelease, loadCommitters, organization]);
  49. const key = getCommitterStoreKey(organization?.slug ?? '', projectSlug, eventId);
  50. return {
  51. committers: store[key]?.committers ?? [],
  52. releaseCommitters: store[key]?.releaseCommitters ?? [],
  53. fetching: store[key]?.committersLoading ?? false,
  54. };
  55. }
  56. export default useCommitters;