groupEventCarousel.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {useTheme} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import omit from 'lodash/omit';
  6. import moment from 'moment-timezone';
  7. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  8. import type {ButtonProps} from 'sentry/components/button';
  9. import {Button} from 'sentry/components/button';
  10. import {CompactSelect} from 'sentry/components/compactSelect';
  11. import DateTime from 'sentry/components/dateTime';
  12. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  13. import TimeSince from 'sentry/components/timeSince';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {
  16. IconChevron,
  17. IconCopy,
  18. IconEllipsis,
  19. IconJson,
  20. IconLink,
  21. IconWarning,
  22. } from 'sentry/icons';
  23. import {t} from 'sentry/locale';
  24. import {space} from 'sentry/styles/space';
  25. import type {Event, Group, Organization} from 'sentry/types';
  26. import {defined, formatBytesBase2} from 'sentry/utils';
  27. import {trackAnalytics} from 'sentry/utils/analytics';
  28. import {eventDetailsRoute, generateEventSlug} from 'sentry/utils/discover/urls';
  29. import {
  30. getAnalyticsDataForEvent,
  31. getAnalyticsDataForGroup,
  32. getShortEventId,
  33. } from 'sentry/utils/events';
  34. import getDynamicText from 'sentry/utils/getDynamicText';
  35. import {projectCanLinkToReplay} from 'sentry/utils/replays/projectSupportsReplay';
  36. import useCopyToClipboard from 'sentry/utils/useCopyToClipboard';
  37. import {useLocation} from 'sentry/utils/useLocation';
  38. import useMedia from 'sentry/utils/useMedia';
  39. import useOrganization from 'sentry/utils/useOrganization';
  40. import {useParams} from 'sentry/utils/useParams';
  41. import {useUser} from 'sentry/utils/useUser';
  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 {hasTraceTimelineFeature} from 'sentry/views/issueDetails/traceTimeline/utils';
  46. import {useDefaultIssueEvent} from 'sentry/views/issueDetails/utils';
  47. import QuickTrace from './quickTrace';
  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. <GuideAnchor target="issue_details_default_event" position="bottom">
  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. </GuideAnchor>
  216. );
  217. }
  218. type GroupEventActionsProps = {
  219. event: Event;
  220. group: Group;
  221. projectSlug: string;
  222. };
  223. export function GroupEventActions({event, group, projectSlug}: GroupEventActionsProps) {
  224. const theme = useTheme();
  225. const xlargeViewport = useMedia(`(min-width: ${theme.breakpoints.xlarge})`);
  226. const organization = useOrganization();
  227. const hasReplay = Boolean(event?.tags?.find(({key}) => key === 'replayId')?.value);
  228. const isReplayEnabled =
  229. organization.features.includes('session-replay') &&
  230. projectCanLinkToReplay(group.project);
  231. const downloadJson = () => {
  232. const host = organization.links.regionUrl;
  233. const jsonUrl = `${host}/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. return (
  257. <Fragment>
  258. <DropdownMenu
  259. position="bottom-end"
  260. triggerProps={{
  261. 'aria-label': t('Event Actions Menu'),
  262. icon: <IconEllipsis />,
  263. showChevron: false,
  264. size: BUTTON_SIZE,
  265. }}
  266. items={[
  267. {
  268. key: 'copy-event-id',
  269. label: t('Copy Event ID'),
  270. onAction: copyEventId,
  271. },
  272. {
  273. key: 'copy-event-url',
  274. label: t('Copy Event Link'),
  275. hidden: xlargeViewport,
  276. onAction: copyLink,
  277. },
  278. {
  279. key: 'json',
  280. label: `JSON (${formatBytesBase2(event.size)})`,
  281. onAction: downloadJson,
  282. hidden: xlargeViewport,
  283. },
  284. {
  285. key: 'full-event-discover',
  286. label: t('Full Event Details'),
  287. hidden: !organization.features.includes('discover-basic'),
  288. to: eventDetailsRoute({
  289. eventSlug: generateEventSlug({project: projectSlug, id: event.id}),
  290. orgSlug: organization.slug,
  291. }),
  292. onAction: () => {
  293. trackAnalytics('issue_details.event_details_clicked', {
  294. organization,
  295. ...getAnalyticsDataForGroup(group),
  296. ...getAnalyticsDataForEvent(event),
  297. });
  298. },
  299. },
  300. {
  301. key: 'replay',
  302. label: t('View Replay'),
  303. hidden: !hasReplay || !isReplayEnabled,
  304. onAction: () => {
  305. const breadcrumbsHeader = document.getElementById('replay');
  306. if (breadcrumbsHeader) {
  307. breadcrumbsHeader.scrollIntoView({behavior: 'smooth'});
  308. }
  309. trackAnalytics('issue_details.header_view_replay_clicked', {
  310. organization,
  311. ...getAnalyticsDataForGroup(group),
  312. ...getAnalyticsDataForEvent(event),
  313. });
  314. },
  315. },
  316. ]}
  317. />
  318. {xlargeViewport && (
  319. <Button
  320. title={t('Copy link to this issue event')}
  321. size={BUTTON_SIZE}
  322. onClick={copyLink}
  323. aria-label={t('Copy Link')}
  324. icon={<IconLink />}
  325. />
  326. )}
  327. {xlargeViewport && (
  328. <Button
  329. title={t('View JSON')}
  330. size={BUTTON_SIZE}
  331. onClick={downloadJson}
  332. aria-label={t('View JSON')}
  333. icon={<IconJson />}
  334. />
  335. )}
  336. </Fragment>
  337. );
  338. }
  339. export function GroupEventCarousel({event, group, projectSlug}: GroupEventCarouselProps) {
  340. const organization = useOrganization();
  341. const location = useLocation();
  342. const user = useUser();
  343. const latencyThreshold = 30 * 60 * 1000; // 30 minutes
  344. const isOverLatencyThreshold =
  345. event.dateReceived &&
  346. event.dateCreated &&
  347. Math.abs(+moment(event.dateReceived) - +moment(event.dateCreated)) > latencyThreshold;
  348. const hasPreviousEvent = defined(event.previousEventID);
  349. const hasNextEvent = defined(event.nextEventID);
  350. const {onClick: copyEventId} = useCopyToClipboard({
  351. successMessage: t('Event ID copied to clipboard'),
  352. text: event.id,
  353. });
  354. const hasTraceTimeline = hasTraceTimelineFeature(organization, user);
  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. {hasTraceTimeline ? (
  404. <TraceLink event={event} />
  405. ) : (
  406. <QuickTrace event={event} organization={organization} location={location} />
  407. )}
  408. </div>
  409. <ActionsWrapper>
  410. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  411. <EventNavigationDropdown
  412. isDisabled={!hasPreviousEvent && !hasNextEvent}
  413. group={group}
  414. event={event}
  415. />
  416. <NavButtons>
  417. <EventNavigationButton
  418. group={group}
  419. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  420. disabled={!hasPreviousEvent}
  421. title={t('Previous Event')}
  422. eventId={event.previousEventID}
  423. referrer="previous-event"
  424. />
  425. <EventNavigationButton
  426. group={group}
  427. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  428. disabled={!hasNextEvent}
  429. title={t('Next Event')}
  430. eventId={event.nextEventID}
  431. referrer="next-event"
  432. />
  433. </NavButtons>
  434. </ActionsWrapper>
  435. </CarouselAndButtonsWrapper>
  436. );
  437. }
  438. const CarouselAndButtonsWrapper = styled('div')`
  439. display: flex;
  440. justify-content: space-between;
  441. align-items: flex-start;
  442. gap: ${space(1)};
  443. margin-bottom: ${space(0.5)};
  444. `;
  445. const EventHeading = styled('div')`
  446. display: flex;
  447. align-items: center;
  448. flex-wrap: wrap;
  449. gap: ${space(1)};
  450. font-size: ${p => p.theme.fontSizeLarge};
  451. @media (max-width: 600px) {
  452. font-size: ${p => p.theme.fontSizeMedium};
  453. }
  454. `;
  455. const ActionsWrapper = styled('div')`
  456. display: flex;
  457. align-items: center;
  458. gap: ${space(0.5)};
  459. `;
  460. const StyledNavButton = styled(Button)`
  461. border-radius: 0;
  462. `;
  463. const NavButtons = styled('div')`
  464. display: flex;
  465. > * {
  466. &:not(:last-child) {
  467. ${StyledNavButton} {
  468. border-right: none;
  469. }
  470. }
  471. &:first-child {
  472. ${StyledNavButton} {
  473. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  474. }
  475. }
  476. &:last-child {
  477. ${StyledNavButton} {
  478. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  479. }
  480. }
  481. }
  482. `;
  483. const EventIdAndTimeContainer = styled('div')`
  484. display: flex;
  485. align-items: center;
  486. column-gap: ${space(0.75)};
  487. row-gap: 0;
  488. flex-wrap: wrap;
  489. `;
  490. const EventIdContainer = styled('div')`
  491. display: flex;
  492. align-items: center;
  493. column-gap: ${space(0.25)};
  494. `;
  495. const EventTimeLabel = styled('span')`
  496. color: ${p => p.theme.subText};
  497. `;
  498. const StyledIconWarning = styled(IconWarning)`
  499. margin-left: ${space(0.25)};
  500. position: relative;
  501. top: 1px;
  502. `;
  503. const EventId = styled('span')`
  504. position: relative;
  505. font-weight: normal;
  506. font-size: ${p => p.theme.fontSizeLarge};
  507. &:hover {
  508. > span {
  509. display: flex;
  510. }
  511. }
  512. @media (max-width: 600px) {
  513. font-size: ${p => p.theme.fontSizeMedium};
  514. }
  515. `;
  516. const CopyIconContainer = styled('span')`
  517. display: none;
  518. align-items: center;
  519. padding: ${space(0.25)};
  520. background: ${p => p.theme.background};
  521. position: absolute;
  522. right: 0;
  523. top: 50%;
  524. transform: translateY(-50%);
  525. `;