sidebarItem.tsx 8.0 KB

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