Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
import { isOutputStage } from '../../../utils/stage';
import { openCreateIndexModal } from '../../../modules/insights';
import {
usePreference,
useIsAIFeatureEnabled,
usePreferences,
} from 'compass-preferences-model/provider';
import { showInput as showAIInput } from '../../../modules/pipeline-builder/pipeline-ai';
import { useAssistantActions } from '@mongodb-js/compass-assistant';
Expand Down Expand Up @@ -85,10 +85,15 @@ export const PipelineActions: React.FunctionComponent<PipelineActionsProps> = ({
onCollectionScanInsightActionButtonClick,
stages,
}) => {
const enableAggregationBuilderExtraOptions = usePreference(
'enableAggregationBuilderExtraOptions'
);
const showInsights = usePreference('showInsights');
const {
readWrite: preferencesReadWrite,
enableAggregationBuilderExtraOptions,
showInsights,
} = usePreferences([
'readWrite',
'enableAggregationBuilderExtraOptions',
'showInsights',
]);
const isAIFeatureEnabled = useIsAIFeatureEnabled();
const { tellMoreAboutInsight } = useAssistantActions();

Expand All @@ -102,8 +107,15 @@ export const PipelineActions: React.FunctionComponent<PipelineActionsProps> = ({
<SignalPopover
signals={{
...PerformanceSignals.get('aggregation-executed-without-index'),
onPrimaryActionButtonClick:
onCollectionScanInsightActionButtonClick,
...(preferencesReadWrite
? {
// Disable insight primary action if can't create indexes
primaryActionButtonLabel: undefined,
}
: {
onPrimaryActionButtonClick:
onCollectionScanInsightActionButtonClick,
}),
onAssistantButtonClick:
tellMoreAboutInsight &&
(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@ import React, { type ComponentProps } from 'react';
import {
renderWithActiveConnection,
screen,
cleanup,
} from '@mongodb-js/testing-library-compass';
import sinon from 'sinon';
import {
WorkspacesServiceProvider,
type WorkspacesService,
} from '@mongodb-js/compass-workspaces/provider';
import type { PreferencesAccess } from 'compass-preferences-model';
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
import { PreferencesProvider } from 'compass-preferences-model/provider';
import { ExperimentTestName } from '@mongodb-js/compass-telemetry/provider';
import { CompassExperimentationProvider } from '@mongodb-js/compass-telemetry';
import type { ConnectionInfo } from '@mongodb-js/compass-connections/provider';

import CollectionHeaderActions from '../collection-header-actions';

describe('CollectionHeaderActions [Component]', function () {
let preferences: PreferencesAccess;
let mockUseAssignment: sinon.SinonStub;

beforeEach(async function () {
preferences = await createSandboxFromDefaultPreferences();
beforeEach(function () {
mockUseAssignment = sinon.stub();
mockUseAssignment.returns({
assignment: {
Expand All @@ -42,7 +36,8 @@ describe('CollectionHeaderActions [Component]', function () {
const renderCollectionHeaderActions = (
props: Partial<ComponentProps<typeof CollectionHeaderActions>> = {},
workspaceService: Partial<WorkspacesService> = {},
connectionInfo?: ConnectionInfo
connectionInfo?: ConnectionInfo,
preferences?: Record<string, boolean>
) => {
return renderWithActiveConnection(
<CompassExperimentationProvider
Expand All @@ -53,21 +48,20 @@ describe('CollectionHeaderActions [Component]', function () {
<WorkspacesServiceProvider
value={workspaceService as WorkspacesService}
>
<PreferencesProvider value={preferences}>
<CollectionHeaderActions
namespace="test.test"
isReadonly={false}
onOpenMockDataModal={sinon.stub()}
hasSchemaAnalysisData={true}
analyzedSchemaDepth={2}
schemaAnalysisStatus="complete"
schemaAnalysisError={null}
{...props}
/>
</PreferencesProvider>
<CollectionHeaderActions
namespace="test.test"
isReadonly={false}
onOpenMockDataModal={sinon.stub()}
hasSchemaAnalysisData={true}
analyzedSchemaDepth={2}
schemaAnalysisStatus="complete"
schemaAnalysisError={null}
{...props}
/>
</WorkspacesServiceProvider>
</CompassExperimentationProvider>,
connectionInfo
connectionInfo,
{ preferences }
);
};

Expand All @@ -80,8 +74,6 @@ describe('CollectionHeaderActions [Component]', function () {
});
});

afterEach(cleanup);

it('does not render any buttons', function () {
expect(
screen.queryByTestId('collection-header-actions-edit-button')
Expand All @@ -93,15 +85,18 @@ describe('CollectionHeaderActions [Component]', function () {
});

context('Compass readonly mode', function () {
it('does not render edit view buttons when in readonly mode', async function () {
await preferences.savePreferences({ readOnly: true });

await renderCollectionHeaderActions({
isReadonly: true,
namespace: 'db.coll2',
sourceName: 'db.someSource',
sourcePipeline: [{ $match: { a: 1 } }],
});
it('does not render edit view buttons when in ReadWrite mode', async function () {
await renderCollectionHeaderActions(
{
isReadonly: true,
namespace: 'db.coll2',
sourceName: 'db.someSource',
sourcePipeline: [{ $match: { a: 1 } }],
},
undefined,
undefined,
{ readWrite: true }
);

expect(
screen.queryByTestId('collection-header-actions-edit-button')
Expand Down Expand Up @@ -142,8 +137,6 @@ describe('CollectionHeaderActions [Component]', function () {
);
});

afterEach(cleanup);

it('shows a button to edit the view pipeline', function () {
expect(
screen.getByTestId('collection-header-actions-edit-button')
Expand Down Expand Up @@ -181,9 +174,6 @@ describe('CollectionHeaderActions [Component]', function () {
}
);
});

afterEach(cleanup);

it('shows a button to return to the view', function () {
expect(
screen.getByTestId('collection-header-actions-return-to-view-button')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ const CollectionHeaderActions: React.FunctionComponent<
const { id: connectionId, atlasMetadata } = connectionInfo;
const { openCollectionWorkspace, openEditViewWorkspace, openShellWorkspace } =
useOpenWorkspace();
const { readOnly: preferencesReadOnly, enableShell: showOpenShellButton } =
usePreferences(['readOnly', 'enableShell']);
const { readWrite: preferencesReadWrite, enableShell: showOpenShellButton } =
usePreferences(['readWrite', 'enableShell']);
const track = useTelemetry();

// Get experiment assignment for Mock Data Generator
Expand Down Expand Up @@ -119,6 +119,10 @@ const CollectionHeaderActions: React.FunctionComponent<
!hasSchemaAnalysisData &&
schemaAnalysisStatus !== SCHEMA_ANALYSIS_STATE_ANALYZING;

const isView = isReadonly && sourceName && !editViewName;

const showViewEdit = isView && !preferencesReadWrite;

return (
<div
className={collectionHeaderActionsStyles}
Expand Down Expand Up @@ -198,7 +202,7 @@ const CollectionHeaderActions: React.FunctionComponent<
Visualize Your Data
</Button>
)}
{isReadonly && sourceName && !editViewName && !preferencesReadOnly && (
{showViewEdit && (
<Button
data-testid="collection-header-actions-edit-button"
size={ButtonSize.Small}
Expand Down
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a ticket to do something about this code, the logic there is minimal and yet we're spreading it across three different files passing down these variables from method to method, it's really hard to navigate this stuff for no good reason

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@mongodb-js/compass-components';
import { useConnectable } from '@mongodb-js/compass-connections/provider';
import type { WorkspaceTab } from '@mongodb-js/compass-workspaces';
import { usePreference } from 'compass-preferences-model/provider';
import { usePreferences } from 'compass-preferences-model/provider';
import type { NavigationItemActions } from './item-actions';
import {
collectionItemActions,
Expand Down Expand Up @@ -56,12 +56,21 @@ const ConnectionsNavigationTree: React.FunctionComponent<
onItemExpand,
onItemAction,
}) => {
const preferencesShellEnabled = usePreference('enableShell');
const preferencesReadOnly = usePreference('readOnly');
const isRenameCollectionEnabled = usePreference(
'enableRenameCollectionModal'
);
const showDisabledConnections = !!usePreference('showDisabledConnections');
const {
enableRenameCollectionModal,
enableShell: preferencesShellEnabled,
readOnly: preferencesReadOnly,
readWrite: preferencesReadWrite,
showDisabledConnections,
} = usePreferences([
'enableShell',
'readOnly',
'readWrite',
'enableRenameCollectionModal',
'showDisabledConnections',
]);
const isRenameCollectionEnabled =
enableRenameCollectionModal && !preferencesReadWrite;

const id = useId();
const getConnectable = useConnectable();
Expand All @@ -71,9 +80,16 @@ const ConnectionsNavigationTree: React.FunctionComponent<
connections,
expandedItems: expanded,
preferencesReadOnly,
preferencesReadWrite,
preferencesShellEnabled,
});
}, [connections, expanded, preferencesReadOnly, preferencesShellEnabled]);
}, [
connections,
expanded,
preferencesReadOnly,
preferencesReadWrite,
preferencesShellEnabled,
]);

const onDefaultAction: OnDefaultAction<SidebarActionableItem> = useCallback(
(item, evt) => {
Expand Down Expand Up @@ -204,6 +220,7 @@ const ConnectionsNavigationTree: React.FunctionComponent<
return {
actions: databaseItemActions({
hasWriteActionsDisabled: item.hasWriteActionsDisabled,
canDeleteDatabase: item.canDeleteDatabase,
}),
};
default:
Expand All @@ -212,6 +229,7 @@ const ConnectionsNavigationTree: React.FunctionComponent<
hasWriteActionsDisabled: item.hasWriteActionsDisabled,
type: item.type,
isRenameCollectionEnabled,
canEditCollection: item.canEditCollection,
}),
};
}
Expand Down Expand Up @@ -268,6 +286,7 @@ const ConnectionsNavigationTree: React.FunctionComponent<
isPerformanceTabAvailable,
isPerformanceTabSupported,
isAtlas: !!connectionInfo.atlasMetadata,
canDeleteDatabase: item.canDeleteDatabase,
})
);
}
Expand All @@ -291,6 +310,7 @@ const ConnectionsNavigationTree: React.FunctionComponent<
isPerformanceTabAvailable,
isPerformanceTabSupported,
isAtlas: !!connectionInfo.atlasMetadata,
canEditCollection: item.canEditCollection,
})
);
}
Expand Down
Loading
Loading