index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import {Component, Fragment, MouseEvent} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Query} from 'history';
  5. import {bulkDelete, bulkUpdate} from 'sentry/actionCreators/group';
  6. import {addLoadingMessage, clearIndicators} from 'sentry/actionCreators/indicator';
  7. import {
  8. ModalRenderProps,
  9. openModal,
  10. openReprocessEventModal,
  11. } from 'sentry/actionCreators/modal';
  12. import {Client} from 'sentry/api';
  13. import Access from 'sentry/components/acl/access';
  14. import Feature from 'sentry/components/acl/feature';
  15. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  16. import ActionButton from 'sentry/components/actions/button';
  17. import IgnoreActions from 'sentry/components/actions/ignore';
  18. import ResolveActions from 'sentry/components/actions/resolve';
  19. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  20. import Button from 'sentry/components/button';
  21. import DropdownMenuControl from 'sentry/components/dropdownMenuControl';
  22. import {IconEllipsis} from 'sentry/icons';
  23. import {t} from 'sentry/locale';
  24. import GroupStore from 'sentry/stores/groupStore';
  25. import space from 'sentry/styles/space';
  26. import {
  27. Group,
  28. GroupStatusResolution,
  29. Organization,
  30. Project,
  31. ResolutionStatus,
  32. SavedQueryVersions,
  33. } from 'sentry/types';
  34. import {Event} from 'sentry/types/event';
  35. import {analytics} from 'sentry/utils/analytics';
  36. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  37. import {getUtcDateString} from 'sentry/utils/dates';
  38. import EventView from 'sentry/utils/discover/eventView';
  39. import {displayReprocessEventAction} from 'sentry/utils/displayReprocessEventAction';
  40. import {getIssueCapability} from 'sentry/utils/groupCapabilities';
  41. import {uniqueId} from 'sentry/utils/guid';
  42. import withApi from 'sentry/utils/withApi';
  43. import withOrganization from 'sentry/utils/withOrganization';
  44. import ReviewAction from 'sentry/views/issueList/actions/reviewAction';
  45. import ShareIssue from 'sentry/views/organizationGroupDetails/actions/shareIssue';
  46. import SubscribeAction from './subscribeAction';
  47. type Props = {
  48. api: Client;
  49. disabled: boolean;
  50. group: Group;
  51. organization: Organization;
  52. project: Project;
  53. event?: Event;
  54. query?: Query;
  55. };
  56. class Actions extends Component<Props> {
  57. getShareUrl(shareId: string) {
  58. if (!shareId) {
  59. return '';
  60. }
  61. const path = `/share/issue/${shareId}/`;
  62. const {host, protocol} = window.location;
  63. return `${protocol}//${host}${path}`;
  64. }
  65. getDiscoverUrl() {
  66. const {group, project, organization} = this.props;
  67. const {title, type, shortId} = group;
  68. const discoverQuery = {
  69. id: undefined,
  70. name: title || type,
  71. fields: ['title', 'release', 'environment', 'user.display', 'timestamp'],
  72. orderby: '-timestamp',
  73. query: `issue:${shortId}`,
  74. projects: [Number(project.id)],
  75. version: 2 as SavedQueryVersions,
  76. range: '90d',
  77. };
  78. const discoverView = EventView.fromSavedQuery(discoverQuery);
  79. return discoverView.getResultsViewUrlTarget(organization.slug);
  80. }
  81. trackIssueAction(
  82. action:
  83. | 'shared'
  84. | 'deleted'
  85. | 'bookmarked'
  86. | 'subscribed'
  87. | 'mark_reviewed'
  88. | 'discarded'
  89. | 'open_in_discover'
  90. | ResolutionStatus
  91. ) {
  92. const {group, project, organization, query = {}} = this.props;
  93. const {alert_date, alert_rule_id, alert_type} = query;
  94. trackAdvancedAnalyticsEvent('issue_details.action_clicked', {
  95. organization,
  96. project_id: parseInt(project.id, 10),
  97. group_id: parseInt(group.id, 10),
  98. issue_category: group.issueCategory,
  99. action_type: action,
  100. // Alert properties track if the user came from email/slack alerts
  101. alert_date:
  102. typeof alert_date === 'string' ? getUtcDateString(Number(alert_date)) : undefined,
  103. alert_rule_id: typeof alert_rule_id === 'string' ? alert_rule_id : undefined,
  104. alert_type: typeof alert_type === 'string' ? alert_type : undefined,
  105. });
  106. }
  107. onDelete = () => {
  108. const {group, project, organization, api} = this.props;
  109. addLoadingMessage(t('Delete event\u2026'));
  110. bulkDelete(
  111. api,
  112. {
  113. orgId: organization.slug,
  114. projectId: project.slug,
  115. itemIds: [group.id],
  116. },
  117. {
  118. complete: () => {
  119. clearIndicators();
  120. browserHistory.push(`/${organization.slug}/${project.slug}/`);
  121. },
  122. }
  123. );
  124. this.trackIssueAction('deleted');
  125. };
  126. onUpdate = (
  127. data:
  128. | {isBookmarked: boolean}
  129. | {isSubscribed: boolean}
  130. | {inbox: boolean}
  131. | GroupStatusResolution
  132. ) => {
  133. const {group, project, organization, api} = this.props;
  134. addLoadingMessage(t('Saving changes\u2026'));
  135. bulkUpdate(
  136. api,
  137. {
  138. orgId: organization.slug,
  139. projectId: project.slug,
  140. itemIds: [group.id],
  141. data,
  142. },
  143. {
  144. complete: clearIndicators,
  145. }
  146. );
  147. if ((data as GroupStatusResolution).status) {
  148. this.trackIssueAction((data as GroupStatusResolution).status);
  149. }
  150. if ((data as {inbox: boolean}).inbox !== undefined) {
  151. this.trackIssueAction('mark_reviewed');
  152. }
  153. };
  154. onReprocessEvent = () => {
  155. const {group, organization} = this.props;
  156. openReprocessEventModal({organization, groupId: group.id});
  157. };
  158. onToggleShare = () => {
  159. const newIsPublic = !this.props.group.isPublic;
  160. if (newIsPublic) {
  161. trackAdvancedAnalyticsEvent('issue.shared_publicly', {
  162. organization: this.props.organization,
  163. });
  164. }
  165. this.trackIssueAction('shared');
  166. };
  167. onToggleBookmark = () => {
  168. this.onUpdate({isBookmarked: !this.props.group.isBookmarked});
  169. this.trackIssueAction('bookmarked');
  170. };
  171. onToggleSubscribe = () => {
  172. this.onUpdate({isSubscribed: !this.props.group.isSubscribed});
  173. this.trackIssueAction('subscribed');
  174. };
  175. onDiscard = () => {
  176. const {group, project, organization, api} = this.props;
  177. const id = uniqueId();
  178. addLoadingMessage(t('Discarding event\u2026'));
  179. GroupStore.onDiscard(id, group.id);
  180. api.request(`/issues/${group.id}/`, {
  181. method: 'PUT',
  182. data: {discard: true},
  183. success: response => {
  184. GroupStore.onDiscardSuccess(id, group.id, response);
  185. browserHistory.push(`/${organization.slug}/${project.slug}/`);
  186. },
  187. error: error => {
  188. GroupStore.onDiscardError(id, group.id, error);
  189. },
  190. complete: clearIndicators,
  191. });
  192. this.trackIssueAction('discarded');
  193. };
  194. renderDiscardModal = ({Body, Footer, closeModal}: ModalRenderProps) => {
  195. const {organization, project} = this.props;
  196. function renderDiscardDisabled({children, ...props}) {
  197. return children({
  198. ...props,
  199. renderDisabled: ({features}: {features: string[]}) => (
  200. <FeatureDisabled
  201. alert
  202. featureName={t('Discard and Delete')}
  203. features={features}
  204. />
  205. ),
  206. });
  207. }
  208. return (
  209. <Feature
  210. features={['projects:discard-groups']}
  211. hookName="feature-disabled:discard-groups"
  212. organization={organization}
  213. project={project}
  214. renderDisabled={renderDiscardDisabled}
  215. >
  216. {({hasFeature, renderDisabled, ...props}) => (
  217. <Fragment>
  218. <Body>
  219. {!hasFeature &&
  220. typeof renderDisabled === 'function' &&
  221. renderDisabled({...props, hasFeature, children: null})}
  222. {t(
  223. `Discarding this event will result in the deletion of most data associated with this issue and future events being discarded before reaching your stream. Are you sure you wish to continue?`
  224. )}
  225. </Body>
  226. <Footer>
  227. <Button onClick={closeModal}>{t('Cancel')}</Button>
  228. <Button
  229. style={{marginLeft: space(1)}}
  230. priority="primary"
  231. onClick={this.onDiscard}
  232. disabled={!hasFeature}
  233. >
  234. {t('Discard Future Events')}
  235. </Button>
  236. </Footer>
  237. </Fragment>
  238. )}
  239. </Feature>
  240. );
  241. };
  242. openDeleteModal = () =>
  243. openModal(({Body, Footer, closeModal}: ModalRenderProps) => (
  244. <Fragment>
  245. <Body>
  246. {t('Deleting this issue is permanent. Are you sure you wish to continue?')}
  247. </Body>
  248. <Footer>
  249. <Button onClick={closeModal}>{t('Cancel')}</Button>
  250. <Button
  251. style={{marginLeft: space(1)}}
  252. priority="primary"
  253. onClick={this.onDelete}
  254. >
  255. {t('Delete')}
  256. </Button>
  257. </Footer>
  258. </Fragment>
  259. ));
  260. openDiscardModal = () => {
  261. const {organization} = this.props;
  262. openModal(this.renderDiscardModal);
  263. analytics('feature.discard_group.modal_opened', {
  264. org_id: parseInt(organization.id, 10),
  265. });
  266. };
  267. handleClick(disabled: boolean, onClick: (event?: MouseEvent) => void) {
  268. return function (event: MouseEvent) {
  269. if (disabled) {
  270. event.preventDefault();
  271. event.stopPropagation();
  272. return;
  273. }
  274. onClick(event);
  275. };
  276. }
  277. render() {
  278. const {group, project, organization, disabled, event} = this.props;
  279. const {status, isBookmarked} = group;
  280. const orgFeatures = new Set(organization.features);
  281. const bookmarkKey = isBookmarked ? 'unbookmark' : 'bookmark';
  282. const bookmarkTitle = isBookmarked ? t('Remove bookmark') : t('Bookmark');
  283. const hasRelease = !!project.features?.includes('releases');
  284. const isResolved = status === 'resolved';
  285. const isIgnored = status === 'ignored';
  286. const deleteCap = getIssueCapability(group.issueCategory, 'delete');
  287. const deleteDiscardCap = getIssueCapability(group.issueCategory, 'deleteAndDiscard');
  288. const shareCap = getIssueCapability(group.issueCategory, 'share');
  289. return (
  290. <Wrapper>
  291. <GuideAnchor target="resolve" position="bottom" offset={20}>
  292. <ResolveActions
  293. disabled={disabled}
  294. disableDropdown={disabled}
  295. hasRelease={hasRelease}
  296. latestRelease={project.latestRelease}
  297. onUpdate={this.onUpdate}
  298. orgSlug={organization.slug}
  299. projectSlug={project.slug}
  300. isResolved={isResolved}
  301. isAutoResolved={
  302. group.status === 'resolved' ? group.statusDetails.autoResolved : undefined
  303. }
  304. />
  305. </GuideAnchor>
  306. <GuideAnchor target="ignore_delete_discard" position="bottom" offset={20}>
  307. <IgnoreActions
  308. isIgnored={isIgnored}
  309. onUpdate={this.onUpdate}
  310. disabled={disabled}
  311. />
  312. </GuideAnchor>
  313. <ReviewAction
  314. onUpdate={this.onUpdate}
  315. disabled={!group.inbox || disabled}
  316. tooltip={t('Issue has been reviewed')}
  317. tooltipProps={{disabled: !!group.inbox || disabled, delay: 300}}
  318. />
  319. <Feature
  320. hookName="feature-disabled:open-in-discover"
  321. features={['discover-basic']}
  322. organization={organization}
  323. >
  324. <ActionButton
  325. disabled={disabled}
  326. to={disabled ? '' : this.getDiscoverUrl()}
  327. onClick={() => this.trackIssueAction('open_in_discover')}
  328. >
  329. <GuideAnchor target="open_in_discover">{t('Open in Discover')}</GuideAnchor>
  330. </ActionButton>
  331. </Feature>
  332. {orgFeatures.has('shared-issues') && (
  333. <ShareIssue
  334. organization={organization}
  335. group={group}
  336. disabled={disabled || !shareCap.enabled}
  337. disabledReason={shareCap.disabledReason}
  338. onToggle={this.onToggleShare}
  339. />
  340. )}
  341. <SubscribeAction
  342. disabled={disabled}
  343. group={group}
  344. onClick={this.handleClick(disabled, this.onToggleSubscribe)}
  345. />
  346. <Access organization={organization} access={['event:admin']}>
  347. {({hasAccess}) => (
  348. <DropdownMenuControl
  349. triggerProps={{
  350. 'aria-label': t('More Actions'),
  351. icon: <IconEllipsis size="xs" />,
  352. showChevron: false,
  353. size: 'xs',
  354. }}
  355. items={[
  356. {
  357. key: bookmarkKey,
  358. label: bookmarkTitle,
  359. hidden: false,
  360. onAction: this.onToggleBookmark,
  361. },
  362. {
  363. key: 'reprocess',
  364. label: t('Reprocess events'),
  365. hidden: !displayReprocessEventAction(organization.features, event),
  366. onAction: this.onReprocessEvent,
  367. },
  368. {
  369. key: 'delete-issue',
  370. priority: 'danger',
  371. label: t('Delete'),
  372. hidden: !hasAccess,
  373. disabled: !deleteCap.enabled,
  374. details: deleteCap.disabledReason,
  375. onAction: this.openDeleteModal,
  376. },
  377. {
  378. key: 'delete-and-discard',
  379. priority: 'danger',
  380. label: t('Delete and discard future events'),
  381. hidden: !hasAccess,
  382. disabled: !deleteDiscardCap.enabled,
  383. details: deleteDiscardCap.disabledReason,
  384. onAction: this.openDiscardModal,
  385. },
  386. ]}
  387. />
  388. )}
  389. </Access>
  390. </Wrapper>
  391. );
  392. }
  393. }
  394. const Wrapper = styled('div')`
  395. display: flex;
  396. flex-wrap: wrap;
  397. justify-content: flex-start;
  398. align-items: center;
  399. grid-auto-flow: column;
  400. gap: ${space(0.5)};
  401. white-space: nowrap;
  402. `;
  403. export {Actions};
  404. export default withApi(withOrganization(Actions));