groupEventCarousel.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 FeatureBadge from 'sentry/components/featureBadge';
  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 {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 {useDefaultIssueEvent} from 'sentry/views/issueDetails/utils';
  44. import QuickTrace from './quickTrace';
  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: (
  136. <div>
  137. {t('Recommended')}
  138. <FeatureBadge type="new" />
  139. </div>
  140. ),
  141. textValue: t('Recommended'),
  142. details: t('Event with the most context'),
  143. },
  144. {
  145. value: EventNavDropdownOption.LATEST,
  146. label: t('Latest'),
  147. details: t('Last seen event in this issue'),
  148. },
  149. {
  150. value: EventNavDropdownOption.OLDEST,
  151. label: t('Oldest'),
  152. details: t('First seen event in this issue'),
  153. },
  154. ...(!selectedValue
  155. ? [
  156. {
  157. value: EventNavDropdownOption.CUSTOM,
  158. label: t('Custom Selection'),
  159. },
  160. ]
  161. : []),
  162. {
  163. options: [{value: EventNavDropdownOption.ALL, label: 'View All Events'}],
  164. },
  165. ];
  166. return (
  167. <GuideAnchor target="issue_details_default_event" position="bottom">
  168. <CompactSelect
  169. size="sm"
  170. disabled={isDisabled}
  171. options={eventNavDropdownOptions}
  172. value={!selectedValue ? EventNavDropdownOption.CUSTOM : selectedValue}
  173. triggerLabel={
  174. !selectedValue ? (
  175. <TimeSince
  176. date={event.dateCreated ?? event.dateReceived}
  177. disabledAbsoluteTooltip
  178. />
  179. ) : selectedValue === EventNavDropdownOption.RECOMMENDED ? (
  180. t('Recommended')
  181. ) : undefined
  182. }
  183. menuWidth={232}
  184. onChange={selectedOption => {
  185. trackAnalytics('issue_details.event_dropdown_option_selected', {
  186. organization,
  187. selected_event_type: selectedOption.value,
  188. from_event_type: selectedValue ?? EventNavDropdownOption.CUSTOM,
  189. event_id: event.id,
  190. group_id: group.id,
  191. });
  192. switch (selectedOption.value) {
  193. case EventNavDropdownOption.RECOMMENDED:
  194. case EventNavDropdownOption.LATEST:
  195. case EventNavDropdownOption.OLDEST:
  196. browserHistory.push({
  197. pathname: normalizeUrl(
  198. makeBaseEventsPath({organization, group}) + selectedOption.value + '/'
  199. ),
  200. query: {...location.query, referrer: `${selectedOption.value}-event`},
  201. });
  202. break;
  203. case EventNavDropdownOption.ALL:
  204. const searchTermWithoutQuery = omit(location.query, 'query');
  205. browserHistory.push({
  206. pathname: normalizeUrl(
  207. `/organizations/${organization.slug}/issues/${group.id}/events/`
  208. ),
  209. query: searchTermWithoutQuery,
  210. });
  211. break;
  212. default:
  213. break;
  214. }
  215. }}
  216. />
  217. </GuideAnchor>
  218. );
  219. }
  220. type GroupEventActionsProps = {
  221. event: Event;
  222. group: Group;
  223. projectSlug: string;
  224. };
  225. export function GroupEventActions({event, group, projectSlug}: GroupEventActionsProps) {
  226. const theme = useTheme();
  227. const xlargeViewport = useMedia(`(min-width: ${theme.breakpoints.xlarge})`);
  228. const organization = useOrganization();
  229. const hasReplay = Boolean(event?.tags?.find(({key}) => key === 'replayId')?.value);
  230. const isReplayEnabled =
  231. organization.features.includes('session-replay') &&
  232. projectCanLinkToReplay(group.project);
  233. const downloadJson = () => {
  234. const jsonUrl = `/api/0/projects/${organization.slug}/${projectSlug}/events/${event.id}/json/`;
  235. window.open(jsonUrl);
  236. trackAnalytics('issue_details.event_json_clicked', {
  237. organization,
  238. group_id: parseInt(`${event.groupID}`, 10),
  239. });
  240. };
  241. const {onClick: copyLink} = useCopyToClipboard({
  242. successMessage: t('Event URL copied to clipboard'),
  243. text:
  244. window.location.origin +
  245. normalizeUrl(`${makeBaseEventsPath({organization, group})}${event.id}/`),
  246. onCopy: () =>
  247. trackAnalytics('issue_details.copy_event_link_clicked', {
  248. organization,
  249. ...getAnalyticsDataForGroup(group),
  250. ...getAnalyticsDataForEvent(event),
  251. }),
  252. });
  253. const {onClick: copyEventId} = useCopyToClipboard({
  254. successMessage: t('Event ID copied to clipboard'),
  255. text: event.id,
  256. });
  257. return (
  258. <Fragment>
  259. <DropdownMenu
  260. position="bottom-end"
  261. triggerProps={{
  262. 'aria-label': t('Event Actions Menu'),
  263. icon: <IconEllipsis size="xs" />,
  264. showChevron: false,
  265. size: BUTTON_SIZE,
  266. }}
  267. items={[
  268. {
  269. key: 'copy-event-id',
  270. label: t('Copy Event ID'),
  271. onAction: copyEventId,
  272. },
  273. {
  274. key: 'copy-event-url',
  275. label: t('Copy Event Link'),
  276. hidden: xlargeViewport,
  277. onAction: copyLink,
  278. },
  279. {
  280. key: 'json',
  281. label: `JSON (${formatBytesBase2(event.size)})`,
  282. onAction: downloadJson,
  283. hidden: xlargeViewport,
  284. },
  285. {
  286. key: 'full-event-discover',
  287. label: t('Full Event Details'),
  288. hidden: !organization.features.includes('discover-basic'),
  289. to: eventDetailsRoute({
  290. eventSlug: generateEventSlug({project: projectSlug, id: event.id}),
  291. orgSlug: organization.slug,
  292. }),
  293. onAction: () => {
  294. trackAnalytics('issue_details.event_details_clicked', {
  295. organization,
  296. ...getAnalyticsDataForGroup(group),
  297. ...getAnalyticsDataForEvent(event),
  298. });
  299. },
  300. },
  301. {
  302. key: 'replay',
  303. label: t('View Replay'),
  304. hidden: !hasReplay || !isReplayEnabled,
  305. onAction: () => {
  306. const breadcrumbsHeader = document.getElementById('replay');
  307. if (breadcrumbsHeader) {
  308. breadcrumbsHeader.scrollIntoView({behavior: 'smooth'});
  309. }
  310. trackAnalytics('issue_details.header_view_replay_clicked', {
  311. organization,
  312. ...getAnalyticsDataForGroup(group),
  313. ...getAnalyticsDataForEvent(event),
  314. });
  315. },
  316. },
  317. ]}
  318. />
  319. {xlargeViewport && (
  320. <Button
  321. title={t('Copy link to this issue event')}
  322. size={BUTTON_SIZE}
  323. onClick={copyLink}
  324. aria-label={t('Copy Link')}
  325. icon={<IconLink />}
  326. />
  327. )}
  328. {xlargeViewport && (
  329. <Button
  330. title={t('View JSON')}
  331. size={BUTTON_SIZE}
  332. onClick={downloadJson}
  333. aria-label={t('View JSON')}
  334. icon={<IconJson />}
  335. />
  336. )}
  337. </Fragment>
  338. );
  339. }
  340. export function GroupEventCarousel({event, group, projectSlug}: GroupEventCarouselProps) {
  341. const organization = useOrganization();
  342. const location = useLocation();
  343. const latencyThreshold = 30 * 60 * 1000; // 30 minutes
  344. const isOverLatencyThreshold =
  345. event.dateReceived &&
  346. event.dateCreated &&
  347. Math.abs(+moment(event.dateReceived) - +moment(event.dateCreated)) > latencyThreshold;
  348. const hasPreviousEvent = defined(event.previousEventID);
  349. const hasNextEvent = defined(event.nextEventID);
  350. const {onClick: copyEventId} = useCopyToClipboard({
  351. successMessage: t('Event ID copied to clipboard'),
  352. text: event.id,
  353. });
  354. return (
  355. <CarouselAndButtonsWrapper>
  356. <div>
  357. <EventHeading>
  358. <EventIdAndTimeContainer>
  359. <EventIdContainer>
  360. <strong>Event ID:</strong>
  361. <Button
  362. aria-label={t('Copy')}
  363. borderless
  364. onClick={copyEventId}
  365. size="zero"
  366. title={event.id}
  367. tooltipProps={{overlayStyle: {maxWidth: 'max-content'}}}
  368. translucentBorder
  369. >
  370. <EventId>
  371. {getShortEventId(event.id)}
  372. <CopyIconContainer>
  373. <IconCopy size="xs" />
  374. </CopyIconContainer>
  375. </EventId>
  376. </Button>
  377. </EventIdContainer>
  378. {(event.dateCreated ?? event.dateReceived) && (
  379. <EventTimeLabel>
  380. {getDynamicText({
  381. fixed: 'Jan 1, 12:00 AM',
  382. value: (
  383. <Tooltip
  384. isHoverable
  385. showUnderline
  386. title={<EventCreatedTooltip event={event} />}
  387. overlayStyle={{maxWidth: 300}}
  388. >
  389. <DateTime date={event.dateCreated ?? event.dateReceived} />
  390. </Tooltip>
  391. ),
  392. })}
  393. {isOverLatencyThreshold && (
  394. <Tooltip title="High latency">
  395. <StyledIconWarning size="xs" color="warningText" />
  396. </Tooltip>
  397. )}
  398. </EventTimeLabel>
  399. )}
  400. </EventIdAndTimeContainer>
  401. </EventHeading>
  402. <QuickTrace event={event} organization={organization} location={location} />
  403. </div>
  404. <ActionsWrapper>
  405. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  406. <EventNavigationDropdown
  407. isDisabled={!hasPreviousEvent && !hasNextEvent}
  408. group={group}
  409. event={event}
  410. />
  411. <NavButtons>
  412. <EventNavigationButton
  413. group={group}
  414. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  415. disabled={!hasPreviousEvent}
  416. title={t('Previous Event')}
  417. eventId={event.previousEventID}
  418. referrer="previous-event"
  419. />
  420. <EventNavigationButton
  421. group={group}
  422. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  423. disabled={!hasNextEvent}
  424. title={t('Next Event')}
  425. eventId={event.nextEventID}
  426. referrer="next-event"
  427. />
  428. </NavButtons>
  429. </ActionsWrapper>
  430. </CarouselAndButtonsWrapper>
  431. );
  432. }
  433. const CarouselAndButtonsWrapper = styled('div')`
  434. display: flex;
  435. justify-content: space-between;
  436. align-items: flex-start;
  437. gap: ${space(1)};
  438. margin-bottom: ${space(0.5)};
  439. `;
  440. const EventHeading = styled('div')`
  441. display: flex;
  442. align-items: center;
  443. flex-wrap: wrap;
  444. gap: ${space(1)};
  445. font-size: ${p => p.theme.fontSizeLarge};
  446. @media (max-width: 600px) {
  447. font-size: ${p => p.theme.fontSizeMedium};
  448. }
  449. `;
  450. const ActionsWrapper = styled('div')`
  451. display: flex;
  452. align-items: center;
  453. gap: ${space(0.5)};
  454. `;
  455. const StyledNavButton = styled(Button)`
  456. border-radius: 0;
  457. `;
  458. const NavButtons = styled('div')`
  459. display: flex;
  460. > * {
  461. &:not(:last-child) {
  462. ${StyledNavButton} {
  463. border-right: none;
  464. }
  465. }
  466. &:first-child {
  467. ${StyledNavButton} {
  468. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  469. }
  470. }
  471. &:last-child {
  472. ${StyledNavButton} {
  473. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  474. }
  475. }
  476. }
  477. `;
  478. const EventIdAndTimeContainer = styled('div')`
  479. display: flex;
  480. align-items: center;
  481. column-gap: ${space(0.75)};
  482. row-gap: 0;
  483. flex-wrap: wrap;
  484. `;
  485. const EventIdContainer = styled('div')`
  486. display: flex;
  487. align-items: center;
  488. column-gap: ${space(0.25)};
  489. `;
  490. const EventTimeLabel = styled('span')`
  491. color: ${p => p.theme.subText};
  492. `;
  493. const StyledIconWarning = styled(IconWarning)`
  494. margin-left: ${space(0.25)};
  495. position: relative;
  496. top: 1px;
  497. `;
  498. const EventId = styled('span')`
  499. position: relative;
  500. font-weight: normal;
  501. font-size: ${p => p.theme.fontSizeLarge};
  502. &:hover {
  503. > span {
  504. display: flex;
  505. }
  506. }
  507. @media (max-width: 600px) {
  508. font-size: ${p => p.theme.fontSizeMedium};
  509. }
  510. `;
  511. const CopyIconContainer = styled('span')`
  512. display: none;
  513. align-items: center;
  514. padding: ${space(0.25)};
  515. background: ${p => p.theme.background};
  516. position: absolute;
  517. right: 0;
  518. top: 50%;
  519. transform: translateY(-50%);
  520. `;