2023-05-15 08:51:32 +08:00
|
|
|
'use client'
|
2023-08-28 19:48:53 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
import { useSelectedLayoutSegment } from 'next/navigation'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
import Link from 'next/link'
|
|
|
|
|
2023-08-28 19:48:53 +08:00
|
|
|
export type NavIcon = React.ComponentType<
|
|
|
|
React.PropsWithoutRef<React.ComponentProps<'svg'>> & {
|
|
|
|
title?: string | undefined
|
|
|
|
titleId?: string | undefined
|
|
|
|
}
|
|
|
|
>
|
|
|
|
|
|
|
|
export type NavLinkProps = {
|
|
|
|
name: string
|
|
|
|
href: string
|
|
|
|
iconMap: {
|
|
|
|
selected: NavIcon
|
|
|
|
normal: NavIcon
|
|
|
|
}
|
2024-01-11 13:26:34 +08:00
|
|
|
mode?: string
|
2023-08-28 19:48:53 +08:00
|
|
|
}
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
export default function NavLink({
|
|
|
|
name,
|
|
|
|
href,
|
|
|
|
iconMap,
|
2023-11-27 11:47:48 +08:00
|
|
|
mode = 'expand',
|
2023-08-28 19:48:53 +08:00
|
|
|
}: NavLinkProps) {
|
2023-05-15 08:51:32 +08:00
|
|
|
const segment = useSelectedLayoutSegment()
|
2023-12-18 15:41:24 +08:00
|
|
|
const formattedSegment = (() => {
|
|
|
|
let res = segment?.toLowerCase()
|
|
|
|
// logs and annotations use the same nav
|
|
|
|
if (res === 'annotations')
|
|
|
|
res = 'logs'
|
|
|
|
|
|
|
|
return res
|
|
|
|
})()
|
|
|
|
const isActive = href.toLowerCase().split('/')?.pop() === formattedSegment
|
2023-05-15 08:51:32 +08:00
|
|
|
const NavIcon = isActive ? iconMap.selected : iconMap.normal
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
key={name}
|
|
|
|
href={href}
|
|
|
|
className={classNames(
|
|
|
|
isActive ? 'bg-primary-50 text-primary-600 font-semibold' : 'text-gray-700 hover:bg-gray-100 hover:text-gray-700',
|
2024-01-11 13:26:34 +08:00
|
|
|
'group flex items-center h-9 rounded-md py-2 text-sm font-normal',
|
|
|
|
mode === 'expand' ? 'px-3' : 'px-2.5',
|
2023-05-15 08:51:32 +08:00
|
|
|
)}
|
2024-05-06 11:34:56 +08:00
|
|
|
title={mode === 'collapse' ? name : ''}
|
2023-05-15 08:51:32 +08:00
|
|
|
>
|
|
|
|
<NavIcon
|
|
|
|
className={classNames(
|
2024-01-11 13:26:34 +08:00
|
|
|
'h-4 w-4 flex-shrink-0',
|
2023-05-15 08:51:32 +08:00
|
|
|
isActive ? 'text-primary-600' : 'text-gray-700',
|
2024-01-11 13:26:34 +08:00
|
|
|
mode === 'expand' ? 'mr-2' : 'mr-0',
|
2023-05-15 08:51:32 +08:00
|
|
|
)}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
2023-11-27 11:47:48 +08:00
|
|
|
{mode === 'expand' && name}
|
2023-05-15 08:51:32 +08:00
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|