eventNavigation.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import {Fragment, useCallback, useEffect, useMemo, useState} from 'react';
  2. import {css, useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {LinkButton} from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import Count from 'sentry/components/count';
  7. import DropdownButton from 'sentry/components/dropdownButton';
  8. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  9. import {TabList, Tabs} from 'sentry/components/tabs';
  10. import {Tooltip} from 'sentry/components/tooltip';
  11. import {IconChevron, IconTelescope} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import type {Event} from 'sentry/types/event';
  15. import type {Group} from 'sentry/types/group';
  16. import {defined} from 'sentry/utils';
  17. import {trackAnalytics} from 'sentry/utils/analytics';
  18. import {SavedQueryDatasets} from 'sentry/utils/discover/types';
  19. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  20. import parseLinkHeader from 'sentry/utils/parseLinkHeader';
  21. import {keepPreviousData, useApiQuery} from 'sentry/utils/queryClient';
  22. import useReplayCountForIssues from 'sentry/utils/replayCount/useReplayCountForIssues';
  23. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  24. import {useLocation} from 'sentry/utils/useLocation';
  25. import useMedia from 'sentry/utils/useMedia';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import {useParams} from 'sentry/utils/useParams';
  28. import {hasDatasetSelector} from 'sentry/views/dashboards/utils';
  29. import {useGroupEventAttachments} from 'sentry/views/issueDetails/groupEventAttachments/useGroupEventAttachments';
  30. import {useIssueDetailsEventView} from 'sentry/views/issueDetails/streamline/useIssueDetailsDiscoverQuery';
  31. import {Tab, TabPaths} from 'sentry/views/issueDetails/types';
  32. import {useGroupDetailsRoute} from 'sentry/views/issueDetails/useGroupDetailsRoute';
  33. import {
  34. getGroupEventQueryKey,
  35. useDefaultIssueEvent,
  36. useEnvironmentsFromUrl,
  37. } from 'sentry/views/issueDetails/utils';
  38. const enum EventNavOptions {
  39. RECOMMENDED = 'recommended',
  40. LATEST = 'latest',
  41. OLDEST = 'oldest',
  42. CUSTOM = 'custom',
  43. }
  44. const EventNavOrder = [
  45. EventNavOptions.OLDEST,
  46. EventNavOptions.LATEST,
  47. EventNavOptions.RECOMMENDED,
  48. EventNavOptions.CUSTOM,
  49. ];
  50. const TabName = {
  51. [Tab.DETAILS]: t('Events'),
  52. [Tab.EVENTS]: t('Events'),
  53. [Tab.REPLAYS]: t('Replays'),
  54. [Tab.ATTACHMENTS]: t('Attachments'),
  55. [Tab.USER_FEEDBACK]: t('Feedback'),
  56. };
  57. interface IssueEventNavigationProps {
  58. event: Event | undefined;
  59. group: Group;
  60. query: string | undefined;
  61. }
  62. export function IssueEventNavigation({event, group, query}: IssueEventNavigationProps) {
  63. const theme = useTheme();
  64. const organization = useOrganization();
  65. const {baseUrl, currentTab} = useGroupDetailsRoute();
  66. const location = useLocation();
  67. const params = useParams<{eventId?: string}>();
  68. const defaultIssueEvent = useDefaultIssueEvent();
  69. const isSmallScreen = useMedia(`(max-width: ${theme.breakpoints.small})`);
  70. const [shouldPreload, setShouldPreload] = useState({next: false, previous: false});
  71. const environments = useEnvironmentsFromUrl();
  72. const eventView = useIssueDetailsEventView({group});
  73. const issueTypeConfig = getConfigForIssueType(group, group.project);
  74. const hideDropdownButton =
  75. !issueTypeConfig.attachments.enabled &&
  76. !issueTypeConfig.userFeedback.enabled &&
  77. !issueTypeConfig.replays.enabled;
  78. const discoverUrl = eventView.getResultsViewUrlTarget(
  79. organization.slug,
  80. false,
  81. hasDatasetSelector(organization) ? SavedQueryDatasets.ERRORS : undefined
  82. );
  83. // Reset shouldPreload when the groupId changes
  84. useEffect(() => {
  85. setShouldPreload({next: false, previous: false});
  86. }, [group.id]);
  87. const handleHoverPagination = useCallback(
  88. (direction: 'next' | 'previous', isEnabled: boolean) => () => {
  89. if (isEnabled) {
  90. setShouldPreload(prev => ({...prev, [direction]: true}));
  91. }
  92. },
  93. []
  94. );
  95. // Prefetch next
  96. useApiQuery(
  97. getGroupEventQueryKey({
  98. orgSlug: organization.slug,
  99. groupId: group.id,
  100. // Will be defined when enabled
  101. eventId: event?.nextEventID!,
  102. environments,
  103. }),
  104. {
  105. enabled: shouldPreload.next && defined(event?.nextEventID),
  106. staleTime: Infinity,
  107. // Ignore state changes from the query
  108. notifyOnChangeProps: [],
  109. }
  110. );
  111. // Prefetch previous
  112. useApiQuery(
  113. getGroupEventQueryKey({
  114. orgSlug: organization.slug,
  115. groupId: group.id,
  116. // Will be defined when enabled
  117. eventId: event?.previousEventID!,
  118. environments,
  119. }),
  120. {
  121. enabled: shouldPreload.previous && defined(event?.previousEventID),
  122. staleTime: Infinity,
  123. // Ignore state changes from the query
  124. notifyOnChangeProps: [],
  125. }
  126. );
  127. const {getReplayCountForIssue} = useReplayCountForIssues({
  128. statsPeriod: '90d',
  129. });
  130. const replaysCount = getReplayCountForIssue(group.id, group.issueCategory) ?? 0;
  131. const attachments = useGroupEventAttachments({
  132. groupId: group.id,
  133. activeAttachmentsTab: 'all',
  134. options: {placeholderData: keepPreviousData},
  135. });
  136. const attachmentPagination = parseLinkHeader(
  137. attachments.getResponseHeader?.('Link') ?? null
  138. );
  139. // Since we reuse whatever page the user was on, we can look at pagination to determine if there are more attachments
  140. const hasManyAttachments =
  141. attachmentPagination.next?.results || attachmentPagination.previous?.results;
  142. const selectedOption = useMemo(() => {
  143. if (query?.trim()) {
  144. return EventNavOptions.CUSTOM;
  145. }
  146. switch (params.eventId) {
  147. case EventNavOptions.RECOMMENDED:
  148. case EventNavOptions.LATEST:
  149. case EventNavOptions.OLDEST:
  150. return params.eventId;
  151. case undefined:
  152. return defaultIssueEvent;
  153. default:
  154. return EventNavOptions.CUSTOM;
  155. }
  156. }, [query, params.eventId, defaultIssueEvent]);
  157. const onTabChange = (tabKey: typeof selectedOption) => {
  158. trackAnalytics('issue_details.event_navigation_selected', {
  159. organization,
  160. content: EventNavLabels[tabKey],
  161. });
  162. };
  163. const baseEventsPath = `/organizations/${organization.slug}/issues/${group.id}/events/`;
  164. const grayText = css`
  165. color: ${theme.subText};
  166. font-weight: ${theme.fontWeightNormal};
  167. `;
  168. const EventNavLabels = {
  169. [EventNavOptions.RECOMMENDED]: isSmallScreen ? t('Rec.') : t('Recommended'),
  170. [EventNavOptions.OLDEST]: t('First'),
  171. [EventNavOptions.LATEST]: t('Last'),
  172. [EventNavOptions.CUSTOM]: t('Specific'),
  173. };
  174. return (
  175. <EventNavigationWrapper role="navigation">
  176. <LargeDropdownButtonWrapper>
  177. <DropdownMenu
  178. items={[
  179. {
  180. key: Tab.DETAILS,
  181. label: (
  182. <DropdownCountWrapper isCurrentTab={currentTab === Tab.DETAILS}>
  183. {TabName[Tab.DETAILS]} <ItemCount value={group.count} />
  184. </DropdownCountWrapper>
  185. ),
  186. textValue: TabName[Tab.DETAILS],
  187. to: {
  188. ...location,
  189. pathname: `${baseUrl}${TabPaths[Tab.DETAILS]}`,
  190. },
  191. onAction: () => {
  192. trackAnalytics('issue_details.issue_content_selected', {
  193. organization,
  194. content: TabName[Tab.DETAILS],
  195. });
  196. },
  197. },
  198. {
  199. key: Tab.REPLAYS,
  200. label: (
  201. <DropdownCountWrapper isCurrentTab={currentTab === Tab.REPLAYS}>
  202. {TabName[Tab.REPLAYS]}{' '}
  203. {replaysCount > 50 ? (
  204. <CustomItemCount>50+</CustomItemCount>
  205. ) : (
  206. <ItemCount value={replaysCount} />
  207. )}
  208. </DropdownCountWrapper>
  209. ),
  210. textValue: TabName[Tab.REPLAYS],
  211. to: {
  212. ...location,
  213. pathname: `${baseUrl}${TabPaths[Tab.REPLAYS]}`,
  214. },
  215. hidden: !issueTypeConfig.replays.enabled,
  216. onAction: () => {
  217. trackAnalytics('issue_details.issue_content_selected', {
  218. organization,
  219. content: TabName[Tab.REPLAYS],
  220. });
  221. },
  222. },
  223. {
  224. key: Tab.ATTACHMENTS,
  225. label: (
  226. <DropdownCountWrapper isCurrentTab={currentTab === Tab.ATTACHMENTS}>
  227. {TabName[Tab.ATTACHMENTS]}
  228. <CustomItemCount>
  229. {hasManyAttachments ? '50+' : attachments.attachments.length}
  230. </CustomItemCount>
  231. </DropdownCountWrapper>
  232. ),
  233. textValue: TabName[Tab.ATTACHMENTS],
  234. to: {
  235. ...location,
  236. pathname: `${baseUrl}${TabPaths[Tab.ATTACHMENTS]}`,
  237. },
  238. hidden: !issueTypeConfig.attachments.enabled,
  239. onAction: () => {
  240. trackAnalytics('issue_details.issue_content_selected', {
  241. organization,
  242. content: TabName[Tab.ATTACHMENTS],
  243. });
  244. },
  245. },
  246. {
  247. key: Tab.USER_FEEDBACK,
  248. label: (
  249. <DropdownCountWrapper isCurrentTab={currentTab === Tab.USER_FEEDBACK}>
  250. {TabName[Tab.USER_FEEDBACK]} <ItemCount value={group.userReportCount} />
  251. </DropdownCountWrapper>
  252. ),
  253. textValue: TabName[Tab.USER_FEEDBACK],
  254. to: {
  255. ...location,
  256. pathname: `${baseUrl}${TabPaths[Tab.USER_FEEDBACK]}`,
  257. },
  258. hidden: !issueTypeConfig.userFeedback.enabled,
  259. onAction: () => {
  260. trackAnalytics('issue_details.issue_content_selected', {
  261. organization,
  262. content: TabName[Tab.USER_FEEDBACK],
  263. });
  264. },
  265. },
  266. ]}
  267. offset={[-2, 1]}
  268. trigger={triggerProps =>
  269. hideDropdownButton ? (
  270. <NavigationLabel>
  271. {TabName[currentTab] ?? TabName[Tab.DETAILS]}
  272. </NavigationLabel>
  273. ) : (
  274. <NavigationDropdownButton
  275. {...triggerProps}
  276. borderless
  277. size="sm"
  278. disabled={hideDropdownButton}
  279. aria-label={t('Select issue content')}
  280. aria-description={TabName[currentTab]}
  281. analyticsEventName="Issue Details: Issue Content Dropdown Opened"
  282. analyticsEventKey="issue_details.issue_content_dropdown_opened"
  283. >
  284. {TabName[currentTab] ?? TabName[Tab.DETAILS]}
  285. </NavigationDropdownButton>
  286. )
  287. }
  288. />
  289. <LargeInThisIssueText aria-hidden>{t('in this issue')}</LargeInThisIssueText>
  290. </LargeDropdownButtonWrapper>
  291. {event ? (
  292. <NavigationWrapper>
  293. {currentTab === Tab.DETAILS && (
  294. <Fragment>
  295. <Navigation>
  296. <Tooltip title={t('Previous Event')} skipWrapper>
  297. <LinkButton
  298. aria-label={t('Previous Event')}
  299. borderless
  300. size="xs"
  301. icon={<IconChevron direction="left" />}
  302. disabled={!defined(event.previousEventID)}
  303. analyticsEventKey="issue_details.previous_event_clicked"
  304. analyticsEventName="Issue Details: Previous Event Clicked"
  305. to={{
  306. pathname: `${baseEventsPath}${event.previousEventID}/`,
  307. query: {...location.query, referrer: 'previous-event'},
  308. }}
  309. css={grayText}
  310. onMouseEnter={handleHoverPagination(
  311. 'previous',
  312. defined(event.previousEventID)
  313. )}
  314. onClick={() => {
  315. // Assume they will continue to paginate
  316. setShouldPreload({next: true, previous: true});
  317. }}
  318. />
  319. </Tooltip>
  320. <Tooltip title={t('Next Event')} skipWrapper>
  321. <LinkButton
  322. aria-label={t('Next Event')}
  323. borderless
  324. size="xs"
  325. icon={<IconChevron direction="right" />}
  326. disabled={!defined(event.nextEventID)}
  327. analyticsEventKey="issue_details.next_event_clicked"
  328. analyticsEventName="Issue Details: Next Event Clicked"
  329. to={{
  330. pathname: `${baseEventsPath}${event.nextEventID}/`,
  331. query: {...location.query, referrer: 'next-event'},
  332. }}
  333. css={grayText}
  334. onMouseEnter={handleHoverPagination(
  335. 'next',
  336. defined(event.nextEventID)
  337. )}
  338. onClick={() => {
  339. // Assume they will continue to paginate
  340. setShouldPreload({next: true, previous: true});
  341. }}
  342. />
  343. </Tooltip>
  344. </Navigation>
  345. <Tabs value={selectedOption} disableOverflow onChange={onTabChange}>
  346. <TabList hideBorder variant="floating">
  347. {EventNavOrder.map(label => {
  348. const eventPath =
  349. label === selectedOption
  350. ? undefined
  351. : {
  352. pathname: normalizeUrl(baseEventsPath + label + '/'),
  353. query: {...location.query, referrer: `${label}-event`},
  354. };
  355. return (
  356. <TabList.Item
  357. to={eventPath}
  358. key={label}
  359. hidden={label === EventNavOptions.CUSTOM}
  360. textValue={EventNavLabels[label]}
  361. >
  362. {EventNavLabels[label]}
  363. </TabList.Item>
  364. );
  365. })}
  366. </TabList>
  367. </Tabs>
  368. </Fragment>
  369. )}
  370. {currentTab === Tab.DETAILS && (
  371. <LinkButton
  372. to={{
  373. pathname: `${baseUrl}${TabPaths[Tab.EVENTS]}`,
  374. query: location.query,
  375. }}
  376. size="xs"
  377. analyticsEventKey="issue_details.all_events_clicked"
  378. analyticsEventName="Issue Details: All Events Clicked"
  379. >
  380. {t('All Events')}
  381. </LinkButton>
  382. )}
  383. {currentTab === Tab.EVENTS && (
  384. <ButtonBar gap={1}>
  385. <LinkButton
  386. to={discoverUrl}
  387. aria-label={t('Open in Discover')}
  388. size="xs"
  389. icon={<IconTelescope />}
  390. analyticsEventKey="issue_details.discover_clicked"
  391. analyticsEventName="Issue Details: Discover Clicked"
  392. >
  393. {t('Discover')}
  394. </LinkButton>
  395. <LinkButton
  396. to={{
  397. pathname: `${baseUrl}${TabPaths[Tab.DETAILS]}`,
  398. query: {...location.query, cursor: undefined},
  399. }}
  400. aria-label={t('Return to event details')}
  401. size="xs"
  402. >
  403. {t('Close')}
  404. </LinkButton>
  405. </ButtonBar>
  406. )}
  407. </NavigationWrapper>
  408. ) : null}
  409. </EventNavigationWrapper>
  410. );
  411. }
  412. const LargeDropdownButtonWrapper = styled('div')`
  413. display: flex;
  414. align-items: center;
  415. gap: ${space(0.25)};
  416. `;
  417. const NavigationDropdownButton = styled(DropdownButton)`
  418. font-size: ${p => p.theme.fontSizeLarge};
  419. font-weight: ${p => p.theme.fontWeightBold};
  420. padding-right: ${space(0.5)};
  421. `;
  422. const NavigationLabel = styled('div')`
  423. font-size: ${p => p.theme.fontSizeLarge};
  424. font-weight: ${p => p.theme.fontWeightBold};
  425. padding-right: ${space(0.25)};
  426. padding-left: ${space(1.5)};
  427. `;
  428. const LargeInThisIssueText = styled('div')`
  429. font-size: ${p => p.theme.fontSizeLarge};
  430. font-weight: ${p => p.theme.fontWeightBold};
  431. color: ${p => p.theme.subText};
  432. `;
  433. const EventNavigationWrapper = styled('div')`
  434. flex-grow: 1;
  435. display: flex;
  436. flex-direction: column;
  437. justify-content: space-between;
  438. font-size: ${p => p.theme.fontSizeSmall};
  439. @media (min-width: ${p => p.theme.breakpoints.xsmall}) {
  440. flex-direction: row;
  441. align-items: center;
  442. }
  443. `;
  444. const NavigationWrapper = styled('div')`
  445. display: flex;
  446. gap: ${space(0.25)};
  447. justify-content: space-between;
  448. @media (min-width: ${p => p.theme.breakpoints.xsmall}) {
  449. gap: ${space(0.5)};
  450. }
  451. `;
  452. const Navigation = styled('div')`
  453. display: flex;
  454. padding-right: ${space(0.25)};
  455. border-right: 1px solid ${p => p.theme.gray100};
  456. `;
  457. const DropdownCountWrapper = styled('div')<{isCurrentTab: boolean}>`
  458. display: flex;
  459. align-items: center;
  460. justify-content: space-between;
  461. gap: ${space(3)};
  462. font-weight: ${p =>
  463. p.isCurrentTab ? p.theme.fontWeightBold : p.theme.fontWeightNormal};
  464. `;
  465. const ItemCount = styled(Count)`
  466. color: ${p => p.theme.subText};
  467. `;
  468. const CustomItemCount = styled('div')`
  469. color: ${p => p.theme.subText};
  470. `;