groupEventCarousel.tsx 16 KB

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