import { useCallback } from 'react'; import classNames from 'classnames'; import { useRouteMatch, NavLink } from 'react-router-dom'; import { Icon } from 'mastodon/components/icon'; import type { IconProp } from 'mastodon/components/icon'; export const ColumnLink: React.FC<{ icon: React.ReactNode; iconComponent?: IconProp; activeIcon?: React.ReactNode; activeIconComponent?: IconProp; isActive?: (match: unknown, location: { pathname: string }) => boolean; text: string; to?: string; href?: string; onClick?: () => void; method?: string; badge?: React.ReactNode; transparent?: boolean; optional?: boolean; children?: React.ReactNode; className?: string; id?: string; }> = ({ icon, activeIcon, iconComponent, activeIconComponent, text, to, href, onClick, method, badge, transparent, optional, children, ...other }) => { const match = useRouteMatch(to ?? ''); const className = classNames('column-link', { 'column-link--transparent': transparent, 'column-link--optional': optional, }); const badgeElement = typeof badge !== 'undefined' ? ( {badge} ) : null; const iconElement = iconComponent ? ( ) : ( icon ); const activeIconElement = activeIcon ?? (activeIconComponent ? ( ) : ( iconElement )); const active = !!match; const childElement = typeof children !== 'undefined' ?

{children}

: null; const handleClick = useCallback((ev: React.MouseEvent) => { ev.preventDefault(); onClick?.(); }, [onClick]); if (href) { return ( {active ? activeIconElement : iconElement} {text} {badgeElement} {childElement} ); } else if (to) { return ( {active ? activeIconElement : iconElement} {text} {badgeElement} {childElement} ); } else if (onClick) { return ( {active ? activeIconElement : iconElement} {text} {badgeElement} {childElement} ); } else { return null; } };