Skip to content
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

feat(compass-workspaces): adapt WorkspacePlugin to support tabs with multiple DataServices COMPASS-7718 #5667

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/compass-aggregations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { activateCreateViewPlugin } from './stores/create-view';
import StageEditor from './components/stage-editor';
import CreateViewModal from './components/create-view-modal';
import {
connectionInfoAccessLocator,
dataServiceLocator,
type DataServiceLocator,
} from '@mongodb-js/compass-connections/provider';
Expand Down Expand Up @@ -39,6 +40,7 @@ export const CompassAggregationsHadronPlugin = registerHadronPlugin(
atlasAuthService: atlasAuthServiceLocator,
atlasAiService: atlasAiServiceLocator,
pipelineStorage: pipelineStorageLocator,
connectionInfoAccess: connectionInfoAccessLocator,
}
);

Expand All @@ -57,6 +59,7 @@ export const CreateViewPlugin = registerHadronPlugin(
dataService: dataServiceLocator as DataServiceLocator<'createView'>,
logger: createLoggerAndTelemetryLocator('COMPASS-CREATE-VIEW-UI'),
workspaces: workspacesServiceLocator,
connectionInfoAccess: connectionInfoAccessLocator,
}
);

Expand Down
12 changes: 10 additions & 2 deletions packages/compass-aggregations/src/modules/create-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
return async (
dispatch,
getState,
{ globalAppRegistry, dataService, logger: { track }, workspaces }
{
globalAppRegistry,
dataService,
logger: { track },
workspaces,
connectionInfoAccess,
}
) => {
const state = getState();

Expand All @@ -214,6 +220,8 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
const options = {};

dispatch(clearError());
const { id: connectionId } =
connectionInfoAccess.getCurrentConnectionInfo();

try {
dispatch(toggleIsRunning(true));
Expand All @@ -226,7 +234,7 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
const ns = `${database}.${viewName}`;
track('Aggregation Saved As View', { num_stages: viewPipeline.length });
globalAppRegistry.emit('view-created', ns);
workspaces.openCollectionWorkspace(ns, { newTab: true });
workspaces.openCollectionWorkspace(connectionId, ns, { newTab: true });
dispatch(reset());
} catch (e) {
dispatch(stopWithError(e as Error));
Expand Down
2 changes: 2 additions & 0 deletions packages/compass-aggregations/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type { AtlasAiService } from '@mongodb-js/compass-generative-ai/provider'
import type { AtlasAuthService } from '@mongodb-js/atlas-service/provider';
import type { MongoDBInstance } from 'mongodb-instance-model';
import type { DataService } from '../modules/data-service';
import type { ConnectionInfoAccess } from '@mongodb-js/compass-connections/provider';
/**
* The main application reducer.
*
Expand Down Expand Up @@ -100,6 +101,7 @@ export type PipelineBuilderExtraArgs = {
atlasAiService: AtlasAiService;
instance: MongoDBInstance;
dataService: DataService;
connectionInfoAccess: ConnectionInfoAccess;
};

export type PipelineBuilderThunkDispatch<A extends Action = AnyAction> =
Expand Down
8 changes: 6 additions & 2 deletions packages/compass-aggregations/src/modules/out-results-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export default function reducer(
export const gotoOutResults = (
namespace: string
): PipelineBuilderThunkAction<void> => {
return (_dispatch, getState, { workspaces }) => {
return (_dispatch, getState, { workspaces, connectionInfoAccess }) => {
const { outResultsFn } = getState();
const { id: connectionId } =
connectionInfoAccess.getCurrentConnectionInfo();
if (outResultsFn) {
outResultsFn(namespace);
} else {
workspaces.openCollectionWorkspace(namespace, { newTab: true });
workspaces.openCollectionWorkspace(connectionId, namespace, {
newTab: true,
});
}
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { getId } from './stage-ids';
import { defaultPreferencesInstance } from 'compass-preferences-model';
import { createNoopLoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
import AppRegistry from 'hadron-app-registry';
import { TEST_CONNECTION_INFO } from '@mongodb-js/compass-connections/provider';

const MATCH_STAGE: StoreStage = mapBuilderStageToStoreStage(
{
Expand Down Expand Up @@ -123,6 +124,11 @@ function createStore({
preferences,
logger: createNoopLoggerAndTelemetry(),
dataService: {} as any,
connectionInfoAccess: {
getCurrentConnectionInfo() {
return TEST_CONNECTION_INFO;
},
},
})
)
);
Expand Down
6 changes: 6 additions & 0 deletions packages/compass-aggregations/src/modules/update-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai';
import { ERROR_UPDATING_VIEW, updateView } from './update-view';
import { createNoopLoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
import AppRegistry from 'hadron-app-registry';
import { TEST_CONNECTION_INFO } from '@mongodb-js/compass-connections/provider';

describe('update-view module', function () {
const thunkArg = {
Expand All @@ -20,6 +21,11 @@ describe('update-view module', function () {
openCollectionWorkspace() {},
},
logger: createNoopLoggerAndTelemetry(),
connectionInfoAccess: {
getCurrentConnectionInfo() {
return TEST_CONNECTION_INFO;
},
},
};

describe('#updateView', function () {
Expand Down
13 changes: 11 additions & 2 deletions packages/compass-aggregations/src/modules/update-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ export const updateView = (): PipelineBuilderThunkAction<Promise<void>> => {
return async (
dispatch,
getState,
{ pipelineBuilder, workspaces, logger: { track, debug }, globalAppRegistry }
{
pipelineBuilder,
workspaces,
logger: { track, debug },
globalAppRegistry,
connectionInfoAccess,
}
) => {
dispatch(dismissViewError());

Expand All @@ -94,6 +100,9 @@ export const updateView = (): PipelineBuilderThunkAction<Promise<void>> => {
return;
}

const { id: connectionId } =
connectionInfoAccess.getCurrentConnectionInfo();

const viewPipeline = getPipelineFromBuilderState(
getState(),
pipelineBuilder
Expand All @@ -111,7 +120,7 @@ export const updateView = (): PipelineBuilderThunkAction<Promise<void>> => {
});
debug('selecting namespace', viewNamespace);
globalAppRegistry.emit('view-edited', viewNamespace);
workspaces.openCollectionWorkspace(viewNamespace);
workspaces.openCollectionWorkspace(connectionId, viewNamespace);
} catch (e: any) {
debug('Unexpected error updating view', e);
dispatch(updateViewErrorOccured(e));
Expand Down
6 changes: 6 additions & 0 deletions packages/compass-aggregations/src/stores/create-view.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AppRegistry from 'hadron-app-registry';
import { activateCreateViewPlugin } from './create-view';
import { expect } from 'chai';
import { TEST_CONNECTION_INFO } from '@mongodb-js/compass-connections/provider';

describe('CreateViewStore [Store]', function () {
if (
Expand All @@ -26,6 +27,11 @@ describe('CreateViewStore [Store]', function () {
dataService: ds,
logger,
workspaces: {} as any,
connectionInfoAccess: {
getCurrentConnectionInfo() {
return TEST_CONNECTION_INFO;
},
},
}
));
});
Expand Down
11 changes: 10 additions & 1 deletion packages/compass-aggregations/src/stores/create-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import type AppRegistry from 'hadron-app-registry';
import type { DataService } from 'mongodb-data-service';
import type { LoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
import type { WorkspacesService } from '@mongodb-js/compass-workspaces/provider';
import type { ConnectionInfoAccess } from '@mongodb-js/compass-connections/provider';

type CreateViewServices = {
globalAppRegistry: AppRegistry;
dataService: Pick<DataService, 'createView'>;
logger: LoggerAndTelemetry;
workspaces: WorkspacesService;
connectionInfoAccess: ConnectionInfoAccess;
};

export function configureStore(services: CreateViewServices) {
Expand All @@ -34,13 +36,20 @@ export type CreateViewThunkAction<

export function activateCreateViewPlugin(
_: unknown,
{ globalAppRegistry, dataService, logger, workspaces }: CreateViewServices
{
globalAppRegistry,
dataService,
logger,
workspaces,
connectionInfoAccess,
}: CreateViewServices
) {
const store = configureStore({
globalAppRegistry,
dataService,
logger,
workspaces,
connectionInfoAccess,
});

const onOpenCreateView = (meta: {
Expand Down
4 changes: 4 additions & 0 deletions packages/compass-aggregations/src/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type { AtlasAiService } from '@mongodb-js/compass-generative-ai/provider'
import type { AtlasAuthService } from '@mongodb-js/atlas-service/provider';
import type { PipelineStorage } from '@mongodb-js/my-queries-storage/provider';
import { maxTimeMSChanged } from '../modules/max-time-ms';
import type { ConnectionInfoAccess } from '@mongodb-js/compass-connections/provider';

export type ConfigureStoreOptions = CollectionTabPluginMetadata &
Partial<{
Expand Down Expand Up @@ -72,6 +73,7 @@ export type AggregationsPluginServices = {
atlasAuthService: AtlasAuthService;
atlasAiService: AtlasAiService;
pipelineStorage?: PipelineStorage;
connectionInfoAccess: ConnectionInfoAccess;
};

export function activateAggregationsPlugin(
Expand All @@ -87,6 +89,7 @@ export function activateAggregationsPlugin(
atlasAiService,
atlasAuthService,
pipelineStorage,
connectionInfoAccess,
}: AggregationsPluginServices,
{ on, cleanup, addCleanup }: ActivateHelpers
) {
Expand Down Expand Up @@ -171,6 +174,7 @@ export function activateAggregationsPlugin(
preferences,
logger,
atlasAiService,
connectionInfoAccess,
})
)
);
Expand Down
6 changes: 6 additions & 0 deletions packages/compass-aggregations/test/configure-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { DataService } from '../src/modules/data-service';
import { ReadOnlyPreferenceAccess } from 'compass-preferences-model/provider';
import { createNoopLoggerAndTelemetry } from '@mongodb-js/compass-logging/provider';
import { AtlasAuthService } from '@mongodb-js/atlas-service/provider';
import { TEST_CONNECTION_INFO } from '@mongodb-js/compass-connections/provider';

export class MockAtlasAuthService extends AtlasAuthService {
isAuthenticated() {
Expand Down Expand Up @@ -71,6 +72,11 @@ export default function configureStore(
logger,
atlasAiService: atlasAiService as any,
atlasAuthService,
connectionInfoAccess: {
getCurrentConnectionInfo() {
return TEST_CONNECTION_INFO;
},
},
...services,
},
createActivateHelpers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('CollectionHeaderActions [Component]', function () {
);
button.click();
expect(openEditViewWorkspaceStub).to.have.been.calledOnceWith(
'TEST',
'db.coll2',
{
sourceName: 'db.someSource',
Expand Down Expand Up @@ -122,6 +123,7 @@ describe('CollectionHeaderActions [Component]', function () {
);
button.click();
expect(openCollectionWorkspaceStub).to.have.been.calledOnceWith(
'TEST',
'db.editing'
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
css,
spacing,
} from '@mongodb-js/compass-components';
import { useConnectionInfo } from '@mongodb-js/compass-connections/provider';
import { useOpenWorkspace } from '@mongodb-js/compass-workspaces/provider';
import React from 'react';

Expand Down Expand Up @@ -32,6 +33,7 @@ const CollectionHeaderActions: React.FunctionComponent<
sourceName,
sourcePipeline,
}: CollectionHeaderActionsProps) => {
const { id: connectionId } = useConnectionInfo();
const { openCollectionWorkspace, openEditViewWorkspace } = useOpenWorkspace();
return (
<div
Expand All @@ -44,7 +46,7 @@ const CollectionHeaderActions: React.FunctionComponent<
size={ButtonSize.Small}
onClick={() => {
if (sourceName && sourcePipeline) {
openEditViewWorkspace(namespace, {
openEditViewWorkspace(connectionId, namespace, {
sourceName,
sourcePipeline,
});
Expand All @@ -61,7 +63,7 @@ const CollectionHeaderActions: React.FunctionComponent<
size={ButtonSize.Small}
onClick={() => {
if (editViewName) {
openCollectionWorkspace(editViewName);
openCollectionWorkspace(connectionId, editViewName);
}
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ describe('CollectionHeader [Component]', function () {
expect(openCollectionsWorkspaceStub.called).to.be.false;
userEvent.click(item);
expect(openCollectionsWorkspaceStub.calledOnce).to.be.true;
expect(openCollectionsWorkspaceStub.firstCall.firstArg).to.deep.equal(
'db'
);
expect(openCollectionsWorkspaceStub.firstCall.args).to.deep.equal([
'TEST',
'db',
]);
});

it('for a view, opens source collection', function () {
Expand All @@ -268,9 +269,10 @@ describe('CollectionHeader [Component]', function () {
expect(openCollectionWorkspaceStub.called).to.be.false;
userEvent.click(item);
expect(openCollectionWorkspaceStub.calledOnce).to.be.true;
expect(openCollectionWorkspaceStub.firstCall.firstArg).to.deep.equal(
'db.coll2'
);
expect(openCollectionWorkspaceStub.firstCall.args).to.deep.equal([
'TEST',
'db.coll2',
]);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { CollectionState } from '../../modules/collection-tab';
import { CollectionBadge } from './badges';
import { useOpenWorkspace } from '@mongodb-js/compass-workspaces/provider';
import { connect } from 'react-redux';
import { useConnectionInfo } from '@mongodb-js/compass-connections/provider';

const collectionHeaderStyles = css({
padding: spacing[3],
Expand Down Expand Up @@ -99,31 +100,34 @@ export const CollectionHeader: React.FunctionComponent<
const showInsights = usePreference('showInsights');
const { openCollectionWorkspace, openCollectionsWorkspace } =
useOpenWorkspace();
const { id: connectionId } = useConnectionInfo();

const breadcrumbItems = useMemo(() => {
return [
// TODO (COMPASS-7684): add connection name
{
name: toNS(namespace).database,
onClick: () => openCollectionsWorkspace(toNS(namespace).database),
onClick: () =>
openCollectionsWorkspace(connectionId, toNS(namespace).database),
},
// When viewing a view, show the source namespace first
sourceName && {
name: toNS(sourceName).collection,
onClick: () => openCollectionWorkspace(sourceName),
onClick: () => openCollectionWorkspace(connectionId, sourceName),
},
// Show the current namespace
{
name: toNS(namespace).collection,
onClick: () => openCollectionWorkspace(namespace),
onClick: () => openCollectionWorkspace(connectionId, namespace),
},
// When editing a view, show the view namespace last
editViewName && {
name: toNS(editViewName).collection,
onClick: () => openCollectionWorkspace(editViewName),
onClick: () => openCollectionWorkspace(connectionId, editViewName),
},
].filter(Boolean) as BreadcrumbItem[];
}, [
connectionId,
namespace,
sourceName,
editViewName,
Expand Down
Loading
Loading