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-12-04 20:47:43 +00:00
|
|
|
import Tippy from '@tippyjs/react';
|
2022-06-28 07:42:42 +00:00
|
|
|
|
2022-06-28 16:36:40 +00:00
|
|
|
import { AutomationTestingProps } from '@/types';
|
|
|
|
|
2022-12-04 20:47:43 +00:00
|
|
|
import 'tippy.js/dist/tippy.css';
|
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
|
|
|
|
);
|
|
|
|
|
2022-12-04 20:47:43 +00:00
|
|
|
const anchor = (
|
2022-06-28 07:42:42 +00:00
|
|
|
<a
|
|
|
|
href={anchorProps.href}
|
|
|
|
onClick={anchorProps.onClick}
|
|
|
|
className={clsx(
|
|
|
|
anchorProps.className,
|
2023-02-12 21:04:24 +00:00
|
|
|
'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-9 be:hover:bg-gray-9 th-dark:hover:bg-gray-true-9',
|
2022-07-06 05:09:14 +00:00
|
|
|
{
|
2023-02-12 21:04:24 +00:00
|
|
|
'w-full justify-start px-3': isOpen,
|
|
|
|
'w-8 justify-center': !isOpen,
|
2022-07-06 05:09:14 +00:00
|
|
|
}
|
2022-06-28 07:42:42 +00:00
|
|
|
)}
|
2022-06-28 16:36:40 +00:00
|
|
|
data-cy={dataCy}
|
2022-06-28 07:42:42 +00:00
|
|
|
>
|
2022-11-28 02:00:28 +00:00
|
|
|
{!!icon && <Icon icon={icon} className={clsx('flex [&>svg]:w-4')} />}
|
2022-07-06 05:09:14 +00:00
|
|
|
{isOpen && <span>{label}</span>}
|
2022-06-28 07:42:42 +00:00
|
|
|
</a>
|
|
|
|
);
|
2022-12-04 20:47:43 +00:00
|
|
|
|
|
|
|
if (isOpen) return anchor;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tippy
|
2023-02-12 21:04:24 +00:00
|
|
|
className="!rounded-md bg-blue-9 !py-2 !px-3 !opacity-100 be:bg-gray-9 th-dark:bg-gray-true-9"
|
2022-12-04 20:47:43 +00:00
|
|
|
content={label}
|
|
|
|
delay={[0, 0]}
|
|
|
|
duration={[0, 0]}
|
|
|
|
zIndex={1000}
|
|
|
|
placement="right"
|
|
|
|
arrow
|
|
|
|
allowHTML
|
|
|
|
interactive
|
|
|
|
>
|
|
|
|
{anchor}
|
|
|
|
</Tippy>
|
|
|
|
);
|
2022-06-28 07:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|