groupEventCarousel.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 {normalizeUrl} from 'sentry/utils/withDomainRequired';
  42. import EventCreatedTooltip from 'sentry/views/issueDetails/eventCreatedTooltip';
  43. import {TraceLink} from 'sentry/views/issueDetails/traceTimeline/traceLink';
  44. import {hasTraceTimelineFeature} from 'sentry/views/issueDetails/traceTimeline/utils';
  45. import {useDefaultIssueEvent} from 'sentry/views/issueDetails/utils';
  46. import QuickTrace from './quickTrace';
  47. type GroupEventCarouselProps = {
  48. event: Event;
  49. group: Group;
  50. projectSlug: string;
  51. };
  52. type GroupEventNavigationProps = {
  53. event: Event;
  54. group: Group;
  55. isDisabled: boolean;
  56. };
  57. type EventNavigationButtonProps = {
  58. disabled: boolean;
  59. group: Group;
  60. icon: ButtonProps['icon'];
  61. referrer: string;
  62. title: string;
  63. eventId?: string | null;
  64. };
  65. enum EventNavDropdownOption {
  66. RECOMMENDED = 'recommended',
  67. LATEST = 'latest',
  68. OLDEST = 'oldest',
  69. CUSTOM = 'custom',
  70. ALL = 'all',
  71. }
  72. const BUTTON_SIZE = 'sm';
  73. const BUTTON_ICON_SIZE = 'sm';
  74. const makeBaseEventsPath = ({
  75. organization,
  76. group,
  77. }: {
  78. group: Group;
  79. organization: Organization;
  80. }) => `/organizations/${organization.slug}/issues/${group.id}/events/`;
  81. function EventNavigationButton({
  82. disabled,
  83. eventId,
  84. group,
  85. icon,
  86. title,
  87. referrer,
  88. }: EventNavigationButtonProps) {
  89. const organization = useOrganization();
  90. const location = useLocation();
  91. const baseEventsPath = makeBaseEventsPath({organization, group});
  92. // Need to wrap with Tooltip because our version of React Router doesn't allow access
  93. // to the anchor ref which is needed by Tooltip to position correctly.
  94. return (
  95. <Tooltip title={title} disabled={disabled} skipWrapper>
  96. <div>
  97. <StyledNavButton
  98. size={BUTTON_SIZE}
  99. icon={icon}
  100. aria-label={title}
  101. to={{
  102. pathname: `${baseEventsPath}${eventId}/`,
  103. query: {...location.query, referrer},
  104. }}
  105. disabled={disabled}
  106. />
  107. </div>
  108. </Tooltip>
  109. );
  110. }
  111. function EventNavigationDropdown({group, event, isDisabled}: GroupEventNavigationProps) {
  112. const location = useLocation();
  113. const params = useParams<{eventId?: string}>();
  114. const theme = useTheme();
  115. const organization = useOrganization();
  116. const largeViewport = useMedia(`(min-width: ${theme.breakpoints.large})`);
  117. const defaultIssueEvent = useDefaultIssueEvent();
  118. if (!largeViewport) {
  119. return null;
  120. }
  121. const getSelectedOption = () => {
  122. switch (params.eventId) {
  123. case EventNavDropdownOption.RECOMMENDED:
  124. case EventNavDropdownOption.LATEST:
  125. case EventNavDropdownOption.OLDEST:
  126. return params.eventId;
  127. case undefined:
  128. return defaultIssueEvent;
  129. default:
  130. return undefined;
  131. }
  132. };
  133. const selectedValue = getSelectedOption();
  134. const eventNavDropdownOptions = [
  135. {
  136. value: EventNavDropdownOption.RECOMMENDED,
  137. label: t('Recommended'),
  138. textValue: t('Recommended'),
  139. details: t('Event with the most context'),
  140. },
  141. {
  142. value: EventNavDropdownOption.LATEST,
  143. label: t('Latest'),
  144. details: t('Last seen event in this issue'),
  145. },
  146. {
  147. value: EventNavDropdownOption.OLDEST,
  148. label: t('Oldest'),
  149. details: t('First seen event in this issue'),
  150. },
  151. ...(!selectedValue
  152. ? [
  153. {
  154. value: EventNavDropdownOption.CUSTOM,
  155. label: t('Custom Selection'),
  156. },
  157. ]
  158. : []),
  159. {
  160. options: [{value: EventNavDropdownOption.ALL, label: 'View All Events'}],
  161. },
  162. ];
  163. return (
  164. <GuideAnchor target="issue_details_default_event" position="bottom">
  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. </GuideAnchor>
  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(event?.tags?.find(({key}) => key === 'replayId')?.value);
  227. const isReplayEnabled =
  228. organization.features.includes('session-replay') &&
  229. projectCanLinkToReplay(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 location = useLocation();
  341. const latencyThreshold = 30 * 60 * 1000; // 30 minutes
  342. const isOverLatencyThreshold =
  343. event.dateReceived &&
  344. event.dateCreated &&
  345. Math.abs(+moment(event.dateReceived) - +moment(event.dateCreated)) > latencyThreshold;
  346. const hasPreviousEvent = defined(event.previousEventID);
  347. const hasNextEvent = defined(event.nextEventID);
  348. const {onClick: copyEventId} = useCopyToClipboard({
  349. successMessage: t('Event ID copied to clipboard'),
  350. text: event.id,
  351. });
  352. const hasTraceTimeline = hasTraceTimelineFeature(organization);
  353. return (
  354. <CarouselAndButtonsWrapper>
  355. <div>
  356. <EventHeading>
  357. <EventIdAndTimeContainer>
  358. <EventIdContainer>
  359. <strong>Event ID:</strong>
  360. <Button
  361. aria-label={t('Copy')}
  362. borderless
  363. onClick={copyEventId}
  364. size="zero"
  365. title={event.id}
  366. tooltipProps={{overlayStyle: {maxWidth: 'max-content'}}}
  367. translucentBorder
  368. >
  369. <EventId>
  370. {getShortEventId(event.id)}
  371. <CopyIconContainer>
  372. <IconCopy size="xs" />
  373. </CopyIconContainer>
  374. </EventId>
  375. </Button>
  376. </EventIdContainer>
  377. {(event.dateCreated ?? event.dateReceived) && (
  378. <EventTimeLabel>
  379. {getDynamicText({
  380. fixed: 'Jan 1, 12:00 AM',
  381. value: (
  382. <Tooltip
  383. isHoverable
  384. showUnderline
  385. title={<EventCreatedTooltip event={event} />}
  386. overlayStyle={{maxWidth: 300}}
  387. >
  388. <DateTime date={event.dateCreated ?? event.dateReceived} />
  389. </Tooltip>
  390. ),
  391. })}
  392. {isOverLatencyThreshold && (
  393. <Tooltip title="High latency">
  394. <StyledIconWarning size="xs" color="warningText" />
  395. </Tooltip>
  396. )}
  397. </EventTimeLabel>
  398. )}
  399. </EventIdAndTimeContainer>
  400. </EventHeading>
  401. {hasTraceTimeline ? (
  402. <TraceLink event={event} />
  403. ) : (
  404. <QuickTrace event={event} organization={organization} location={location} />
  405. )}
  406. </div>
  407. <ActionsWrapper>
  408. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  409. <EventNavigationDropdown
  410. isDisabled={!hasPreviousEvent && !hasNextEvent}
  411. group={group}
  412. event={event}
  413. />
  414. <NavButtons>
  415. <EventNavigationButton
  416. group={group}
  417. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  418. disabled={!hasPreviousEvent}
  419. title={t('Previous Event')}
  420. eventId={event.previousEventID}
  421. referrer="previous-event"
  422. />
  423. <EventNavigationButton
  424. group={group}
  425. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  426. disabled={!hasNextEvent}
  427. title={t('Next Event')}
  428. eventId={event.nextEventID}
  429. referrer="next-event"
  430. />
  431. </NavButtons>
  432. </ActionsWrapper>
  433. </CarouselAndButtonsWrapper>
  434. );
  435. }
  436. const CarouselAndButtonsWrapper = styled('div')`
  437. display: flex;
  438. justify-content: space-between;
  439. align-items: flex-start;
  440. gap: ${space(1)};
  441. margin-bottom: ${space(0.5)};
  442. `;
  443. const EventHeading = styled('div')`
  444. display: flex;
  445. align-items: center;
  446. flex-wrap: wrap;
  447. gap: ${space(1)};
  448. font-size: ${p => p.theme.fontSizeLarge};
  449. @media (max-width: 600px) {
  450. font-size: ${p => p.theme.fontSizeMedium};
  451. }
  452. `;
  453. const ActionsWrapper = styled('div')`
  454. display: flex;
  455. align-items: center;
  456. gap: ${space(0.5)};
  457. `;
  458. const StyledNavButton = styled(Button)`
  459. border-radius: 0;
  460. `;
  461. const NavButtons = styled('div')`
  462. display: flex;
  463. > * {
  464. &:not(:last-child) {
  465. ${StyledNavButton} {
  466. border-right: none;
  467. }
  468. }
  469. &:first-child {
  470. ${StyledNavButton} {
  471. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  472. }
  473. }
  474. &:last-child {
  475. ${StyledNavButton} {
  476. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  477. }
  478. }
  479. }
  480. `;
  481. const EventIdAndTimeContainer = styled('div')`
  482. display: flex;
  483. align-items: center;
  484. column-gap: ${space(0.75)};
  485. row-gap: 0;
  486. flex-wrap: wrap;
  487. `;
  488. const EventIdContainer = styled('div')`
  489. display: flex;
  490. align-items: center;
  491. column-gap: ${space(0.25)};
  492. `;
  493. const EventTimeLabel = styled('span')`
  494. color: ${p => p.theme.subText};
  495. `;
  496. const StyledIconWarning = styled(IconWarning)`
  497. margin-left: ${space(0.25)};
  498. position: relative;
  499. top: 1px;
  500. `;
  501. const EventId = styled('span')`
  502. position: relative;
  503. font-weight: normal;
  504. font-size: ${p => p.theme.fontSizeLarge};
  505. &:hover {
  506. > span {
  507. display: flex;
  508. }
  509. }
  510. @media (max-width: 600px) {
  511. font-size: ${p => p.theme.fontSizeMedium};
  512. }
  513. `;
  514. const CopyIconContainer = styled('span')`
  515. display: none;
  516. align-items: center;
  517. padding: ${space(0.25)};
  518. background: ${p => p.theme.background};
  519. position: absolute;
  520. right: 0;
  521. top: 50%;
  522. transform: translateY(-50%);
  523. `;