OrgMenuContent.utils.ts51 lines · main
| 1 | import type { ReactNode } from 'react' |
| 2 | |
| 3 | export interface OrgNavItem { |
| 4 | label: string |
| 5 | href: string |
| 6 | key: string |
| 7 | icon: ReactNode |
| 8 | } |
| 9 | |
| 10 | export function getOrgActiveRoute(pathname: string): string | undefined { |
| 11 | const segments = pathname.split('/').filter(Boolean) |
| 12 | const orgIndex = segments.indexOf('org') |
| 13 | if (orgIndex === -1 || segments.length <= orgIndex + 1) return undefined |
| 14 | return segments[orgIndex + 2] |
| 15 | } |
| 16 | |
| 17 | const ORG_SETTINGS_ROUTES: string[] = [ |
| 18 | 'general', |
| 19 | 'apps', |
| 20 | 'audit', |
| 21 | 'documents', |
| 22 | 'security', |
| 23 | 'sso', |
| 24 | ] as const |
| 25 | |
| 26 | export function getOrgSectionKeyFromPathname(activeRoute: string | undefined): string | null { |
| 27 | if (activeRoute && ORG_SETTINGS_ROUTES.includes(activeRoute)) { |
| 28 | return 'settings' |
| 29 | } |
| 30 | return null |
| 31 | } |
| 32 | |
| 33 | export function isOrgMenuActive( |
| 34 | item: OrgNavItem, |
| 35 | index: number, |
| 36 | pathname: string, |
| 37 | activeRoute: string | undefined |
| 38 | ): boolean { |
| 39 | if (index === 0) { |
| 40 | return activeRoute === undefined |
| 41 | } |
| 42 | if (item.key === 'settings') { |
| 43 | const route = activeRoute ?? getOrgActiveRoute(pathname) |
| 44 | if (route === undefined) return false |
| 45 | return ( |
| 46 | route === 'settings' || |
| 47 | ORG_SETTINGS_ROUTES.includes(route as (typeof ORG_SETTINGS_ROUTES)[number]) |
| 48 | ) |
| 49 | } |
| 50 | return activeRoute === item.key |
| 51 | } |