Skip to content

Commit 2ba7e14

Browse files
committed
track not as an action
1 parent 2a7010c commit 2ba7e14

File tree

2 files changed

+98
-66
lines changed

2 files changed

+98
-66
lines changed

packages/compass-schema/src/components/export-schema-modal.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
trackSchemaExported,
2727
type SchemaFormat,
2828
type ExportStatus,
29-
trackSchemaExportFailed,
3029
downloadSchema,
3130
} from '../stores/schema-export-reducer';
3231

@@ -210,7 +209,6 @@ export default connect(
210209
{
211210
onExportedSchemaCopied: trackSchemaExported,
212211
onExportedSchema: trackSchemaExported,
213-
onSchemaExportFailed: trackSchemaExportFailed,
214212
onCancelSchemaExport: cancelExportSchema,
215213
onChangeSchemaExportFormat: changeExportSchemaFormat,
216214
onClose: closeExportSchema,

packages/compass-schema/src/stores/schema-export-reducer.ts

Lines changed: 98 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { openToast } from '@mongodb-js/compass-components';
1212
import type { SchemaThunkAction } from './store';
1313
import { isAction } from '../utils';
1414
import { calculateSchemaMetadata } from '../modules/schema-analysis';
15+
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
16+
import type { ConnectionInfoRef } from '@mongodb-js/compass-connections/provider';
1517

1618
export type SchemaFormat =
1719
| 'standardJSON'
@@ -154,95 +156,114 @@ async function getSchemaByFormat({
154156
}
155157

156158
export const downloadSchema = (): SchemaThunkAction<void> => {
157-
return (dispatch, getState) => {
159+
return (dispatch, getState, { track, connectionInfoRef }) => {
160+
const {
161+
schemaExport: { exportedSchema, exportFormat, filename },
162+
} = getState();
163+
if (!exportedSchema) return;
158164
try {
159-
const {
160-
schemaExport: { exportedSchema, filename },
161-
} = getState();
162-
if (!exportedSchema) return;
163165
const blob = new Blob([exportedSchema], {
164166
type: 'application/json',
165167
});
166168
const url = window.URL.createObjectURL(blob);
169+
167170
const link = document.createElement('a');
168171
link.href = url;
169172
link.download = filename || 'export.json';
170173
link.click();
171-
window.URL.revokeObjectURL(url);
174+
setTimeout(() => {
175+
window.URL.revokeObjectURL(url);
176+
link.remove();
177+
}, 0);
172178
dispatch(trackSchemaExported);
173179
} catch (error) {
174-
dispatch(trackSchemaExportFailed('download button clicked'));
180+
_trackSchemaExportFailed({
181+
stage: 'download button clicked',
182+
track,
183+
connectionInfoRef,
184+
exportedSchema,
185+
exportFormat,
186+
});
175187
throw error;
176188
}
177189
};
178190
};
179191

180-
export const trackSchemaExportFailed = (
181-
stage: string
182-
): SchemaThunkAction<void> => {
183-
return (dispatch, getState, { track, connectionInfoRef }) => {
184-
const { exportedSchema, exportFormat } = getState().schemaExport;
185-
track(
186-
'Schema Export Failed',
187-
{
188-
has_schema: !!exportedSchema,
189-
schema_length: exportedSchema?.length || 0,
190-
format: exportFormat,
191-
stage,
192-
},
193-
connectionInfoRef.current
194-
);
195-
};
192+
const _trackSchemaExportFailed = ({
193+
stage,
194+
track,
195+
connectionInfoRef,
196+
exportedSchema,
197+
exportFormat,
198+
}: {
199+
stage: string;
200+
track: TrackFunction;
201+
connectionInfoRef: ConnectionInfoRef;
202+
exportedSchema: SchemaExportState['exportedSchema'];
203+
exportFormat: SchemaExportState['exportFormat'];
204+
}) => {
205+
track(
206+
'Schema Export Failed',
207+
{
208+
has_schema: !!exportedSchema,
209+
schema_length: exportedSchema?.length || 0,
210+
format: exportFormat,
211+
stage,
212+
},
213+
connectionInfoRef.current
214+
);
196215
};
197216

198217
const _trackSchemaExported = ({
199218
schema,
200219
source,
201220
format,
221+
track,
222+
connectionInfoRef,
202223
}: {
203224
schema: InternalSchema | null;
204225
source: 'app_menu' | 'schema_tab';
205226
format: SchemaFormat;
206-
}): SchemaThunkAction<void> => {
207-
return (dispatch, getState, { track, connectionInfoRef }) => {
208-
// Use a function here to a) ensure that the calculations here
209-
// are only made when telemetry is enabled and b) that errors from
210-
// those calculations are caught and logged rather than displayed to
211-
// users as errors from the core schema sharing logic.
212-
const trackEvent = async () => {
213-
const { geo_data, schema_depth } = schema
214-
? await calculateSchemaMetadata(schema)
215-
: {
216-
geo_data: false,
217-
schema_depth: 0,
218-
};
219-
220-
return {
221-
has_schema: schema !== null,
222-
format,
223-
source,
224-
schema_width: schema?.fields?.length ?? 0,
225-
schema_depth,
226-
geo_data,
227-
};
227+
track: TrackFunction;
228+
connectionInfoRef: ConnectionInfoRef;
229+
}) => {
230+
// Use a function here to a) ensure that the calculations here
231+
// are only made when telemetry is enabled and b) that errors from
232+
// those calculations are caught and logged rather than displayed to
233+
// users as errors from the core schema sharing logic.
234+
const trackEvent = async () => {
235+
const { geo_data, schema_depth } = schema
236+
? await calculateSchemaMetadata(schema)
237+
: {
238+
geo_data: false,
239+
schema_depth: 0,
240+
};
241+
242+
return {
243+
has_schema: schema !== null,
244+
format,
245+
source,
246+
schema_width: schema?.fields?.length ?? 0,
247+
schema_depth,
248+
geo_data,
228249
};
229-
track('Schema Exported', trackEvent, connectionInfoRef.current);
230250
};
251+
track('Schema Exported', trackEvent, connectionInfoRef.current);
231252
};
232253

233254
export const trackSchemaExported = (): SchemaThunkAction<void> => {
234-
return (dispatch, getState) => {
255+
return (dispatch, getState, { track, connectionInfoRef }) => {
235256
const {
236257
schemaAnalysis: { schema },
237258
schemaExport: { exportFormat },
238259
} = getState();
239-
dispatch(
240-
_trackSchemaExported({
241-
schema,
242-
format: exportFormat,
243-
source: 'schema_tab',
244-
})
245-
);
260+
_trackSchemaExported({
261+
schema,
262+
format: exportFormat,
263+
source: 'schema_tab',
264+
track,
265+
connectionInfoRef,
266+
});
246267
};
247268
};
248269

@@ -257,7 +278,14 @@ export const changeExportSchemaFormat = (
257278
return async (
258279
dispatch,
259280
getState,
260-
{ logger: { log }, exportAbortControllerRef, schemaAccessorRef, namespace }
281+
{
282+
logger: { log },
283+
exportAbortControllerRef,
284+
schemaAccessorRef,
285+
namespace,
286+
track,
287+
connectionInfoRef,
288+
}
261289
) => {
262290
// If we're already in progress we abort their current operation.
263291
exportAbortControllerRef.current?.abort();
@@ -308,7 +336,13 @@ export const changeExportSchemaFormat = (
308336
type: SchemaExportActions.changeExportSchemaFormatError,
309337
errorMessage: (err as Error).message,
310338
});
311-
dispatch(trackSchemaExportFailed('switching format'));
339+
_trackSchemaExportFailed({
340+
stage: 'switching format',
341+
track,
342+
connectionInfoRef,
343+
exportedSchema: undefined,
344+
exportFormat,
345+
});
312346
return;
313347
}
314348

@@ -509,21 +543,21 @@ export const switchToSchemaExport = (): SchemaThunkAction<void> => {
509543

510544
export const confirmedExportLegacySchemaToClipboard =
511545
(): SchemaThunkAction<void> => {
512-
return (dispatch, getState, { namespace }) => {
546+
return (dispatch, getState, { namespace, track, connectionInfoRef }) => {
513547
const {
514548
schemaAnalysis: { schema },
515549
} = getState();
516550
const hasSchema = schema !== null;
517551
if (hasSchema) {
518552
void navigator.clipboard.writeText(JSON.stringify(schema, null, ' '));
519553
}
520-
dispatch(
521-
_trackSchemaExported({
522-
schema,
523-
source: 'app_menu',
524-
format: 'legacyJSON',
525-
})
526-
);
554+
_trackSchemaExported({
555+
schema,
556+
source: 'app_menu',
557+
format: 'legacyJSON',
558+
track,
559+
connectionInfoRef,
560+
});
527561
dispatch({ type: SchemaExportActions.closeLegacyBanner });
528562
openToast(
529563
'share-schema',

0 commit comments

Comments
 (0)