index.tsx 20 KB

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