Skip to content

Commit b0f3436

Browse files
committed
finished adding error and segment event for covered queries
1 parent 2e08874 commit b0f3436

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

packages/compass-indexes/src/components/create-index-form/index-flow-section.tsx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import {
1313
Tooltip,
1414
} from '@mongodb-js/compass-components';
1515
import React, { useState, useCallback, useEffect } from 'react';
16-
import type { Field } from '../../modules/create-index';
16+
import { errorEncountered, type Field } from '../../modules/create-index';
1717
import MDBCodeViewer from './mdb-code-viewer';
1818
import { areAllFieldsFilledIn } from '../../utils/create-index-modal-validation';
19+
import { connect } from 'react-redux';
20+
import {
21+
TrackFunction,
22+
useTelemetry,
23+
} from '@mongodb-js/compass-telemetry/provider';
1924

2025
const flexContainerStyles = css({
2126
display: 'flex',
@@ -78,10 +83,12 @@ export type IndexFlowSectionProps = {
7883
createIndexFieldsComponent: JSX.Element | null;
7984
dbName: string;
8085
collectionName: string;
86+
onErrorEncountered: (error: string) => void;
8187
};
8288

8389
const generateCoveredQueries = (
84-
coveredQueriesArr: Array<Record<string, number>>
90+
coveredQueriesArr: Array<Record<string, number>>,
91+
track: TrackFunction
8592
) => {
8693
const rows = [];
8794
for (let i = 0; i < coveredQueriesArr.length; i++) {
@@ -93,6 +100,14 @@ const generateCoveredQueries = (
93100
);
94101
}
95102

103+
if (rows.length === 0) {
104+
track('Error generating covered queries', {
105+
context: 'Create Index Modal',
106+
});
107+
throw new Error(
108+
'Error generating covered query examples. Please try again later.'
109+
);
110+
}
96111
return <>{rows}</>;
97112
};
98113

@@ -149,6 +164,7 @@ const IndexFlowSection = ({
149164
fields,
150165
dbName,
151166
collectionName,
167+
onErrorEncountered,
152168
}: IndexFlowSectionProps) => {
153169
const [isCodeEquivalentToggleChecked, setIsCodeEquivalentToggleChecked] =
154170
useState(false);
@@ -157,6 +173,7 @@ const IndexFlowSection = ({
157173
const hasUnsupportedQueryTypes = fields.some((field) => {
158174
return field.type === '2dsphere' || field.type === 'text';
159175
});
176+
const track = useTelemetry();
160177

161178
const isCoveredQueriesButtonDisabled =
162179
!areAllFieldsFilledIn(fields) ||
@@ -188,13 +205,18 @@ const IndexFlowSection = ({
188205
return { [field.name]: index + 1 };
189206
});
190207

191-
setCoveredQueriesObj({
192-
coveredQueries: generateCoveredQueries(coveredQueriesArr),
193-
optimalQueries: generateOptimalQueries(coveredQueriesArr),
194-
showCoveredQueries: true,
195-
});
208+
try {
209+
setCoveredQueriesObj({
210+
coveredQueries: generateCoveredQueries(coveredQueriesArr, track),
211+
optimalQueries: generateOptimalQueries(coveredQueriesArr),
212+
showCoveredQueries: true,
213+
});
214+
} catch (e) {
215+
onErrorEncountered(e.message);
216+
}
217+
196218
setHasFieldChanges(false);
197-
}, [fields]);
219+
}, [fields, onErrorEncountered, track]);
198220

199221
useEffect(() => {
200222
setHasFieldChanges(true);
@@ -320,4 +342,12 @@ const IndexFlowSection = ({
320342
);
321343
};
322344

323-
export default IndexFlowSection;
345+
const mapState = () => {
346+
return {};
347+
};
348+
349+
const mapDispatch = {
350+
onErrorEncountered: errorEncountered,
351+
};
352+
353+
export default connect(mapState, mapDispatch)(IndexFlowSection);

packages/compass-indexes/src/components/create-index-form/query-flow-section.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const insightStyles = css({
8181
display: 'flex',
8282
alignItems: 'center',
8383
gap: spacing[100],
84+
marginBottom: spacing[200],
85+
height: spacing[500],
8486
});
8587

8688
const QueryFlowSection = ({
@@ -107,9 +109,11 @@ const QueryFlowSection = ({
107109
initialQuery: Document | null;
108110
}) => {
109111
const [inputQuery, setInputQuery] = React.useState(
110-
JSON.stringify(initialQuery?.filter ?? {}, null, 2)
112+
JSON.stringify(initialQuery?.filter, null, 2)
113+
);
114+
const [hasNewChanges, setHasNewChanges] = React.useState(
115+
initialQuery !== null
111116
);
112-
const [hasNewChanges, setHasNewChanges] = React.useState(false);
113117
const completer = useMemo(
114118
() =>
115119
createQueryAutocompleter({

packages/compass-indexes/src/modules/create-index.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ export const createIndexClosed = () => ({
348348
type: ActionTypes.CreateIndexClosed,
349349
});
350350

351-
const errorEncountered = (error: string): ErrorEncounteredAction => ({
351+
export const errorEncountered = (error: string): ErrorEncounteredAction => ({
352352
type: ActionTypes.ErrorEncountered,
353353
error,
354354
});
@@ -497,16 +497,8 @@ export const createIndexFormSubmitted = (): IndexesThunkAction<
497497
});
498498

499499
// Check for field errors.
500-
if (isQueryFlow) {
501-
if (!indexSuggestions) {
502-
dispatch(
503-
errorEncountered(
504-
'No suggested index found. Please choose "Start with an Index" at the top to continue.'
505-
)
506-
);
507-
return;
508-
}
509-
} else if (
500+
if (
501+
!isQueryFlow &&
510502
getState().createIndex.fields.some(
511503
(field: Field) => field.name === '' || field.type === ''
512504
)

0 commit comments

Comments
 (0)