sidebarItem.tsx 7.8 KB

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