groupEventCarousel.tsx 16 KB

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