groupEventCarousel.tsx 16 KB

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