Skip to content

COMPASS-9667: Add view support to indexes tab #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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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 @@ -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,
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();
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
Loading
Loading