useCommitters.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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} 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. }
  18. async function fetchCommitters(
  19. api: Client,
  20. organization: Organization,
  21. projectSlug: string,
  22. eventId: string
  23. ) {
  24. const repoData = CommitterStore.get(organization.slug, projectSlug, eventId);
  25. if ((!repoData.committers && !repoData.committersLoading) || repoData.committersError) {
  26. await getCommitters(api, {
  27. orgSlug: organization.slug,
  28. projectSlug,
  29. eventId,
  30. });
  31. }
  32. }
  33. function useCommitters({group, eventId, projectSlug}: Props): Result {
  34. const api = useApi();
  35. const organization = useOrganization();
  36. const store = useLegacyStore(CommitterStore);
  37. const loadCommitters = useCallback(async () => {
  38. await fetchCommitters(api, organization!, projectSlug, eventId);
  39. }, [api, organization, projectSlug, eventId]);
  40. useEffect(() => {
  41. // No committers if group doesn't have any releases
  42. if (!group?.firstRelease || !organization) {
  43. return;
  44. }
  45. loadCommitters();
  46. }, [eventId, group?.firstRelease, loadCommitters, organization]);
  47. const key = getCommitterStoreKey(organization?.slug ?? '', projectSlug, eventId);
  48. return {
  49. committers: store[key]?.committers ?? [],
  50. fetching: store[key]?.committersLoading ?? false,
  51. };
  52. }
  53. export default useCommitters;