Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import type { SidebarRoute } from '@/types'
|
|
|
|
const matchesPath = (pathname: string, path: string) => pathname === path || pathname.startsWith(`${path}/`)
|
|
|
|
export const isSidebarRouteActive = (pathname: string, item: SidebarRoute): boolean => {
|
|
if (item.link && matchesPath(pathname, item.link)) {
|
|
return true
|
|
}
|
|
|
|
if (item.subLink && matchesPath(pathname, item.subLink)) {
|
|
return true
|
|
}
|
|
|
|
if (item.matchPaths?.some((path) => matchesPath(pathname, path))) {
|
|
return true
|
|
}
|
|
|
|
return item.children?.some((child) => isSidebarRouteActive(pathname, child)) ?? false
|
|
}
|
|
|
|
export const findActiveSidebarRoute = (pathname: string, items: SidebarRoute[]): SidebarRoute | undefined => {
|
|
for (const item of items) {
|
|
if (item.link && matchesPath(pathname, item.link)) {
|
|
return item
|
|
}
|
|
|
|
if (item.subLink && matchesPath(pathname, item.subLink)) {
|
|
return item
|
|
}
|
|
|
|
if (item.matchPaths?.some((path) => matchesPath(pathname, path))) {
|
|
return item
|
|
}
|
|
|
|
if (item.children) {
|
|
const activeChild = findActiveSidebarRoute(pathname, item.children)
|
|
|
|
if (activeChild) {
|
|
return activeChild
|
|
}
|
|
}
|
|
}
|
|
|
|
return undefined
|
|
}
|
|
|
|
export const isSidebarGroupOpen = (pathname: string, item: SidebarRoute): boolean =>
|
|
item.children?.some((child) => isSidebarRouteActive(pathname, child)) ?? false
|