groupEventCarousel.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {useTheme} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import omit from 'lodash/omit';
  6. import moment from 'moment-timezone';
  7. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  8. import {Button, ButtonProps} from 'sentry/components/button';
  9. import {CompactSelect} from 'sentry/components/compactSelect';
  10. import DateTime from 'sentry/components/dateTime';
  11. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  12. import FeatureBadge from 'sentry/components/featureBadge';
  13. import TimeSince from 'sentry/components/timeSince';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {
  16. IconChevron,
  17. IconCopy,
  18. IconEllipsis,
  19. IconJson,
  20. IconLink,
  21. IconWarning,
  22. } from 'sentry/icons';
  23. import {t} from 'sentry/locale';
  24. import {space} from 'sentry/styles/space';
  25. import {Event, Group, IssueType, Organization} from 'sentry/types';
  26. import {defined, formatBytesBase2} from 'sentry/utils';
  27. import {trackAnalytics} from 'sentry/utils/analytics';
  28. import {eventDetailsRoute, generateEventSlug} from 'sentry/utils/discover/urls';
  29. import {
  30. getAnalyticsDataForEvent,
  31. getAnalyticsDataForGroup,
  32. getShortEventId,
  33. } from 'sentry/utils/events';
  34. import getDynamicText from 'sentry/utils/getDynamicText';
  35. import {projectCanLinkToReplay} from 'sentry/utils/replays/projectSupportsReplay';
  36. import useCopyToClipboard from 'sentry/utils/useCopyToClipboard';
  37. import {useLocation} from 'sentry/utils/useLocation';
  38. import useMedia from 'sentry/utils/useMedia';
  39. import useOrganization from 'sentry/utils/useOrganization';
  40. import {useParams} from 'sentry/utils/useParams';
  41. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  42. import EventCreatedTooltip from 'sentry/views/issueDetails/eventCreatedTooltip';
  43. import {useDefaultIssueEvent} from 'sentry/views/issueDetails/utils';
  44. import QuickTrace from './quickTrace';
  45. type GroupEventCarouselProps = {
  46. event: Event;
  47. group: Group;
  48. projectSlug: string;
  49. };
  50. type GroupEventNavigationProps = {
  51. event: Event;
  52. group: Group;
  53. isDisabled: boolean;
  54. };
  55. type EventNavigationButtonProps = {
  56. disabled: boolean;
  57. group: Group;
  58. icon: ButtonProps['icon'];
  59. referrer: string;
  60. title: string;
  61. eventId?: string | null;
  62. };
  63. enum EventNavDropdownOption {
  64. RECOMMENDED = 'recommended',
  65. LATEST = 'latest',
  66. OLDEST = 'oldest',
  67. CUSTOM = 'custom',
  68. ALL = 'all',
  69. }
  70. const BUTTON_SIZE = 'sm';
  71. const BUTTON_ICON_SIZE = 'sm';
  72. const makeBaseEventsPath = ({
  73. organization,
  74. group,
  75. }: {
  76. group: Group;
  77. organization: Organization;
  78. }) => `/organizations/${organization.slug}/issues/${group.id}/events/`;
  79. function EventNavigationButton({
  80. disabled,
  81. eventId,
  82. group,
  83. icon,
  84. title,
  85. referrer,
  86. }: EventNavigationButtonProps) {
  87. const organization = useOrganization();
  88. const location = useLocation();
  89. const baseEventsPath = makeBaseEventsPath({organization, group});
  90. // Need to wrap with Tooltip because our version of React Router doesn't allow access
  91. // to the anchor ref which is needed by Tooltip to position correctly.
  92. return (
  93. <Tooltip title={title} disabled={disabled} skipWrapper>
  94. <div>
  95. <StyledNavButton
  96. size={BUTTON_SIZE}
  97. icon={icon}
  98. aria-label={title}
  99. to={{
  100. pathname: `${baseEventsPath}${eventId}/`,
  101. query: {...location.query, referrer},
  102. }}
  103. disabled={disabled}
  104. />
  105. </div>
  106. </Tooltip>
  107. );
  108. }
  109. function EventNavigationDropdown({group, event, isDisabled}: GroupEventNavigationProps) {
  110. const location = useLocation();
  111. const params = useParams<{eventId?: string}>();
  112. const theme = useTheme();
  113. const organization = useOrganization();
  114. const largeViewport = useMedia(`(min-width: ${theme.breakpoints.large})`);
  115. const defaultIssueEvent = useDefaultIssueEvent();
  116. if (!largeViewport) {
  117. return null;
  118. }
  119. const getSelectedOption = () => {
  120. switch (params.eventId) {
  121. case EventNavDropdownOption.RECOMMENDED:
  122. case EventNavDropdownOption.LATEST:
  123. case EventNavDropdownOption.OLDEST:
  124. return params.eventId;
  125. case undefined:
  126. return defaultIssueEvent;
  127. default:
  128. return undefined;
  129. }
  130. };
  131. const selectedValue = getSelectedOption();
  132. const eventNavDropdownOptions = [
  133. {
  134. value: EventNavDropdownOption.RECOMMENDED,
  135. label: (
  136. <div>
  137. {t('Recommended')}
  138. <FeatureBadge type="new" />
  139. </div>
  140. ),
  141. textValue: t('Recommended'),
  142. details: t('Event with the most context'),
  143. },
  144. {
  145. value: EventNavDropdownOption.LATEST,
  146. label: t('Latest'),
  147. details: t('Last seen event in this issue'),
  148. },
  149. {
  150. value: EventNavDropdownOption.OLDEST,
  151. label: t('Oldest'),
  152. details: t('First seen event in this issue'),
  153. },
  154. ...(!selectedValue
  155. ? [
  156. {
  157. value: EventNavDropdownOption.CUSTOM,
  158. label: t('Custom Selection'),
  159. },
  160. ]
  161. : []),
  162. {
  163. options: [{value: EventNavDropdownOption.ALL, label: 'View All Events'}],
  164. },
  165. ];
  166. return (
  167. <GuideAnchor target="issue_details_default_event" position="bottom">
  168. <CompactSelect
  169. size="sm"
  170. disabled={isDisabled}
  171. options={eventNavDropdownOptions}
  172. value={!selectedValue ? EventNavDropdownOption.CUSTOM : selectedValue}
  173. triggerLabel={
  174. !selectedValue ? (
  175. <TimeSince
  176. date={event.dateCreated ?? event.dateReceived}
  177. disabledAbsoluteTooltip
  178. />
  179. ) : selectedValue === EventNavDropdownOption.RECOMMENDED ? (
  180. t('Recommended')
  181. ) : undefined
  182. }
  183. menuWidth={232}
  184. onChange={selectedOption => {
  185. trackAnalytics('issue_details.event_dropdown_option_selected', {
  186. organization,
  187. selected_event_type: selectedOption.value,
  188. from_event_type: selectedValue ?? EventNavDropdownOption.CUSTOM,
  189. event_id: event.id,
  190. group_id: group.id,
  191. });
  192. switch (selectedOption.value) {
  193. case EventNavDropdownOption.RECOMMENDED:
  194. case EventNavDropdownOption.LATEST:
  195. case EventNavDropdownOption.OLDEST:
  196. browserHistory.push({
  197. pathname: normalizeUrl(
  198. makeBaseEventsPath({organization, group}) + selectedOption.value + '/'
  199. ),
  200. query: {...location.query, referrer: `${selectedOption.value}-event`},
  201. });
  202. break;
  203. case EventNavDropdownOption.ALL:
  204. const searchTermWithoutQuery = omit(location.query, 'query');
  205. browserHistory.push({
  206. pathname: normalizeUrl(
  207. `/organizations/${organization.slug}/issues/${group.id}/events/`
  208. ),
  209. query: searchTermWithoutQuery,
  210. });
  211. break;
  212. default:
  213. break;
  214. }
  215. }}
  216. />
  217. </GuideAnchor>
  218. );
  219. }
  220. type GroupEventActionsProps = {
  221. event: Event;
  222. group: Group;
  223. projectSlug: string;
  224. };
  225. export function GroupEventActions({event, group, projectSlug}: GroupEventActionsProps) {
  226. const theme = useTheme();
  227. const xlargeViewport = useMedia(`(min-width: ${theme.breakpoints.xlarge})`);
  228. const organization = useOrganization();
  229. const hasReplay = Boolean(event?.tags?.find(({key}) => key === 'replayId')?.value);
  230. const isReplayEnabled =
  231. organization.features.includes('session-replay') &&
  232. projectCanLinkToReplay(group.project);
  233. const isDurationRegressionIssue =
  234. group?.issueType === IssueType.PERFORMANCE_DURATION_REGRESSION;
  235. const downloadJson = () => {
  236. const jsonUrl = `/api/0/projects/${organization.slug}/${projectSlug}/events/${event.id}/json/`;
  237. window.open(jsonUrl);
  238. trackAnalytics('issue_details.event_json_clicked', {
  239. organization,
  240. group_id: parseInt(`${event.groupID}`, 10),
  241. });
  242. };
  243. const {onClick: copyLink} = useCopyToClipboard({
  244. successMessage: t('Event URL copied to clipboard'),
  245. text:
  246. window.location.origin +
  247. normalizeUrl(`${makeBaseEventsPath({organization, group})}${event.id}/`),
  248. onCopy: () =>
  249. trackAnalytics('issue_details.copy_event_link_clicked', {
  250. organization,
  251. ...getAnalyticsDataForGroup(group),
  252. ...getAnalyticsDataForEvent(event),
  253. }),
  254. });
  255. const {onClick: copyEventDetailLink} = useCopyToClipboard({
  256. successMessage: t('Event URL copied to clipboard'),
  257. text:
  258. window.location.origin +
  259. normalizeUrl(
  260. eventDetailsRoute({
  261. eventSlug: generateEventSlug({project: projectSlug, id: event.id}),
  262. orgSlug: organization.slug,
  263. })
  264. ),
  265. });
  266. const {onClick: copyEventId} = useCopyToClipboard({
  267. successMessage: t('Event ID copied to clipboard'),
  268. text: event.id,
  269. });
  270. return (
  271. <Fragment>
  272. <DropdownMenu
  273. position="bottom-end"
  274. triggerProps={{
  275. 'aria-label': t('Event Actions Menu'),
  276. icon: <IconEllipsis size="xs" />,
  277. showChevron: false,
  278. size: BUTTON_SIZE,
  279. }}
  280. items={[
  281. {
  282. key: 'copy-event-id',
  283. label: t('Copy Event ID'),
  284. onAction: copyEventId,
  285. },
  286. {
  287. key: 'copy-event-url',
  288. label: t('Copy Event Link'),
  289. hidden: xlargeViewport,
  290. onAction: isDurationRegressionIssue ? copyEventDetailLink : copyLink,
  291. },
  292. {
  293. key: 'json',
  294. label: `JSON (${formatBytesBase2(event.size)})`,
  295. onAction: downloadJson,
  296. hidden: xlargeViewport,
  297. },
  298. {
  299. key: 'full-event-discover',
  300. label: t('Full Event Details'),
  301. hidden: !organization.features.includes('discover-basic'),
  302. to: eventDetailsRoute({
  303. eventSlug: generateEventSlug({project: projectSlug, id: event.id}),
  304. orgSlug: organization.slug,
  305. }),
  306. onAction: () => {
  307. trackAnalytics('issue_details.event_details_clicked', {
  308. organization,
  309. ...getAnalyticsDataForGroup(group),
  310. ...getAnalyticsDataForEvent(event),
  311. });
  312. },
  313. },
  314. {
  315. key: 'replay',
  316. label: t('View Replay'),
  317. hidden: !hasReplay || !isReplayEnabled,
  318. onAction: () => {
  319. const breadcrumbsHeader = document.getElementById('replay');
  320. if (breadcrumbsHeader) {
  321. breadcrumbsHeader.scrollIntoView({behavior: 'smooth'});
  322. }
  323. trackAnalytics('issue_details.header_view_replay_clicked', {
  324. organization,
  325. ...getAnalyticsDataForGroup(group),
  326. ...getAnalyticsDataForEvent(event),
  327. });
  328. },
  329. },
  330. ]}
  331. />
  332. {xlargeViewport && !isDurationRegressionIssue && (
  333. <Button
  334. title={t('Copy link to this issue event')}
  335. size={BUTTON_SIZE}
  336. onClick={copyLink}
  337. aria-label={t('Copy Link')}
  338. icon={<IconLink />}
  339. />
  340. )}
  341. {xlargeViewport && isDurationRegressionIssue && (
  342. <Button
  343. title={t('Copy link to this event')}
  344. size={BUTTON_SIZE}
  345. onClick={copyEventDetailLink}
  346. aria-label={t('Copy Link')}
  347. icon={<IconLink />}
  348. />
  349. )}
  350. {xlargeViewport && (
  351. <Button
  352. title={t('View JSON')}
  353. size={BUTTON_SIZE}
  354. onClick={downloadJson}
  355. aria-label={t('View JSON')}
  356. icon={<IconJson />}
  357. />
  358. )}
  359. </Fragment>
  360. );
  361. }
  362. export function GroupEventCarousel({event, group, projectSlug}: GroupEventCarouselProps) {
  363. const organization = useOrganization();
  364. const location = useLocation();
  365. const latencyThreshold = 30 * 60 * 1000; // 30 minutes
  366. const isOverLatencyThreshold =
  367. event.dateReceived &&
  368. event.dateCreated &&
  369. Math.abs(+moment(event.dateReceived) - +moment(event.dateCreated)) > latencyThreshold;
  370. const hasPreviousEvent = defined(event.previousEventID);
  371. const hasNextEvent = defined(event.nextEventID);
  372. const {onClick: copyEventId} = useCopyToClipboard({
  373. successMessage: t('Event ID copied to clipboard'),
  374. text: event.id,
  375. });
  376. return (
  377. <CarouselAndButtonsWrapper>
  378. <div>
  379. <EventHeading>
  380. <EventIdAndTimeContainer>
  381. <EventIdContainer>
  382. <strong>Event ID:</strong>
  383. <Button
  384. aria-label={t('Copy')}
  385. borderless
  386. onClick={copyEventId}
  387. size="zero"
  388. title={event.id}
  389. tooltipProps={{overlayStyle: {maxWidth: 'max-content'}}}
  390. translucentBorder
  391. >
  392. <EventId>
  393. {getShortEventId(event.id)}
  394. <CopyIconContainer>
  395. <IconCopy size="xs" />
  396. </CopyIconContainer>
  397. </EventId>
  398. </Button>
  399. </EventIdContainer>
  400. {(event.dateCreated ?? event.dateReceived) && (
  401. <EventTimeLabel>
  402. {getDynamicText({
  403. fixed: 'Jan 1, 12:00 AM',
  404. value: (
  405. <Tooltip
  406. isHoverable
  407. showUnderline
  408. title={<EventCreatedTooltip event={event} />}
  409. overlayStyle={{maxWidth: 300}}
  410. >
  411. <DateTime date={event.dateCreated ?? event.dateReceived} />
  412. </Tooltip>
  413. ),
  414. })}
  415. {isOverLatencyThreshold && (
  416. <Tooltip title="High latency">
  417. <StyledIconWarning size="xs" color="warningText" />
  418. </Tooltip>
  419. )}
  420. </EventTimeLabel>
  421. )}
  422. </EventIdAndTimeContainer>
  423. </EventHeading>
  424. <QuickTrace event={event} organization={organization} location={location} />
  425. </div>
  426. <ActionsWrapper>
  427. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  428. <EventNavigationDropdown
  429. isDisabled={!hasPreviousEvent && !hasNextEvent}
  430. group={group}
  431. event={event}
  432. />
  433. <NavButtons>
  434. <EventNavigationButton
  435. group={group}
  436. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  437. disabled={!hasPreviousEvent}
  438. title={t('Previous Event')}
  439. eventId={event.previousEventID}
  440. referrer="previous-event"
  441. />
  442. <EventNavigationButton
  443. group={group}
  444. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  445. disabled={!hasNextEvent}
  446. title={t('Next Event')}
  447. eventId={event.nextEventID}
  448. referrer="next-event"
  449. />
  450. </NavButtons>
  451. </ActionsWrapper>
  452. </CarouselAndButtonsWrapper>
  453. );
  454. }
  455. const CarouselAndButtonsWrapper = styled('div')`
  456. display: flex;
  457. justify-content: space-between;
  458. align-items: flex-start;
  459. gap: ${space(1)};
  460. margin-bottom: ${space(0.5)};
  461. `;
  462. const EventHeading = styled('div')`
  463. display: flex;
  464. align-items: center;
  465. flex-wrap: wrap;
  466. gap: ${space(1)};
  467. font-size: ${p => p.theme.fontSizeLarge};
  468. @media (max-width: 600px) {
  469. font-size: ${p => p.theme.fontSizeMedium};
  470. }
  471. `;
  472. const ActionsWrapper = styled('div')`
  473. display: flex;
  474. align-items: center;
  475. gap: ${space(0.5)};
  476. `;
  477. const StyledNavButton = styled(Button)`
  478. border-radius: 0;
  479. `;
  480. const NavButtons = styled('div')`
  481. display: flex;
  482. > * {
  483. &:not(:last-child) {
  484. ${StyledNavButton} {
  485. border-right: none;
  486. }
  487. }
  488. &:first-child {
  489. ${StyledNavButton} {
  490. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  491. }
  492. }
  493. &:last-child {
  494. ${StyledNavButton} {
  495. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  496. }
  497. }
  498. }
  499. `;
  500. const EventIdAndTimeContainer = styled('div')`
  501. display: flex;
  502. align-items: center;
  503. column-gap: ${space(0.75)};
  504. row-gap: 0;
  505. flex-wrap: wrap;
  506. `;
  507. const EventIdContainer = styled('div')`
  508. display: flex;
  509. align-items: center;
  510. column-gap: ${space(0.25)};
  511. `;
  512. const EventTimeLabel = styled('span')`
  513. color: ${p => p.theme.subText};
  514. `;
  515. const StyledIconWarning = styled(IconWarning)`
  516. margin-left: ${space(0.25)};
  517. position: relative;
  518. top: 1px;
  519. `;
  520. const EventId = styled('span')`
  521. position: relative;
  522. font-weight: normal;
  523. font-size: ${p => p.theme.fontSizeLarge};
  524. &:hover {
  525. > span {
  526. display: flex;
  527. }
  528. }
  529. @media (max-width: 600px) {
  530. font-size: ${p => p.theme.fontSizeMedium};
  531. }
  532. `;
  533. const CopyIconContainer = styled('span')`
  534. display: none;
  535. align-items: center;
  536. padding: ${space(0.25)};
  537. background: ${p => p.theme.background};
  538. position: absolute;
  539. right: 0;
  540. top: 50%;
  541. transform: translateY(-50%);
  542. `;