-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Make createMenuItems be based on a signal
Fixes #2752
- Loading branch information
Showing
5 changed files
with
55 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,41 @@ | ||
import { signal } from '@vaadin/hilla-react-signals'; | ||
import type { VaadinWindow } from '../shared/internal.js'; | ||
import type { MenuItem, ViewConfig } from '../types.js'; | ||
|
||
export const viewsSignal = signal((window as VaadinWindow).Vaadin?.views); | ||
|
||
/** | ||
* Creates menu items from the views provided by the server. The views are sorted according to the | ||
* {@link ViewConfig.menu.order}, filtered out if they are explicitly excluded via {@link ViewConfig.menu.exclude}. | ||
* Note that views with no order are put below views with an order. Ties are resolved based on the path string | ||
* comparison. | ||
* | ||
* @param vaadinObject - The Vaadin object containing the server views. | ||
* @returns A list of menu items. | ||
*/ | ||
export function createMenuItems(vaadinObject = (window as VaadinWindow).Vaadin): readonly MenuItem[] { | ||
export function createMenuItems(): readonly MenuItem[] { | ||
// @ts-expect-error: esbuild injection | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
__REGISTER__('createMenuItems', vaadinObject); | ||
__REGISTER__('createMenuItems', (window as VaadinWindow).Vaadin?.views); | ||
const collator = new Intl.Collator('en-US'); | ||
return vaadinObject?.views | ||
? Object.entries(vaadinObject.views) | ||
// Filter out the views that are explicitly excluded from the menu. | ||
.filter(([_, value]) => !value.menu?.exclude) | ||
// Map the views to menu items. | ||
.map(([path, config]) => ({ | ||
to: path, | ||
icon: config.menu?.icon, | ||
title: config.menu?.title ?? config.title, | ||
order: config.menu?.order, | ||
})) | ||
// Sort views according to the order specified in the view configuration. | ||
.sort((menuA, menuB) => { | ||
const ordersDiff = (menuA.order ?? Number.MAX_VALUE) - (menuB.order ?? Number.MAX_VALUE); | ||
return ordersDiff !== 0 ? ordersDiff : collator.compare(menuA.to, menuB.to); | ||
}) | ||
: []; | ||
if (!viewsSignal.value) { | ||
return []; | ||
} | ||
|
||
return ( | ||
Object.entries(viewsSignal.value) | ||
// Filter out the views that are explicitly excluded from the menu. | ||
.filter(([_, value]) => !value.menu?.exclude) | ||
// Map the views to menu items. | ||
.map(([path, config]) => ({ | ||
to: path, | ||
icon: config.menu?.icon, | ||
title: config.menu?.title ?? config.title, | ||
order: config.menu?.order, | ||
})) | ||
// Sort views according to the order specified in the view configuration. | ||
.sort((menuA, menuB) => { | ||
const ordersDiff = (menuA.order ?? Number.MAX_VALUE) - (menuB.order ?? Number.MAX_VALUE); | ||
return ordersDiff !== 0 ? ordersDiff : collator.compare(menuA.to, menuB.to); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
(global as any).window = { Vaadin: {} }; | ||
|
||
export {}; |