header.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {ReactNode} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Badge from 'sentry/components/badge';
  5. import Button from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import GlobalEventProcessingAlert from 'sentry/components/globalEventProcessingAlert';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  11. import QueryCount from 'sentry/components/queryCount';
  12. import {Item, TabList, Tabs} from 'sentry/components/tabs';
  13. import Tooltip from 'sentry/components/tooltip';
  14. import {SLOW_TOOLTIP_DELAY} from 'sentry/constants';
  15. import {IconPause, IconPlay, IconStar} from 'sentry/icons';
  16. import {t, tct} from 'sentry/locale';
  17. import space from 'sentry/styles/space';
  18. import {Organization, SavedSearch} from 'sentry/types';
  19. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  20. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  21. import useProjects from 'sentry/utils/useProjects';
  22. import {useSyncedLocalStorageState} from 'sentry/utils/useSyncedLocalStorageState';
  23. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  24. import IssueListSetAsDefault from 'sentry/views/issueList/issueListSetAsDefault';
  25. import {
  26. getTabs,
  27. IssueSortOptions,
  28. Query,
  29. QueryCounts,
  30. SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY,
  31. TAB_MAX_COUNT,
  32. } from './utils';
  33. type IssueListHeaderProps = {
  34. displayReprocessingTab: boolean;
  35. onRealtimeChange: (realtime: boolean) => void;
  36. organization: Organization;
  37. query: string;
  38. queryCounts: QueryCounts;
  39. realtimeActive: boolean;
  40. router: InjectedRouter;
  41. savedSearch: SavedSearch | null;
  42. selectedProjectIds: number[];
  43. sort: string;
  44. queryCount?: number;
  45. };
  46. type IssueListHeaderTabProps = {
  47. name: string;
  48. query: string;
  49. count?: number;
  50. hasMore?: boolean;
  51. tooltipHoverable?: boolean;
  52. tooltipTitle?: ReactNode;
  53. };
  54. const EXTRA_TAB_KEY = 'extra-tab-key';
  55. function IssueListHeaderTabContent({
  56. count = 0,
  57. hasMore = false,
  58. name,
  59. query,
  60. tooltipHoverable,
  61. tooltipTitle,
  62. }: IssueListHeaderTabProps) {
  63. return (
  64. <Tooltip
  65. title={tooltipTitle}
  66. position="bottom"
  67. isHoverable={tooltipHoverable}
  68. delay={SLOW_TOOLTIP_DELAY}
  69. >
  70. {name}{' '}
  71. {count > 0 && (
  72. <Badge type={query === Query.FOR_REVIEW && count > 0 ? 'review' : 'default'}>
  73. <QueryCount hideParens count={count} max={hasMore ? TAB_MAX_COUNT : 1000} />
  74. </Badge>
  75. )}
  76. </Tooltip>
  77. );
  78. }
  79. function IssueListHeader({
  80. organization,
  81. query,
  82. sort,
  83. queryCount,
  84. queryCounts,
  85. realtimeActive,
  86. onRealtimeChange,
  87. savedSearch,
  88. router,
  89. displayReprocessingTab,
  90. selectedProjectIds,
  91. }: IssueListHeaderProps) {
  92. const [isSavedSearchesOpen, setIsSavedSearchesOpen] = useSyncedLocalStorageState(
  93. SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY,
  94. false
  95. );
  96. const {projects} = useProjects();
  97. const tabs = getTabs(organization);
  98. const visibleTabs = displayReprocessingTab
  99. ? tabs
  100. : tabs.filter(([tab]) => tab !== Query.REPROCESSING);
  101. const savedSearchTabActive = !visibleTabs.some(([tabQuery]) => tabQuery === query);
  102. // Remove cursor and page when switching tabs
  103. const {cursor: _, page: __, ...queryParms} = router?.location?.query ?? {};
  104. const sortParam =
  105. queryParms.sort === IssueSortOptions.INBOX ? undefined : queryParms.sort;
  106. function onSavedSearchesToggleClicked() {
  107. const newOpenState = !isSavedSearchesOpen;
  108. trackAdvancedAnalyticsEvent('search.saved_search_sidebar_toggle_clicked', {
  109. organization,
  110. open: newOpenState,
  111. });
  112. setIsSavedSearchesOpen(newOpenState);
  113. }
  114. function trackTabClick(tabQuery: string) {
  115. // Clicking on inbox tab and currently another tab is active
  116. if (tabQuery === Query.FOR_REVIEW && query !== Query.FOR_REVIEW) {
  117. trackAnalyticsEvent({
  118. eventKey: 'inbox_tab.clicked',
  119. eventName: 'Clicked Inbox Tab',
  120. organization_id: organization.id,
  121. });
  122. }
  123. }
  124. const selectedProjects = projects.filter(({id}) =>
  125. selectedProjectIds.includes(Number(id))
  126. );
  127. const realtimeTitle = realtimeActive
  128. ? t('Pause real-time updates')
  129. : t('Enable real-time updates');
  130. return (
  131. <Layout.Header>
  132. <Layout.HeaderContent>
  133. <StyledLayoutTitle>
  134. {t('Issues')}
  135. <PageHeadingQuestionTooltip
  136. title={tct(
  137. 'Detailed views of errors and performance problems in your application grouped by events with a similar set of characteristics. [link: Read the docs].',
  138. {link: <ExternalLink href="https://docs.sentry.io/product/issues/" />}
  139. )}
  140. />
  141. </StyledLayoutTitle>
  142. </Layout.HeaderContent>
  143. <Layout.HeaderActions>
  144. <ButtonBar gap={1}>
  145. <IssueListSetAsDefault {...{sort, query, organization}} />
  146. <Button
  147. size="sm"
  148. icon={<IconStar size="sm" isSolid={isSavedSearchesOpen} />}
  149. onClick={onSavedSearchesToggleClicked}
  150. >
  151. {isSavedSearchesOpen ? t('Hide Searches') : t('Saved Searches')}
  152. </Button>
  153. <Button
  154. size="sm"
  155. data-test-id="real-time"
  156. title={realtimeTitle}
  157. aria-label={realtimeTitle}
  158. icon={realtimeActive ? <IconPause size="xs" /> : <IconPlay size="xs" />}
  159. onClick={() => onRealtimeChange(!realtimeActive)}
  160. />
  161. </ButtonBar>
  162. </Layout.HeaderActions>
  163. <StyledGlobalEventProcessingAlert projects={selectedProjects} />
  164. <StyledTabs
  165. onSelectionChange={key =>
  166. trackTabClick(key === EXTRA_TAB_KEY ? query : key.toString())
  167. }
  168. selectedKey={savedSearchTabActive ? EXTRA_TAB_KEY : query}
  169. >
  170. <TabList hideBorder>
  171. {[
  172. ...visibleTabs.map(
  173. ([tabQuery, {name: queryName, tooltipTitle, tooltipHoverable}]) => {
  174. const to = normalizeUrl({
  175. query: {
  176. ...queryParms,
  177. query: tabQuery,
  178. sort:
  179. tabQuery === Query.FOR_REVIEW ? IssueSortOptions.INBOX : sortParam,
  180. },
  181. pathname: `/organizations/${organization.slug}/issues/`,
  182. });
  183. return (
  184. <Item key={tabQuery} to={to} textValue={queryName}>
  185. <IssueListHeaderTabContent
  186. tooltipTitle={tooltipTitle}
  187. tooltipHoverable={tooltipHoverable}
  188. name={queryName}
  189. count={queryCounts[tabQuery]?.count}
  190. hasMore={queryCounts[tabQuery]?.hasMore}
  191. query={tabQuery}
  192. />
  193. </Item>
  194. );
  195. }
  196. ),
  197. <Item
  198. hidden={!savedSearchTabActive}
  199. key={EXTRA_TAB_KEY}
  200. to={{query: queryParms, pathname: location.pathname}}
  201. textValue={savedSearch?.name ?? t('Custom Search')}
  202. >
  203. <IssueListHeaderTabContent
  204. name={savedSearch?.name ?? t('Custom Search')}
  205. count={queryCount}
  206. query={query}
  207. />
  208. </Item>,
  209. ]}
  210. </TabList>
  211. </StyledTabs>
  212. </Layout.Header>
  213. );
  214. }
  215. export default IssueListHeader;
  216. const StyledLayoutTitle = styled(Layout.Title)`
  217. margin-top: ${space(0.5)};
  218. `;
  219. const StyledGlobalEventProcessingAlert = styled(GlobalEventProcessingAlert)`
  220. grid-column: 1/-1;
  221. margin-top: ${space(1)};
  222. margin-bottom: ${space(1)};
  223. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  224. margin-top: ${space(2)};
  225. margin-bottom: 0;
  226. }
  227. `;
  228. const StyledTabs = styled(Tabs)`
  229. grid-column: 1/-1;
  230. `;