-
Notifications
You must be signed in to change notification settings - Fork 254
chore: refactor and centralize app menu access COMPASS-9976 #7493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a95c2a4
chore: refactor and centralize app menu access
addaleax 79cc9be
fixup: check + test
addaleax e98afa3
fixup: separate package
addaleax 500892a
fixup: compass-data-modeling does not yet depend on compass-electron-…
addaleax 07da41f
fixup: remove peer dep check workaround
addaleax 020d08e
fixup: cr
addaleax 299d026
fixup: cr
addaleax 49936b6
Merge remote-tracking branch 'origin/main' into 9976-dev-part-p1
addaleax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,141 @@ | ||
| import React, { useContext, useEffect } from 'react'; | ||
| import { createServiceLocator } from '@mongodb-js/compass-app-registry'; | ||
|
|
||
| // Type-only import in a separate entry point, so this is fine | ||
| // eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
| import type { MenuItemConstructorOptions } from 'electron'; | ||
|
|
||
| export type CompassAppMenu<ClickHandlerType = () => void> = Omit< | ||
| MenuItemConstructorOptions, | ||
| 'click' | 'submenu' | ||
| > & { click?: ClickHandlerType; submenu?: CompassAppMenu<ClickHandlerType>[] }; | ||
|
|
||
| export interface ApplicationMenuProvider { | ||
| // These functions return 'unsubscribe'-style listeners to remove | ||
| // the handlers again | ||
| showApplicationMenu(this: void, menu: CompassAppMenu): () => void; | ||
| handleMenuRole( | ||
| this: void, | ||
| role: MenuItemConstructorOptions['role'], | ||
| handler: () => void | ||
| ): () => void; | ||
| } | ||
|
|
||
| const ApplicationMenuContext = React.createContext<ApplicationMenuProvider>({ | ||
| showApplicationMenu: () => () => {}, | ||
| handleMenuRole: () => () => {}, | ||
| }); | ||
|
|
||
| export function ApplicationMenuContextProvider({ | ||
| provider, | ||
| children, | ||
| }: { | ||
| provider: ApplicationMenuProvider; | ||
| children: React.ReactNode; | ||
| }) { | ||
| return ( | ||
| <ApplicationMenuContext.Provider value={provider}> | ||
| {children} | ||
| </ApplicationMenuContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| function useApplicationMenuService(): ApplicationMenuProvider { | ||
| return useContext(ApplicationMenuContext); | ||
| } | ||
|
|
||
| export const applicationMenuServiceLocator = createServiceLocator( | ||
| useApplicationMenuService, | ||
| 'applicationMenuServiceLocator' | ||
| ); | ||
|
|
||
| // Shared helper that is useful in a few places since we need to | ||
| // translate between 'real function' click handlers and | ||
| // string identifiers for those click handlers in a few places. | ||
| export function transformAppMenu<T, U>( | ||
| menu: CompassAppMenu<T>, | ||
| transform: ( | ||
| cb: Omit<CompassAppMenu<T>, 'submenu'> | ||
| ) => Omit<CompassAppMenu<U>, 'submenu'> | ||
| ): CompassAppMenu<U> { | ||
| return { | ||
| ...transform({ ...menu }), | ||
| submenu: menu.submenu | ||
| ? menu.submenu.map((sub) => transformAppMenu(sub, transform)) | ||
| : undefined, | ||
| }; | ||
| } | ||
|
|
||
| const objectIds = new WeakMap<object, number>(); | ||
| let objectIdCounter = 0; | ||
|
|
||
| function getObjectId(obj: object): number { | ||
| let id = objectIds.get(obj); | ||
| if (id === undefined) { | ||
| id = ++objectIdCounter; | ||
| objectIds.set(obj, id); | ||
| } | ||
| return id; | ||
| } | ||
|
|
||
| // Hook to set up an additional application menu, as well as | ||
| // override handlers for pre-defined Electron menu roles. | ||
| // | ||
| // Example usage: | ||
| // | ||
| // useApplicationMenu({ | ||
| // menu: { | ||
| // label: '&MyMenu', | ||
| // submenu: [ | ||
| // { | ||
| // label: 'Do Something', | ||
| // click: () => { ... } | ||
| // } | ||
| // ] | ||
| // }, | ||
| // roles: { | ||
| // undo: () => { ... }, | ||
| // redo: () => { ... } | ||
| // } | ||
| // }); | ||
| // | ||
| // You will typically want to memoize the callbacks used in these objects | ||
| // since they end up as part of the dependency array for this hook. | ||
| export function useApplicationMenu({ | ||
| menu, | ||
| roles, | ||
| }: { | ||
| menu?: CompassAppMenu; | ||
| roles?: Partial< | ||
| Record<NonNullable<MenuItemConstructorOptions['role']>, () => void> | ||
| >; | ||
| }): void { | ||
| const { showApplicationMenu, handleMenuRole } = useApplicationMenuService(); | ||
|
|
||
| useEffect(() => { | ||
| const hideMenu = menu && showApplicationMenu(menu); | ||
| const subscriptions = Object.entries(roles ?? {}).map(([role, handler]) => | ||
| handleMenuRole(role as MenuItemConstructorOptions['role'], handler) | ||
| ); | ||
|
|
||
| return () => { | ||
| hideMenu?.(); | ||
| for (const unsubscribe of subscriptions) unsubscribe(); | ||
| }; | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [ | ||
| showApplicationMenu, | ||
| handleMenuRole, | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| menu | ||
| ? JSON.stringify( | ||
| transformAppMenu(menu, (item) => ({ | ||
| ...item, | ||
| click: item.click && getObjectId(item.click), | ||
| })) | ||
| ) | ||
| : undefined, | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| roles ? JSON.stringify(Object.values(roles).map(getObjectId)) : undefined, | ||
| ]); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.