groupEventCarousel.tsx 16 KB

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