index.tsx 15 KB

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