groupEventCarousel.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 {Button, ButtonProps} 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 {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 {projectCanLinkToReplay} from 'sentry/utils/replays/projectSupportsReplay';
  35. import useCopyToClipboard from 'sentry/utils/useCopyToClipboard';
  36. import {useLocation} from 'sentry/utils/useLocation';
  37. import useMedia from 'sentry/utils/useMedia';
  38. import useOrganization from 'sentry/utils/useOrganization';
  39. import {useParams} from 'sentry/utils/useParams';
  40. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  41. import EventCreatedTooltip from 'sentry/views/issueDetails/eventCreatedTooltip';
  42. import {useDefaultIssueEvent} from 'sentry/views/issueDetails/utils';
  43. import QuickTrace from './quickTrace';
  44. type GroupEventCarouselProps = {
  45. event: Event;
  46. group: Group;
  47. projectSlug: string;
  48. };
  49. type GroupEventNavigationProps = {
  50. event: Event;
  51. group: Group;
  52. isDisabled: boolean;
  53. };
  54. type EventNavigationButtonProps = {
  55. disabled: boolean;
  56. group: Group;
  57. icon: ButtonProps['icon'];
  58. referrer: string;
  59. title: string;
  60. eventId?: string | null;
  61. };
  62. enum EventNavDropdownOption {
  63. RECOMMENDED = 'recommended',
  64. LATEST = 'latest',
  65. OLDEST = 'oldest',
  66. CUSTOM = 'custom',
  67. ALL = 'all',
  68. }
  69. const BUTTON_SIZE = 'sm';
  70. const BUTTON_ICON_SIZE = 'sm';
  71. const makeBaseEventsPath = ({
  72. organization,
  73. group,
  74. }: {
  75. group: Group;
  76. organization: Organization;
  77. }) => `/organizations/${organization.slug}/issues/${group.id}/events/`;
  78. function EventNavigationButton({
  79. disabled,
  80. eventId,
  81. group,
  82. icon,
  83. title,
  84. referrer,
  85. }: EventNavigationButtonProps) {
  86. const organization = useOrganization();
  87. const location = useLocation();
  88. const baseEventsPath = makeBaseEventsPath({organization, group});
  89. // Need to wrap with Tooltip because our version of React Router doesn't allow access
  90. // to the anchor ref which is needed by Tooltip to position correctly.
  91. return (
  92. <Tooltip title={title} disabled={disabled} skipWrapper>
  93. <div>
  94. <StyledNavButton
  95. size={BUTTON_SIZE}
  96. icon={icon}
  97. aria-label={title}
  98. to={{
  99. pathname: `${baseEventsPath}${eventId}/`,
  100. query: {...location.query, referrer},
  101. }}
  102. disabled={disabled}
  103. />
  104. </div>
  105. </Tooltip>
  106. );
  107. }
  108. function EventNavigationDropdown({group, event, isDisabled}: GroupEventNavigationProps) {
  109. const location = useLocation();
  110. const params = useParams<{eventId?: string}>();
  111. const theme = useTheme();
  112. const organization = useOrganization();
  113. const largeViewport = useMedia(`(min-width: ${theme.breakpoints.large})`);
  114. const defaultIssueEvent = useDefaultIssueEvent();
  115. if (!largeViewport) {
  116. return null;
  117. }
  118. const getSelectedOption = () => {
  119. switch (params.eventId) {
  120. case EventNavDropdownOption.RECOMMENDED:
  121. case EventNavDropdownOption.LATEST:
  122. case EventNavDropdownOption.OLDEST:
  123. return params.eventId;
  124. case undefined:
  125. return defaultIssueEvent;
  126. default:
  127. return undefined;
  128. }
  129. };
  130. const selectedValue = getSelectedOption();
  131. const eventNavDropdownOptions = [
  132. {
  133. value: EventNavDropdownOption.RECOMMENDED,
  134. label: t('Recommended'),
  135. textValue: t('Recommended'),
  136. details: t('Event with the most context'),
  137. },
  138. {
  139. value: EventNavDropdownOption.LATEST,
  140. label: t('Latest'),
  141. details: t('Last seen event in this issue'),
  142. },
  143. {
  144. value: EventNavDropdownOption.OLDEST,
  145. label: t('Oldest'),
  146. details: t('First seen event in this issue'),
  147. },
  148. ...(!selectedValue
  149. ? [
  150. {
  151. value: EventNavDropdownOption.CUSTOM,
  152. label: t('Custom Selection'),
  153. },
  154. ]
  155. : []),
  156. {
  157. options: [{value: EventNavDropdownOption.ALL, label: 'View All Events'}],
  158. },
  159. ];
  160. return (
  161. <GuideAnchor target="issue_details_default_event" position="bottom">
  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. </GuideAnchor>
  212. );
  213. }
  214. type GroupEventActionsProps = {
  215. event: Event;
  216. group: Group;
  217. projectSlug: string;
  218. };
  219. export function GroupEventActions({event, group, projectSlug}: GroupEventActionsProps) {
  220. const theme = useTheme();
  221. const xlargeViewport = useMedia(`(min-width: ${theme.breakpoints.xlarge})`);
  222. const organization = useOrganization();
  223. const hasReplay = Boolean(event?.tags?.find(({key}) => key === 'replayId')?.value);
  224. const isReplayEnabled =
  225. organization.features.includes('session-replay') &&
  226. projectCanLinkToReplay(group.project);
  227. const downloadJson = () => {
  228. const jsonUrl = `/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 size="xs" />,
  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 organization = useOrganization();
  336. const location = useLocation();
  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. <QuickTrace event={event} organization={organization} location={location} />
  397. </div>
  398. <ActionsWrapper>
  399. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  400. <EventNavigationDropdown
  401. isDisabled={!hasPreviousEvent && !hasNextEvent}
  402. group={group}
  403. event={event}
  404. />
  405. <NavButtons>
  406. <EventNavigationButton
  407. group={group}
  408. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  409. disabled={!hasPreviousEvent}
  410. title={t('Previous Event')}
  411. eventId={event.previousEventID}
  412. referrer="previous-event"
  413. />
  414. <EventNavigationButton
  415. group={group}
  416. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  417. disabled={!hasNextEvent}
  418. title={t('Next Event')}
  419. eventId={event.nextEventID}
  420. referrer="next-event"
  421. />
  422. </NavButtons>
  423. </ActionsWrapper>
  424. </CarouselAndButtonsWrapper>
  425. );
  426. }
  427. const CarouselAndButtonsWrapper = styled('div')`
  428. display: flex;
  429. justify-content: space-between;
  430. align-items: flex-start;
  431. gap: ${space(1)};
  432. margin-bottom: ${space(0.5)};
  433. `;
  434. const EventHeading = styled('div')`
  435. display: flex;
  436. align-items: center;
  437. flex-wrap: wrap;
  438. gap: ${space(1)};
  439. font-size: ${p => p.theme.fontSizeLarge};
  440. @media (max-width: 600px) {
  441. font-size: ${p => p.theme.fontSizeMedium};
  442. }
  443. `;
  444. const ActionsWrapper = styled('div')`
  445. display: flex;
  446. align-items: center;
  447. gap: ${space(0.5)};
  448. `;
  449. const StyledNavButton = styled(Button)`
  450. border-radius: 0;
  451. `;
  452. const NavButtons = styled('div')`
  453. display: flex;
  454. > * {
  455. &:not(:last-child) {
  456. ${StyledNavButton} {
  457. border-right: none;
  458. }
  459. }
  460. &:first-child {
  461. ${StyledNavButton} {
  462. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  463. }
  464. }
  465. &:last-child {
  466. ${StyledNavButton} {
  467. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  468. }
  469. }
  470. }
  471. `;
  472. const EventIdAndTimeContainer = styled('div')`
  473. display: flex;
  474. align-items: center;
  475. column-gap: ${space(0.75)};
  476. row-gap: 0;
  477. flex-wrap: wrap;
  478. `;
  479. const EventIdContainer = styled('div')`
  480. display: flex;
  481. align-items: center;
  482. column-gap: ${space(0.25)};
  483. `;
  484. const EventTimeLabel = styled('span')`
  485. color: ${p => p.theme.subText};
  486. `;
  487. const StyledIconWarning = styled(IconWarning)`
  488. margin-left: ${space(0.25)};
  489. position: relative;
  490. top: 1px;
  491. `;
  492. const EventId = styled('span')`
  493. position: relative;
  494. font-weight: normal;
  495. font-size: ${p => p.theme.fontSizeLarge};
  496. &:hover {
  497. > span {
  498. display: flex;
  499. }
  500. }
  501. @media (max-width: 600px) {
  502. font-size: ${p => p.theme.fontSizeMedium};
  503. }
  504. `;
  505. const CopyIconContainer = styled('span')`
  506. display: none;
  507. align-items: center;
  508. padding: ${space(0.25)};
  509. background: ${p => p.theme.background};
  510. position: absolute;
  511. right: 0;
  512. top: 50%;
  513. transform: translateY(-50%);
  514. `;