import { Count, Badge, Gridicon } from '@automattic/components'; import { Icon, chevronRightSmall, external } from '@wordpress/icons'; import clsx from 'clsx'; import PropTypes from 'prop-types'; import { useEffect } from 'react'; import { useSelector } from 'react-redux'; import TranslatableString from 'calypso/components/translatable/proptype'; import { decodeEntities, stripHTML } from 'calypso/lib/formatting'; import { isExternal } from 'calypso/lib/url'; import { preload } from 'calypso/sections-helper'; import { getSidebarIsCollapsed } from 'calypso/state/ui/selectors'; export default function SidebarItem( props ) { const isExternalLink = isExternal( props.link ); const showAsExternal = ( isExternalLink && ! props.forceInternalLink ) || props.forceExternalLink; const classes = clsx( props.className, props.tipTarget, { selected: props.selected, 'has-unseen': props.hasUnseen, 'tooltip tooltip-right': !! props.tooltip, } ); const sidebarIsCollapsed = useSelector( getSidebarIsCollapsed ); const { icon, customIcon, count, badge } = props; let _preloaded = false; const itemPreload = () => { if ( ! _preloaded && props.preloadSectionName ) { _preloaded = true; preload(); } }; const handleNavigate = ( event ) => { props.onNavigate?.( event, props.link ); }; const expandSectionIfSelected = () => { const { expandSection, selected } = props; if ( selected && typeof expandSection === 'function' ) { expandSection(); } }; useEffect( expandSectionIfSelected, [ props.selected ] ); const linkProps = showAsExternal ? { target: '_blank', rel: 'noreferrer' } : {}; return (
  • { icon && ( typeof icon === 'string' ? ( ) : ( ) ) } { customIcon && customIcon } { /* eslint-disable wpcalypso/jsx-classname-namespace */ } { // String labels should be sanitized, whereas React components should be rendered as is 'string' === typeof props.label ? stripHTML( decodeEntities( props.label ) ) : props.label } { !! count && } { !! badge && ( { badge } ) } { ( showAsExternal || props.forceShowExternalIcon ) && ! sidebarIsCollapsed && ( ) } { props.forceChevronIcon && } { props.children }
  • ); } SidebarItem.propTypes = { label: TranslatableString.isRequired, tooltip: TranslatableString, className: PropTypes.string, link: PropTypes.string.isRequired, onNavigate: PropTypes.func, icon: PropTypes.oneOfType( [ PropTypes.string, PropTypes.func, PropTypes.object ] ), customIcon: PropTypes.object, selected: PropTypes.bool, expandSection: PropTypes.func, preloadSectionName: PropTypes.string, forceExternalLink: PropTypes.bool, forceInternalLink: PropTypes.bool, forceShowExternalIcon: PropTypes.bool, testTarget: PropTypes.string, tipTarget: PropTypes.string, count: PropTypes.number, badge: PropTypes.string, };