Skip to content

feat(compass-indexes): Add view support to indexes tab COMPASS-9667 #7182

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

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0b4ab54
add banners to indexes tab
DarshanaVenkatesh Aug 7, 2025
0c96520
update banner to include data explorer versions
DarshanaVenkatesh Aug 7, 2025
6bc7021
fetching search indexes for view
DarshanaVenkatesh Aug 8, 2025
8cc8504
fix indexes-toolbar
DarshanaVenkatesh Aug 8, 2025
aaf0f70
update ui
DarshanaVenkatesh Aug 11, 2025
cdbbfc8
Merge branch 'main' into COMPASS-9667
DarshanaVenkatesh Aug 11, 2025
481bbd4
update indexes-toolbar test
DarshanaVenkatesh Aug 11, 2025
b9be029
add tests to indexes.spec.tsx
DarshanaVenkatesh Aug 12, 2025
6d9d8b8
only fetch indexes for version 8.1+
DarshanaVenkatesh Aug 12, 2025
8f2ec15
fix conditionals
DarshanaVenkatesh Aug 12, 2025
5b976d3
Merge branch 'main' into COMPASS-9667
DarshanaVenkatesh Aug 12, 2025
fbbb21f
change or to and
DarshanaVenkatesh Aug 12, 2025
50cee9a
add to useEffect conditional
DarshanaVenkatesh Aug 12, 2025
aa59476
remove fetch for version
DarshanaVenkatesh Aug 12, 2025
26e428b
add verison check back but remove from store
DarshanaVenkatesh Aug 12, 2025
73b536b
pr review changes
DarshanaVenkatesh Aug 13, 2025
6961789
move function
DarshanaVenkatesh Aug 13, 2025
f6cee34
Merge branch 'main' into COMPASS-9667
DarshanaVenkatesh Aug 13, 2025
cd9de5b
fix indexes-toolbar.spec
DarshanaVenkatesh Aug 13, 2025
6c47264
fix initial state import
DarshanaVenkatesh Aug 13, 2025
4dccafe
Merge branch 'main' into COMPASS-9667
DarshanaVenkatesh Aug 14, 2025
dad9efd
cleanup comparisons
DarshanaVenkatesh Aug 15, 2025
815f210
Merge branch 'main' into COMPASS-9667
DarshanaVenkatesh Aug 15, 2025
f88bf80
remove de errors
DarshanaVenkatesh Aug 15, 2025
f7983f1
Merge branch 'main' into COMPASS-9667
DarshanaVenkatesh Aug 18, 2025
7bc8bb7
add missing dep
DarshanaVenkatesh Aug 18, 2025
e060334
installed for lockfile
DarshanaVenkatesh Aug 18, 2025
3d29b2c
replace button onClick with href
DarshanaVenkatesh Aug 18, 2025
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 @@ -113,27 +113,49 @@ describe('IndexesToolbar Component', function () {
});
});
});

it('should not render a warning', function () {
expect(screen.queryByText('Readonly views may not contain indexes')).to
.not.exist;
});
});

describe('when it is a readonly view', function () {
beforeEach(function () {
renderIndexesToolbar({
isReadonlyView: true,
describe('and server version is < 8.1+', function () {
beforeEach(function () {
renderIndexesToolbar({
isReadonlyView: true,
indexView: 'search-indexes',
});
});
});

it('should not render the create index button', function () {
expect(screen.queryByText('Create Index')).to.not.exist;
it('should not render the create index button', function () {
expect(screen.queryByText('Create Index')).to.not.exist;
});

it('should not render the create search index button', function () {
expect(screen.queryByText('Create Search Index')).to.not.exist;
});

it('should not render the refresh button', function () {
expect(screen.queryByText('Refresh')).to.not.exist;
});
});
describe('and server version is > 8.1+', function () {
beforeEach(function () {
renderIndexesToolbar({
isReadonlyView: true,
serverVersion: '8.1.0',
indexView: 'search-indexes',
});
});

it('should not render the create index button', function () {
expect(screen.queryByText('Create Index')).to.not.exist;
});

it('should render a warning', function () {
expect(screen.getByText('Readonly views may not contain indexes.')).to.be
.visible;
it('should render the create search index button <8.1', function () {
expect(screen.getByText('Create Search Index')).to.be.visible;
});

it('should not render the refresh button', function () {
expect(screen.queryByText('Refresh')).to.be.visible;
});
});
});

Expand Down Expand Up @@ -332,5 +354,27 @@ describe('IndexesToolbar Component', function () {
expect(segmentControl.closest('button')).to.have.attr('disabled');
expect(onChangeViewCallback).to.not.have.been.calledOnce;
});

describe('and readonly view >8.1', function () {
beforeEach(function () {
renderIndexesToolbar({
isReadonlyView: true,
onIndexViewChanged: onChangeViewCallback,
serverVersion: '8.1.0',
});
});

it('it renders tabs with Indexes disabled and automatically selects Search Indexes', function () {
const indexesTab = screen.getByText('Indexes');
expect(indexesTab).to.be.visible;
expect(indexesTab.closest('button')).to.have.attr('disabled');

expect(screen.getByText('Search Indexes')).to.be.visible;
expect(onChangeViewCallback).to.have.been.calledOnce;
expect(onChangeViewCallback.firstCall.args[0]).to.equal(
'search-indexes'
);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import React, { useCallback } from 'react';
import React, { useCallback, useEffect } from 'react';
import { connect } from 'react-redux';
import {
withPreferences,
usePreference,
withPreferences,
} from 'compass-preferences-model/provider';
import {
Button,
ErrorSummary,
Tooltip,
WarningSummary,
Link,
css,
spacing,
DropdownMenuButton,
ErrorSummary,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a small note for the future, I'm not sure what's doing the re-ordering of the imports here, we don't have this style requirement in compass, but it makes reviewing this code way harder because it's not clear what exactly changed in the imports. If it's possible at all not to do, that would be very helpful 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup, will keep this in mind. I think it's probably an intelllij setting I have set up that I can disable.

Icon,
SpinLoader,
SignalPopover,
Link,
PerformanceSignals,
DropdownMenuButton,
SegmentedControl,
SegmentedControlOption,
SignalPopover,
spacing,
SpinLoader,
Tooltip,
} from '@mongodb-js/compass-components';
import { useConnectionInfo } from '@mongodb-js/compass-connections/provider';
import semver from 'semver';
Expand Down Expand Up @@ -107,9 +106,15 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
readOnly, // preferences readOnly.
}) => {
const isSearchManagementActive = usePreference('enableAtlasSearchIndexes');
const mongoDBMajorVersion = parseFloat(
serverVersion.split('.').slice(0, 2).join('.')
);
const { atlasMetadata } = useConnectionInfo();
Copy link
Collaborator

Choose a reason for hiding this comment

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

We use semver package for handling mongodb version comparison, you can't really rely fully on the version here being fully parseable as a number (because it's not). There are some examples in the code here that already use semver for similar checks (see serverSupportsSearchIndexManagement for example), please update your code to the similar pattern

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me know if the way I changed this works! The majority of cases check 8.1+ but i had a few cases that differed so created a function that defaulted to 8.1+ but let me override which operator and comparison version.

const showInsights = usePreference('showInsights') && !errorMessage;
const showCreateIndexButton = !isReadonlyView && !readOnly && !errorMessage;
const showCreateIndexButton =
(!isReadonlyView || mongoDBMajorVersion > 8.0) &&
!readOnly &&
!errorMessage;
const refreshButtonIcon = isRefreshing ? (
<div className={spinnerStyles}>
<SpinLoader title="Refreshing Indexes" />
Expand All @@ -118,12 +123,19 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
<Icon glyph="Refresh" title="Refresh Indexes" />
);

useEffect(() => {
// If the view is readonly, set the default tab to 'search-indexes'
if (isReadonlyView) {
onIndexViewChanged('search-indexes'); // Update redux state
}
}, [isReadonlyView, onIndexViewChanged]);

return (
<div
className={indexesToolbarContainerStyles}
data-testid="indexes-toolbar-container"
>
{!isReadonlyView && (
{(!isReadonlyView || mongoDBMajorVersion > 8.0) && (
<div data-testid="indexes-toolbar">
<div className={toolbarButtonsContainer}>
{showCreateIndexButton && (
Expand All @@ -139,7 +151,9 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
isWritable={isWritable}
onCreateRegularIndexClick={onCreateRegularIndexClick}
onCreateSearchIndexClick={onCreateSearchIndexClick}
></CreateIndexButton>
isReadonlyView={isReadonlyView}
indexView={indexView}
/>
</div>
}
>
Expand Down Expand Up @@ -173,6 +187,7 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
signals={PerformanceSignals.get('too-many-indexes')}
/>
)}

{isSearchManagementActive && (
<SegmentedControl
size="xsmall"
Expand All @@ -182,13 +197,34 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
value={indexView}
data-testid="indexes-segment-controls"
>
<SegmentedControlOption
data-testid="regular-indexes-tab"
value="regular-indexes"
>
Indexes
</SegmentedControlOption>
{!isSearchIndexesSupported && (
{isReadonlyView && (
<Tooltip
align="top"
justify="end"
enabled={true}
renderMode="portal"
trigger={
<SegmentedControlOption
data-testid="regular-indexes-tab"
value="regular-indexes"
disabled={isReadonlyView}
>
Indexes
</SegmentedControlOption>
}
>
Readonly views may not contain standard indexes.
</Tooltip>
)}
{!isReadonlyView && (
<SegmentedControlOption
data-testid="regular-indexes-tab"
value="regular-indexes"
>
Indexes
</SegmentedControlOption>
)}
{!isSearchIndexesSupported && !isReadonlyView && (
<Tooltip
align="top"
justify="end"
Expand Down Expand Up @@ -227,7 +263,7 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
)}
</Tooltip>
)}
{isSearchIndexesSupported && (
{(isSearchIndexesSupported || isReadonlyView) && (
<SegmentedControlOption
data-testid="search-indexes-tab"
value="search-indexes"
Expand All @@ -240,14 +276,8 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
</div>
</div>
)}
{isReadonlyView ? (
<WarningSummary
warnings={['Readonly views may not contain indexes.']}
/>
) : (
!!errorMessage && (
<ErrorSummary data-testid="indexes-error" errors={[errorMessage]} />
)
{!!errorMessage && (
<ErrorSummary data-testid="indexes-error" errors={[errorMessage]} />
)}
</div>
);
Expand All @@ -259,6 +289,8 @@ type CreateIndexButtonProps = {
isWritable: boolean;
onCreateRegularIndexClick: () => void;
onCreateSearchIndexClick: () => void;
isReadonlyView: boolean;
indexView: IndexView;
};

type CreateIndexActions = 'createRegularIndex' | 'createSearchIndex';
Expand All @@ -271,6 +303,8 @@ export const CreateIndexButton: React.FunctionComponent<
isWritable,
onCreateRegularIndexClick,
onCreateSearchIndexClick,
isReadonlyView,
indexView,
}) => {
const onActionDispatch = useCallback(
(action: CreateIndexActions) => {
Expand All @@ -284,7 +318,23 @@ export const CreateIndexButton: React.FunctionComponent<
[onCreateRegularIndexClick, onCreateSearchIndexClick]
);

if (isSearchIndexesSupported && isSearchManagementActive) {
if (isReadonlyView && isSearchManagementActive) {
if (indexView === 'search-indexes') {
return (
<Button
disabled={!isWritable}
onClick={onCreateSearchIndexClick}
variant="primary"
size="small"
>
Create Search Index
</Button>
);
}

return null;
}
if (!isReadonlyView && isSearchIndexesSupported && isSearchManagementActive) {
return (
<DropdownMenuButton
data-testid="multiple-index-types-creation-dropdown"
Expand Down
80 changes: 66 additions & 14 deletions packages/compass-indexes/src/components/indexes/indexes.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,8 @@ const renderIndexes = async (

if (props) {
const state = store.getState();

const allProps: Partial<RootState> = {
indexView: props.indexView ?? 'regular-indexes',
regularIndexes: {
...state.regularIndexes,
...props.regularIndexes,
},
searchIndexes: {
...state.searchIndexes,
...props.searchIndexes,
},
};

Object.assign(store.getState(), allProps);
const newState = { ...state, ...props };
Object.assign(store.getState(), newState);
}

render(
Expand Down Expand Up @@ -332,5 +320,69 @@ describe('Indexes Component', function () {

expect(getSearchIndexesStub.callCount).to.equal(2);
});

it('renders search indexes list if isReadonlyView >8.0 and has indexes', async function () {
const getSearchIndexesStub = sinon.stub().resolves(searchIndexes);
const dataProvider = {
getSearchIndexes: getSearchIndexesStub,
};
await renderIndexes(undefined, dataProvider, {
indexView: 'search-indexes',
isReadonlyView: true,
serverVersion: '8.1.0',
});

await waitFor(() => {
expect(screen.getByTestId('search-indexes-list')).to.exist;
});
});

it('renders correct empty state if isReadonlyView >8.0 and has no indexes', async function () {
const getSearchIndexesStub = sinon.stub().resolves([]);
const dataProvider = {
getSearchIndexes: getSearchIndexesStub,
};
await renderIndexes(undefined, dataProvider, {
indexView: 'search-indexes',
isReadonlyView: true,
serverVersion: '8.1.0',
});

expect(screen.getByText('No search indexes yet')).to.be.visible;
expect(screen.getByText('Create Atlas Search Index')).to.be.visible;
});

it('renders correct empty state if isReadonlyView 8.0 and has no indexes', async function () {
const getSearchIndexesStub = sinon.stub().resolves([]);
const dataProvider = {
getSearchIndexes: getSearchIndexesStub,
};
await renderIndexes(undefined, dataProvider, {
indexView: 'search-indexes',
isReadonlyView: true,
serverVersion: '8.0.0',
});

expect(screen.queryByTestId('upgrade-cluster-banner-8.0')).to.exist;
expect(screen.queryByText('No standard indexes')).to.exist;
expect(screen.queryByText('Create Atlas Search Index')).to.not.exist;
});
it('renders correct empty state if isReadonlyView <8.0 and has no indexes', async function () {
const getSearchIndexesStub = sinon.stub().resolves([]);
const dataProvider = {
getSearchIndexes: getSearchIndexesStub,
};
await renderIndexes(undefined, dataProvider, {
indexView: 'search-indexes',
isReadonlyView: true,
serverVersion: '7.0.0',
});

expect(
screen.queryByTestId('upgrade-cluster-banner-less-than-8.0')
).to.exist;
expect(screen.queryByText('No standard indexes')).to.exist;
expect(screen.queryByText('Create Atlas Search Index')).to.not.exist;
});
});
});
Loading
Loading