groupEventCarousel.tsx 17 KB

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