commits.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import type {Location} from 'history';
  4. import {CommitRow} from 'sentry/components/commitRow';
  5. import * as Layout from 'sentry/components/layouts/thirds';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import Pagination from 'sentry/components/pagination';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelBody from 'sentry/components/panels/panelBody';
  11. import PanelHeader from 'sentry/components/panels/panelHeader';
  12. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  13. import {t} from 'sentry/locale';
  14. import type {Commit, Project, Repository} from 'sentry/types';
  15. import {formatVersion} from 'sentry/utils/formatters';
  16. import {useApiQuery} from 'sentry/utils/queryClient';
  17. import routeTitleGen from 'sentry/utils/routeTitle';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import {useParams} from 'sentry/utils/useParams';
  21. import {getCommitsByRepository, getQuery, getReposToRender} from '../utils';
  22. import EmptyState from './emptyState';
  23. import RepositorySwitcher from './repositorySwitcher';
  24. import withReleaseRepos from './withReleaseRepos';
  25. interface CommitsProps extends RouteComponentProps<{release: string}, {}> {
  26. location: Location;
  27. projectSlug: Project['slug'];
  28. releaseRepos: Repository[];
  29. activeReleaseRepo?: Repository;
  30. }
  31. function Commits({activeReleaseRepo, releaseRepos, projectSlug}: CommitsProps) {
  32. const location = useLocation();
  33. const params = useParams<{release: string}>();
  34. const organization = useOrganization();
  35. const query = getQuery({location, activeRepository: activeReleaseRepo});
  36. const {
  37. data: commitList = [],
  38. isLoading: isLoadingCommitList,
  39. error: commitListError,
  40. refetch,
  41. getResponseHeader,
  42. } = useApiQuery<Commit[]>(
  43. [
  44. `/organizations/${organization.slug}/releases/${encodeURIComponent(
  45. params.release
  46. )}/commits/`,
  47. {query},
  48. ],
  49. {
  50. staleTime: Infinity,
  51. }
  52. );
  53. const commitsByRepository = getCommitsByRepository(commitList);
  54. const reposToRender = getReposToRender(Object.keys(commitsByRepository));
  55. const activeRepoName: string | undefined = activeReleaseRepo
  56. ? activeReleaseRepo.name
  57. : reposToRender[0];
  58. return (
  59. <Layout.Body>
  60. <Layout.Main fullWidth>
  61. <SentryDocumentTitle
  62. title={routeTitleGen(
  63. t('Commits - Release %s', formatVersion(params.release)),
  64. organization.slug,
  65. false,
  66. projectSlug
  67. )}
  68. />
  69. {releaseRepos.length > 1 && (
  70. <RepositorySwitcher
  71. repositories={releaseRepos}
  72. activeRepository={activeReleaseRepo}
  73. />
  74. )}
  75. {commitListError && <LoadingError onRetry={refetch} />}
  76. {isLoadingCommitList ? (
  77. <LoadingIndicator />
  78. ) : commitList.length && activeRepoName ? (
  79. <Fragment>
  80. <Panel>
  81. <PanelHeader>{activeRepoName}</PanelHeader>
  82. <PanelBody>
  83. {commitsByRepository[activeRepoName]?.map(commit => (
  84. <CommitRow key={commit.id} commit={commit} />
  85. ))}
  86. </PanelBody>
  87. </Panel>
  88. <Pagination pageLinks={getResponseHeader?.('Link')} />
  89. </Fragment>
  90. ) : (
  91. <EmptyState>
  92. {activeReleaseRepo
  93. ? t(
  94. 'There are no commits associated with this release in the %s repository.',
  95. activeReleaseRepo.name
  96. )
  97. : t('There are no commits associated with this release.')}
  98. </EmptyState>
  99. )}
  100. </Layout.Main>
  101. </Layout.Body>
  102. );
  103. }
  104. export default withReleaseRepos(Commits);