2022-06-28 07:42:42 +00:00
|
|
|
import {
|
|
|
|
TransitionOptions,
|
|
|
|
useCurrentStateAndParams,
|
|
|
|
useSrefActive as useUiRouterSrefActive,
|
|
|
|
} from '@uirouter/react';
|
|
|
|
import clsx from 'clsx';
|
|
|
|
import { ComponentProps } from 'react';
|
2022-07-06 05:09:14 +00:00
|
|
|
import ReactTooltip from 'react-tooltip';
|
2022-06-28 07:42:42 +00:00
|
|
|
|
2022-06-28 16:36:40 +00:00
|
|
|
import { AutomationTestingProps } from '@/types';
|
|
|
|
|
2022-06-28 07:42:42 +00:00
|
|
|
import { Link } from '@@/Link';
|
|
|
|
import { IconProps, Icon } from '@@/Icon';
|
|
|
|
|
|
|
|
import { useSidebarState } from '../useSidebarState';
|
|
|
|
|
2022-06-28 16:36:40 +00:00
|
|
|
interface Props
|
|
|
|
extends IconProps,
|
|
|
|
ComponentProps<typeof Link>,
|
|
|
|
AutomationTestingProps {
|
2022-06-28 07:42:42 +00:00
|
|
|
label: string;
|
|
|
|
ignorePaths?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Head({
|
|
|
|
to,
|
|
|
|
options,
|
|
|
|
params = {},
|
|
|
|
label,
|
|
|
|
icon,
|
|
|
|
ignorePaths = [],
|
2022-06-28 16:36:40 +00:00
|
|
|
'data-cy': dataCy,
|
2022-06-28 07:42:42 +00:00
|
|
|
}: Props) {
|
|
|
|
const { isOpen } = useSidebarState();
|
|
|
|
const anchorProps = useSrefActive(
|
|
|
|
to,
|
2022-09-08 01:49:09 +00:00
|
|
|
'bg-blue-8 be:bg-gray-8 th-dark:bg-gray-true-8',
|
2022-06-28 07:42:42 +00:00
|
|
|
params,
|
|
|
|
options,
|
|
|
|
ignorePaths
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
href={anchorProps.href}
|
|
|
|
onClick={anchorProps.onClick}
|
|
|
|
className={clsx(
|
|
|
|
anchorProps.className,
|
2022-07-06 05:09:14 +00:00
|
|
|
'text-inherit no-underline hover:no-underline hover:text-inherit focus:no-underline focus:text-inherit',
|
|
|
|
'w-full flex-1 rounded-md flex items-center h-8 space-x-4 text-sm',
|
2022-09-08 01:49:09 +00:00
|
|
|
'hover:bg-blue-9 th-dark:hover:bg-gray-true-9 be:hover:bg-gray-9 transition-colors duration-200',
|
2022-07-06 05:09:14 +00:00
|
|
|
{
|
|
|
|
'px-3 justify-start w-full': isOpen,
|
|
|
|
'justify-center w-8': !isOpen,
|
|
|
|
}
|
2022-06-28 07:42:42 +00:00
|
|
|
)}
|
2022-07-06 05:09:14 +00:00
|
|
|
data-tip={label}
|
2022-06-28 16:36:40 +00:00
|
|
|
data-cy={dataCy}
|
2022-06-28 07:42:42 +00:00
|
|
|
>
|
2022-07-06 05:09:14 +00:00
|
|
|
{!!icon && (
|
|
|
|
<Icon icon={icon} feather className={clsx('flex [&>svg]:w-4')} />
|
|
|
|
)}
|
|
|
|
{isOpen && <span>{label}</span>}
|
|
|
|
|
|
|
|
<ReactTooltip
|
|
|
|
type="info"
|
|
|
|
place="right"
|
|
|
|
effect="solid"
|
|
|
|
className="!opacity-100 bg-blue-9 be:bg-gray-9 !rounded-md !py-1 !px-2"
|
|
|
|
arrowColor="transparent"
|
|
|
|
disable={isOpen}
|
|
|
|
/>
|
2022-06-28 07:42:42 +00:00
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function useSrefActive(
|
|
|
|
to: string,
|
|
|
|
activeClassName: string,
|
|
|
|
params: Partial<Record<string, string>> = {},
|
|
|
|
options: TransitionOptions = {},
|
|
|
|
ignorePaths: string[] = []
|
|
|
|
) {
|
2022-07-06 05:09:14 +00:00
|
|
|
const { state: { name: stateName = '' } = {} } = useCurrentStateAndParams();
|
2022-06-28 07:42:42 +00:00
|
|
|
const anchorProps = useUiRouterSrefActive(
|
|
|
|
to,
|
|
|
|
params || {},
|
|
|
|
activeClassName,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
|
2022-07-06 05:09:14 +00:00
|
|
|
const className = ignorePaths.some((path) => stateName.includes(path))
|
2022-06-28 07:42:42 +00:00
|
|
|
? ''
|
|
|
|
: anchorProps.className;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...anchorProps,
|
|
|
|
className,
|
|
|
|
};
|
|
|
|
}
|