commits.tsx 4.0 KB

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