Skip to content

Commit 73b536b

Browse files
pr review changes
1 parent 26e428b commit 73b536b

File tree

6 files changed

+66
-38
lines changed

6 files changed

+66
-38
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect } from 'react';
1+
import React, { useCallback } from 'react';
22
import { connect } from 'react-redux';
33
import {
44
usePreference,
@@ -28,6 +28,7 @@ import { getAtlasSearchIndexesLink } from '../../utils/atlas-search-indexes-link
2828
import { createIndexOpened } from '../../modules/create-index';
2929
import type { IndexView } from '../../modules/index-view';
3030
import { indexViewChanged } from '../../modules/index-view';
31+
import { compareVersionForViewCompatibility } from '../indexes/indexes';
3132

3233
const toolbarButtonsContainer = css({
3334
display: 'flex',
@@ -106,13 +107,10 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
106107
readOnly, // preferences readOnly.
107108
}) => {
108109
const isSearchManagementActive = usePreference('enableAtlasSearchIndexes');
109-
const mongoDBMajorVersion = parseFloat(
110-
serverVersion.split('.').slice(0, 2).join('.')
111-
);
112110
const { atlasMetadata } = useConnectionInfo();
113111
const showInsights = usePreference('showInsights') && !errorMessage;
114112
const showCreateIndexButton =
115-
(!isReadonlyView || mongoDBMajorVersion > 8.0) &&
113+
(!isReadonlyView || compareVersionForViewCompatibility(serverVersion)) &&
116114
!readOnly &&
117115
!errorMessage;
118116
const refreshButtonIcon = isRefreshing ? (
@@ -123,19 +121,13 @@ export const IndexesToolbar: React.FunctionComponent<IndexesToolbarProps> = ({
123121
<Icon glyph="Refresh" title="Refresh Indexes" />
124122
);
125123

126-
useEffect(() => {
127-
// If the view is readonly, set the default tab to 'search-indexes'
128-
if (isReadonlyView && indexView !== 'search-indexes') {
129-
onIndexViewChanged('search-indexes'); // Update redux state
130-
}
131-
}, [indexView, isReadonlyView, onIndexViewChanged]);
132-
133124
return (
134125
<div
135126
className={indexesToolbarContainerStyles}
136127
data-testid="indexes-toolbar-container"
137128
>
138-
{(!isReadonlyView || mongoDBMajorVersion > 8.0) && (
129+
{(!isReadonlyView ||
130+
compareVersionForViewCompatibility(serverVersion)) && (
139131
<div data-testid="indexes-toolbar">
140132
<div className={toolbarButtonsContainer}>
141133
{showCreateIndexButton && (

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { getAtlasSearchIndexesLink } from '../../utils/atlas-search-indexes-link
3232
import CreateIndexModal from '../create-index-modal/create-index-modal';
3333
import { ZeroGraphic } from '../search-indexes-table/zero-graphic';
3434
import { ViewVersionIncompatibleBanner } from '../view-version-incompatible-banners/view-version-incompatible-banners';
35+
import semver from 'semver';
3536

3637
// This constant is used as a trigger to show an insight whenever number of
3738
// indexes in a collection is more than what is specified here.
@@ -51,14 +52,31 @@ const linkTitle = 'Search and Vector Search.';
5152
const DISMISSED_SEARCH_INDEXES_BANNER_LOCAL_STORAGE_KEY =
5253
'mongodb_compass_dismissedSearchIndexesBanner' as const;
5354

55+
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
56+
export const compareVersionForViewCompatibility = (
57+
//default to 8.1+
58+
serverVersion: string,
59+
comparator: 'gt' | 'gte' | 'lt' | 'lte' = 'gte',
60+
compareVersion: string = MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
61+
) => {
62+
try {
63+
return semver[comparator](serverVersion, compareVersion);
64+
} catch {
65+
return false;
66+
}
67+
};
68+
5469
const ViewVersionIncompatibleEmptyState = ({
55-
mongoDBMajorVersion,
70+
serverVersion,
5671
enableAtlasSearchIndexes,
5772
}: {
58-
mongoDBMajorVersion: number;
73+
serverVersion: string;
5974
enableAtlasSearchIndexes: boolean;
6075
}) => {
61-
if (mongoDBMajorVersion > 8.0 && enableAtlasSearchIndexes) {
76+
if (
77+
compareVersionForViewCompatibility(serverVersion) &&
78+
enableAtlasSearchIndexes
79+
) {
6280
return null;
6381
}
6482
return (
@@ -176,9 +194,6 @@ export function Indexes({
176194
: refreshSearchIndexes;
177195

178196
const enableAtlasSearchIndexes = usePreference('enableAtlasSearchIndexes');
179-
const mongoDBMajorVersion = parseFloat(
180-
serverVersion.split('.').slice(0, 2).join('.')
181-
);
182197
const { atlasMetadata } = useConnectionInfo();
183198

184199
return (
@@ -202,12 +217,12 @@ export function Indexes({
202217
{isReadonlyView && (
203218
<ViewVersionIncompatibleBanner
204219
serverVersion={serverVersion}
205-
mongoDBMajorVersion={mongoDBMajorVersion}
206220
enableAtlasSearchIndexes={enableAtlasSearchIndexes}
207221
atlasMetadata={atlasMetadata}
208222
/>
209223
)}
210-
{(!isReadonlyView || mongoDBMajorVersion >= 8.0) &&
224+
{(!isReadonlyView ||
225+
compareVersionForViewCompatibility(serverVersion, 'gte')) &&
211226
!enableAtlasSearchIndexes && (
212227
<AtlasIndexesBanner
213228
namespace={namespace}
@@ -220,11 +235,12 @@ export function Indexes({
220235
{!isReadonlyView && currentIndexesView === 'regular-indexes' && (
221236
<RegularIndexesTable />
222237
)}
223-
{(!isReadonlyView || mongoDBMajorVersion > 8.0) &&
238+
{(!isReadonlyView ||
239+
compareVersionForViewCompatibility(serverVersion)) &&
224240
currentIndexesView === 'search-indexes' && <SearchIndexesTable />}
225241
{isReadonlyView && searchIndexes.indexes.length === 0 && (
226242
<ViewVersionIncompatibleEmptyState
227-
mongoDBMajorVersion={mongoDBMajorVersion}
243+
serverVersion={serverVersion}
228244
enableAtlasSearchIndexes={enableAtlasSearchIndexes}
229245
/>
230246
)}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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 { compareVersionForViewCompatibility } from '../indexes/indexes';
1011

1112
const viewContentStyles = css({
1213
display: 'flex',
@@ -17,26 +18,25 @@ const viewContentStyles = css({
1718

1819
export const ViewVersionIncompatibleBanner = ({
1920
serverVersion,
20-
mongoDBMajorVersion,
2121
enableAtlasSearchIndexes,
2222
atlasMetadata,
2323
}: {
2424
serverVersion: string;
25-
mongoDBMajorVersion: number;
2625
enableAtlasSearchIndexes: boolean;
2726
atlasMetadata: AtlasClusterMetadata | undefined;
2827
}) => {
2928
const searchIndexOnViewsVersion = enableAtlasSearchIndexes ? '8.1' : '8.0';
3029

3130
if (
32-
mongoDBMajorVersion > 8.0 ||
33-
(mongoDBMajorVersion === 8.0 && !enableAtlasSearchIndexes)
31+
compareVersionForViewCompatibility(serverVersion) ||
32+
(compareVersionForViewCompatibility(serverVersion, 'gte', '8.0.0') &&
33+
!enableAtlasSearchIndexes)
3434
) {
3535
// return if 8.1+ on compass or 8.0+ for data explorer
3636
return null;
3737
}
3838

39-
if (mongoDBMajorVersion < 8.0) {
39+
if (compareVersionForViewCompatibility(serverVersion, 'lt', '8.0.0')) {
4040
// data explorer <8.0 and compass <8.0
4141
return (
4242
<Banner
@@ -72,7 +72,11 @@ export const ViewVersionIncompatibleBanner = ({
7272
);
7373
}
7474

75-
if (mongoDBMajorVersion === 8.0 && enableAtlasSearchIndexes) {
75+
if (
76+
compareVersionForViewCompatibility(serverVersion, 'gte', '8.0.0') &&
77+
compareVersionForViewCompatibility(serverVersion, 'lt', '8.1.0') &&
78+
enableAtlasSearchIndexes
79+
) {
7680
// compass 8.0
7781
return (
7882
<Banner

packages/compass-indexes/src/modules/index-view.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ export type IndexViewChangedAction = {
1818
view: IndexView;
1919
};
2020

21-
export const INITIAL_STATE: IndexView = 'regular-indexes';
21+
export const COLL_INITIAL_STATE: IndexView = 'regular-indexes';
22+
export const VIEW_INITIAL_STATE: IndexView = 'search-indexes';
2223

2324
export default function reducer(
24-
state = INITIAL_STATE,
25+
state = COLL_INITIAL_STATE,
2526
action: AnyAction
2627
): IndexView {
2728
// The create index button has a dropdown where you can select regular or

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ import type { FetchReason } from '../utils/fetch-reason';
1717
import type { IndexesThunkAction } from '.';
1818
import { switchToSearchIndexes } from './index-view';
1919
import type { IndexViewChangedAction } from './index-view';
20+
import semver from 'semver';
2021

2122
const ATLAS_SEARCH_SERVER_ERRORS: Record<string, string> = {
2223
InvalidIndexSpecificationOption: 'Invalid index definition.',
2324
IndexAlreadyExists:
2425
'This index name is already in use. Please choose another one.',
2526
};
2627

28+
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0';
29+
30+
const isVersionSearchCompatibleForViews = (serverVersion: string) => {
31+
try {
32+
return semver.gte(
33+
serverVersion,
34+
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS
35+
);
36+
} catch {
37+
return false;
38+
}
39+
};
40+
2741
export enum ActionTypes {
2842
// Fetch indexes
2943
FetchSearchIndexesStarted = 'compass-indexes/search-indexes/fetch-search-indexes-started',
@@ -606,11 +620,11 @@ const fetchIndexes = (
606620
searchIndexes: { status },
607621
} = getState();
608622

609-
const mongoDBMajorVersion = parseFloat(
610-
serverVersion.split('.').slice(0, 2).join('.')
611-
);
612-
if ((isReadonlyView && mongoDBMajorVersion < 8.1) || !isWritable) {
613-
return; // fetch for views 8.1+
623+
if (
624+
(isReadonlyView && !isVersionSearchCompatibleForViews(serverVersion)) ||
625+
!isWritable
626+
) {
627+
return; // return if view is not search compatible
614628
}
615629

616630
// If we are already fetching indexes, we will wait for that

packages/compass-indexes/src/stores/store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import reducer from '../modules';
55
import thunk from 'redux-thunk';
66
import { writeStateChanged } from '../modules/is-writable';
77
import { getDescription } from '../modules/description';
8-
import { INITIAL_STATE as INDEX_LIST_INITIAL_STATE } from '../modules/index-view';
8+
import { COLL_INITIAL_STATE, VIEW_INITIAL_STATE } from '../modules/index-view';
9+
910
import { createIndexOpened } from '../modules/create-index';
1011
import {
1112
fetchRegularIndexes,
@@ -106,7 +107,7 @@ export function activateIndexesPlugin(
106107
serverVersion: options.serverVersion,
107108
isReadonlyView: options.isReadonly,
108109
isSearchIndexesSupported: options.isSearchIndexesSupported,
109-
indexView: INDEX_LIST_INITIAL_STATE,
110+
indexView: options.isReadonly ? VIEW_INITIAL_STATE : COLL_INITIAL_STATE,
110111
collectionStats: extractCollectionStats(collectionModel),
111112
},
112113
applyMiddleware(

0 commit comments

Comments
 (0)