2023-05-15 08:51:32 +08:00
|
|
|
'use client'
|
2025-07-11 16:41:01 +08:00
|
|
|
import React from 'react'
|
2023-05-15 08:51:32 +08:00
|
|
|
import { useSelectedLayoutSegment } from 'next/navigation'
|
|
|
|
import Link from 'next/link'
|
2024-07-09 15:05:40 +08:00
|
|
|
import classNames from '@/utils/classnames'
|
2025-02-27 15:11:42 +08:00
|
|
|
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<
|
2025-05-29 14:06:12 +08:00
|
|
|
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
|
|
|
|
}
|
2024-01-11 13:26:34 +08:00
|
|
|
mode?: string
|
2025-05-29 14:06:12 +08:00
|
|
|
disabled?: boolean
|
2023-08-28 19:48:53 +08:00
|
|
|
}
|
|
|
|
|
2025-07-11 16:41:01 +08:00
|
|
|
const NavLink = ({
|
2023-05-15 08:51:32 +08:00
|
|
|
name,
|
|
|
|
href,
|
|
|
|
iconMap,
|
2023-11-27 11:47:48 +08:00
|
|
|
mode = 'expand',
|
2025-05-29 14:06:12 +08:00
|
|
|
disabled = false,
|
2025-07-11 16:41:01 +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
|
|
|
|
|
2025-05-29 14:06:12 +08:00
|
|
|
if (disabled) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
key={name}
|
|
|
|
type='button'
|
|
|
|
disabled
|
|
|
|
className={classNames(
|
2025-07-10 10:21:45 +08:00
|
|
|
'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',
|
2025-07-08 20:04:15 +08:00
|
|
|
mode === 'expand' ? 'pl-3 pr-1' : 'px-1.5',
|
2025-05-29 14:06:12 +08:00
|
|
|
)}
|
|
|
|
title={mode === 'collapse' ? name : ''}
|
|
|
|
aria-disabled
|
|
|
|
>
|
|
|
|
<NavIcon
|
|
|
|
className={classNames(
|
2025-07-08 20:04:15 +08:00
|
|
|
'h-4 w-4 shrink-0',
|
2025-05-29 14:06:12 +08:00
|
|
|
mode === 'expand' ? 'mr-2' : 'mr-0',
|
|
|
|
)}
|
2025-07-08 20:04:15 +08:00
|
|
|
aria-hidden='true'
|
2025-05-29 14:06:12 +08:00
|
|
|
/>
|
|
|
|
{mode === 'expand' && name}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
key={name}
|
|
|
|
href={href}
|
|
|
|
className={classNames(
|
2025-07-08 20:04:15 +08:00
|
|
|
isActive
|
2025-07-10 10:21:45 +08:00
|
|
|
? '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',
|
2025-07-08 20:04:15 +08:00
|
|
|
'flex h-8 items-center rounded-lg',
|
|
|
|
mode === 'expand' ? 'pl-3 pr-1' : 'px-1.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(
|
2025-07-08 20:04:15 +08:00
|
|
|
'h-4 w-4 shrink-0',
|
2024-01-11 13:26:34 +08:00
|
|
|
mode === 'expand' ? 'mr-2' : 'mr-0',
|
2023-05-15 08:51:32 +08:00
|
|
|
)}
|
2025-07-08 20:04:15 +08:00
|
|
|
aria-hidden='true'
|
2023-05-15 08:51:32 +08:00
|
|
|
/>
|
2023-11-27 11:47:48 +08:00
|
|
|
{mode === 'expand' && name}
|
2023-05-15 08:51:32 +08:00
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
2025-07-11 16:41:01 +08:00
|
|
|
|
|
|
|
export default React.memo(NavLink)
|