index.tsx 17 KB

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