AccountLayout.utils.ts38 lines · main
1import type { SidebarSection } from './AccountLayout.types'
2import type { SubMenuSection } from '@/components/ui/ProductMenu/ProductMenu.types'
3
4/**
5 * Converts AccountLayout SidebarSection[] to SubMenuSection[] for SubMenu/ProductMenu.
6 * Defensive: handles missing or malformed sections/links.
7 */
8export function toSubMenuSections(sections: SidebarSection[]): SubMenuSection[] {
9 if (!Array.isArray(sections)) return []
10 return sections
11 .filter((s): s is SidebarSection => s != null && typeof s === 'object')
12 .map((s) => ({
13 key: s.key ?? '',
14 heading: s.heading,
15 links: (s.links ?? [])
16 .filter((l) => l != null && typeof l === 'object' && l.key && l.label != null)
17 .map((l) => ({
18 key: l.key,
19 label: l.label,
20 href: l.href,
21 })),
22 }))
23 .filter((s) => s.key || s.heading)
24}
25
26/**
27 * Returns the key of the first active link across all sections.
28 * Used to highlight the current page in SubMenu.
29 */
30export function getActiveKey(sections: SidebarSection[]): string | undefined {
31 if (!Array.isArray(sections)) return undefined
32 for (const section of sections) {
33 if (!section?.links || !Array.isArray(section.links)) continue
34 const active = section.links.find((l) => l?.isActive === true)
35 if (active?.key) return active.key
36 }
37 return undefined
38}