settingsNavItem.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import type {ReactElement} from 'react';
  2. import {Fragment} from 'react';
  3. import {NavLink as RouterNavLink} from 'react-router-dom';
  4. import styled from '@emotion/styled';
  5. import type {LocationDescriptor} from 'history';
  6. import Badge from 'sentry/components/badge/badge';
  7. import FeatureBadge from 'sentry/components/badge/featureBadge';
  8. import HookOrDefault from 'sentry/components/hookOrDefault';
  9. import {Tooltip} from 'sentry/components/tooltip';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import {locationDescriptorToTo} from 'sentry/utils/reactRouter6Compat/location';
  13. type Props = {
  14. label: React.ReactNode;
  15. to: LocationDescriptor;
  16. badge?: string | number | null | ReactElement;
  17. id?: string;
  18. index?: boolean;
  19. onClick?: (e: React.MouseEvent) => void;
  20. };
  21. const LabelHook = HookOrDefault({
  22. hookName: 'sidebar:item-label',
  23. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  24. });
  25. function SettingsNavItem({badge, label, index, id, to, ...props}: Props) {
  26. let renderedBadge: React.ReactNode;
  27. if (badge === 'new') {
  28. renderedBadge = <FeatureBadge type="new" />;
  29. } else if (badge === 'beta') {
  30. renderedBadge = <FeatureBadge type="beta" />;
  31. } else if (badge === 'warning') {
  32. renderedBadge = (
  33. <Tooltip title={t('This setting needs review')} position="right">
  34. <StyledBadge text={badge} type="warning" />
  35. </Tooltip>
  36. );
  37. } else if (typeof badge === 'string' || typeof badge === 'number') {
  38. renderedBadge = <StyledBadge text={badge} />;
  39. } else {
  40. renderedBadge = badge;
  41. }
  42. return (
  43. <StyledNavItem end={index} to={locationDescriptorToTo(to)} {...props}>
  44. <LabelHook id={id}>{label}</LabelHook>
  45. {badge ? renderedBadge : null}
  46. </StyledNavItem>
  47. );
  48. }
  49. const StyledNavItem = styled(RouterNavLink)`
  50. display: block;
  51. color: ${p => p.theme.gray300};
  52. font-size: 14px;
  53. line-height: 30px;
  54. position: relative;
  55. &.active {
  56. color: ${p => p.theme.textColor};
  57. &:before {
  58. background: ${p => p.theme.active};
  59. }
  60. }
  61. &:hover,
  62. &:focus,
  63. &:active {
  64. color: ${p => p.theme.textColor};
  65. outline: none;
  66. }
  67. &:focus-visible {
  68. outline: none;
  69. background: ${p => p.theme.backgroundSecondary};
  70. padding-left: 15px;
  71. margin-left: -15px;
  72. border-radius: 3px;
  73. &:before {
  74. left: -15px;
  75. }
  76. }
  77. &:before {
  78. position: absolute;
  79. content: '';
  80. display: block;
  81. top: 4px;
  82. left: -30px;
  83. height: 20px;
  84. width: 4px;
  85. background: transparent;
  86. border-radius: 0 2px 2px 0;
  87. }
  88. `;
  89. const StyledBadge = styled(Badge)`
  90. font-weight: ${p => p.theme.fontWeightNormal};
  91. height: auto;
  92. line-height: 1;
  93. font-size: ${p => p.theme.fontSizeExtraSmall};
  94. padding: 3px ${space(0.75)};
  95. vertical-align: middle;
  96. `;
  97. export default SettingsNavItem;