index.tsx 15 KB

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