@selfhelp/shared 1.20.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,61 +3,79 @@ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
3
  SPDX-License-Identifier: MPL-2.0
4
4
  */
5
5
  import { describe, it, expect } from 'vitest';
6
- import type { IPageItem } from '../../types/pages';
6
+ import type { INavigationMenuItem, INavigationPayload } from '../navigationPayload';
7
7
  import {
8
- DEFAULT_MOBILE_NAV_RENDER,
9
- DEFAULT_WEB_NAV_RENDER,
10
- isMobileNavRender,
11
- isWebNavRender,
12
- MOBILE_NAV_RENDER_OPTIONS,
13
- resolveMobileNavRender,
14
- resolveWebNavRender,
15
- WEB_NAV_RENDER_OPTIONS,
16
- } from '../navRender';
8
+ getNavigationItemHref,
9
+ getNavigationItemLabel,
10
+ flattenNavigationMenuItems,
11
+ } from '../navLinks';
12
+ import { pageUrlToMobileRoute } from '../mobileRoute';
13
+ import {
14
+ DEFAULT_WEB_HEADER_PRESET,
15
+ isWebHeaderPreset,
16
+ resolveWebHeaderPreset,
17
+ WEB_HEADER_PRESET_VALUES,
18
+ } from '../headerPreset';
17
19
  import { DEFAULT_MOBILE_ICON, isMobileIconName, MOBILE_ICON_SET, resolveMobileIcon } from '../mobileIcons';
18
- import { getMenuVisibleChildren, isMenuVisible, isNavigationPage } from '../menuVisibility';
20
+ import {
21
+ flattenMenuItems,
22
+ isPageOnMobileMenu,
23
+ isPageOnWebMenu,
24
+ } from '../menuVisibility';
25
+ import {
26
+ resolveHolderRedirectPath,
27
+ resolveMobileSegmentGroup,
28
+ resolveWebBranchNavGroup,
29
+ } from '../branchNav';
30
+ import { searchMenuPagesInPayload } from '../menuSearch';
19
31
 
20
- function page(partial: Partial<IPageItem>): IPageItem {
32
+ function menuItem(partial: Partial<INavigationMenuItem>): INavigationMenuItem {
21
33
  return {
22
- id: 0,
23
- keyword: 'k',
24
- url: null,
25
- parent_page_id: null,
26
- is_headless: false,
27
- navPosition: null,
28
- footerPosition: null,
34
+ id: 1,
35
+ item_type: 'page',
36
+ label: 'Team',
37
+ position: 10,
38
+ page: {
39
+ id: 9,
40
+ keyword: 'team',
41
+ url: '/team',
42
+ title: 'Team',
43
+ },
29
44
  ...partial,
30
45
  };
31
46
  }
32
47
 
33
- describe('navRender', () => {
34
- it('validates web/mobile render values', () => {
35
- expect(isWebNavRender('tabs')).toBe(true);
36
- expect(isWebNavRender('bottom-tabs')).toBe(false); // mobile-only value
37
- expect(isMobileNavRender('bottom-tabs')).toBe(true);
38
- expect(isMobileNavRender('header-dropdown')).toBe(false); // web-only value
39
- expect(isWebNavRender(null)).toBe(false);
48
+ describe('navLinks', () => {
49
+ it('builds href from page url', () => {
50
+ expect(getNavigationItemHref(menuItem({}))).toBe('/team');
51
+ expect(getNavigationItemHref(menuItem({ page: { id: 1, keyword: 'home', url: '/', title: 'Home' } }))).toBe('/');
40
52
  });
41
53
 
42
- it('resolves to the value when valid and falls back otherwise', () => {
43
- expect(resolveWebNavRender('hero-cards')).toBe('hero-cards');
44
- expect(resolveWebNavRender('nope')).toBe(DEFAULT_WEB_NAV_RENDER);
45
- expect(resolveWebNavRender(null, 'header-dropdown')).toBe('header-dropdown');
46
- expect(resolveMobileNavRender('drawer')).toBe('drawer');
47
- expect(resolveMobileNavRender(undefined)).toBe(DEFAULT_MOBILE_NAV_RENDER);
54
+ it('flattens nested menu items', () => {
55
+ const root = menuItem({
56
+ children: [menuItem({ id: 2, label: 'Child' })],
57
+ });
58
+ expect(flattenNavigationMenuItems([root])).toHaveLength(2);
59
+ expect(getNavigationItemLabel(menuItem({ label: '', page: { id: 1, keyword: 'x', url: null, title: 'T' } }))).toBe('T');
48
60
  });
61
+ });
49
62
 
50
- it('option metadata covers every value with a label + description', () => {
51
- for (const opt of WEB_NAV_RENDER_OPTIONS) {
52
- expect(isWebNavRender(opt.value)).toBe(true);
53
- expect(opt.label.length).toBeGreaterThan(0);
54
- expect(opt.description.length).toBeGreaterThan(0);
55
- }
56
- for (const opt of MOBILE_NAV_RENDER_OPTIONS) {
57
- expect(isMobileNavRender(opt.value)).toBe(true);
58
- expect(opt.label.length).toBeGreaterThan(0);
59
- expect(opt.description.length).toBeGreaterThan(0);
60
- }
63
+ describe('mobileRoute', () => {
64
+ it('maps canonical urls to expo keyword routes', () => {
65
+ expect(pageUrlToMobileRoute('/', 'home')).toBe('/index');
66
+ expect(pageUrlToMobileRoute('/about', 'about')).toBe('/about');
67
+ expect(pageUrlToMobileRoute('/parent/child', 'child')).toBe('/child');
68
+ expect(pageUrlToMobileRoute(null, 'profile')).toBe('/profile');
69
+ });
70
+ });
71
+
72
+ describe('headerPreset', () => {
73
+ it('validates and resolves web header presets', () => {
74
+ expect(isWebHeaderPreset('dropdown')).toBe(true);
75
+ expect(isWebHeaderPreset('nope')).toBe(false);
76
+ expect(resolveWebHeaderPreset('mega-menu')).toBe('mega-menu');
77
+ expect(resolveWebHeaderPreset(null)).toBe(DEFAULT_WEB_HEADER_PRESET);
78
+ expect(WEB_HEADER_PRESET_VALUES).toContain('double-dropdown');
61
79
  });
62
80
  });
63
81
 
@@ -77,30 +95,100 @@ describe('mobileIcons', () => {
77
95
  });
78
96
  });
79
97
 
80
- describe('menuVisibility', () => {
81
- it('treats a page with a nav position and not headless as a menu page', () => {
82
- expect(isMenuVisible(page({ navPosition: 1 }))).toBe(true);
83
- expect(isMenuVisible(page({ navPosition: null }))).toBe(false);
84
- expect(isMenuVisible(page({ navPosition: 1, is_headless: true }))).toBe(false);
98
+ function navigationPayload(items: INavigationMenuItem[]): INavigationPayload {
99
+ return {
100
+ menus: {
101
+ web_header: { key: 'web_header', platform: 'web', surface: 'header', items },
102
+ web_footer: { key: 'web_footer', platform: 'web', surface: 'footer', items: [] },
103
+ mobile_drawer: { key: 'mobile_drawer', platform: 'mobile', surface: 'drawer', items },
104
+ mobile_bottom_tabs: { key: 'mobile_bottom_tabs', platform: 'mobile', surface: 'bottom_tabs', items: [] },
105
+ },
106
+ startup: {
107
+ web_guest_start_page: { keyword: 'home', url: '/', title: 'Home' },
108
+ web_user_start_page: { keyword: 'home', url: '/', title: 'Home' },
109
+ web_user_start_mode: 'fixed_page',
110
+ mobile_guest_start_page: { keyword: 'home', url: '/', title: 'Home' },
111
+ mobile_user_start_page: { keyword: 'home', url: '/', title: 'Home' },
112
+ mobile_user_start_mode: 'fixed_page',
113
+ mobile_start_page_source: 'same_as_web',
114
+ },
115
+ search: {
116
+ mode: 'content_index',
117
+ min_chars: 2,
118
+ result_limit: 8,
119
+ default_visibility: 'all_accessible_pages',
120
+ field_policy: 'all_display_text',
121
+ },
122
+ };
123
+ }
124
+
125
+ describe('menu membership + branch nav', () => {
126
+ const parent = menuItem({
127
+ id: 10,
128
+ page: { id: 100, keyword: 'parent', url: '/parent', title: 'Parent' },
129
+ children: [
130
+ menuItem({ id: 11, page: { id: 101, keyword: 'child-a', url: '/parent/child-a', title: 'A' } }),
131
+ menuItem({ id: 12, page: { id: 102, keyword: 'child-b', url: '/parent/child-b', title: 'B' } }),
132
+ ],
85
133
  });
86
134
 
87
- it('returns only menu-visible children in nav order', () => {
88
- const parent = page({
89
- keyword: 'team',
90
- navPosition: 1,
91
- children: [
92
- page({ keyword: 'b', navPosition: 2 }),
93
- page({ keyword: 'a', navPosition: 1 }),
94
- page({ keyword: 'hidden', navPosition: null }),
95
- page({ keyword: 'headless', navPosition: 3, is_headless: true }),
96
- ],
97
- });
98
- const visible = getMenuVisibleChildren(parent);
99
- expect(visible.map((child) => child.keyword)).toEqual(['a', 'b']);
100
- expect(isNavigationPage(parent)).toBe(true);
135
+ it('detects page membership in resolved menus', () => {
136
+ const payload = navigationPayload([parent]);
137
+ expect(isPageOnWebMenu(payload, 100)).toBe(true);
138
+ expect(isPageOnWebMenu(payload, 999)).toBe(false);
139
+ expect(isPageOnMobileMenu(payload, 101)).toBe(true);
140
+ });
141
+
142
+ it('resolves child segment group for branch navigation', () => {
143
+ const payload = navigationPayload([parent]);
144
+ const segments = resolveWebBranchNavGroup(payload, 100);
145
+ expect(segments?.map((s) => s.keyword)).toEqual(['child-a', 'child-b']);
146
+ });
147
+
148
+ it('redirects holder pages to the first menu-visible child', () => {
149
+ const payload = navigationPayload([parent]);
150
+ expect(resolveHolderRedirectPath(payload, 100, 'web', false)).toBe('/parent/child-a');
151
+ expect(resolveHolderRedirectPath(payload, 100, 'web', true)).toBeNull();
101
152
  });
102
153
 
103
- it('a leaf page (no menu-visible children) is not a navigation page', () => {
104
- expect(isNavigationPage(page({ navPosition: 1, children: [] }))).toBe(false);
154
+ it('flattens menu items for admin helpers', () => {
155
+ expect(flattenMenuItems([parent])).toHaveLength(3);
156
+ });
157
+ });
158
+
159
+ describe('mobile segment group', () => {
160
+ it('prepends self segment when tab page has content and children', () => {
161
+ const payload = navigationPayload([
162
+ menuItem({
163
+ id: 1,
164
+ page: { id: 5, keyword: 'root', url: '/root', title: 'Root', has_content: true },
165
+ children: [
166
+ menuItem({
167
+ id: 2,
168
+ page: { id: 6, keyword: 'one', url: '/root/one', title: 'One' },
169
+ }),
170
+ menuItem({
171
+ id: 3,
172
+ page: { id: 7, keyword: 'two', url: '/root/two', title: 'Two' },
173
+ }),
174
+ ],
175
+ }),
176
+ ]);
177
+ const segments = resolveMobileSegmentGroup(payload, 5);
178
+ expect(segments?.map((s) => s.keyword)).toEqual(['root', 'one', 'two']);
179
+ });
180
+ });
181
+
182
+ describe('menu search', () => {
183
+ it('filters resolved menu pages client-side', () => {
184
+ const payload = navigationPayload([
185
+ menuItem({
186
+ id: 1,
187
+ page: { id: 10, keyword: 'about', url: '/about', title: 'About us' },
188
+ }),
189
+ ]);
190
+ const hits = searchMenuPagesInPayload(payload, 'about');
191
+ expect(hits).toHaveLength(1);
192
+ expect(hits[0]?.keyword).toBe('about');
105
193
  });
106
194
  });
@@ -0,0 +1,33 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import { describe, expect, it } from 'vitest';
6
+ import type { INavigationStartupConfig } from '../navigationPayload';
7
+ import { resolveMobileStartupKeyword, resolveMobileStartupRoute } from '../startup';
8
+
9
+ const baseStartup: INavigationStartupConfig = {
10
+ web_guest_start_page: { id: 1, keyword: 'home', url: '/', title: 'Home' },
11
+ web_user_start_page: { id: 2, keyword: 'dashboard', url: '/dashboard', title: 'Dashboard' },
12
+ web_user_start_mode: 'fixed_page',
13
+ mobile_guest_start_page: { id: 3, keyword: 'welcome', url: '/welcome', title: 'Welcome' },
14
+ mobile_user_start_page: { id: 4, keyword: 'app-home', url: '/app-home', title: 'App home' },
15
+ mobile_user_start_mode: 'fixed_page',
16
+ mobile_start_page_source: 'custom_mobile_pages',
17
+ };
18
+
19
+ describe('startup helpers', () => {
20
+ it('resolveMobileStartupRoute uses custom mobile guest page', () => {
21
+ expect(resolveMobileStartupRoute(baseStartup, false)).toBe('/welcome');
22
+ });
23
+
24
+ it('resolveMobileStartupRoute prefers last visited for logged-in users', () => {
25
+ const startup: INavigationStartupConfig = {
26
+ ...baseStartup,
27
+ mobile_user_start_mode: 'last_visited_then_fixed_page',
28
+ mobile_user_last_visited_page: { id: 9, keyword: 'resume', url: '/resume', title: 'Resume' },
29
+ };
30
+ expect(resolveMobileStartupRoute(startup, true)).toBe('/resume');
31
+ expect(resolveMobileStartupKeyword(startup, true)).toBe('resume');
32
+ });
33
+ });
@@ -0,0 +1,216 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import type { INavigationMenuItem, INavigationPayload } from './navigationPayload';
6
+ import { collectPageIdsFromMenuItems } from './menuMembership';
7
+ import { pageUrlToMobileRoute } from './mobileRoute';
8
+
9
+ export interface IBranchNavSegment {
10
+ pageId: number;
11
+ keyword: string;
12
+ url: string | null;
13
+ label: string;
14
+ icon?: string | null;
15
+ mobile_icon?: string | null;
16
+ }
17
+
18
+ function itemLabel(item: INavigationMenuItem): string {
19
+ return item.label || item.page?.title || item.page?.keyword || '';
20
+ }
21
+
22
+ function toSegment(item: INavigationMenuItem): IBranchNavSegment | null {
23
+ if (!item.page) {
24
+ return null;
25
+ }
26
+ return {
27
+ pageId: item.page.id,
28
+ keyword: item.page.keyword,
29
+ url: item.page.url,
30
+ label: itemLabel(item),
31
+ icon: item.icon ?? item.page.icon,
32
+ mobile_icon: item.page.mobile_icon,
33
+ };
34
+ }
35
+
36
+ function findParentItem(
37
+ items: INavigationMenuItem[],
38
+ pageId: number,
39
+ parent: INavigationMenuItem | null = null,
40
+ ): { parent: INavigationMenuItem | null; siblings: INavigationMenuItem[] } | null {
41
+ for (const item of items) {
42
+ if (item.page?.id === pageId) {
43
+ return { parent, siblings: items };
44
+ }
45
+ if (item.children?.length) {
46
+ const found = findParentItem(item.children, pageId, item);
47
+ if (found) {
48
+ return found;
49
+ }
50
+ }
51
+ }
52
+ return null;
53
+ }
54
+
55
+ function menuVisibleChildItems(item: INavigationMenuItem): INavigationMenuItem[] {
56
+ return (item.children ?? []).filter((child) => child.page != null && child.is_active !== false);
57
+ }
58
+
59
+ function findMenuItemByPageId(
60
+ items: INavigationMenuItem[],
61
+ pageId: number,
62
+ ): INavigationMenuItem | null {
63
+ for (const item of items) {
64
+ if (item.page?.id === pageId) {
65
+ return item;
66
+ }
67
+ if (item.children?.length) {
68
+ const nested = findMenuItemByPageId(item.children, pageId);
69
+ if (nested) {
70
+ return nested;
71
+ }
72
+ }
73
+ }
74
+ return null;
75
+ }
76
+
77
+ function selfSegment(item: INavigationMenuItem): IBranchNavSegment | null {
78
+ return toSegment(item);
79
+ }
80
+
81
+ /**
82
+ * Resolve the sibling/child segment group for in-page branch navigation.
83
+ *
84
+ * 1. If current page has menu-visible children in resolved menus → show children.
85
+ * 2. Else if parent has menu-visible siblings → show sibling group.
86
+ * 3. Else null.
87
+ */
88
+ export function resolveBranchNavGroup(
89
+ payload: INavigationPayload,
90
+ currentPageId: number,
91
+ menuKeys: Array<keyof INavigationPayload['menus']>,
92
+ ): IBranchNavSegment[] | null {
93
+ const allItems: INavigationMenuItem[] = [];
94
+ for (const key of menuKeys) {
95
+ const menu = payload.menus[key];
96
+ if (menu?.items) {
97
+ allItems.push(...menu.items);
98
+ }
99
+ }
100
+
101
+ for (const root of allItems) {
102
+ const selfChildren = menuVisibleChildItems(root);
103
+ if (root.page?.id === currentPageId && selfChildren.length > 0) {
104
+ const segments = selfChildren.map(toSegment).filter((s): s is IBranchNavSegment => s != null);
105
+ return segments.length > 0 ? segments : null;
106
+ }
107
+ const located = findParentItem([root], currentPageId);
108
+ if (located) {
109
+ const directChildren = menuVisibleChildItems(
110
+ located.parent ?? { id: 0, item_type: 'group', label: '', position: 0, children: located.siblings },
111
+ );
112
+ if (located.parent && located.parent.page?.id === currentPageId && directChildren.length > 0) {
113
+ const segments = directChildren.map(toSegment).filter((s): s is IBranchNavSegment => s != null);
114
+ return segments.length > 0 ? segments : null;
115
+ }
116
+ const siblings = located.siblings.filter((s) => s.page != null && s.is_active !== false);
117
+ if (siblings.length > 1) {
118
+ const segments = siblings.map(toSegment).filter((s): s is IBranchNavSegment => s != null);
119
+ return segments.length > 0 ? segments : null;
120
+ }
121
+ }
122
+ }
123
+
124
+ return null;
125
+ }
126
+
127
+ export function resolveMobileSegmentGroup(
128
+ payload: INavigationPayload,
129
+ currentPageId: number,
130
+ ): IBranchNavSegment[] | null {
131
+ const allItems: INavigationMenuItem[] = [];
132
+ for (const key of ['mobile_drawer', 'mobile_bottom_tabs'] as const) {
133
+ const menu = payload.menus[key];
134
+ if (menu?.items) {
135
+ allItems.push(...menu.items);
136
+ }
137
+ }
138
+
139
+ const currentItem = findMenuItemByPageId(allItems, currentPageId);
140
+ if (currentItem) {
141
+ const selfChildren = menuVisibleChildItems(currentItem);
142
+ if (selfChildren.length > 0) {
143
+ const childSegments = selfChildren
144
+ .map(toSegment)
145
+ .filter((segment): segment is IBranchNavSegment => segment != null);
146
+ if (childSegments.length > 0) {
147
+ if (currentItem.page?.has_content) {
148
+ const self = selfSegment(currentItem);
149
+ return self ? [self, ...childSegments] : childSegments;
150
+ }
151
+ return childSegments;
152
+ }
153
+ }
154
+ }
155
+
156
+ return resolveBranchNavGroup(payload, currentPageId, ['mobile_drawer', 'mobile_bottom_tabs']);
157
+ }
158
+
159
+ export function resolveWebBranchNavGroup(
160
+ payload: INavigationPayload,
161
+ currentPageId: number,
162
+ ): IBranchNavSegment[] | null {
163
+ return resolveBranchNavGroup(payload, currentPageId, ['web_header', 'web_footer']);
164
+ }
165
+
166
+ export type TMobilePagePresentation = 'route' | 'modal';
167
+
168
+ export function resolveMobilePagePresentation(
169
+ payload: INavigationPayload,
170
+ pageId: number,
171
+ ): TMobilePagePresentation {
172
+ return isOnAnyMobileMenuFromPayload(payload, pageId) ? 'route' : 'modal';
173
+ }
174
+
175
+ export function isOnAnyMobileMenuFromPayload(payload: INavigationPayload, pageId: number): boolean {
176
+ const drawerIds = collectPageIdsFromMenuItems(payload.menus.mobile_drawer?.items ?? []);
177
+ const tabIds = collectPageIdsFromMenuItems(payload.menus.mobile_bottom_tabs?.items ?? []);
178
+ return drawerIds.has(pageId) || tabIds.has(pageId);
179
+ }
180
+
181
+ /**
182
+ * When a page is a holder (no authored content) but has menu-visible children,
183
+ * return the first child route to auto-select.
184
+ */
185
+ export function resolveHolderRedirectPath(
186
+ payload: INavigationPayload,
187
+ currentPageId: number,
188
+ platform: 'web' | 'mobile',
189
+ hasContent: boolean,
190
+ ): string | null {
191
+ if (hasContent) {
192
+ return null;
193
+ }
194
+
195
+ const segments = platform === 'mobile'
196
+ ? resolveMobileSegmentGroup(payload, currentPageId)
197
+ : resolveWebBranchNavGroup(payload, currentPageId);
198
+ if (!segments || segments.length === 0) {
199
+ return null;
200
+ }
201
+
202
+ const target = segments.find((segment) => segment.pageId !== currentPageId) ?? segments[0];
203
+ if (target.pageId === currentPageId) {
204
+ return null;
205
+ }
206
+
207
+ if (platform === 'mobile') {
208
+ return pageUrlToMobileRoute(target.url, target.keyword);
209
+ }
210
+
211
+ if (target.url && target.url !== '') {
212
+ return target.url.startsWith('/') ? target.url : `/${target.url}`;
213
+ }
214
+
215
+ return target.keyword === 'home' ? '/' : `/${target.keyword}`;
216
+ }
@@ -0,0 +1,52 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Global web header presentation presets (menu-builder `web_header` menu).
7
+ */
8
+
9
+ export type TWebHeaderPreset =
10
+ | 'simple'
11
+ | 'dropdown'
12
+ | 'mega-menu'
13
+ | 'tabs'
14
+ | 'double-dropdown'
15
+ | 'double-mega-menu';
16
+
17
+ export const WEB_HEADER_PRESET_VALUES: readonly TWebHeaderPreset[] = [
18
+ 'simple',
19
+ 'dropdown',
20
+ 'mega-menu',
21
+ 'tabs',
22
+ 'double-dropdown',
23
+ 'double-mega-menu',
24
+ ] as const;
25
+
26
+ export const DEFAULT_WEB_HEADER_PRESET: TWebHeaderPreset = 'dropdown';
27
+
28
+ export interface IWebHeaderPresetOption {
29
+ value: TWebHeaderPreset;
30
+ label: string;
31
+ description: string;
32
+ }
33
+
34
+ export const WEB_HEADER_PRESET_OPTIONS: readonly IWebHeaderPresetOption[] = [
35
+ { value: 'simple', label: 'Simple', description: 'Flat header links for small sites.' },
36
+ { value: 'dropdown', label: 'Dropdown', description: 'Default — nested dropdown menus.' },
37
+ { value: 'mega-menu', label: 'Mega menu', description: 'Rich sections with descriptions and icons.' },
38
+ { value: 'tabs', label: 'Tabs', description: 'Top-level sections as tabs.' },
39
+ { value: 'double-dropdown', label: 'Double header (dropdown)', description: 'Utility row plus main dropdown nav.' },
40
+ { value: 'double-mega-menu', label: 'Double header (mega menu)', description: 'Utility row plus mega menu.' },
41
+ ] as const;
42
+
43
+ export function isWebHeaderPreset(value: unknown): value is TWebHeaderPreset {
44
+ return typeof value === 'string' && (WEB_HEADER_PRESET_VALUES as readonly string[]).includes(value);
45
+ }
46
+
47
+ export function resolveWebHeaderPreset(
48
+ value: unknown,
49
+ fallback: TWebHeaderPreset = DEFAULT_WEB_HEADER_PRESET,
50
+ ): TWebHeaderPreset {
51
+ return isWebHeaderPreset(value) ? value : fallback;
52
+ }
@@ -2,11 +2,13 @@
2
2
  SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
3
  SPDX-License-Identifier: MPL-2.0
4
4
  */
5
- /**
6
- * Navigation contract: render types + option metadata, the curated mobile icon
7
- * set, and the shared menu-visibility predicate. Imported by the web frontend
8
- * (admin selects + renderers) and the mobile app (renderers).
9
- */
10
- export * from './navRender';
11
- export * from './mobileIcons';
5
+ export * from './headerPreset';
6
+ export * from './navigationPayload';
7
+ export * from './menuMembership';
8
+ export * from './branchNav';
9
+ export * from './navLinks';
10
+ export * from './mobileRoute';
12
11
  export * from './menuVisibility';
12
+ export * from './mobileIcons';
13
+ export * from './startup';
14
+ export * from './menuSearch';
@@ -0,0 +1,63 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ import type { INavigationMenu, INavigationMenuItem, TNavigationMenuKey } from './navigationPayload';
6
+
7
+ function flattenItems(items: INavigationMenuItem[]): INavigationMenuItem[] {
8
+ const out: INavigationMenuItem[] = [];
9
+ for (const item of items) {
10
+ out.push(item);
11
+ if (item.children?.length) {
12
+ out.push(...flattenItems(item.children));
13
+ }
14
+ }
15
+ return out;
16
+ }
17
+
18
+ export function collectPageIdsFromMenuItems(items: INavigationMenuItem[]): Set<number> {
19
+ const ids = new Set<number>();
20
+ for (const item of flattenItems(items)) {
21
+ if (item.page?.id != null) {
22
+ ids.add(item.page.id);
23
+ }
24
+ }
25
+ return ids;
26
+ }
27
+
28
+ export function isPageInMenuTree(items: INavigationMenuItem[], pageId: number): boolean {
29
+ return collectPageIdsFromMenuItems(items).has(pageId);
30
+ }
31
+
32
+ export function isPageInResolvedMenus(
33
+ menus: Partial<Record<TNavigationMenuKey, INavigationMenu>>,
34
+ menuKeys: TNavigationMenuKey[],
35
+ pageId: number,
36
+ ): boolean {
37
+ for (const key of menuKeys) {
38
+ const menu = menus[key];
39
+ if (menu && isPageInMenuTree(menu.items, pageId)) {
40
+ return true;
41
+ }
42
+ }
43
+ return false;
44
+ }
45
+
46
+ export function isOnAnyMobileMenu(
47
+ menus: Partial<Record<TNavigationMenuKey, INavigationMenu>>,
48
+ pageId: number,
49
+ ): boolean {
50
+ return isPageInResolvedMenus(menus, ['mobile_drawer', 'mobile_bottom_tabs'], pageId);
51
+ }
52
+
53
+ export function findMenuItemByPageId(
54
+ items: INavigationMenuItem[],
55
+ pageId: number,
56
+ ): INavigationMenuItem | null {
57
+ for (const item of flattenItems(items)) {
58
+ if (item.page?.id === pageId) {
59
+ return item;
60
+ }
61
+ }
62
+ return null;
63
+ }