releaseActions.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {archiveRelease, restoreRelease} from 'sentry/actionCreators/release';
  6. import {Client} from 'sentry/api';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import {openConfirmModal} from 'sentry/components/confirm';
  9. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  10. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  11. import NavigationButtonGroup from 'sentry/components/navigationButtonGroup';
  12. import TextOverflow from 'sentry/components/textOverflow';
  13. import {Tooltip} from 'sentry/components/tooltip';
  14. import {IconEllipsis} from 'sentry/icons';
  15. import {t, tct, tn} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Organization, Release, ReleaseMeta} from 'sentry/types';
  18. import {trackAnalytics} from 'sentry/utils/analytics';
  19. import {formatVersion} from 'sentry/utils/formatters';
  20. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  21. import {isReleaseArchived} from '../../utils';
  22. type Props = {
  23. location: Location;
  24. organization: Organization;
  25. projectSlug: string;
  26. refetchData: () => void;
  27. release: Release;
  28. releaseMeta: ReleaseMeta;
  29. };
  30. function ReleaseActions({
  31. location,
  32. organization,
  33. projectSlug,
  34. release,
  35. releaseMeta,
  36. refetchData,
  37. }: Props) {
  38. async function handleArchive() {
  39. try {
  40. await archiveRelease(new Client(), {
  41. orgSlug: organization.slug,
  42. projectSlug,
  43. releaseVersion: release.version,
  44. });
  45. browserHistory.push(normalizeUrl(`/organizations/${organization.slug}/releases/`));
  46. } catch {
  47. // do nothing, action creator is already displaying error message
  48. }
  49. }
  50. async function handleRestore() {
  51. try {
  52. await restoreRelease(new Client(), {
  53. orgSlug: organization.slug,
  54. projectSlug,
  55. releaseVersion: release.version,
  56. });
  57. refetchData();
  58. } catch {
  59. // do nothing, action creator is already displaying error message
  60. }
  61. }
  62. function getProjectList() {
  63. const maxVisibleProjects = 5;
  64. const visibleProjects = releaseMeta.projects.slice(0, maxVisibleProjects);
  65. const numberOfCollapsedProjects =
  66. releaseMeta.projects.length - visibleProjects.length;
  67. return (
  68. <Fragment>
  69. {visibleProjects.map(project => (
  70. <ProjectBadge key={project.slug} project={project} avatarSize={18} />
  71. ))}
  72. {numberOfCollapsedProjects > 0 && (
  73. <span>
  74. <Tooltip
  75. title={release.projects
  76. .slice(maxVisibleProjects)
  77. .map(p => p.slug)
  78. .join(', ')}
  79. >
  80. + {tn('%s other project', '%s other projects', numberOfCollapsedProjects)}
  81. </Tooltip>
  82. </span>
  83. )}
  84. </Fragment>
  85. );
  86. }
  87. function getModalHeader(title: React.ReactNode) {
  88. return (
  89. <h4>
  90. <TextOverflow>{title}</TextOverflow>
  91. </h4>
  92. );
  93. }
  94. function getModalMessage(message: React.ReactNode) {
  95. return (
  96. <Fragment>
  97. {message}
  98. <ProjectsWrapper>{getProjectList()}</ProjectsWrapper>
  99. {t('Are you sure you want to do this?')}
  100. </Fragment>
  101. );
  102. }
  103. function replaceReleaseUrl(toRelease: string | null) {
  104. return toRelease
  105. ? {
  106. pathname: location.pathname
  107. .replace(encodeURIComponent(release.version), toRelease)
  108. .replace(release.version, toRelease),
  109. query: {...location.query, activeRepo: undefined},
  110. }
  111. : '';
  112. }
  113. function handleNavigationClick(direction: string) {
  114. trackAnalytics(`release_detail.pagination`, {
  115. organization,
  116. direction,
  117. });
  118. }
  119. const menuItems = [
  120. isReleaseArchived(release)
  121. ? {
  122. key: 'restore',
  123. label: t('Restore'),
  124. onAction: () =>
  125. openConfirmModal({
  126. onConfirm: handleRestore,
  127. header: getModalHeader(
  128. tct('Restore Release [release]', {
  129. release: formatVersion(release.version),
  130. })
  131. ),
  132. message: getModalMessage(
  133. tn(
  134. 'You are restoring this release for the following project:',
  135. 'By restoring this release, you are also restoring it for the following projects:',
  136. releaseMeta.projects.length
  137. )
  138. ),
  139. cancelText: t('Nevermind'),
  140. confirmText: t('Restore'),
  141. }),
  142. }
  143. : {
  144. key: 'archive',
  145. label: t('Archive'),
  146. onAction: () =>
  147. openConfirmModal({
  148. onConfirm: handleArchive,
  149. header: getModalHeader(
  150. tct('Archive Release [release]', {
  151. release: formatVersion(release.version),
  152. })
  153. ),
  154. message: getModalMessage(
  155. tn(
  156. 'You are archiving this release for the following project:',
  157. 'By archiving this release, you are also archiving it for the following projects:',
  158. releaseMeta.projects.length
  159. )
  160. ),
  161. cancelText: t('Nevermind'),
  162. confirmText: t('Archive'),
  163. }),
  164. },
  165. ];
  166. const {
  167. nextReleaseVersion,
  168. prevReleaseVersion,
  169. firstReleaseVersion,
  170. lastReleaseVersion,
  171. } = release.currentProjectMeta;
  172. return (
  173. <ButtonBar gap={1}>
  174. <NavigationButtonGroup
  175. size="sm"
  176. hasPrevious={!!prevReleaseVersion}
  177. hasNext={!!nextReleaseVersion}
  178. links={[
  179. replaceReleaseUrl(firstReleaseVersion),
  180. replaceReleaseUrl(prevReleaseVersion),
  181. replaceReleaseUrl(nextReleaseVersion),
  182. replaceReleaseUrl(lastReleaseVersion),
  183. ]}
  184. onOldestClick={() => handleNavigationClick('oldest')}
  185. onOlderClick={() => handleNavigationClick('older')}
  186. onNewerClick={() => handleNavigationClick('newer')}
  187. onNewestClick={() => handleNavigationClick('newest')}
  188. />
  189. <DropdownMenu
  190. size="sm"
  191. items={menuItems}
  192. triggerProps={{
  193. showChevron: false,
  194. icon: <IconEllipsis />,
  195. 'aria-label': t('Actions'),
  196. }}
  197. position="bottom-end"
  198. />
  199. </ButtonBar>
  200. );
  201. }
  202. const ProjectsWrapper = styled('div')`
  203. margin: ${space(2)} 0 ${space(2)} ${space(2)};
  204. display: grid;
  205. gap: ${space(0.5)};
  206. img {
  207. border: none !important;
  208. box-shadow: none !important;
  209. }
  210. `;
  211. export default ReleaseActions;