eventNavigation.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/hooks/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. onAction={key => {
  179. trackAnalytics('issue_details.issue_content_selected', {
  180. organization,
  181. content: TabName[key],
  182. });
  183. }}
  184. items={[
  185. {
  186. key: Tab.DETAILS,
  187. label: (
  188. <DropdownCountWrapper isCurrentTab={currentTab === Tab.DETAILS}>
  189. {TabName[Tab.DETAILS]} <ItemCount value={group.count} />
  190. </DropdownCountWrapper>
  191. ),
  192. textValue: TabName[Tab.DETAILS],
  193. to: {
  194. ...location,
  195. pathname: `${baseUrl}${TabPaths[Tab.DETAILS]}`,
  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. },
  217. {
  218. key: Tab.ATTACHMENTS,
  219. label: (
  220. <DropdownCountWrapper isCurrentTab={currentTab === Tab.ATTACHMENTS}>
  221. {TabName[Tab.ATTACHMENTS]}
  222. <CustomItemCount>
  223. {hasManyAttachments ? '50+' : attachments.attachments.length}
  224. </CustomItemCount>
  225. </DropdownCountWrapper>
  226. ),
  227. textValue: TabName[Tab.ATTACHMENTS],
  228. to: {
  229. ...location,
  230. pathname: `${baseUrl}${TabPaths[Tab.ATTACHMENTS]}`,
  231. },
  232. hidden: !issueTypeConfig.attachments.enabled,
  233. },
  234. {
  235. key: Tab.USER_FEEDBACK,
  236. label: (
  237. <DropdownCountWrapper isCurrentTab={currentTab === Tab.USER_FEEDBACK}>
  238. {TabName[Tab.USER_FEEDBACK]} <ItemCount value={group.userReportCount} />
  239. </DropdownCountWrapper>
  240. ),
  241. textValue: TabName[Tab.USER_FEEDBACK],
  242. to: {
  243. ...location,
  244. pathname: `${baseUrl}${TabPaths[Tab.USER_FEEDBACK]}`,
  245. },
  246. hidden: !issueTypeConfig.userFeedback.enabled,
  247. },
  248. ]}
  249. offset={[-2, 1]}
  250. trigger={(triggerProps, isOpen) =>
  251. hideDropdownButton ? (
  252. <NavigationLabel>
  253. {TabName[currentTab] ?? TabName[Tab.DETAILS]}
  254. </NavigationLabel>
  255. ) : (
  256. <NavigationDropdownButton
  257. {...triggerProps}
  258. isOpen={isOpen}
  259. borderless
  260. size="sm"
  261. disabled={hideDropdownButton}
  262. aria-label={t('Select issue content')}
  263. aria-description={TabName[currentTab]}
  264. analyticsEventName="Issue Details: Issue Content Dropdown Opened"
  265. analyticsEventKey="issue_details.issue_content_dropdown_opened"
  266. >
  267. {TabName[currentTab] ?? TabName[Tab.DETAILS]}
  268. </NavigationDropdownButton>
  269. )
  270. }
  271. />
  272. <LargeInThisIssueText aria-hidden>{t('in this issue')}</LargeInThisIssueText>
  273. </LargeDropdownButtonWrapper>
  274. {event ? (
  275. <NavigationWrapper>
  276. {currentTab === Tab.DETAILS && (
  277. <Fragment>
  278. <Navigation>
  279. <Tooltip title={t('Previous Event')} skipWrapper>
  280. <LinkButton
  281. aria-label={t('Previous Event')}
  282. borderless
  283. size="xs"
  284. icon={<IconChevron direction="left" />}
  285. disabled={!defined(event.previousEventID)}
  286. analyticsEventKey="issue_details.previous_event_clicked"
  287. analyticsEventName="Issue Details: Previous Event Clicked"
  288. to={{
  289. pathname: `${baseEventsPath}${event.previousEventID}/`,
  290. query: {...location.query, referrer: 'previous-event'},
  291. }}
  292. css={grayText}
  293. onMouseEnter={handleHoverPagination(
  294. 'previous',
  295. defined(event.previousEventID)
  296. )}
  297. onClick={() => {
  298. // Assume they will continue to paginate
  299. setShouldPreload({next: true, previous: true});
  300. }}
  301. />
  302. </Tooltip>
  303. <Tooltip title={t('Next Event')} skipWrapper>
  304. <LinkButton
  305. aria-label={t('Next Event')}
  306. borderless
  307. size="xs"
  308. icon={<IconChevron direction="right" />}
  309. disabled={!defined(event.nextEventID)}
  310. analyticsEventKey="issue_details.next_event_clicked"
  311. analyticsEventName="Issue Details: Next Event Clicked"
  312. to={{
  313. pathname: `${baseEventsPath}${event.nextEventID}/`,
  314. query: {...location.query, referrer: 'next-event'},
  315. }}
  316. css={grayText}
  317. onMouseEnter={handleHoverPagination(
  318. 'next',
  319. defined(event.nextEventID)
  320. )}
  321. onClick={() => {
  322. // Assume they will continue to paginate
  323. setShouldPreload({next: true, previous: true});
  324. }}
  325. />
  326. </Tooltip>
  327. </Navigation>
  328. <Tabs value={selectedOption} disableOverflow onChange={onTabChange}>
  329. <TabList hideBorder variant="floating">
  330. {EventNavOrder.map(label => {
  331. const eventPath =
  332. label === selectedOption
  333. ? undefined
  334. : {
  335. pathname: normalizeUrl(baseEventsPath + label + '/'),
  336. query: {...location.query, referrer: `${label}-event`},
  337. };
  338. return (
  339. <TabList.Item
  340. to={eventPath}
  341. key={label}
  342. hidden={label === EventNavOptions.CUSTOM}
  343. textValue={EventNavLabels[label]}
  344. >
  345. {EventNavLabels[label]}
  346. </TabList.Item>
  347. );
  348. })}
  349. </TabList>
  350. </Tabs>
  351. </Fragment>
  352. )}
  353. {currentTab === Tab.DETAILS && (
  354. <LinkButton
  355. to={{
  356. pathname: `${baseUrl}${TabPaths[Tab.EVENTS]}`,
  357. query: location.query,
  358. }}
  359. size="xs"
  360. analyticsEventKey="issue_details.all_events_clicked"
  361. analyticsEventName="Issue Details: All Events Clicked"
  362. >
  363. {t('All Events')}
  364. </LinkButton>
  365. )}
  366. {currentTab === Tab.EVENTS && (
  367. <ButtonBar gap={1}>
  368. <LinkButton
  369. to={discoverUrl}
  370. aria-label={t('Open in Discover')}
  371. size="xs"
  372. icon={<IconTelescope />}
  373. analyticsEventKey="issue_details.discover_clicked"
  374. analyticsEventName="Issue Details: Discover Clicked"
  375. >
  376. {t('Discover')}
  377. </LinkButton>
  378. <LinkButton
  379. to={{
  380. pathname: `${baseUrl}${TabPaths[Tab.DETAILS]}`,
  381. query: {...location.query, cursor: undefined},
  382. }}
  383. aria-label={t('Return to event details')}
  384. size="xs"
  385. >
  386. {t('Close')}
  387. </LinkButton>
  388. </ButtonBar>
  389. )}
  390. </NavigationWrapper>
  391. ) : null}
  392. </EventNavigationWrapper>
  393. );
  394. }
  395. const LargeDropdownButtonWrapper = styled('div')`
  396. display: flex;
  397. align-items: center;
  398. gap: ${space(0.25)};
  399. `;
  400. const NavigationDropdownButton = styled(DropdownButton)`
  401. font-size: ${p => p.theme.fontSizeLarge};
  402. font-weight: ${p => p.theme.fontWeightBold};
  403. padding-right: ${space(0.5)};
  404. `;
  405. const NavigationLabel = styled('div')`
  406. font-size: ${p => p.theme.fontSizeLarge};
  407. font-weight: ${p => p.theme.fontWeightBold};
  408. padding-right: ${space(0.25)};
  409. padding-left: ${space(1.5)};
  410. `;
  411. const LargeInThisIssueText = styled('div')`
  412. font-size: ${p => p.theme.fontSizeLarge};
  413. font-weight: ${p => p.theme.fontWeightBold};
  414. color: ${p => p.theme.subText};
  415. `;
  416. const EventNavigationWrapper = styled('div')`
  417. flex-grow: 1;
  418. display: flex;
  419. flex-direction: column;
  420. justify-content: space-between;
  421. font-size: ${p => p.theme.fontSizeSmall};
  422. @media (min-width: ${p => p.theme.breakpoints.xsmall}) {
  423. flex-direction: row;
  424. align-items: center;
  425. }
  426. `;
  427. const NavigationWrapper = styled('div')`
  428. display: flex;
  429. gap: ${space(0.25)};
  430. justify-content: space-between;
  431. @media (min-width: ${p => p.theme.breakpoints.xsmall}) {
  432. gap: ${space(0.5)};
  433. }
  434. `;
  435. const Navigation = styled('div')`
  436. display: flex;
  437. padding-right: ${space(0.25)};
  438. border-right: 1px solid ${p => p.theme.gray100};
  439. `;
  440. const DropdownCountWrapper = styled('div')<{isCurrentTab: boolean}>`
  441. display: flex;
  442. align-items: center;
  443. justify-content: space-between;
  444. gap: ${space(3)};
  445. font-variant-numeric: tabular-nums;
  446. font-weight: ${p =>
  447. p.isCurrentTab ? p.theme.fontWeightBold : p.theme.fontWeightNormal};
  448. `;
  449. const ItemCount = styled(Count)`
  450. color: ${p => p.theme.subText};
  451. `;
  452. const CustomItemCount = styled('div')`
  453. color: ${p => p.theme.subText};
  454. `;