Skip to content

Commit

Permalink
chore(compass-schema-validation): convert to TS and new-style plugin C…
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Nov 23, 2023
1 parent 2e47c57 commit c184d26
Show file tree
Hide file tree
Showing 66 changed files with 1,486 additions and 1,282 deletions.
23 changes: 16 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useContext, useRef } from 'react';
import type { CollectionTabPluginMetadata } from '../modules/collection-tab';

export interface CollectionTabPlugin {
name: string;
component: React.ComponentType<CollectionTabPluginMetadata>;
}

const CollectionTabsContext = React.createContext<CollectionTabPlugin[]>([]);

export const CollectionTabsProvider: React.FunctionComponent<{
tabs: CollectionTabPlugin[];
}> = ({ tabs, children }) => {
const tabsRef = useRef(tabs);
return (
<CollectionTabsContext.Provider value={tabsRef.current}>
{children}
</CollectionTabsContext.Provider>
);
};

export function useCollectionTabPlugins(): CollectionTabPlugin[] {
return useContext(CollectionTabsContext);
}
31 changes: 25 additions & 6 deletions packages/compass-collection/src/components/collection-tab.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react';
import type { Store } from 'redux';
import { connect, Provider } from 'react-redux';
import type { CollectionTabPluginMetadata } from '../modules/collection-tab';
import {
returnToView,
selectDatabase,
Expand All @@ -9,10 +9,13 @@ import {
selectTab,
renderScopedModals,
renderTabs,
createCollectionStoreMetadata,
} from '../modules/collection-tab';
import { css, ErrorBoundary, TabNavBar } from '@mongodb-js/compass-components';
import CollectionHeader from './collection-header';
import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging';
import type { configureStore } from '../stores/collection-tab';
import { useCollectionTabPlugins } from './collection-tab-provider';

const { log, mongoLogId, track } = createLoggerAndTelemetry(
'COMPASS-COLLECTION-TAB-UI'
Expand Down Expand Up @@ -58,11 +61,26 @@ const collectionModalContainerStyles = css({

const CollectionTab: React.FunctionComponent<{
currentTab: string;
collectionTabPluginMetadata: CollectionTabPluginMetadata;
renderScopedModals(): React.ReactElement[];
renderTabs(): { name: string; component: React.ReactElement }[];
onTabClick(name: string): void;
}> = ({ currentTab, renderScopedModals, renderTabs, onTabClick }) => {
const tabs = renderTabs();
}> = ({
currentTab,
collectionTabPluginMetadata,
renderScopedModals,
renderTabs,
onTabClick,
}) => {
const pluginTabs = useCollectionTabPlugins();
const legacyTabs = renderTabs();
const tabs = [
...legacyTabs,
...pluginTabs.map(({ name, component: Component }) => ({
name,
component: <Component {...collectionTabPluginMetadata} />,
})),
];
const activeTabIndex = tabs.findIndex((tab) => tab.name === currentTab);

useEffect(() => {
Expand Down Expand Up @@ -121,6 +139,7 @@ const ConnectedCollectionTab = connect(
(state: CollectionState) => {
return {
currentTab: state.currentTab,
collectionTabPluginMetadata: createCollectionStoreMetadata(state),
};
},
{
Expand All @@ -130,9 +149,9 @@ const ConnectedCollectionTab = connect(
}
)(CollectionTab);

const CollectionTabPlugin: React.FunctionComponent<{ store: Store }> = ({
store,
}) => {
const CollectionTabPlugin: React.FunctionComponent<{
store: ReturnType<typeof configureStore>;
}> = ({ store }) => {
return (
<Provider store={store}>
<ConnectedCollectionTab></ConnectedCollectionTab>
Expand Down
2 changes: 2 additions & 0 deletions packages/compass-collection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ function deactivate(appRegistry: AppRegistry): void {
export default CollectionTabsPlugin;
export { activate, deactivate };
export { default as metadata } from '../package.json';
export type { CollectionTabPluginMetadata } from './modules/collection-tab';
export { CollectionTabsProvider } from './components/collection-tab-provider';
21 changes: 15 additions & 6 deletions packages/compass-collection/src/modules/collection-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export const selectTab = (
localAppRegistry.emit('subtab-changed', tabName);
};
};

export const selectDatabase = (): CollectionThunkAction<void> => {
return (dispatch, getState, { globalAppRegistry }) => {
const { metadata } = getState();
Expand All @@ -164,6 +163,20 @@ export const returnToView = (): CollectionThunkAction<void> => {
};
};

export function createCollectionStoreMetadata(state: CollectionState) {
return {
...state.metadata,
query: state.initialQuery,
aggregation: state.initialAggregation,
pipelineText: state.initialPipelineText,
editViewName: state.editViewName,
};
}

export type CollectionTabPluginMetadata = ReturnType<
typeof createCollectionStoreMetadata
>;

const setupRole = (
roleName: string
): CollectionThunkAction<{ name: string; component: React.ReactElement }[]> => {
Expand All @@ -187,7 +200,7 @@ const setupRole = (
} = role;

const collectionStoreMetadata = {
...getState().metadata,
...createCollectionStoreMetadata(getState()),
localAppRegistry,
globalAppRegistry,
dataProvider: {
Expand All @@ -196,10 +209,6 @@ const setupRole = (
error: null,
dataProvider: dataService,
},
query: getState().initialQuery,
aggregation: getState().initialAggregation,
pipelineText: getState().initialPipelineText,
editViewName: getState().editViewName,
};

let actions;
Expand Down
10 changes: 7 additions & 3 deletions packages/compass-collection/src/modules/tabs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import type { AnyAction, Reducer } from 'redux';
import type { ThunkAction } from 'redux-thunk';
import AppRegistry from 'hadron-app-registry';
import AppRegistry, { AppRegistryProvider } from 'hadron-app-registry';
import { ObjectId } from 'bson';
import type { CollectionMetadata } from 'mongodb-collection-model';
import type { DataService } from 'mongodb-data-service';
Expand Down Expand Up @@ -213,8 +213,12 @@ const createNewTab = (
localAppRegistry,
...collectionMetadata,
});
const component = React.createElement(collectionTabRole.component, {
store,
const component = React.createElement(AppRegistryProvider, {
localAppRegistry: localAppRegistry,
deactivateOnUnmount: false,
children: React.createElement(collectionTabRole.component, {
store,
}),
});
const tab: CollectionTab = {
id: new ObjectId().toHexString(),
Expand Down
4 changes: 4 additions & 0 deletions packages/compass-home/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@mongodb-js/atlas-service": "^0.10.1",
"@mongodb-js/compass-aggregations": "^9.20.0",
"@mongodb-js/compass-app-stores": "^7.6.1",
"@mongodb-js/compass-collection": "^4.19.1",
"@mongodb-js/compass-components": "^1.19.0",
"@mongodb-js/compass-connections": "^1.20.1",
"@mongodb-js/compass-database": "^3.19.1",
Expand All @@ -46,6 +47,7 @@
"@mongodb-js/compass-instance": "^4.19.1",
"@mongodb-js/compass-logging": "^1.2.6",
"@mongodb-js/compass-saved-aggregations-queries": "^1.20.1",
"@mongodb-js/compass-schema-validation": "^6.20.0",
"@mongodb-js/compass-serverstats": "^16.19.1",
"@mongodb-js/compass-settings": "^0.21.1",
"@mongodb-js/compass-shell": "^3.19.1",
Expand All @@ -61,6 +63,7 @@
"@mongodb-js/atlas-service": "^0.10.1",
"@mongodb-js/compass-aggregations": "^9.20.0",
"@mongodb-js/compass-app-stores": "^7.6.1",
"@mongodb-js/compass-collection": "^4.19.1",
"@mongodb-js/compass-components": "^1.19.0",
"@mongodb-js/compass-connections": "^1.20.1",
"@mongodb-js/compass-database": "^3.19.1",
Expand All @@ -70,6 +73,7 @@
"@mongodb-js/compass-instance": "^4.19.1",
"@mongodb-js/compass-logging": "^1.2.6",
"@mongodb-js/compass-saved-aggregations-queries": "^1.20.1",
"@mongodb-js/compass-schema-validation": "^6.20.0",
"@mongodb-js/compass-serverstats": "^16.19.1",
"@mongodb-js/compass-settings": "^0.21.1",
"@mongodb-js/compass-shell": "^3.19.1",
Expand Down
8 changes: 7 additions & 1 deletion packages/compass-home/src/components/workspace-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
CompassDatabasePlugin,
DatabaseTabsProvider,
} from '@mongodb-js/compass-database';
import { CompassSchemaValidationPlugin } from '@mongodb-js/compass-schema-validation';
import CompassSavedAggregationsQueriesPlugin from '@mongodb-js/compass-saved-aggregations-queries';
import { InstanceTab as DatabasesTabPlugin } from '@mongodb-js/compass-databases-collections';
import { InstanceTab as PerformanceTabPlugin } from '@mongodb-js/compass-serverstats';
import type Namespace from '../types/namespace';
import { CollectionTabsProvider } from '@mongodb-js/compass-collection';

const EmptyComponent: React.FunctionComponent = () => null;

Expand All @@ -27,7 +29,11 @@ const WorkspaceContent: React.FunctionComponent<{ namespace: Namespace }> = ({
useAppRegistryComponent('Collection.Workspace') ?? EmptyComponent;

if (namespace.collection) {
return <Collection></Collection>;
return (
<CollectionTabsProvider tabs={[CompassSchemaValidationPlugin]}>
<Collection />
</CollectionTabsProvider>
);
}

if (namespace.database) {
Expand Down
Loading

0 comments on commit c184d26

Please sign in to comment.