groupEventCarousel.tsx 16 KB

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