sidebarItem.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import {Fragment, isValidElement} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import {css} from '@emotion/react';
  5. import styled from '@emotion/styled';
  6. import FeatureBadge from 'sentry/components/featureBadge';
  7. import HookOrDefault from 'sentry/components/hookOrDefault';
  8. import Link from 'sentry/components/links/link';
  9. import TextOverflow from 'sentry/components/textOverflow';
  10. import Tooltip from 'sentry/components/tooltip';
  11. import {Organization} from 'sentry/types';
  12. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  13. import localStorage from 'sentry/utils/localStorage';
  14. import {Theme} from 'sentry/utils/theme';
  15. import {SidebarOrientation} from './types';
  16. const LabelHook = HookOrDefault({
  17. hookName: 'sidebar:item-label',
  18. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  19. });
  20. type Props = WithRouterProps & {
  21. /**
  22. * Icon to display
  23. */
  24. icon: React.ReactNode;
  25. /**
  26. * Key of the sidebar item. Used for label hooks
  27. */
  28. id: string;
  29. /**
  30. * Label to display (only when expanded)
  31. */
  32. label: React.ReactNode;
  33. /**
  34. * Sidebar is at "top" or "left" of screen
  35. */
  36. orientation: SidebarOrientation;
  37. /**
  38. * Is this sidebar item active
  39. */
  40. active?: boolean;
  41. /**
  42. * Additional badge to display after label
  43. */
  44. badge?: number;
  45. className?: string;
  46. /**
  47. * Is sidebar in a collapsed state
  48. */
  49. collapsed?: boolean;
  50. /**
  51. * Sidebar has a panel open
  52. */
  53. hasPanel?: boolean;
  54. href?: string;
  55. index?: boolean;
  56. /**
  57. * Additional badge letting users know a tab is in alpha.
  58. */
  59. isAlpha?: boolean;
  60. /**
  61. * Additional badge letting users know a tab is in beta.
  62. */
  63. isBeta?: boolean;
  64. /**
  65. * Additional badge letting users know a tab is new.
  66. */
  67. isNew?: boolean;
  68. /**
  69. * An optional prefix that can be used to reset the "new" indicator
  70. */
  71. isNewSeenKeySuffix?: string;
  72. onClick?: (id: string, e: React.MouseEvent<HTMLAnchorElement>) => void;
  73. /**
  74. * The current organization. Useful for analytics.
  75. */
  76. organization?: Organization;
  77. to?: string;
  78. };
  79. const SidebarItem = ({
  80. router,
  81. id,
  82. href,
  83. to,
  84. icon,
  85. label,
  86. badge,
  87. active,
  88. hasPanel,
  89. isNew,
  90. isBeta,
  91. isAlpha,
  92. collapsed,
  93. className,
  94. orientation,
  95. isNewSeenKeySuffix,
  96. organization,
  97. onClick,
  98. ...props
  99. }: Props) => {
  100. // label might be wrapped in a guideAnchor
  101. let labelString = label;
  102. if (isValidElement(label)) {
  103. labelString = label?.props?.children ?? label;
  104. }
  105. // If there is no active panel open and if path is active according to react-router
  106. const isActiveRouter =
  107. (!hasPanel && router && to && location.pathname.startsWith(to)) ||
  108. (labelString === 'Discover' && location.pathname.includes('/discover/')) ||
  109. (labelString === 'Dashboards' &&
  110. (location.pathname.includes('/dashboards/') ||
  111. location.pathname.includes('/dashboard/')) &&
  112. !location.pathname.startsWith('/settings/')) ||
  113. // TODO: this won't be necessary once we remove settingsHome
  114. (labelString === 'Settings' && location.pathname.startsWith('/settings/')) ||
  115. (labelString === 'Alerts' &&
  116. location.pathname.includes('/alerts/') &&
  117. !location.pathname.startsWith('/settings/'));
  118. const isActive = active || isActiveRouter;
  119. const isTop = orientation === 'top';
  120. const placement = isTop ? 'bottom' : 'right';
  121. const seenSuffix = isNewSeenKeySuffix ?? '';
  122. const isNewSeenKey = `sidebar-new-seen:${id}${seenSuffix}`;
  123. const showIsNew = isNew && !localStorage.getItem(isNewSeenKey);
  124. const recordAnalytics = () => {
  125. trackAdvancedAnalyticsEvent('growth.clicked_sidebar', {
  126. item: id,
  127. organization: organization || null,
  128. });
  129. };
  130. return (
  131. <Tooltip disabled={!collapsed} title={label} position={placement}>
  132. <StyledSidebarItem
  133. data-test-id={props['data-test-id']}
  134. id={`sidebar-item-${id}`}
  135. active={isActive ? 'true' : undefined}
  136. to={(to ? to : href) || '#'}
  137. className={className}
  138. onClick={(event: React.MouseEvent<HTMLAnchorElement>) => {
  139. !(to || href) && event.preventDefault();
  140. recordAnalytics();
  141. onClick?.(id, event);
  142. showIsNew && localStorage.setItem(isNewSeenKey, 'true');
  143. }}
  144. >
  145. <SidebarItemWrapper>
  146. <SidebarItemIcon>{icon}</SidebarItemIcon>
  147. {!collapsed && !isTop && (
  148. <SidebarItemLabel>
  149. <LabelHook id={id}>
  150. <TextOverflow>{label}</TextOverflow>
  151. {showIsNew && <FeatureBadge type="new" noTooltip />}
  152. {isBeta && <FeatureBadge type="beta" noTooltip />}
  153. {isAlpha && <FeatureBadge type="alpha" noTooltip />}
  154. </LabelHook>
  155. </SidebarItemLabel>
  156. )}
  157. {collapsed && showIsNew && <CollapsedFeatureBadge type="new" />}
  158. {collapsed && isBeta && <CollapsedFeatureBadge type="beta" />}
  159. {collapsed && isAlpha && <CollapsedFeatureBadge type="alpha" />}
  160. {badge !== undefined && badge > 0 && (
  161. <SidebarItemBadge collapsed={collapsed}>{badge}</SidebarItemBadge>
  162. )}
  163. </SidebarItemWrapper>
  164. </StyledSidebarItem>
  165. </Tooltip>
  166. );
  167. };
  168. export default withRouter(SidebarItem);
  169. const getActiveStyle = ({active, theme}: {active?: string; theme?: Theme}) => {
  170. if (!active) {
  171. return '';
  172. }
  173. return css`
  174. color: ${theme?.white};
  175. &:active,
  176. &:focus,
  177. &:hover {
  178. color: ${theme?.white};
  179. }
  180. &:before {
  181. background-color: ${theme?.active};
  182. }
  183. `;
  184. };
  185. const StyledSidebarItem = styled(Link)`
  186. display: flex;
  187. color: inherit;
  188. position: relative;
  189. cursor: pointer;
  190. font-size: 15px;
  191. line-height: 32px;
  192. height: 34px;
  193. flex-shrink: 0;
  194. transition: 0.15s color linear;
  195. &:before {
  196. display: block;
  197. content: '';
  198. position: absolute;
  199. top: 4px;
  200. left: -20px;
  201. bottom: 6px;
  202. width: 5px;
  203. border-radius: 0 3px 3px 0;
  204. background-color: transparent;
  205. transition: 0.15s background-color linear;
  206. }
  207. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  208. margin: 0 4px;
  209. &:before {
  210. top: auto;
  211. left: 5px;
  212. bottom: -10px;
  213. height: 5px;
  214. width: auto;
  215. right: 5px;
  216. border-radius: 3px 3px 0 0;
  217. }
  218. }
  219. &:hover,
  220. &:focus {
  221. color: ${p => p.theme.white};
  222. }
  223. &.focus-visible {
  224. outline: none;
  225. background: #584c66;
  226. padding: 0 19px;
  227. margin: 0 -19px;
  228. &:before {
  229. left: 0;
  230. }
  231. }
  232. ${getActiveStyle};
  233. `;
  234. const SidebarItemWrapper = styled('div')`
  235. display: flex;
  236. align-items: center;
  237. width: 100%;
  238. `;
  239. const SidebarItemIcon = styled('span')`
  240. content: '';
  241. display: inline-flex;
  242. width: 32px;
  243. height: 22px;
  244. font-size: 20px;
  245. align-items: center;
  246. flex-shrink: 0;
  247. svg {
  248. display: block;
  249. margin: 0 auto;
  250. }
  251. `;
  252. const SidebarItemLabel = styled('span')`
  253. margin-left: 12px;
  254. white-space: nowrap;
  255. opacity: 1;
  256. flex: 1;
  257. display: flex;
  258. align-items: center;
  259. justify-content: space-between;
  260. `;
  261. const getCollapsedBadgeStyle = ({collapsed, theme}) => {
  262. if (!collapsed) {
  263. return '';
  264. }
  265. return css`
  266. text-indent: -99999em;
  267. position: absolute;
  268. right: 0;
  269. top: 1px;
  270. background: ${theme.red300};
  271. width: ${theme.sidebar.smallBadgeSize};
  272. height: ${theme.sidebar.smallBadgeSize};
  273. border-radius: ${theme.sidebar.smallBadgeSize};
  274. line-height: ${theme.sidebar.smallBadgeSize};
  275. box-shadow: ${theme.sidebar.boxShadow};
  276. `;
  277. };
  278. const SidebarItemBadge = styled(({collapsed: _, ...props}) => <span {...props} />)`
  279. display: block;
  280. text-align: center;
  281. color: ${p => p.theme.white};
  282. font-size: 12px;
  283. background: ${p => p.theme.red300};
  284. width: ${p => p.theme.sidebar.badgeSize};
  285. height: ${p => p.theme.sidebar.badgeSize};
  286. border-radius: ${p => p.theme.sidebar.badgeSize};
  287. line-height: ${p => p.theme.sidebar.badgeSize};
  288. ${getCollapsedBadgeStyle};
  289. `;
  290. const CollapsedFeatureBadge = styled(FeatureBadge)`
  291. position: absolute;
  292. top: 0;
  293. right: 0;
  294. `;
  295. CollapsedFeatureBadge.defaultProps = {
  296. variant: 'indicator',
  297. noTooltip: true,
  298. };