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 {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 host = organization.links.regionUrl;
  229. const jsonUrl = `${host}/api/0/projects/${organization.slug}/${projectSlug}/events/${event.id}/json/`;
  230. window.open(jsonUrl);
  231. trackAnalytics('issue_details.event_json_clicked', {
  232. organization,
  233. group_id: parseInt(`${event.groupID}`, 10),
  234. });
  235. };
  236. const {onClick: copyLink} = useCopyToClipboard({
  237. successMessage: t('Event URL copied to clipboard'),
  238. text:
  239. window.location.origin +
  240. normalizeUrl(`${makeBaseEventsPath({organization, group})}${event.id}/`),
  241. onCopy: () =>
  242. trackAnalytics('issue_details.copy_event_link_clicked', {
  243. organization,
  244. ...getAnalyticsDataForGroup(group),
  245. ...getAnalyticsDataForEvent(event),
  246. }),
  247. });
  248. const {onClick: copyEventId} = useCopyToClipboard({
  249. successMessage: t('Event ID copied to clipboard'),
  250. text: event.id,
  251. });
  252. return (
  253. <Fragment>
  254. <DropdownMenu
  255. position="bottom-end"
  256. triggerProps={{
  257. 'aria-label': t('Event Actions Menu'),
  258. icon: <IconEllipsis size="xs" />,
  259. showChevron: false,
  260. size: BUTTON_SIZE,
  261. }}
  262. items={[
  263. {
  264. key: 'copy-event-id',
  265. label: t('Copy Event ID'),
  266. onAction: copyEventId,
  267. },
  268. {
  269. key: 'copy-event-url',
  270. label: t('Copy Event Link'),
  271. hidden: xlargeViewport,
  272. onAction: copyLink,
  273. },
  274. {
  275. key: 'json',
  276. label: `JSON (${formatBytesBase2(event.size)})`,
  277. onAction: downloadJson,
  278. hidden: xlargeViewport,
  279. },
  280. {
  281. key: 'full-event-discover',
  282. label: t('Full Event Details'),
  283. hidden: !organization.features.includes('discover-basic'),
  284. to: eventDetailsRoute({
  285. eventSlug: generateEventSlug({project: projectSlug, id: event.id}),
  286. orgSlug: organization.slug,
  287. }),
  288. onAction: () => {
  289. trackAnalytics('issue_details.event_details_clicked', {
  290. organization,
  291. ...getAnalyticsDataForGroup(group),
  292. ...getAnalyticsDataForEvent(event),
  293. });
  294. },
  295. },
  296. {
  297. key: 'replay',
  298. label: t('View Replay'),
  299. hidden: !hasReplay || !isReplayEnabled,
  300. onAction: () => {
  301. const breadcrumbsHeader = document.getElementById('replay');
  302. if (breadcrumbsHeader) {
  303. breadcrumbsHeader.scrollIntoView({behavior: 'smooth'});
  304. }
  305. trackAnalytics('issue_details.header_view_replay_clicked', {
  306. organization,
  307. ...getAnalyticsDataForGroup(group),
  308. ...getAnalyticsDataForEvent(event),
  309. });
  310. },
  311. },
  312. ]}
  313. />
  314. {xlargeViewport && (
  315. <Button
  316. title={t('Copy link to this issue event')}
  317. size={BUTTON_SIZE}
  318. onClick={copyLink}
  319. aria-label={t('Copy Link')}
  320. icon={<IconLink />}
  321. />
  322. )}
  323. {xlargeViewport && (
  324. <Button
  325. title={t('View JSON')}
  326. size={BUTTON_SIZE}
  327. onClick={downloadJson}
  328. aria-label={t('View JSON')}
  329. icon={<IconJson />}
  330. />
  331. )}
  332. </Fragment>
  333. );
  334. }
  335. export function GroupEventCarousel({event, group, projectSlug}: GroupEventCarouselProps) {
  336. const organization = useOrganization();
  337. const location = useLocation();
  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. return (
  350. <CarouselAndButtonsWrapper>
  351. <div>
  352. <EventHeading>
  353. <EventIdAndTimeContainer>
  354. <EventIdContainer>
  355. <strong>Event ID:</strong>
  356. <Button
  357. aria-label={t('Copy')}
  358. borderless
  359. onClick={copyEventId}
  360. size="zero"
  361. title={event.id}
  362. tooltipProps={{overlayStyle: {maxWidth: 'max-content'}}}
  363. translucentBorder
  364. >
  365. <EventId>
  366. {getShortEventId(event.id)}
  367. <CopyIconContainer>
  368. <IconCopy size="xs" />
  369. </CopyIconContainer>
  370. </EventId>
  371. </Button>
  372. </EventIdContainer>
  373. {(event.dateCreated ?? event.dateReceived) && (
  374. <EventTimeLabel>
  375. {getDynamicText({
  376. fixed: 'Jan 1, 12:00 AM',
  377. value: (
  378. <Tooltip
  379. isHoverable
  380. showUnderline
  381. title={<EventCreatedTooltip event={event} />}
  382. overlayStyle={{maxWidth: 300}}
  383. >
  384. <DateTime date={event.dateCreated ?? event.dateReceived} />
  385. </Tooltip>
  386. ),
  387. })}
  388. {isOverLatencyThreshold && (
  389. <Tooltip title="High latency">
  390. <StyledIconWarning size="xs" color="warningText" />
  391. </Tooltip>
  392. )}
  393. </EventTimeLabel>
  394. )}
  395. </EventIdAndTimeContainer>
  396. </EventHeading>
  397. <QuickTrace event={event} organization={organization} location={location} />
  398. </div>
  399. <ActionsWrapper>
  400. <GroupEventActions event={event} group={group} projectSlug={projectSlug} />
  401. <EventNavigationDropdown
  402. isDisabled={!hasPreviousEvent && !hasNextEvent}
  403. group={group}
  404. event={event}
  405. />
  406. <NavButtons>
  407. <EventNavigationButton
  408. group={group}
  409. icon={<IconChevron direction="left" size={BUTTON_ICON_SIZE} />}
  410. disabled={!hasPreviousEvent}
  411. title={t('Previous Event')}
  412. eventId={event.previousEventID}
  413. referrer="previous-event"
  414. />
  415. <EventNavigationButton
  416. group={group}
  417. icon={<IconChevron direction="right" size={BUTTON_ICON_SIZE} />}
  418. disabled={!hasNextEvent}
  419. title={t('Next Event')}
  420. eventId={event.nextEventID}
  421. referrer="next-event"
  422. />
  423. </NavButtons>
  424. </ActionsWrapper>
  425. </CarouselAndButtonsWrapper>
  426. );
  427. }
  428. const CarouselAndButtonsWrapper = styled('div')`
  429. display: flex;
  430. justify-content: space-between;
  431. align-items: flex-start;
  432. gap: ${space(1)};
  433. margin-bottom: ${space(0.5)};
  434. `;
  435. const EventHeading = styled('div')`
  436. display: flex;
  437. align-items: center;
  438. flex-wrap: wrap;
  439. gap: ${space(1)};
  440. font-size: ${p => p.theme.fontSizeLarge};
  441. @media (max-width: 600px) {
  442. font-size: ${p => p.theme.fontSizeMedium};
  443. }
  444. `;
  445. const ActionsWrapper = styled('div')`
  446. display: flex;
  447. align-items: center;
  448. gap: ${space(0.5)};
  449. `;
  450. const StyledNavButton = styled(Button)`
  451. border-radius: 0;
  452. `;
  453. const NavButtons = styled('div')`
  454. display: flex;
  455. > * {
  456. &:not(:last-child) {
  457. ${StyledNavButton} {
  458. border-right: none;
  459. }
  460. }
  461. &:first-child {
  462. ${StyledNavButton} {
  463. border-radius: ${p => p.theme.borderRadius} 0 0 ${p => p.theme.borderRadius};
  464. }
  465. }
  466. &:last-child {
  467. ${StyledNavButton} {
  468. border-radius: 0 ${p => p.theme.borderRadius} ${p => p.theme.borderRadius} 0;
  469. }
  470. }
  471. }
  472. `;
  473. const EventIdAndTimeContainer = styled('div')`
  474. display: flex;
  475. align-items: center;
  476. column-gap: ${space(0.75)};
  477. row-gap: 0;
  478. flex-wrap: wrap;
  479. `;
  480. const EventIdContainer = styled('div')`
  481. display: flex;
  482. align-items: center;
  483. column-gap: ${space(0.25)};
  484. `;
  485. const EventTimeLabel = styled('span')`
  486. color: ${p => p.theme.subText};
  487. `;
  488. const StyledIconWarning = styled(IconWarning)`
  489. margin-left: ${space(0.25)};
  490. position: relative;
  491. top: 1px;
  492. `;
  493. const EventId = styled('span')`
  494. position: relative;
  495. font-weight: normal;
  496. font-size: ${p => p.theme.fontSizeLarge};
  497. &:hover {
  498. > span {
  499. display: flex;
  500. }
  501. }
  502. @media (max-width: 600px) {
  503. font-size: ${p => p.theme.fontSizeMedium};
  504. }
  505. `;
  506. const CopyIconContainer = styled('span')`
  507. display: none;
  508. align-items: center;
  509. padding: ${space(0.25)};
  510. background: ${p => p.theme.background};
  511. position: absolute;
  512. right: 0;
  513. top: 50%;
  514. transform: translateY(-50%);
  515. `;