Skip to content

Commit 45b67d8

Browse files
authored
chore: refactor and centralize app menu access COMPASS-9976 (#7493)
Instead of having the logic for accessing the Electron application menu from the collection tabs spread out through the codebase, bundle it in a single interface that gives all Compass plugins access to the menu as needed.
1 parent 04336ad commit 45b67d8

26 files changed

+1345
-281
lines changed

package-lock.json

Lines changed: 231 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-collection/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@mongodb-js/compass-components": "^1.56.0",
5555
"@mongodb-js/compass-connections": "^1.84.0",
5656
"@mongodb-js/compass-editor": "^0.58.0",
57+
"@mongodb-js/compass-electron-menu": "^0.1.0",
5758
"@mongodb-js/compass-generative-ai": "^0.63.0",
5859
"@mongodb-js/compass-logging": "^1.7.22",
5960
"@mongodb-js/compass-telemetry": "^1.19.0",

packages/compass-collection/src/components/collection-tab.tsx

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useCallback, useEffect } from 'react';
22
import { connect } from 'react-redux';
33
import { type CollectionState, selectTab } from '../modules/collection-tab';
44
import { css, ErrorBoundary, TabNavBar } from '@mongodb-js/compass-components';
@@ -19,6 +19,11 @@ import {
1919
useConnectionSupports,
2020
} from '@mongodb-js/compass-connections/provider';
2121
import { usePreference } from 'compass-preferences-model/provider';
22+
import { useApplicationMenu } from '@mongodb-js/compass-electron-menu';
23+
import {
24+
useGlobalAppRegistry,
25+
useLocalAppRegistry,
26+
} from '@mongodb-js/compass-app-registry';
2227

2328
type CollectionSubtabTrackingId = Lowercase<CollectionSubtab> extends infer U
2429
? U extends string
@@ -228,13 +233,88 @@ const CollectionTabWithMetadata: React.FunctionComponent<
228233
);
229234
};
230235

236+
// Setup the Electron application menu for the collection tab
237+
function useCollectionTabApplicationMenu(
238+
collectionMetadata: CollectionMetadata | null
239+
) {
240+
const localAppRegistry = useLocalAppRegistry();
241+
const globalAppRegistry = useGlobalAppRegistry();
242+
const connectionInfoRef = useConnectionInfoRef();
243+
const preferencesReadOnly = usePreference('readOnly');
244+
245+
const shareSchemaClick = useCallback(() => {
246+
localAppRegistry.emit('menu-share-schema-json');
247+
}, [localAppRegistry]);
248+
249+
const importClick = useCallback(() => {
250+
if (!collectionMetadata) return;
251+
globalAppRegistry.emit(
252+
'open-import',
253+
{
254+
namespace: collectionMetadata.namespace,
255+
origin: 'menu',
256+
},
257+
{
258+
connectionId: connectionInfoRef.current.id,
259+
},
260+
{}
261+
);
262+
}, [collectionMetadata, globalAppRegistry, connectionInfoRef]);
263+
264+
const exportClick = useCallback(() => {
265+
if (!collectionMetadata) return;
266+
globalAppRegistry.emit(
267+
'open-export',
268+
{
269+
exportFullCollection: true,
270+
namespace: collectionMetadata.namespace,
271+
origin: 'menu',
272+
},
273+
{
274+
connectionId: connectionInfoRef.current.id,
275+
}
276+
);
277+
}, [collectionMetadata, globalAppRegistry, connectionInfoRef]);
278+
279+
useApplicationMenu({
280+
menu: collectionMetadata
281+
? {
282+
label: '&Collection',
283+
submenu: [
284+
{
285+
label: '&Share Schema as JSON (Legacy)',
286+
accelerator: 'Alt+CmdOrCtrl+S',
287+
click: shareSchemaClick,
288+
},
289+
{
290+
type: 'separator',
291+
},
292+
...(preferencesReadOnly || collectionMetadata?.isReadonly
293+
? []
294+
: [
295+
{
296+
label: '&Import Data',
297+
click: importClick,
298+
},
299+
]),
300+
{
301+
label: '&Export Collection',
302+
click: exportClick,
303+
},
304+
],
305+
}
306+
: undefined,
307+
});
308+
}
309+
231310
const CollectionTab = ({
232311
collectionMetadata,
233312
...props
234313
}: Omit<CollectionTabProps, 'collectionMetadata'> & {
235314
collectionMetadata: CollectionMetadata | null;
236315
}) => {
237316
const QueryBarPlugin = useCollectionQueryBar();
317+
useCollectionTabApplicationMenu(collectionMetadata);
238318

239319
if (!collectionMetadata) {
240320
return null;

0 commit comments

Comments
 (0)