Skip to content

Commit dad9efd

Browse files
cleanup comparisons
1 parent 4dccafe commit dad9efd

File tree

5 files changed

+88
-109
lines changed

5 files changed

+88
-109
lines changed

packages/compass-indexes/src/components/indexes-toolbar/indexes-toolbar.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import semver from 'semver';
2424

2525
import type { RootState } from '../../modules';
2626
import {
27-
compareVersionForViewSearchCompatibility,
27+
isVersionSearchCompatibleForViews,
2828
createSearchIndexOpened,
2929
} from '../../modules/search-indexes';
3030
import { getAtlasSearchIndexesLink } from '../../utils/atlas-search-indexes-link';
@@ -112,8 +112,7 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
112112
const { atlasMetadata } = useConnectionInfo();
113113
const showInsights = usePreference('showInsights') && !errorMessage;
114114
const showCreateIndexButton =
115-
(!isReadonlyView ||
116-
compareVersionForViewSearchCompatibility(serverVersion)) &&
115+
(!isReadonlyView || isVersionSearchCompatibleForViews(serverVersion)) &&
117116
!readOnly &&
118117
!errorMessage;
119118
const refreshButtonIcon = isRefreshing ? (
@@ -130,7 +129,7 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
130129
data-testid="indexes-toolbar-container"
131130
>
132131
{(!isReadonlyView ||
133-
compareVersionForViewSearchCompatibility(serverVersion)) && (
132+
isVersionSearchCompatibleForViews(serverVersion)) && (
134133
<div data-testid="indexes-toolbar">
135134
<div className={toolbarButtonsContainer}>
136135
{showCreateIndexButton && (

packages/compass-indexes/src/components/indexes/indexes.spec.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe('Indexes Component', function () {
321321
expect(getSearchIndexesStub.callCount).to.equal(2);
322322
});
323323

324-
it('renders search indexes list if isReadonlyView >8.0 and has indexes', async function () {
324+
it('renders search indexes list if isReadonlyView 8.1+ and has indexes', async function () {
325325
const getSearchIndexesStub = sinon.stub().resolves(searchIndexes);
326326
const dataProvider = {
327327
getSearchIndexes: getSearchIndexesStub,
@@ -337,7 +337,7 @@ describe('Indexes Component', function () {
337337
});
338338
});
339339

340-
it('renders correct empty state if isReadonlyView >8.0 and has no indexes', async function () {
340+
it('renders correct empty state if isReadonlyView 8.1+ and has no indexes', async function () {
341341
const getSearchIndexesStub = sinon.stub().resolves([]);
342342
const dataProvider = {
343343
getSearchIndexes: getSearchIndexesStub,
@@ -363,10 +363,15 @@ describe('Indexes Component', function () {
363363
serverVersion: '8.0.0',
364364
});
365365

366-
expect(screen.queryByTestId('upgrade-cluster-banner-8.0')).to.exist;
366+
expect(
367+
screen.queryByText(
368+
/Upgrade your cluster or manage search indexes on views in the Atlas UI./i
369+
)
370+
).to.exist;
367371
expect(screen.queryByText('No standard indexes')).to.exist;
368372
expect(screen.queryByText('Create Atlas Search Index')).to.not.exist;
369373
});
374+
370375
it('renders correct empty state if isReadonlyView <8.0 and has no indexes', async function () {
371376
const getSearchIndexesStub = sinon.stub().resolves([]);
372377
const dataProvider = {
@@ -379,7 +384,9 @@ describe('Indexes Component', function () {
379384
});
380385

381386
expect(
382-
screen.queryByTestId('upgrade-cluster-banner-less-than-8.0')
387+
screen.queryByText(
388+
/Upgrade your cluster to create search indexes on views./i
389+
)
383390
).to.exist;
384391
expect(screen.queryByText('No standard indexes')).to.exist;
385392
expect(screen.queryByText('Create Atlas Search Index')).to.not.exist;

packages/compass-indexes/src/components/indexes/indexes.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import RegularIndexesTable from '../regular-indexes-table/regular-indexes-table'
1616
import SearchIndexesTable from '../search-indexes-table/search-indexes-table';
1717
import { refreshRegularIndexes } from '../../modules/regular-indexes';
1818
import {
19-
compareVersionForViewSearchCompatibility,
19+
isVersionSearchCompatibleForViews,
2020
refreshSearchIndexes,
2121
} from '../../modules/search-indexes';
2222
import type { State as RegularIndexesState } from '../../modules/regular-indexes';
@@ -35,6 +35,7 @@ import { getAtlasSearchIndexesLink } from '../../utils/atlas-search-indexes-link
3535
import CreateIndexModal from '../create-index-modal/create-index-modal';
3636
import { ZeroGraphic } from '../search-indexes-table/zero-graphic';
3737
import { ViewVersionIncompatibleBanner } from '../view-version-incompatible-banners/view-version-incompatible-banners';
38+
import semver from 'semver';
3839

3940
// This constant is used as a trigger to show an insight whenever number of
4041
// indexes in a collection is more than what is specified here.
@@ -54,6 +55,20 @@ const linkTitle = 'Search and Vector Search.';
5455
const DISMISSED_SEARCH_INDEXES_BANNER_LOCAL_STORAGE_KEY =
5556
'mongodb_compass_dismissedSearchIndexesBanner' as const;
5657

58+
export const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE = '8.0.0';
59+
export const isVersionSearchCompatibleForViewsDataExplorer = (
60+
serverVersion: string
61+
) => {
62+
try {
63+
return semver.gte(
64+
serverVersion,
65+
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE
66+
);
67+
} catch {
68+
return false;
69+
}
70+
};
71+
5772
const ViewVersionIncompatibleEmptyState = ({
5873
serverVersion,
5974
enableAtlasSearchIndexes,
@@ -62,7 +77,7 @@ const ViewVersionIncompatibleEmptyState = ({
6277
enableAtlasSearchIndexes: boolean;
6378
}) => {
6479
if (
65-
compareVersionForViewSearchCompatibility(serverVersion) &&
80+
isVersionSearchCompatibleForViews(serverVersion) &&
6681
enableAtlasSearchIndexes
6782
) {
6883
return null;
@@ -210,21 +225,21 @@ export function Indexes({
210225
/>
211226
)}
212227
{(!isReadonlyView ||
213-
compareVersionForViewSearchCompatibility(serverVersion, 'gte')) &&
214-
!enableAtlasSearchIndexes && (
215-
<AtlasIndexesBanner
216-
namespace={namespace}
217-
dismissed={atlasBannerDismissed}
218-
onDismissClick={() => {
219-
setDismissed(true);
220-
}}
221-
/>
222-
)}
228+
(isVersionSearchCompatibleForViewsDataExplorer(serverVersion) &&
229+
!enableAtlasSearchIndexes)) && (
230+
<AtlasIndexesBanner // cta to Atlas Search Indexes Page
231+
namespace={namespace}
232+
dismissed={atlasBannerDismissed}
233+
onDismissClick={() => {
234+
setDismissed(true);
235+
}}
236+
/>
237+
)}
223238
{!isReadonlyView && currentIndexesView === 'regular-indexes' && (
224239
<RegularIndexesTable />
225240
)}
226241
{(!isReadonlyView ||
227-
compareVersionForViewSearchCompatibility(serverVersion)) &&
242+
isVersionSearchCompatibleForViews(serverVersion)) &&
228243
currentIndexesView === 'search-indexes' && <SearchIndexesTable />}
229244
{isReadonlyView && searchIndexes.indexes.length === 0 && (
230245
<ViewVersionIncompatibleEmptyState

packages/compass-indexes/src/components/view-version-incompatible-banners/view-version-incompatible-banners.tsx

Lines changed: 41 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
import { getAtlasUpgradeClusterLink } from '../../utils/atlas-upgrade-cluster-link';
88
import React from 'react';
99
import type { AtlasClusterMetadata } from '@mongodb-js/connection-info';
10-
import { compareVersionForViewSearchCompatibility } from '../../modules/search-indexes';
10+
import { isVersionSearchCompatibleForViews } from '../../modules/search-indexes';
11+
import { isVersionSearchCompatibleForViewsDataExplorer } from '../indexes/indexes';
1112

1213
const viewContentStyles = css({
1314
display: 'flex',
@@ -25,76 +26,49 @@ export const ViewVersionIncompatibleBanner = ({
2526
enableAtlasSearchIndexes: boolean;
2627
atlasMetadata: AtlasClusterMetadata | undefined;
2728
}) => {
28-
const searchIndexOnViewsVersion = enableAtlasSearchIndexes ? '8.1' : '8.0';
29-
29+
// return if compatible, 8.1+ for compass and 8.0+ for data explorer
3030
if (
31-
compareVersionForViewSearchCompatibility(serverVersion) ||
32-
(compareVersionForViewSearchCompatibility(serverVersion, 'gte', '8.0.0') &&
31+
isVersionSearchCompatibleForViews(serverVersion) ||
32+
(isVersionSearchCompatibleForViewsDataExplorer(serverVersion) &&
3333
!enableAtlasSearchIndexes)
3434
) {
35-
// return if 8.1+ on compass or 8.0+ for data explorer
3635
return null;
3736
}
3837

39-
if (compareVersionForViewSearchCompatibility(serverVersion, 'lt', '8.0.0')) {
40-
// data explorer <8.0 and compass <8.0
41-
return (
42-
<Banner
43-
variant={BannerVariant.Warning}
44-
data-testid="upgrade-cluster-banner-less-than-8.0"
45-
>
46-
<b>Looking for search indexes?</b>
47-
<br />
48-
<div className={viewContentStyles}>
49-
<span>
50-
Your MongoDB version is {serverVersion}. Creating and managing
51-
search indexes on views {enableAtlasSearchIndexes && 'in Compass'}{' '}
52-
is supported on MongoDB version {searchIndexOnViewsVersion} or
53-
higher. Upgrade your cluster to create search indexes on views.
54-
</span>
55-
{atlasMetadata && (
56-
<Button
57-
size="xsmall"
58-
onClick={() => {
59-
window.open(
60-
getAtlasUpgradeClusterLink({
61-
clusterName: atlasMetadata.clusterName,
62-
}),
63-
'_blank'
64-
);
65-
}}
66-
>
67-
Upgrade Cluster
68-
</Button>
69-
)}
70-
</div>
71-
</Banner>
72-
);
73-
}
74-
75-
if (
76-
compareVersionForViewSearchCompatibility(serverVersion, 'gte', '8.0.0') &&
77-
compareVersionForViewSearchCompatibility(serverVersion, 'lt', '8.1.0') &&
78-
enableAtlasSearchIndexes
79-
) {
80-
// compass 8.0
81-
return (
82-
<Banner
83-
variant={BannerVariant.Warning}
84-
data-testid="upgrade-cluster-banner-8.0"
85-
>
86-
<b>Looking for search indexes?</b>
87-
<br />
88-
<div className={viewContentStyles}>
89-
<span>
90-
Your MongoDB version is {serverVersion}. Creating and managing
91-
search indexes on views in Compass is supported on MongoDB version{' '}
92-
{searchIndexOnViewsVersion} or higher. Upgrade your cluster or
93-
manage search indexes on views in the Atlas UI.
94-
</span>
95-
</div>
96-
</Banner>
97-
);
98-
}
99-
return null;
38+
const searchIndexOnViewsMinVersion = enableAtlasSearchIndexes ? '8.1' : '8.0';
39+
// if compass version matches min compatibility for DE, we recommend Atlas UI as well
40+
const recommendedCta =
41+
enableAtlasSearchIndexes &&
42+
isVersionSearchCompatibleForViewsDataExplorer(serverVersion)
43+
? 'Upgrade your cluster or manage search indexes on views in the Atlas UI.'
44+
: 'Upgrade your cluster to create search indexes on views.';
45+
return (
46+
<Banner variant={BannerVariant.Warning}>
47+
<b>Looking for search indexes?</b>
48+
<br />
49+
<div className={viewContentStyles}>
50+
<span>
51+
Your MongoDB version is {serverVersion}. Creating and managing search
52+
indexes on views {enableAtlasSearchIndexes && 'in Compass'} is
53+
supported on MongoDB version {searchIndexOnViewsMinVersion} or higher.{' '}
54+
{recommendedCta}
55+
</span>
56+
{atlasMetadata && (
57+
<Button
58+
size="xsmall"
59+
onClick={() => {
60+
window.open(
61+
getAtlasUpgradeClusterLink({
62+
clusterName: atlasMetadata.clusterName,
63+
}),
64+
'_blank'
65+
);
66+
}}
67+
>
68+
Upgrade Cluster
69+
</Button>
70+
)}
71+
</div>
72+
</Banner>
73+
);
10074
};

packages/compass-indexes/src/modules/search-indexes.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,16 @@ const ATLAS_SEARCH_SERVER_ERRORS: Record<string, string> = {
2525
'This index name is already in use. Please choose another one.',
2626
};
2727

28-
/*const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
29-
30-
const isVersionSearchCompatibleForViews = (serverVersion: string) => {
28+
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
29+
export const isVersionSearchCompatibleForViews = (serverVersion: string) => {
3130
try {
3231
return semver.gte(
33-
serverVersion,
34-
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
32+
serverVersion,
33+
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
3534
);
3635
} catch {
3736
return false;
3837
}
39-
};*/
40-
41-
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
42-
export const compareVersionForViewSearchCompatibility = (
43-
//default to 8.1+
44-
serverVersion: string,
45-
comparator: 'gt' | 'gte' | 'lt' | 'lte' = 'gte',
46-
compareVersion: string = MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
47-
) => {
48-
try {
49-
return semver[comparator](serverVersion, compareVersion);
50-
} catch {
51-
return false;
52-
}
5338
};
5439

5540
export enum ActionTypes {
@@ -635,8 +620,7 @@ const fetchIndexes = (
635620
} = getState();
636621

637622
if (
638-
(isReadonlyView &&
639-
!compareVersionForViewSearchCompatibility(serverVersion)) ||
623+
(isReadonlyView && !isVersionSearchCompatibleForViews(serverVersion)) ||
640624
!isWritable
641625
) {
642626
return; // return if view is not search compatible

0 commit comments

Comments
 (0)