groupEventCarousel.tsx 17 KB

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