settingsNavItem.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type {ReactElement} from 'react';
  2. import {Fragment} from 'react';
  3. import styled from '@emotion/styled';
  4. import Badge from 'sentry/components/badge/badge';
  5. import FeatureBadge from 'sentry/components/badge/featureBadge';
  6. import HookOrDefault from 'sentry/components/hookOrDefault';
  7. import {SecondaryNav} from 'sentry/components/nav/secondary';
  8. import {Tooltip} from 'sentry/components/tooltip';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. type Props = {
  12. label: React.ReactNode;
  13. to: string;
  14. badge?: string | number | null | ReactElement;
  15. id?: string;
  16. index?: boolean;
  17. onClick?: (e: React.MouseEvent) => void;
  18. };
  19. const LabelHook = HookOrDefault({
  20. hookName: 'sidebar:item-label',
  21. defaultComponent: ({children}) => <Fragment>{children}</Fragment>,
  22. });
  23. function SettingsNavBadge({badge}: {badge: string | number | null | ReactElement}) {
  24. if (badge === 'new' || badge === 'beta' || badge === 'alpha') {
  25. return <FeatureBadge type={badge} variant="short" />;
  26. }
  27. if (badge === 'warning') {
  28. return (
  29. <Tooltip title={t('This setting needs review')} position="right">
  30. <StyledBadge text={badge} type="warning" />
  31. </Tooltip>
  32. );
  33. }
  34. if (typeof badge === 'string' || typeof badge === 'number') {
  35. return <StyledBadge text={badge} />;
  36. }
  37. return badge;
  38. }
  39. function SettingsNavItem({badge, label, id, to, index, ...props}: Props) {
  40. return (
  41. <SecondaryNav.Item
  42. to={to}
  43. end={index}
  44. trailingItems={badge ? <SettingsNavBadge badge={badge} /> : null}
  45. {...props}
  46. >
  47. <LabelHook id={id}>{label}</LabelHook>
  48. </SecondaryNav.Item>
  49. );
  50. }
  51. const StyledBadge = styled(Badge)`
  52. font-weight: ${p => p.theme.fontWeightNormal};
  53. height: auto;
  54. line-height: 1;
  55. font-size: ${p => p.theme.fontSizeExtraSmall};
  56. padding: 3px ${space(0.75)};
  57. vertical-align: middle;
  58. `;
  59. export default SettingsNavItem;