Skip to content

Commit 7705104

Browse files
authored
feat: Compass Experimentation Provider CLOUDP-333843 (#7151)
* WIP * Copilot review comment * Rename provider to CompassExperimentationProvider * useMemo -> useRef * Import SDK * initialContext nit comment * Package Lock * Tweak * WIP
1 parent cd7e32c commit 7705104

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-telemetry/.eslintrc.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,26 @@ module.exports = {
55
tsconfigRootDir: __dirname,
66
project: ['./tsconfig-lint.json'],
77
},
8+
overrides: [
9+
{
10+
files: ['./src/**/*.ts', './src/**/*.tsx'],
11+
rules: {
12+
'no-restricted-imports': 'off',
13+
'@typescript-eslint/no-restricted-imports': [
14+
'error',
15+
{
16+
paths: [
17+
{
18+
name: '@mongodb-js/mdb-experiment-js',
19+
message:
20+
'Use type-only imports from @mongodb-js/mdb-experiment-js',
21+
allowTypeImports: true,
22+
},
23+
],
24+
},
25+
],
26+
'@typescript-eslint/no-redundant-type-constituents': 'off',
27+
},
28+
},
29+
],
830
};

packages/compass-telemetry/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"@mongodb-js/compass-logging": "^1.7.9",
5656
"@mongodb-js/compass-app-registry": "^9.4.18",
5757
"hadron-ipc": "^3.5.8",
58-
"react": "^17.0.2"
58+
"react": "^17.0.2",
59+
"@mongodb-js/mdb-experiment-js": "1.9.0"
5960
},
6061
"devDependencies": {
6162
"@mongodb-js/eslint-config-compass": "^1.4.5",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { createContext, useContext, useRef } from 'react';
2+
import type { types } from '@mongodb-js/mdb-experiment-js';
3+
import type { typesReact } from '@mongodb-js/mdb-experiment-js/react';
4+
5+
type UseAssignmentHook = (
6+
experimentName: string,
7+
trackIsInSample: boolean,
8+
options?: typesReact.UseAssignmentOptions<types.TypeData>
9+
) => typesReact.UseAssignmentResponse<types.TypeData>;
10+
11+
type AssignExperimentFn = (
12+
experimentName: string,
13+
options?: types.AssignOptions<string>
14+
) => Promise<types.AsyncStatus | null>;
15+
16+
interface CompassExperimentationProviderContextValue {
17+
useAssignment: UseAssignmentHook;
18+
assignExperiment: AssignExperimentFn;
19+
}
20+
21+
const initialContext: CompassExperimentationProviderContextValue = {
22+
useAssignment() {
23+
return {
24+
assignment: null,
25+
asyncStatus: null,
26+
error: null,
27+
isLoading: false,
28+
isError: false,
29+
isSuccess: true,
30+
};
31+
},
32+
assignExperiment() {
33+
return Promise.resolve(null);
34+
},
35+
};
36+
37+
const ExperimentationContext =
38+
createContext<CompassExperimentationProviderContextValue>(initialContext);
39+
40+
// Provider component that accepts MMS experiment utils as props
41+
export const CompassExperimentationProvider: React.FC<{
42+
children: React.ReactNode;
43+
useAssignment: UseAssignmentHook;
44+
assignExperiment: AssignExperimentFn;
45+
}> = ({ children, useAssignment, assignExperiment }) => {
46+
// Use useRef to keep the functions up-to-date; Use mutation pattern to maintain the
47+
// same object reference to prevent unnecessary re-renders of consuming components
48+
const { current: contextValue } = useRef({ useAssignment, assignExperiment });
49+
contextValue.useAssignment = useAssignment;
50+
contextValue.assignExperiment = assignExperiment;
51+
52+
return (
53+
<ExperimentationContext.Provider value={contextValue}>
54+
{children}
55+
</ExperimentationContext.Provider>
56+
);
57+
};
58+
59+
// Hook for components to access experiment assignment
60+
export const useAssignment = (...args: Parameters<UseAssignmentHook>) => {
61+
return useContext(ExperimentationContext).useAssignment(...args);
62+
};

packages/compass-telemetry/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export type {
55
IdentifyTraits,
66
ExtraConnectionData,
77
} from './types';
8+
9+
export { CompassExperimentationProvider } from './experimentation-provider';

packages/compass-web/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export type {
44
OpenWorkspaceOptions,
55
WorkspaceTab,
66
} from '@mongodb-js/compass-workspaces';
7+
8+
export { CompassExperimentationProvider } from '@mongodb-js/compass-telemetry';

0 commit comments

Comments
 (0)