groupEventCarousel.tsx 17 KB

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