95 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

2023-05-15 08:51:32 +08:00
'use client'
import React from 'react'
2023-05-15 08:51:32 +08:00
import { useSelectedLayoutSegment } from 'next/navigation'
import Link from 'next/link'
import classNames from '@/utils/classnames'
import type { RemixiconComponentType } from '@remixicon/react'
2023-05-15 08:51:32 +08:00
2023-08-28 19:48:53 +08:00
export type NavIcon = React.ComponentType<
React.PropsWithoutRef<React.ComponentProps<'svg'>> & {
title?: string | undefined
titleId?: string | undefined
}> | RemixiconComponentType
2023-08-28 19:48:53 +08:00
export type NavLinkProps = {
name: string
href: string
iconMap: {
selected: NavIcon
normal: NavIcon
}
mode?: string
disabled?: boolean
2023-08-28 19:48:53 +08:00
}
const NavLink = ({
2023-05-15 08:51:32 +08:00
name,
href,
iconMap,
mode = 'expand',
disabled = false,
}: NavLinkProps) => {
2023-05-15 08:51:32 +08:00
const segment = useSelectedLayoutSegment()
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
if (disabled) {
return (
<button
key={name}
type='button'
disabled
className={classNames(
'system-sm-medium flex h-8 cursor-not-allowed items-center rounded-lg text-components-menu-item-text opacity-30 hover:bg-components-menu-item-bg-hover',
mode === 'expand' ? 'pl-3 pr-1' : 'px-1.5',
)}
title={mode === 'collapse' ? name : ''}
aria-disabled
>
<NavIcon
className={classNames(
'h-4 w-4 shrink-0',
mode === 'expand' ? 'mr-2' : 'mr-0',
)}
aria-hidden='true'
/>
{mode === 'expand' && name}
</button>
)
}
2023-05-15 08:51:32 +08:00
return (
<Link
key={name}
href={href}
className={classNames(
isActive
? 'system-sm-semibold border-b-[0.25px] border-l-[0.75px] border-r-[0.25px] border-t-[0.75px] border-effects-highlight-lightmode-off bg-components-menu-item-bg-active text-text-accent-light-mode-only'
: 'system-sm-medium text-components-menu-item-text hover:bg-components-menu-item-bg-hover hover:text-components-menu-item-text-hover',
'flex h-8 items-center rounded-lg',
mode === 'expand' ? 'pl-3 pr-1' : 'px-1.5',
2023-05-15 08:51:32 +08:00
)}
title={mode === 'collapse' ? name : ''}
2023-05-15 08:51:32 +08:00
>
<NavIcon
className={classNames(
'h-4 w-4 shrink-0',
mode === 'expand' ? 'mr-2' : 'mr-0',
2023-05-15 08:51:32 +08:00
)}
aria-hidden='true'
2023-05-15 08:51:32 +08:00
/>
{mode === 'expand' && name}
2023-05-15 08:51:32 +08:00
</Link>
)
}
export default React.memo(NavLink)