index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. import {Fragment, useEffect} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {css} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import {Location} from 'history';
  6. import * as qs from 'query-string';
  7. import {hideSidebar, showSidebar} from 'sentry/actionCreators/preferences';
  8. import SidebarPanelActions from 'sentry/actions/sidebarPanelActions';
  9. import Feature from 'sentry/components/acl/feature';
  10. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  11. import HookOrDefault from 'sentry/components/hookOrDefault';
  12. import {extractSelectionParameters} from 'sentry/components/organizations/pageFilters/utils';
  13. import {
  14. IconActivity,
  15. IconChevron,
  16. IconGraph,
  17. IconIssues,
  18. IconLab,
  19. IconLightning,
  20. IconProject,
  21. IconReleases,
  22. IconSettings,
  23. IconSiren,
  24. IconStats,
  25. IconSupport,
  26. IconTelescope,
  27. } from 'sentry/icons';
  28. import {t} from 'sentry/locale';
  29. import ConfigStore from 'sentry/stores/configStore';
  30. import HookStore from 'sentry/stores/hookStore';
  31. import PreferencesStore from 'sentry/stores/preferencesStore';
  32. import SidebarPanelStore from 'sentry/stores/sidebarPanelStore';
  33. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  34. import space from 'sentry/styles/space';
  35. import {Organization} from 'sentry/types';
  36. import {getDiscoverLandingUrl} from 'sentry/utils/discover/urls';
  37. import theme from 'sentry/utils/theme';
  38. import useMedia from 'sentry/utils/useMedia';
  39. import Broadcasts from './broadcasts';
  40. import SidebarHelp from './help';
  41. import OnboardingStatus from './onboardingStatus';
  42. import ServiceIncidents from './serviceIncidents';
  43. import SidebarDropdown from './sidebarDropdown';
  44. import SidebarItem from './sidebarItem';
  45. import {SidebarOrientation, SidebarPanelKey} from './types';
  46. const SidebarOverride = HookOrDefault({
  47. hookName: 'sidebar:item-override',
  48. defaultComponent: ({children}) => <Fragment>{children({})}</Fragment>,
  49. });
  50. type Props = {
  51. organization?: Organization;
  52. location?: Location;
  53. };
  54. function Sidebar({location, organization}: Props) {
  55. const config = useLegacyStore(ConfigStore);
  56. const preferences = useLegacyStore(PreferencesStore);
  57. const activePanel = useLegacyStore(SidebarPanelStore);
  58. const collapsed = !!preferences.collapsed;
  59. const horizontal = useMedia(`(max-width: ${theme.breakpoints[1]})`);
  60. const toggleCollapse = () => {
  61. const action = collapsed ? showSidebar : hideSidebar;
  62. action();
  63. };
  64. const togglePanel = (panel: SidebarPanelKey) => SidebarPanelActions.togglePanel(panel);
  65. const hidePanel = () => SidebarPanelActions.hidePanel();
  66. const bcl = document.body.classList;
  67. // Close panel on any navigation
  68. useEffect(() => void hidePanel(), [location?.pathname]);
  69. // Add classname to body
  70. useEffect(() => {
  71. bcl.add('body-sidebar');
  72. return () => bcl.remove('body-sidebar');
  73. }, []);
  74. // Add sidebar collapse classname to body
  75. useEffect(() => {
  76. if (collapsed) {
  77. bcl.add('collapsed');
  78. } else {
  79. bcl.remove('collapsed');
  80. }
  81. return () => bcl.remove('collapsed');
  82. }, [collapsed]);
  83. // Trigger panels depending on the location hash
  84. useEffect(() => {
  85. if (location?.hash === '#welcome') {
  86. togglePanel(SidebarPanelKey.OnboardingWizard);
  87. }
  88. }, [location?.hash]);
  89. /**
  90. * Navigate to a path, but keep the global selection query strings.
  91. */
  92. const navigateWithGlobalSelection = (
  93. pathname: string,
  94. evt: React.MouseEvent<HTMLAnchorElement>
  95. ) => {
  96. const globalSelectionRoutes = [
  97. 'alerts',
  98. 'alerts/rules',
  99. 'dashboards',
  100. 'issues',
  101. 'releases',
  102. 'user-feedback',
  103. 'discover',
  104. 'discover/results', // Team plans do not have query landing page
  105. 'performance',
  106. ].map(route => `/organizations/${organization?.slug}/${route}/`);
  107. // Only keep the querystring if the current route matches one of the above
  108. if (globalSelectionRoutes.includes(pathname)) {
  109. const query = extractSelectionParameters(location?.query);
  110. // Handle cmd-click (mac) and meta-click (linux)
  111. if (evt.metaKey) {
  112. const q = qs.stringify(query);
  113. evt.currentTarget.href = `${evt.currentTarget.href}?${q}`;
  114. return;
  115. }
  116. evt.preventDefault();
  117. browserHistory.push({pathname, query});
  118. }
  119. };
  120. const hasPanel = !!activePanel;
  121. const hasOrganization = !!organization;
  122. const orientation: SidebarOrientation = horizontal ? 'top' : 'left';
  123. const sidebarItemProps = {
  124. orientation,
  125. collapsed,
  126. hasPanel,
  127. };
  128. const projects = hasOrganization && (
  129. <SidebarItem
  130. {...sidebarItemProps}
  131. index
  132. icon={<IconProject size="md" />}
  133. label={<GuideAnchor target="projects">{t('Projects')}</GuideAnchor>}
  134. to={`/organizations/${organization.slug}/projects/`}
  135. id="projects"
  136. />
  137. );
  138. const issues = hasOrganization && (
  139. <SidebarItem
  140. {...sidebarItemProps}
  141. onClick={(_id, evt) =>
  142. navigateWithGlobalSelection(`/organizations/${organization.slug}/issues/`, evt)
  143. }
  144. icon={<IconIssues size="md" />}
  145. label={<GuideAnchor target="issues">{t('Issues')}</GuideAnchor>}
  146. to={`/organizations/${organization.slug}/issues/`}
  147. id="issues"
  148. />
  149. );
  150. const discover2 = hasOrganization && (
  151. <Feature
  152. hookName="feature-disabled:discover2-sidebar-item"
  153. features={['discover-basic']}
  154. organization={organization}
  155. >
  156. <SidebarItem
  157. {...sidebarItemProps}
  158. onClick={(_id, evt) =>
  159. navigateWithGlobalSelection(getDiscoverLandingUrl(organization), evt)
  160. }
  161. icon={<IconTelescope size="md" />}
  162. label={<GuideAnchor target="discover">{t('Discover')}</GuideAnchor>}
  163. to={getDiscoverLandingUrl(organization)}
  164. id="discover-v2"
  165. />
  166. </Feature>
  167. );
  168. const performance = hasOrganization && (
  169. <Feature
  170. hookName="feature-disabled:performance-sidebar-item"
  171. features={['performance-view']}
  172. organization={organization}
  173. >
  174. <SidebarOverride id="performance-override">
  175. {(overideProps: Partial<React.ComponentProps<typeof SidebarItem>>) => (
  176. <SidebarItem
  177. {...sidebarItemProps}
  178. onClick={(_id, evt) =>
  179. navigateWithGlobalSelection(
  180. `/organizations/${organization.slug}/performance/`,
  181. evt
  182. )
  183. }
  184. icon={<IconLightning size="md" />}
  185. label={<GuideAnchor target="performance">{t('Performance')}</GuideAnchor>}
  186. to={`/organizations/${organization.slug}/performance/`}
  187. id="performance"
  188. {...overideProps}
  189. />
  190. )}
  191. </SidebarOverride>
  192. </Feature>
  193. );
  194. const releases = hasOrganization && (
  195. <SidebarItem
  196. {...sidebarItemProps}
  197. onClick={(_id, evt) =>
  198. navigateWithGlobalSelection(`/organizations/${organization.slug}/releases/`, evt)
  199. }
  200. icon={<IconReleases size="md" />}
  201. label={<GuideAnchor target="releases">{t('Releases')}</GuideAnchor>}
  202. to={`/organizations/${organization.slug}/releases/`}
  203. id="releases"
  204. />
  205. );
  206. const userFeedback = hasOrganization && (
  207. <SidebarItem
  208. {...sidebarItemProps}
  209. onClick={(_id, evt) =>
  210. navigateWithGlobalSelection(
  211. `/organizations/${organization.slug}/user-feedback/`,
  212. evt
  213. )
  214. }
  215. icon={<IconSupport size="md" />}
  216. label={t('User Feedback')}
  217. to={`/organizations/${organization.slug}/user-feedback/`}
  218. id="user-feedback"
  219. />
  220. );
  221. const alerts = hasOrganization && (
  222. <SidebarItem
  223. {...sidebarItemProps}
  224. onClick={(_id, evt) =>
  225. navigateWithGlobalSelection(
  226. `/organizations/${organization.slug}/alerts/rules/`,
  227. evt
  228. )
  229. }
  230. icon={<IconSiren size="md" />}
  231. label={t('Alerts')}
  232. to={`/organizations/${organization.slug}/alerts/rules/`}
  233. id="alerts"
  234. />
  235. );
  236. const monitors = hasOrganization && (
  237. <Feature features={['monitors']} organization={organization}>
  238. <SidebarItem
  239. {...sidebarItemProps}
  240. onClick={(_id, evt) =>
  241. navigateWithGlobalSelection(
  242. `/organizations/${organization.slug}/monitors/`,
  243. evt
  244. )
  245. }
  246. icon={<IconLab size="md" />}
  247. label={t('Monitors')}
  248. to={`/organizations/${organization.slug}/monitors/`}
  249. id="monitors"
  250. />
  251. </Feature>
  252. );
  253. const dashboards = hasOrganization && (
  254. <Feature
  255. hookName="feature-disabled:dashboards-sidebar-item"
  256. features={['discover', 'discover-query', 'dashboards-basic', 'dashboards-edit']}
  257. organization={organization}
  258. requireAll={false}
  259. >
  260. <SidebarItem
  261. {...sidebarItemProps}
  262. index
  263. onClick={(_id, evt) =>
  264. navigateWithGlobalSelection(
  265. `/organizations/${organization.slug}/dashboards/`,
  266. evt
  267. )
  268. }
  269. icon={<IconGraph size="md" />}
  270. label={t('Dashboards')}
  271. to={`/organizations/${organization.slug}/dashboards/`}
  272. id="customizable-dashboards"
  273. />
  274. </Feature>
  275. );
  276. const activity = hasOrganization && (
  277. <SidebarItem
  278. {...sidebarItemProps}
  279. icon={<IconActivity size="md" />}
  280. label={t('Activity')}
  281. to={`/organizations/${organization.slug}/activity/`}
  282. id="activity"
  283. />
  284. );
  285. const stats = hasOrganization && (
  286. <SidebarItem
  287. {...sidebarItemProps}
  288. icon={<IconStats size="md" />}
  289. label={t('Stats')}
  290. to={`/organizations/${organization.slug}/stats/`}
  291. id="stats"
  292. />
  293. );
  294. const settings = hasOrganization && (
  295. <SidebarItem
  296. {...sidebarItemProps}
  297. icon={<IconSettings size="md" />}
  298. label={t('Settings')}
  299. to={`/settings/${organization.slug}/`}
  300. id="settings"
  301. />
  302. );
  303. return (
  304. <SidebarWrapper collapsed={collapsed}>
  305. <SidebarSectionGroupPrimary>
  306. <SidebarSection>
  307. <SidebarDropdown
  308. orientation={orientation}
  309. collapsed={collapsed}
  310. org={organization}
  311. user={config.user}
  312. config={config}
  313. />
  314. </SidebarSection>
  315. <PrimaryItems>
  316. {hasOrganization && (
  317. <Fragment>
  318. <SidebarSection>
  319. {projects}
  320. {issues}
  321. {performance}
  322. {releases}
  323. {userFeedback}
  324. {alerts}
  325. {discover2}
  326. {dashboards}
  327. </SidebarSection>
  328. <SidebarSection>{monitors}</SidebarSection>
  329. <SidebarSection>
  330. {activity}
  331. {stats}
  332. </SidebarSection>
  333. <SidebarSection>{settings}</SidebarSection>
  334. </Fragment>
  335. )}
  336. </PrimaryItems>
  337. </SidebarSectionGroupPrimary>
  338. {hasOrganization && (
  339. <SidebarSectionGroup>
  340. <SidebarSection noMargin noPadding>
  341. <OnboardingStatus
  342. org={organization}
  343. currentPanel={activePanel}
  344. onShowPanel={() => togglePanel(SidebarPanelKey.OnboardingWizard)}
  345. hidePanel={hidePanel}
  346. {...sidebarItemProps}
  347. />
  348. </SidebarSection>
  349. <SidebarSection>
  350. {HookStore.get('sidebar:bottom-items').length > 0 &&
  351. HookStore.get('sidebar:bottom-items')[0]({
  352. organization,
  353. ...sidebarItemProps,
  354. })}
  355. <SidebarHelp
  356. orientation={orientation}
  357. collapsed={collapsed}
  358. hidePanel={hidePanel}
  359. organization={organization}
  360. />
  361. <Broadcasts
  362. orientation={orientation}
  363. collapsed={collapsed}
  364. currentPanel={activePanel}
  365. onShowPanel={() => togglePanel(SidebarPanelKey.Broadcasts)}
  366. hidePanel={hidePanel}
  367. organization={organization}
  368. />
  369. <ServiceIncidents
  370. orientation={orientation}
  371. collapsed={collapsed}
  372. currentPanel={activePanel}
  373. onShowPanel={() => togglePanel(SidebarPanelKey.StatusUpdate)}
  374. hidePanel={hidePanel}
  375. />
  376. </SidebarSection>
  377. {!horizontal && (
  378. <SidebarSection>
  379. <SidebarCollapseItem
  380. id="collapse"
  381. data-test-id="sidebar-collapse"
  382. {...sidebarItemProps}
  383. icon={<StyledIconChevron collapsed={collapsed} />}
  384. label={collapsed ? t('Expand') : t('Collapse')}
  385. onClick={toggleCollapse}
  386. />
  387. </SidebarSection>
  388. )}
  389. </SidebarSectionGroup>
  390. )}
  391. </SidebarWrapper>
  392. );
  393. }
  394. export default Sidebar;
  395. const responsiveFlex = css`
  396. display: flex;
  397. flex-direction: column;
  398. @media (max-width: ${theme.breakpoints[1]}) {
  399. flex-direction: row;
  400. }
  401. `;
  402. export const SidebarWrapper = styled('nav')<{collapsed: boolean}>`
  403. background: ${p => p.theme.sidebarGradient};
  404. color: ${p => p.theme.sidebar.color};
  405. line-height: 1;
  406. padding: 12px 0 2px; /* Allows for 32px avatars */
  407. width: ${p => p.theme.sidebar[p.collapsed ? 'collapsedWidth' : 'expandedWidth']};
  408. position: fixed;
  409. top: ${p => (ConfigStore.get('demoMode') ? p.theme.demo.headerSize : 0)};
  410. left: 0;
  411. bottom: 0;
  412. justify-content: space-between;
  413. z-index: ${p => p.theme.zIndex.sidebar};
  414. border-right: solid 1px ${p => p.theme.sidebarBorder};
  415. ${responsiveFlex};
  416. @media (max-width: ${p => p.theme.breakpoints[1]}) {
  417. top: 0;
  418. left: 0;
  419. right: 0;
  420. height: ${p => p.theme.sidebar.mobileHeight};
  421. bottom: auto;
  422. width: auto;
  423. padding: 0 ${space(1)};
  424. align-items: center;
  425. border-right: none;
  426. border-bottom: solid 1px ${p => p.theme.sidebarBorder};
  427. }
  428. `;
  429. const SidebarSectionGroup = styled('div')`
  430. ${responsiveFlex};
  431. flex-shrink: 0; /* prevents shrinking on Safari */
  432. `;
  433. const SidebarSectionGroupPrimary = styled('div')`
  434. ${responsiveFlex};
  435. /* necessary for child flexing on msedge and ff */
  436. min-height: 0;
  437. min-width: 0;
  438. flex: 1;
  439. /* expand to fill the entire height on mobile */
  440. @media (max-width: ${p => p.theme.breakpoints[1]}) {
  441. height: 100%;
  442. align-items: center;
  443. }
  444. `;
  445. const PrimaryItems = styled('div')`
  446. overflow: auto;
  447. flex: 1;
  448. display: flex;
  449. flex-direction: column;
  450. -ms-overflow-style: -ms-autohiding-scrollbar;
  451. @media (max-height: 675px) and (min-width: ${p => p.theme.breakpoints[1]}) {
  452. border-bottom: 1px solid ${p => p.theme.gray400};
  453. padding-bottom: ${space(1)};
  454. box-shadow: rgba(0, 0, 0, 0.15) 0px -10px 10px inset;
  455. &::-webkit-scrollbar {
  456. background-color: transparent;
  457. width: 8px;
  458. }
  459. &::-webkit-scrollbar-thumb {
  460. background: ${p => p.theme.gray400};
  461. border-radius: 8px;
  462. }
  463. }
  464. @media (max-width: ${p => p.theme.breakpoints[1]}) {
  465. overflow-y: visible;
  466. flex-direction: row;
  467. height: 100%;
  468. align-items: center;
  469. border-right: 1px solid ${p => p.theme.gray400};
  470. padding-right: ${space(1)};
  471. margin-right: ${space(0.5)};
  472. box-shadow: rgba(0, 0, 0, 0.15) -10px 0px 10px inset;
  473. ::-webkit-scrollbar {
  474. display: none;
  475. }
  476. }
  477. `;
  478. const SidebarSection = styled(SidebarSectionGroup)<{
  479. noMargin?: boolean;
  480. noPadding?: boolean;
  481. }>`
  482. ${p => !p.noMargin && `margin: ${space(1)} 0`};
  483. ${p => !p.noPadding && 'padding: 0 19px'};
  484. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  485. margin: 0;
  486. padding: 0;
  487. }
  488. &:empty {
  489. display: none;
  490. }
  491. `;
  492. const ExpandedIcon = css`
  493. transition: 0.3s transform ease;
  494. transform: rotate(270deg);
  495. `;
  496. const CollapsedIcon = css`
  497. transform: rotate(90deg);
  498. `;
  499. const StyledIconChevron = styled(({collapsed, ...props}) => (
  500. <IconChevron
  501. direction="left"
  502. size="md"
  503. isCircled
  504. css={[ExpandedIcon, collapsed && CollapsedIcon]}
  505. {...props}
  506. />
  507. ))``;
  508. const SidebarCollapseItem = styled(SidebarItem)`
  509. @media (max-width: ${p => p.theme.breakpoints[1]}) {
  510. display: none;
  511. }
  512. `;