index.tsx 20 KB

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