2023-10-09 18:23:12 +00:00
|
|
|
import { Icon as IconTest } from 'lucide-react';
|
|
|
|
import clsx from 'clsx';
|
2023-10-16 20:23:23 +00:00
|
|
|
import { MouseEventHandler, PropsWithChildren } from 'react';
|
2022-06-23 07:25:56 +00:00
|
|
|
|
2022-06-28 16:36:40 +00:00
|
|
|
import { AutomationTestingProps } from '@/types';
|
|
|
|
|
2023-10-09 18:23:12 +00:00
|
|
|
import { Icon } from '@@/Icon';
|
|
|
|
|
|
|
|
import { useSidebarState } from '../useSidebarState';
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
import { Wrapper } from './Wrapper';
|
2023-10-09 18:23:12 +00:00
|
|
|
import { SidebarTooltip } from './SidebarTooltip';
|
|
|
|
import { useSidebarSrefActive } from './useSidebarSrefActive';
|
2022-06-23 07:25:56 +00:00
|
|
|
|
2022-06-28 16:36:40 +00:00
|
|
|
interface Props extends AutomationTestingProps {
|
2023-10-09 18:23:12 +00:00
|
|
|
icon?: IconTest;
|
2022-06-23 07:25:56 +00:00
|
|
|
to: string;
|
|
|
|
params?: object;
|
|
|
|
label: string;
|
2023-10-09 18:23:12 +00:00
|
|
|
isSubMenu?: boolean;
|
|
|
|
ignorePaths?: string[];
|
|
|
|
includePaths?: string[];
|
2022-06-28 07:42:42 +00:00
|
|
|
}
|
2022-06-23 07:25:56 +00:00
|
|
|
|
|
|
|
export function SidebarItem({
|
2022-06-28 07:42:42 +00:00
|
|
|
icon,
|
2022-06-23 07:25:56 +00:00
|
|
|
to,
|
|
|
|
params,
|
|
|
|
label,
|
2023-10-09 18:23:12 +00:00
|
|
|
isSubMenu = false,
|
|
|
|
ignorePaths = [],
|
|
|
|
includePaths = [],
|
2022-06-28 16:36:40 +00:00
|
|
|
'data-cy': dataCy,
|
2022-06-23 07:25:56 +00:00
|
|
|
}: Props) {
|
2023-10-09 18:23:12 +00:00
|
|
|
const { isOpen } = useSidebarState();
|
|
|
|
const anchorProps = useSidebarSrefActive(to, undefined, params, undefined, {
|
|
|
|
ignorePaths,
|
|
|
|
includePaths,
|
|
|
|
});
|
|
|
|
|
2023-10-16 20:23:23 +00:00
|
|
|
const sidebarAnchor = (
|
2023-10-09 18:23:12 +00:00
|
|
|
<Wrapper label={label}>
|
2023-10-16 20:23:23 +00:00
|
|
|
<ItemAnchor
|
2023-10-09 18:23:12 +00:00
|
|
|
href={anchorProps.href}
|
|
|
|
onClick={anchorProps.onClick}
|
2023-10-16 20:23:23 +00:00
|
|
|
className={anchorProps.className}
|
|
|
|
dataCy={dataCy}
|
|
|
|
isOpen={isOpen}
|
|
|
|
isSubMenu={isSubMenu}
|
2023-10-09 18:23:12 +00:00
|
|
|
>
|
|
|
|
{!!icon && <Icon icon={icon} className={clsx('flex [&>svg]:w-4')} />}
|
|
|
|
{(isOpen || isSubMenu) && <span>{label}</span>}
|
2023-10-16 20:23:23 +00:00
|
|
|
</ItemAnchor>
|
2023-10-09 18:23:12 +00:00
|
|
|
</Wrapper>
|
2022-06-23 07:25:56 +00:00
|
|
|
);
|
|
|
|
|
2023-10-16 20:23:23 +00:00
|
|
|
if (isOpen || isSubMenu) return sidebarAnchor;
|
2023-10-09 18:23:12 +00:00
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
return (
|
2023-10-16 20:23:23 +00:00
|
|
|
<SidebarTooltip
|
|
|
|
content={
|
|
|
|
<div className="bg-blue-8 be:bg-gray-8 th-dark:bg-gray-true-8 th-highcontrast:bg-black th-highcontrast:border th-highcontrast:border-solid th-highcontrast:border-white rounded">
|
|
|
|
<Wrapper label={label}>
|
|
|
|
<ItemAnchor
|
|
|
|
href={anchorProps.href}
|
|
|
|
onClick={anchorProps.onClick}
|
|
|
|
className={anchorProps.className}
|
|
|
|
dataCy={dataCy}
|
|
|
|
isOpen={isOpen}
|
|
|
|
isSubMenu={isSubMenu}
|
|
|
|
>
|
|
|
|
<span className="px-3">{label}</span>
|
|
|
|
</ItemAnchor>
|
|
|
|
</Wrapper>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<span className="w-full">{sidebarAnchor}</span>
|
2023-10-09 18:23:12 +00:00
|
|
|
</SidebarTooltip>
|
2022-06-23 07:25:56 +00:00
|
|
|
);
|
|
|
|
}
|
2023-10-16 20:23:23 +00:00
|
|
|
|
|
|
|
type ItemAnchorProps = {
|
|
|
|
href?: string;
|
|
|
|
onClick: MouseEventHandler<unknown>;
|
|
|
|
className: string;
|
|
|
|
isOpen: boolean;
|
|
|
|
isSubMenu: boolean;
|
|
|
|
dataCy?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function ItemAnchor({
|
|
|
|
href,
|
|
|
|
onClick,
|
|
|
|
className,
|
|
|
|
isOpen,
|
|
|
|
isSubMenu,
|
|
|
|
dataCy,
|
|
|
|
children,
|
|
|
|
}: PropsWithChildren<ItemAnchorProps>) {
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
href={href}
|
|
|
|
onClick={onClick}
|
|
|
|
className={clsx(
|
|
|
|
className,
|
|
|
|
'text-inherit no-underline hover:text-inherit hover:no-underline focus:text-inherit focus:no-underline',
|
|
|
|
'flex h-8 w-full flex-1 items-center space-x-4 rounded-md text-sm',
|
|
|
|
'transition-colors duration-200 hover:bg-blue-5/20 be:hover:bg-gray-5/20 th-dark:hover:bg-gray-true-5/20',
|
|
|
|
{
|
|
|
|
// submenu items are always expanded (in a tooltip or in the sidebar)
|
|
|
|
'w-full justify-start px-3': isOpen || isSubMenu,
|
|
|
|
'w-8 justify-center': !isOpen && !isSubMenu,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
data-cy={dataCy}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|