groupEventCarousel.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import omit from 'lodash/omit';
  5. import moment from 'moment-timezone';
  6. import type {ButtonProps} from 'sentry/components/button';
  7. import {Button, LinkButton} from 'sentry/components/button';
  8. import {CompactSelect} from 'sentry/components/compactSelect';
  9. import {DateTime} from 'sentry/components/dateTime';
  10. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  11. import TimeSince from 'sentry/components/timeSince';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {
  14. IconChevron,
  15. IconCopy,
  16. IconEllipsis,
  17. IconJson,
  18. IconLink,
  19. IconWarning,
  20. } from 'sentry/icons';
  21. import {t} from 'sentry/locale';
  22. import {space} from 'sentry/styles/space';
  23. import type {Event} from 'sentry/types/event';
  24. import type {Group} from 'sentry/types/group';
  25. import type {Organization} from 'sentry/types/organization';
  26. import {defined} from 'sentry/utils';
  27. import {trackAnalytics} from 'sentry/utils/analytics';
  28. import {browserHistory} from 'sentry/utils/browserHistory';
  29. import {formatBytesBase2} from 'sentry/utils/bytes/formatBytesBase2';
  30. import {eventDetailsRoute, generateEventSlug} from 'sentry/utils/discover/urls';
  31. import {
  32. getAnalyticsDataForEvent,
  33. getAnalyticsDataForGroup,
  34. getShortEventId,
  35. } from 'sentry/utils/events';
  36. import getDynamicText from 'sentry/utils/getDynamicText';
  37. import {getReplayIdFromEvent} from 'sentry/utils/replays/getReplayIdFromEvent';
  38. import {projectCanLinkToReplay} from 'sentry/utils/replays/projectSupportsReplay';
  39. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  40. import useCopyToClipboard from 'sentry/utils/useCopyToClipboard';
  41. import {useLocation} from 'sentry/utils/useLocation';
  42. import useMedia from 'sentry/utils/useMedia';
  43. import useOrganization from 'sentry/utils/useOrganization';
  44. import {useParams} from 'sentry/utils/useParams';
  45. import EventCreatedTooltip from 'sentry/views/issueDetails/eventCreatedTooltip';
  46. import {useDefaultIssueEvent} from 'sentry/views/issueDetails/utils';
  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. <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. );
  214. }
  215. type GroupEventActionsProps = {
  216. event: Event;
  217. group: Group;
  218. projectSlug: string;
  219. };
  220. export function GroupEventActions({event, group, projectSlug}: GroupEventActionsProps) {
  221. const theme = useTheme();
  222. const xlargeViewport = useMedia(`(min-width: ${theme.breakpoints.xlarge})`);
  223. const organization = useOrganization();
  224. const hasReplay = Boolean(getReplayIdFromEvent(event));
  225. const isReplayEnabled =
  226. organization.features.includes('session-replay') &&
  227. projectCanLinkToReplay(organization, group.project);
  228. const downloadJson = () => {
  229. const host = organization.links.regionUrl;
  230. const jsonUrl = `${host}/api/0/projects/${organization.slug}/${projectSlug}/events/${event.id}/json/`;
  231. window.open(jsonUrl);
  232. trackAnalytics('issue_details.event_json_clicked', {
  233. organization,
  234. group_id: parseInt(`${event.groupID}`, 10),
  235. });
  236. };
  237. const {onClick: copyLink} = useCopyToClipboard({
  238. successMessage: t('Event URL copied to clipboard'),
  239. text:
  240. window.location.origin +
  241. normalizeUrl(`${makeBaseEventsPath({organization, group})}${event.id}/`),
  242. onCopy: () =>
  243. trackAnalytics('issue_details.copy_event_link_clicked', {
  244. organization,
  245. ...getAnalyticsDataForGroup(group),
  246. ...getAnalyticsDataForEvent(event),
  247. }),
  248. });
  249. const {onClick: copyEventId} = useCopyToClipboard({
  250. successMessage: t('Event ID copied to clipboard'),
  251. text: event.id,
  252. });
  253. return (
  254. <Fragment>
  255. <DropdownMenu
  256. position="bottom-end"
  257. triggerProps={{
  258. 'aria-label': t('Event Actions Menu'),
  259. icon: <IconEllipsis />,
  260. showChevron: false,
  261. size: BUTTON_SIZE,
  262. }}
  263. items={[
  264. {
  265. key: 'copy-event-id',
  266. label: t('Copy Event ID'),
  267. onAction: copyEventId,
  268. },
  269. {
  270. key: 'copy-event-url',
  271. label: t('Copy Event Link'),
  272. hidden: xlargeViewport,
  273. onAction: copyLink,
  274. },
  275. {
  276. key: 'json',
  277. label: `JSON (${formatBytesBase2(event.size)})`,
  278. onAction: downloadJson,
  279. hidden: xlargeViewport,
  280. },
  281. {
  282. key: 'full-event-discover',
  283. label: t('Full Event Details'),
  284. hidden: !organization.features.includes('discover-basic'),
  285. to: eventDetailsRoute({
  286. eventSlug: generateEventSlug({project: projectSlug, id: event.id}),
  287. orgSlug: organization.slug,
  288. }),
  289. onAction: () => {
  290. trackAnalytics('issue_details.event_details_clicked', {
  291. organization,
  292. ...getAnalyticsDataForGroup(group),
  293. ...getAnalyticsDataForEvent(event),
  294. });
  295. },
  296. },
  297. {
  298. key: 'replay',
  299. label: t('View Replay'),
  300. hidden: !hasReplay || !isReplayEnabled,
  301. onAction: () => {
  302. const breadcrumbsHeader = document.getElementById('replay');
  303. if (breadcrumbsHeader) {
  304. breadcrumbsHeader.scrollIntoView({behavior: 'smooth'});
  305. }
  306. trackAnalytics('issue_details.header_view_replay_clicked', {
  307. organization,
  308. ...getAnalyticsDataForGroup(group),
  309. ...getAnalyticsDataForEvent(event),
  310. });
  311. },
  312. },
  313. ]}
  314. />
  315. {xlargeViewport && (
  316. <Button
  317. title={t('Copy link to this issue event')}
  318. size={BUTTON_SIZE}
  319. onClick={copyLink}
  320. aria-label={t('Copy Link')}
  321. icon={<IconLink />}
  322. />
  323. )}
  324. {xlargeViewport && (
  325. <Button
  326. title={t('View JSON')}
  327. size={BUTTON_SIZE}
  328. onClick={downloadJson}
  329. aria-label={t('View JSON')}
  330. icon={<IconJson />}
  331. />
  332. )}
  333. </Fragment>
  334. );
  335. }
  336. export function GroupEventCarousel({event, group, projectSlug}: GroupEventCarouselProps) {
  337. const latencyThreshold = 30 * 60 * 1000; // 30 minutes
  338. const isOverLatencyThreshold =
  339. event.dateReceived &&
  340. event.dateCreated &&
  341. Math.abs(+moment(event.dateReceived) - +moment(event.dateCreated)) > latencyThreshold;
  342. const hasPreviousEvent = defined(event.previousEventID);
  343. const hasNextEvent = defined(event.nextEventID);
  344. const {onClick: copyEventId} = useCopyToClipboard({
  345. successMessage: t('Event ID copied to clipboard'),
  346. text: event.id,
  347. });
  348. return (
  349. <CarouselAndButtonsWrapper>
  350. <div>
  351. <EventHeading>
  352. <EventIdAndTimeContainer>
  353. <EventIdContainer>
  354. <strong>Event ID:</strong>
  355. <Button
  356. aria-label={t('Copy')}
  357. borderless
  358. onClick={copyEventId}
  359. size="zero"
  360. title={event.id}
  361. tooltipProps={{overlayStyle: {maxWidth: 'max-content'}}}
  362. translucentBorder
  363. >
  364. <EventId>
  365. {getShortEventId(event.id)}
  366. <CopyIconContainer>
  367. <IconCopy size="xs" />
  368. </CopyIconContainer>
  369. </EventId>
  370. </Button>
  371. </EventIdContainer>
  372. {(event.dateCreated ?? event.dateReceived) && (
  373. <EventTimeLabel>
  374. {getDynamicText({
  375. fixed: 'Jan 1, 12:00 AM',
  376. value: (
  377. <Tooltip
  378. isHoverable
  379. showUnderline
  380. title={<EventCreatedTooltip event={event} />}
  381. overlayStyle={{maxWidth: 300}}
  382. >
  383. <DateTime date={event.dateCreated ?? event.dateReceived} />
  384. </Tooltip>
  385. ),
  386. })}
  387. {isOverLatencyThreshold && (
  388. <Tooltip title="High latency">
  389. <StyledIconWarning size="xs" color="warningText" />
  390. </Tooltip>
  391. )}
  392. </EventTimeLabel>
  393. )}
  394. </EventIdAndTimeContainer>
  395. </EventHeading>
  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(LinkButton)`
  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: ${p => p.theme.fontWeightNormal};
  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. `;