groupEventCarousel.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  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 {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 latencyThreshold = 30 * 60 * 1000; // 30 minutes
  343. const isOverLatencyThreshold =
  344. event.dateReceived &&
  345. event.dateCreated &&
  346. Math.abs(+moment(event.dateReceived) - +moment(event.dateCreated)) > latencyThreshold;
  347. const hasPreviousEvent = defined(event.previousEventID);
  348. const hasNextEvent = defined(event.nextEventID);
  349. const {onClick: copyEventId} = useCopyToClipboard({
  350. successMessage: t('Event ID copied to clipboard'),
  351. text: event.id,
  352. });
  353. const hasTraceTimeline = hasTraceTimelineFeature(organization);
  354. const issueTypeConfig = getConfigForIssueType(group, group.project);
  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. issueTypeConfig.traceTimeline ? (
  405. <TraceLink event={event} />
  406. ) : null
  407. ) : (
  408. <QuickTrace event={event} organization={organization} location={location} />
  409. )}
  410. </div>
  411. <ActionsWrapper>
  412. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  413. <EventNavigationDropdown
  414. isDisabled={!hasPreviousEvent && !hasNextEvent}
  415. group={group}
  416. event={event}
  417. />
  418. <NavButtons>
  419. <EventNavigationButton
  420. group={group}
  421. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  422. disabled={!hasPreviousEvent}
  423. title={t('Previous Event')}
  424. eventId={event.previousEventID}
  425. referrer="previous-event"
  426. />
  427. <EventNavigationButton
  428. group={group}
  429. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  430. disabled={!hasNextEvent}
  431. title={t('Next Event')}
  432. eventId={event.nextEventID}
  433. referrer="next-event"
  434. />
  435. </NavButtons>
  436. </ActionsWrapper>
  437. </CarouselAndButtonsWrapper>
  438. );
  439. }
  440. const CarouselAndButtonsWrapper = styled('div')`
  441. display: flex;
  442. justify-content: space-between;
  443. align-items: flex-start;
  444. gap: ${space(1)};
  445. margin-bottom: ${space(0.5)};
  446. `;
  447. const EventHeading = styled('div')`
  448. display: flex;
  449. align-items: center;
  450. flex-wrap: wrap;
  451. gap: ${space(1)};
  452. font-size: ${p => p.theme.fontSizeLarge};
  453. @media (max-width: 600px) {
  454. font-size: ${p => p.theme.fontSizeMedium};
  455. }
  456. `;
  457. const ActionsWrapper = styled('div')`
  458. display: flex;
  459. align-items: center;
  460. gap: ${space(0.5)};
  461. `;
  462. const StyledNavButton = styled(Button)`
  463. border-radius: 0;
  464. `;
  465. const NavButtons = styled('div')`
  466. display: flex;
  467. > * {
  468. &:not(:last-child) {
  469. ${StyledNavButton} {
  470. border-right: none;
  471. }
  472. }
  473. &:first-child {
  474. ${StyledNavButton} {
  475. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  476. }
  477. }
  478. &:last-child {
  479. ${StyledNavButton} {
  480. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  481. }
  482. }
  483. }
  484. `;
  485. const EventIdAndTimeContainer = styled('div')`
  486. display: flex;
  487. align-items: center;
  488. column-gap: ${space(0.75)};
  489. row-gap: 0;
  490. flex-wrap: wrap;
  491. `;
  492. const EventIdContainer = styled('div')`
  493. display: flex;
  494. align-items: center;
  495. column-gap: ${space(0.25)};
  496. `;
  497. const EventTimeLabel = styled('span')`
  498. color: ${p => p.theme.subText};
  499. `;
  500. const StyledIconWarning = styled(IconWarning)`
  501. margin-left: ${space(0.25)};
  502. position: relative;
  503. top: 1px;
  504. `;
  505. const EventId = styled('span')`
  506. position: relative;
  507. font-weight: normal;
  508. font-size: ${p => p.theme.fontSizeLarge};
  509. &:hover {
  510. > span {
  511. display: flex;
  512. }
  513. }
  514. @media (max-width: 600px) {
  515. font-size: ${p => p.theme.fontSizeMedium};
  516. }
  517. `;
  518. const CopyIconContainer = styled('span')`
  519. display: none;
  520. align-items: center;
  521. padding: ${space(0.25)};
  522. background: ${p => p.theme.background};
  523. position: absolute;
  524. right: 0;
  525. top: 50%;
  526. transform: translateY(-50%);
  527. `;