resolve.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {openModal} from 'sentry/actionCreators/modal';
  4. import {Button} from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import {openConfirmModal} from 'sentry/components/confirm';
  7. import CustomCommitsResolutionModal from 'sentry/components/customCommitsResolutionModal';
  8. import CustomResolutionModal from 'sentry/components/customResolutionModal';
  9. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  10. import DropdownMenu from 'sentry/components/dropdownMenu';
  11. import Tooltip from 'sentry/components/tooltip';
  12. import {IconCheckmark, IconChevron} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {
  15. GroupStatusResolution,
  16. Organization,
  17. Release,
  18. ResolutionStatus,
  19. ResolutionStatusDetails,
  20. } from 'sentry/types';
  21. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  22. import {formatVersion} from 'sentry/utils/formatters';
  23. import withOrganization from 'sentry/utils/withOrganization';
  24. const defaultProps = {
  25. isResolved: false,
  26. isAutoResolved: false,
  27. confirmLabel: t('Resolve'),
  28. };
  29. type Props = {
  30. hasRelease: boolean;
  31. onUpdate: (data: GroupStatusResolution) => void;
  32. orgSlug: string;
  33. organization: Organization;
  34. confirmMessage?: React.ReactNode;
  35. disableDropdown?: boolean;
  36. disableTooltip?: boolean;
  37. disabled?: boolean;
  38. hideIcon?: boolean;
  39. latestRelease?: Release;
  40. priority?: 'primary';
  41. projectFetchError?: boolean;
  42. projectSlug?: string;
  43. shouldConfirm?: boolean;
  44. size?: 'xs' | 'sm';
  45. } & Partial<typeof defaultProps>;
  46. class ResolveActions extends Component<Props> {
  47. static defaultProps = defaultProps;
  48. handleCommitResolution(statusDetails: ResolutionStatusDetails) {
  49. const {onUpdate} = this.props;
  50. onUpdate({
  51. status: ResolutionStatus.RESOLVED,
  52. statusDetails,
  53. });
  54. }
  55. handleAnotherExistingReleaseResolution(statusDetails: ResolutionStatusDetails) {
  56. const {organization, onUpdate} = this.props;
  57. onUpdate({
  58. status: ResolutionStatus.RESOLVED,
  59. statusDetails,
  60. });
  61. trackAdvancedAnalyticsEvent('resolve_issue', {
  62. organization,
  63. release: 'anotherExisting',
  64. });
  65. }
  66. handleCurrentReleaseResolution = () => {
  67. const {onUpdate, organization, hasRelease, latestRelease} = this.props;
  68. hasRelease &&
  69. onUpdate({
  70. status: ResolutionStatus.RESOLVED,
  71. statusDetails: {
  72. inRelease: latestRelease ? latestRelease.version : 'latest',
  73. },
  74. });
  75. trackAdvancedAnalyticsEvent('resolve_issue', {
  76. organization,
  77. release: 'current',
  78. });
  79. };
  80. handleNextReleaseResolution = () => {
  81. const {onUpdate, organization, hasRelease} = this.props;
  82. hasRelease &&
  83. onUpdate({
  84. status: ResolutionStatus.RESOLVED,
  85. statusDetails: {
  86. inNextRelease: true,
  87. },
  88. });
  89. trackAdvancedAnalyticsEvent('resolve_issue', {
  90. organization,
  91. release: 'next',
  92. });
  93. };
  94. renderResolved() {
  95. const {isAutoResolved, onUpdate} = this.props;
  96. return (
  97. <Tooltip
  98. title={
  99. isAutoResolved
  100. ? t(
  101. 'This event is resolved due to the Auto Resolve configuration for this project'
  102. )
  103. : t('Unresolve')
  104. }
  105. >
  106. <Button
  107. priority="primary"
  108. size="xs"
  109. icon={<IconCheckmark size="xs" />}
  110. aria-label={t('Unresolve')}
  111. disabled={isAutoResolved}
  112. onClick={() =>
  113. onUpdate({status: ResolutionStatus.UNRESOLVED, statusDetails: {}})
  114. }
  115. />
  116. </Tooltip>
  117. );
  118. }
  119. renderDropdownMenu() {
  120. const {
  121. projectSlug,
  122. isResolved,
  123. hasRelease,
  124. latestRelease,
  125. confirmMessage,
  126. shouldConfirm,
  127. disabled,
  128. confirmLabel,
  129. disableDropdown,
  130. size = 'xs',
  131. priority,
  132. } = this.props;
  133. if (isResolved) {
  134. return this.renderResolved();
  135. }
  136. const actionTitle = !hasRelease
  137. ? t('Set up release tracking in order to use this feature.')
  138. : '';
  139. const onActionOrConfirm = onAction => {
  140. openConfirmModal({
  141. bypass: !shouldConfirm,
  142. onConfirm: onAction,
  143. message: confirmMessage,
  144. confirmText: confirmLabel,
  145. });
  146. };
  147. const items: MenuItemProps[] = [
  148. {
  149. key: 'next-release',
  150. label: t('The next release'),
  151. details: actionTitle,
  152. onAction: () => onActionOrConfirm(this.handleNextReleaseResolution),
  153. },
  154. {
  155. key: 'current-release',
  156. label: latestRelease
  157. ? t('The current release (%s)', formatVersion(latestRelease.version))
  158. : t('The current release'),
  159. details: actionTitle,
  160. onAction: () => onActionOrConfirm(this.handleCurrentReleaseResolution),
  161. },
  162. {
  163. key: 'another-release',
  164. label: t('Another existing release\u2026'),
  165. onAction: () => this.openCustomReleaseModal(),
  166. },
  167. {
  168. key: 'a-commit',
  169. label: t('A commit\u2026'),
  170. onAction: () => this.openCustomCommitModal(),
  171. },
  172. ];
  173. const isDisabled = !projectSlug ? disabled : disableDropdown;
  174. return (
  175. <DropdownMenu
  176. items={items}
  177. trigger={triggerProps => (
  178. <DropdownTrigger
  179. {...triggerProps}
  180. size={size}
  181. priority={priority}
  182. aria-label={t('More resolve options')}
  183. icon={<IconChevron direction="down" size="xs" />}
  184. disabled={isDisabled}
  185. />
  186. )}
  187. disabledKeys={
  188. disabled || !hasRelease
  189. ? ['next-release', 'current-release', 'another-release']
  190. : []
  191. }
  192. menuTitle={t('Resolved In')}
  193. isDisabled={isDisabled}
  194. />
  195. );
  196. }
  197. openCustomCommitModal() {
  198. const {orgSlug, projectSlug} = this.props;
  199. openModal(deps => (
  200. <CustomCommitsResolutionModal
  201. {...deps}
  202. onSelected={(statusDetails: ResolutionStatusDetails) =>
  203. this.handleCommitResolution(statusDetails)
  204. }
  205. orgSlug={orgSlug}
  206. projectSlug={projectSlug}
  207. />
  208. ));
  209. }
  210. openCustomReleaseModal() {
  211. const {orgSlug, projectSlug} = this.props;
  212. openModal(deps => (
  213. <CustomResolutionModal
  214. {...deps}
  215. onSelected={(statusDetails: ResolutionStatusDetails) =>
  216. this.handleAnotherExistingReleaseResolution(statusDetails)
  217. }
  218. orgSlug={orgSlug}
  219. projectSlug={projectSlug}
  220. />
  221. ));
  222. }
  223. render() {
  224. const {
  225. isResolved,
  226. onUpdate,
  227. confirmMessage,
  228. shouldConfirm,
  229. disabled,
  230. confirmLabel,
  231. projectFetchError,
  232. disableTooltip,
  233. priority,
  234. size = 'xs',
  235. hideIcon = false,
  236. } = this.props;
  237. if (isResolved) {
  238. return this.renderResolved();
  239. }
  240. const onResolve = () =>
  241. openConfirmModal({
  242. bypass: !shouldConfirm,
  243. onConfirm: () => onUpdate({status: ResolutionStatus.RESOLVED, statusDetails: {}}),
  244. message: confirmMessage,
  245. confirmText: confirmLabel,
  246. });
  247. return (
  248. <Tooltip disabled={!projectFetchError} title={t('Error fetching project')}>
  249. <ButtonBar merged>
  250. <ResolveButton
  251. priority={priority}
  252. size={size}
  253. title={t(
  254. 'Resolves the issue. The issue will get unresolved if it happens again.'
  255. )}
  256. tooltipProps={{delay: 300, disabled: disabled || disableTooltip}}
  257. icon={hideIcon ? null : <IconCheckmark size={size} />}
  258. onClick={onResolve}
  259. disabled={disabled}
  260. >
  261. {t('Resolve')}
  262. </ResolveButton>
  263. {this.renderDropdownMenu()}
  264. </ButtonBar>
  265. </Tooltip>
  266. );
  267. }
  268. }
  269. export default withOrganization(ResolveActions);
  270. const ResolveButton = styled(Button)<{priority?: 'primary'}>`
  271. box-shadow: none;
  272. border-radius: ${p => p.theme.borderRadiusLeft};
  273. ${p =>
  274. p.priority === 'primary'
  275. ? `
  276. &::after {
  277. content: '';
  278. position: absolute;
  279. top: -1px;
  280. bottom: -1px;
  281. right: -1px;
  282. border-right: solid 1px currentColor;
  283. opacity: 0.25;
  284. }
  285. `
  286. : ''}
  287. `;
  288. const DropdownTrigger = styled(Button)`
  289. box-shadow: none;
  290. border-radius: ${p => p.theme.borderRadiusRight};
  291. border-left: none;
  292. `;