index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import {Fragment, MouseEvent, useMemo} 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 Feature from 'sentry/components/acl/feature';
  14. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  15. import ArchiveActions, {getArchiveActions} from 'sentry/components/actions/archive';
  16. import ActionButton from 'sentry/components/actions/button';
  17. import ResolveActions from 'sentry/components/actions/resolve';
  18. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  19. import {Button} from 'sentry/components/button';
  20. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  21. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  22. import {IconEllipsis, IconSubscribed, IconUnsubscribed} from 'sentry/icons';
  23. import {t} from 'sentry/locale';
  24. import GroupStore from 'sentry/stores/groupStore';
  25. import IssueListCacheStore from 'sentry/stores/IssueListCacheStore';
  26. import {space} from 'sentry/styles/space';
  27. import {
  28. Group,
  29. GroupStatus,
  30. GroupStatusResolution,
  31. GroupSubstatus,
  32. IssueCategory,
  33. MarkReviewed,
  34. Organization,
  35. Project,
  36. SavedQueryVersions,
  37. } from 'sentry/types';
  38. import {Event} from 'sentry/types/event';
  39. import {trackAnalytics} from 'sentry/utils/analytics';
  40. import {getUtcDateString} from 'sentry/utils/dates';
  41. import EventView from 'sentry/utils/discover/eventView';
  42. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  43. import {displayReprocessEventAction} from 'sentry/utils/displayReprocessEventAction';
  44. import {getAnalyticsDataForGroup} from 'sentry/utils/events';
  45. import {uniqueId} from 'sentry/utils/guid';
  46. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  47. import {getAnalyicsDataForProject} from 'sentry/utils/projects';
  48. import withApi from 'sentry/utils/withApi';
  49. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  50. import withOrganization from 'sentry/utils/withOrganization';
  51. import {NewIssueExperienceButton} from 'sentry/views/issueDetails/actions/newIssueExperienceButton';
  52. import ShareIssueModal from './shareModal';
  53. import SubscribeAction from './subscribeAction';
  54. type UpdateData =
  55. | {isBookmarked: boolean}
  56. | {isSubscribed: boolean}
  57. | MarkReviewed
  58. | GroupStatusResolution;
  59. const isResolutionStatus = (data: UpdateData): data is GroupStatusResolution => {
  60. return (data as GroupStatusResolution).status !== undefined;
  61. };
  62. type Props = {
  63. api: Client;
  64. disabled: boolean;
  65. group: Group;
  66. organization: Organization;
  67. project: Project;
  68. event?: Event;
  69. query?: Query;
  70. };
  71. export function Actions(props: Props) {
  72. const {api, group, project, organization, disabled, event, query = {}} = props;
  73. const {status, isBookmarked} = group;
  74. const bookmarkKey = isBookmarked ? 'unbookmark' : 'bookmark';
  75. const bookmarkTitle = isBookmarked ? t('Remove bookmark') : t('Bookmark');
  76. const hasRelease = !!project.features?.includes('releases');
  77. const isResolved = status === 'resolved';
  78. const isAutoResolved =
  79. group.status === 'resolved' ? group.statusDetails.autoResolved : undefined;
  80. const isIgnored = status === 'ignored';
  81. const hasDeleteAccess = organization.access.includes('event:admin');
  82. const config = useMemo(() => getConfigForIssueType(group, project), [group, project]);
  83. const {
  84. actions: {
  85. archiveUntilOccurrence: archiveUntilOccurrenceCap,
  86. delete: deleteCap,
  87. deleteAndDiscard: deleteDiscardCap,
  88. share: shareCap,
  89. resolveInRelease: resolveInReleaseCap,
  90. },
  91. discover: discoverCap,
  92. } = config;
  93. const getDiscoverUrl = () => {
  94. const {title, type, shortId} = group;
  95. const groupIsOccurrenceBacked =
  96. group.issueCategory === IssueCategory.PERFORMANCE && !!event?.occurrence;
  97. const discoverQuery = {
  98. id: undefined,
  99. name: title || type,
  100. fields: ['title', 'release', 'environment', 'user.display', 'timestamp'],
  101. orderby: '-timestamp',
  102. query: `issue:${shortId}`,
  103. projects: [Number(project.id)],
  104. version: 2 as SavedQueryVersions,
  105. range: '90d',
  106. dataset:
  107. config.usesIssuePlatform || groupIsOccurrenceBacked
  108. ? DiscoverDatasets.ISSUE_PLATFORM
  109. : undefined,
  110. };
  111. const discoverView = EventView.fromSavedQuery(discoverQuery);
  112. return discoverView.getResultsViewUrlTarget(organization.slug);
  113. };
  114. const trackIssueAction = (
  115. action:
  116. | 'shared'
  117. | 'deleted'
  118. | 'bookmarked'
  119. | 'subscribed'
  120. | 'mark_reviewed'
  121. | 'discarded'
  122. | 'open_in_discover'
  123. | GroupStatus,
  124. substatus?: GroupSubstatus | null,
  125. statusDetailsKey?: string
  126. ) => {
  127. const {alert_date, alert_rule_id, alert_type} = query;
  128. trackAnalytics('issue_details.action_clicked', {
  129. organization,
  130. action_type: action,
  131. action_substatus: substatus ?? undefined,
  132. action_status_details: statusDetailsKey,
  133. // Alert properties track if the user came from email/slack alerts
  134. alert_date:
  135. typeof alert_date === 'string' ? getUtcDateString(Number(alert_date)) : undefined,
  136. alert_rule_id: typeof alert_rule_id === 'string' ? alert_rule_id : undefined,
  137. alert_type: typeof alert_type === 'string' ? alert_type : undefined,
  138. ...getAnalyticsDataForGroup(group),
  139. ...getAnalyicsDataForProject(project),
  140. });
  141. };
  142. const onDelete = () => {
  143. addLoadingMessage(t('Delete event\u2026'));
  144. bulkDelete(
  145. api,
  146. {
  147. orgId: organization.slug,
  148. projectId: project.slug,
  149. itemIds: [group.id],
  150. },
  151. {
  152. complete: () => {
  153. clearIndicators();
  154. browserHistory.push(
  155. normalizeUrl({
  156. pathname: `/organizations/${organization.slug}/issues/`,
  157. query: {project: project.id},
  158. })
  159. );
  160. },
  161. }
  162. );
  163. trackIssueAction('deleted');
  164. IssueListCacheStore.reset();
  165. };
  166. const onUpdate = (data: UpdateData) => {
  167. addLoadingMessage(t('Saving changes\u2026'));
  168. bulkUpdate(
  169. api,
  170. {
  171. orgId: organization.slug,
  172. projectId: project.slug,
  173. itemIds: [group.id],
  174. data,
  175. },
  176. {
  177. complete: clearIndicators,
  178. }
  179. );
  180. if (isResolutionStatus(data)) {
  181. trackIssueAction(
  182. data.status,
  183. data.substatus,
  184. Object.keys(data.statusDetails || {})[0]
  185. );
  186. }
  187. if ((data as {inbox: boolean}).inbox !== undefined) {
  188. trackIssueAction('mark_reviewed');
  189. }
  190. IssueListCacheStore.reset();
  191. };
  192. const onReprocessEvent = () => {
  193. openReprocessEventModal({organization, groupId: group.id});
  194. };
  195. const onToggleShare = () => {
  196. const newIsPublic = !group.isPublic;
  197. if (newIsPublic) {
  198. trackAnalytics('issue.shared_publicly', {
  199. organization,
  200. });
  201. }
  202. trackIssueAction('shared');
  203. };
  204. const onToggleBookmark = () => {
  205. onUpdate({isBookmarked: !group.isBookmarked});
  206. trackIssueAction('bookmarked');
  207. };
  208. const onToggleSubscribe = () => {
  209. onUpdate({isSubscribed: !group.isSubscribed});
  210. trackIssueAction('subscribed');
  211. };
  212. const onDiscard = () => {
  213. const id = uniqueId();
  214. addLoadingMessage(t('Discarding event\u2026'));
  215. GroupStore.onDiscard(id, group.id);
  216. api.request(`/issues/${group.id}/`, {
  217. method: 'PUT',
  218. data: {discard: true},
  219. success: response => {
  220. GroupStore.onDiscardSuccess(id, group.id, response);
  221. browserHistory.push(
  222. normalizeUrl({
  223. pathname: `/organizations/${organization.slug}/issues/`,
  224. query: {project: project.id},
  225. })
  226. );
  227. },
  228. error: error => {
  229. GroupStore.onDiscardError(id, group.id, error);
  230. },
  231. complete: clearIndicators,
  232. });
  233. trackIssueAction('discarded');
  234. IssueListCacheStore.reset();
  235. };
  236. const renderDiscardModal = ({Body, Footer, closeModal}: ModalRenderProps) => {
  237. function renderDiscardDisabled({children, ...innerProps}) {
  238. return children({
  239. ...innerProps,
  240. renderDisabled: ({features}: {features: string[]}) => (
  241. <FeatureDisabled
  242. alert
  243. featureName={t('Discard and Delete')}
  244. features={features}
  245. />
  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, ...innerProps}) => (
  258. <Fragment>
  259. <Body>
  260. {!hasFeature &&
  261. typeof renderDisabled === 'function' &&
  262. renderDisabled({...innerProps, 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={onDiscard}
  273. disabled={!hasFeature}
  274. >
  275. {t('Discard Future Events')}
  276. </Button>
  277. </Footer>
  278. </Fragment>
  279. )}
  280. </Feature>
  281. );
  282. };
  283. const openDeleteModal = () =>
  284. openModal(({Body, Footer, closeModal}: ModalRenderProps) => (
  285. <Fragment>
  286. <Body>
  287. {t('Deleting this issue is permanent. Are you sure you wish to continue?')}
  288. </Body>
  289. <Footer>
  290. <Button onClick={closeModal}>{t('Cancel')}</Button>
  291. <Button style={{marginLeft: space(1)}} priority="primary" onClick={onDelete}>
  292. {t('Delete')}
  293. </Button>
  294. </Footer>
  295. </Fragment>
  296. ));
  297. const openDiscardModal = () => {
  298. openModal(renderDiscardModal);
  299. };
  300. const openShareModal = () => {
  301. openModal(modalProps => (
  302. <ShareIssueModal
  303. {...modalProps}
  304. organization={organization}
  305. projectSlug={group.project.slug}
  306. groupId={group.id}
  307. onToggle={onToggleShare}
  308. />
  309. ));
  310. };
  311. const handleClick = (onClick: (event?: MouseEvent) => void) => {
  312. return function (innerEvent: MouseEvent) {
  313. if (disabled) {
  314. innerEvent.preventDefault();
  315. innerEvent.stopPropagation();
  316. return;
  317. }
  318. onClick(innerEvent);
  319. };
  320. };
  321. const {dropdownItems: archiveDropdownItems} = getArchiveActions({
  322. onUpdate,
  323. });
  324. return (
  325. <ActionWrapper>
  326. <DropdownMenu
  327. triggerProps={{
  328. 'aria-label': t('More Actions'),
  329. icon: <IconEllipsis />,
  330. showChevron: false,
  331. size: 'sm',
  332. }}
  333. items={[
  334. ...(isIgnored
  335. ? []
  336. : [
  337. {
  338. key: 'Archive',
  339. className: 'hidden-sm hidden-md hidden-lg',
  340. label: t('Archive'),
  341. isSubmenu: true,
  342. disabled,
  343. children: archiveDropdownItems,
  344. },
  345. ]),
  346. {
  347. key: 'open-in-discover',
  348. className: 'hidden-sm hidden-md hidden-lg',
  349. label: t('Open in Discover'),
  350. to: disabled ? '' : getDiscoverUrl(),
  351. onAction: () => trackIssueAction('open_in_discover'),
  352. },
  353. {
  354. key: group.isSubscribed ? 'unsubscribe' : 'subscribe',
  355. className: 'hidden-sm hidden-md hidden-lg',
  356. label: group.isSubscribed ? t('Unsubscribe') : t('Subscribe'),
  357. disabled: disabled || group.subscriptionDetails?.disabled,
  358. onAction: onToggleSubscribe,
  359. },
  360. {
  361. key: 'mark-review',
  362. label: t('Mark reviewed'),
  363. disabled: !group.inbox || disabled,
  364. details: !group.inbox || disabled ? t('Issue has been reviewed') : undefined,
  365. onAction: () => onUpdate({inbox: false}),
  366. },
  367. {
  368. key: 'share',
  369. label: t('Share'),
  370. disabled: disabled || !shareCap.enabled,
  371. hidden: !organization.features.includes('shared-issues'),
  372. onAction: openShareModal,
  373. },
  374. {
  375. key: bookmarkKey,
  376. label: bookmarkTitle,
  377. onAction: onToggleBookmark,
  378. },
  379. {
  380. key: 'reprocess',
  381. label: t('Reprocess events'),
  382. hidden: !displayReprocessEventAction(organization.features, event),
  383. onAction: onReprocessEvent,
  384. },
  385. {
  386. key: 'delete-issue',
  387. priority: 'danger',
  388. label: t('Delete'),
  389. hidden: !hasDeleteAccess,
  390. disabled: !deleteCap.enabled,
  391. details: deleteCap.disabledReason,
  392. onAction: openDeleteModal,
  393. },
  394. {
  395. key: 'delete-and-discard',
  396. priority: 'danger',
  397. label: t('Delete and discard future events'),
  398. hidden: !hasDeleteAccess,
  399. disabled: !deleteDiscardCap.enabled,
  400. details: deleteDiscardCap.disabledReason,
  401. onAction: openDiscardModal,
  402. },
  403. ]}
  404. />
  405. {organization.features.includes('issue-details-new-experience-toggle') ? (
  406. <NewIssueExperienceButton />
  407. ) : null}
  408. <SubscribeAction
  409. className="hidden-xs"
  410. disabled={disabled}
  411. disablePriority
  412. group={group}
  413. onClick={handleClick(onToggleSubscribe)}
  414. icon={group.isSubscribed ? <IconSubscribed /> : <IconUnsubscribed />}
  415. size="sm"
  416. />
  417. <div className="hidden-xs">
  418. <EnvironmentPageFilter position="bottom-end" size="sm" />
  419. </div>
  420. {discoverCap.enabled && (
  421. <Feature
  422. hookName="feature-disabled:open-in-discover"
  423. features="discover-basic"
  424. organization={organization}
  425. >
  426. <ActionButton
  427. className="hidden-xs"
  428. disabled={disabled}
  429. to={disabled ? '' : getDiscoverUrl()}
  430. onClick={() => trackIssueAction('open_in_discover')}
  431. size="sm"
  432. >
  433. <GuideAnchor target="open_in_discover">{t('Open in Discover')}</GuideAnchor>
  434. </ActionButton>
  435. </Feature>
  436. )}
  437. {isResolved || isIgnored ? (
  438. <ActionButton
  439. priority="primary"
  440. title={
  441. isAutoResolved
  442. ? t(
  443. 'This event is resolved due to the Auto Resolve configuration for this project'
  444. )
  445. : t('Change status to unresolved')
  446. }
  447. size="sm"
  448. disabled={disabled || isAutoResolved}
  449. onClick={() =>
  450. onUpdate({
  451. status: GroupStatus.UNRESOLVED,
  452. statusDetails: {},
  453. substatus: GroupSubstatus.ONGOING,
  454. })
  455. }
  456. >
  457. {isIgnored ? t('Archived') : t('Resolved')}
  458. </ActionButton>
  459. ) : (
  460. <Fragment>
  461. <GuideAnchor target="issue_details_archive_button" position="bottom">
  462. <ArchiveActions
  463. className="hidden-xs"
  464. size="sm"
  465. isArchived={isIgnored}
  466. onUpdate={onUpdate}
  467. disabled={disabled}
  468. disableArchiveUntilOccurrence={!archiveUntilOccurrenceCap.enabled}
  469. />
  470. </GuideAnchor>
  471. <GuideAnchor target="resolve" position="bottom" offset={20}>
  472. <ResolveActions
  473. disableResolveInRelease={!resolveInReleaseCap.enabled}
  474. disabled={disabled}
  475. disableDropdown={disabled}
  476. hasRelease={hasRelease}
  477. latestRelease={project.latestRelease}
  478. onUpdate={onUpdate}
  479. projectSlug={project.slug}
  480. isResolved={isResolved}
  481. isAutoResolved={isAutoResolved}
  482. size="sm"
  483. priority="primary"
  484. />
  485. </GuideAnchor>
  486. </Fragment>
  487. )}
  488. </ActionWrapper>
  489. );
  490. }
  491. const ActionWrapper = styled('div')`
  492. display: flex;
  493. align-items: center;
  494. gap: ${space(0.5)};
  495. `;
  496. export default withApi(withOrganization(Actions));