resolve.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 DropdownMenuControl from 'sentry/components/dropdownMenuControl';
  10. import type {MenuItemProps} from 'sentry/components/dropdownMenuItem';
  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. showDividers: !actionTitle,
  154. },
  155. {
  156. key: 'current-release',
  157. label: latestRelease
  158. ? t('The current release (%s)', formatVersion(latestRelease.version))
  159. : t('The current release'),
  160. details: actionTitle,
  161. onAction: () => onActionOrConfirm(this.handleCurrentReleaseResolution),
  162. showDividers: !actionTitle,
  163. },
  164. {
  165. key: 'another-release',
  166. label: t('Another existing release\u2026'),
  167. onAction: () => this.openCustomReleaseModal(),
  168. showDividers: !actionTitle,
  169. },
  170. {
  171. key: 'a-commit',
  172. label: t('A commit\u2026'),
  173. onAction: () => this.openCustomCommitModal(),
  174. showDividers: !actionTitle,
  175. },
  176. ];
  177. const isDisabled = !projectSlug ? disabled : disableDropdown;
  178. return (
  179. <DropdownMenuControl
  180. items={items}
  181. trigger={triggerProps => (
  182. <DropdownTrigger
  183. {...triggerProps}
  184. type="button"
  185. size={size}
  186. priority={priority}
  187. aria-label={t('More resolve options')}
  188. icon={<IconChevron direction="down" size="xs" />}
  189. disabled={isDisabled}
  190. />
  191. )}
  192. disabledKeys={
  193. disabled || !hasRelease
  194. ? ['next-release', 'current-release', 'another-release']
  195. : []
  196. }
  197. menuTitle={t('Resolved In')}
  198. isDisabled={isDisabled}
  199. />
  200. );
  201. }
  202. openCustomCommitModal() {
  203. const {orgSlug, projectSlug} = this.props;
  204. openModal(deps => (
  205. <CustomCommitsResolutionModal
  206. {...deps}
  207. onSelected={(statusDetails: ResolutionStatusDetails) =>
  208. this.handleCommitResolution(statusDetails)
  209. }
  210. orgSlug={orgSlug}
  211. projectSlug={projectSlug}
  212. />
  213. ));
  214. }
  215. openCustomReleaseModal() {
  216. const {orgSlug, projectSlug} = this.props;
  217. openModal(deps => (
  218. <CustomResolutionModal
  219. {...deps}
  220. onSelected={(statusDetails: ResolutionStatusDetails) =>
  221. this.handleAnotherExistingReleaseResolution(statusDetails)
  222. }
  223. orgSlug={orgSlug}
  224. projectSlug={projectSlug}
  225. />
  226. ));
  227. }
  228. render() {
  229. const {
  230. isResolved,
  231. onUpdate,
  232. confirmMessage,
  233. shouldConfirm,
  234. disabled,
  235. confirmLabel,
  236. projectFetchError,
  237. disableTooltip,
  238. priority,
  239. size = 'xs',
  240. hideIcon = false,
  241. } = this.props;
  242. if (isResolved) {
  243. return this.renderResolved();
  244. }
  245. const onResolve = () =>
  246. openConfirmModal({
  247. bypass: !shouldConfirm,
  248. onConfirm: () => onUpdate({status: ResolutionStatus.RESOLVED, statusDetails: {}}),
  249. message: confirmMessage,
  250. confirmText: confirmLabel,
  251. });
  252. return (
  253. <Tooltip disabled={!projectFetchError} title={t('Error fetching project')}>
  254. <ButtonBar merged>
  255. <ResolveButton
  256. type="button"
  257. priority={priority}
  258. size={size}
  259. title={t(
  260. 'Resolves the issue. The issue will get unresolved if it happens again.'
  261. )}
  262. tooltipProps={{delay: 300, disabled: disabled || disableTooltip}}
  263. icon={hideIcon ? null : <IconCheckmark size={size} />}
  264. onClick={onResolve}
  265. disabled={disabled}
  266. >
  267. {t('Resolve')}
  268. </ResolveButton>
  269. {this.renderDropdownMenu()}
  270. </ButtonBar>
  271. </Tooltip>
  272. );
  273. }
  274. }
  275. export default withOrganization(ResolveActions);
  276. const ResolveButton = styled(Button)<{priority?: 'primary'}>`
  277. box-shadow: none;
  278. border-radius: ${p => p.theme.borderRadiusLeft};
  279. ${p => (p.priority === 'primary' ? `border-right-color: ${p.theme.background};` : '')}
  280. `;
  281. const DropdownTrigger = styled(Button)`
  282. box-shadow: none;
  283. border-radius: ${p => p.theme.borderRadiusRight};
  284. border-left: none;
  285. `;