Skip to content

Commit a6f9fdc

Browse files
committed
fix the cached feature flag
1 parent b27ccc2 commit a6f9fdc

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

packages/compass-assistant/src/compass-assistant-provider.spec.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('useAssistantActions', function () {
107107
return TestWrapper;
108108
};
109109

110-
it('returns empty object when AI features are disabled via isAIFeatureEnabled', function () {
110+
it('returns mostly empty object when AI features are disabled via isAIFeatureEnabled', function () {
111111
const { result } = renderHook(() => useAssistantActions(), {
112112
wrapper: createWrapper(createMockChat({ messages: [] })),
113113
preferences: {
@@ -119,10 +119,10 @@ describe('useAssistantActions', function () {
119119
},
120120
});
121121

122-
expect(result.current).to.deep.equal({});
122+
expect(result.current).to.have.keys(['getIsAssistantEnabled']);
123123
});
124124

125-
it('returns empty object when enableGenAIFeaturesAtlasOrg is disabled', function () {
125+
it('returns mostly empty object when enableGenAIFeaturesAtlasOrg is disabled', function () {
126126
const { result } = renderHook(() => useAssistantActions(), {
127127
wrapper: createWrapper(createMockChat({ messages: [] })),
128128
preferences: {
@@ -133,10 +133,10 @@ describe('useAssistantActions', function () {
133133
},
134134
});
135135

136-
expect(result.current).to.deep.equal({});
136+
expect(result.current).to.have.keys(['getIsAssistantEnabled']);
137137
});
138138

139-
it('returns empty object when cloudFeatureRolloutAccess is disabled', function () {
139+
it('returns mostly empty object when cloudFeatureRolloutAccess is disabled', function () {
140140
const { result } = renderHook(() => useAssistantActions(), {
141141
wrapper: createWrapper(createMockChat({ messages: [] })),
142142
preferences: {
@@ -147,10 +147,10 @@ describe('useAssistantActions', function () {
147147
},
148148
});
149149

150-
expect(result.current).to.deep.equal({});
150+
expect(result.current).to.have.keys(['getIsAssistantEnabled']);
151151
});
152152

153-
it('returns empty object when enableAIAssistant preference is disabled', function () {
153+
it('returns mostly empty object when enableAIAssistant preference is disabled', function () {
154154
const { result } = renderHook(() => useAssistantActions(), {
155155
wrapper: createWrapper(createMockChat({ messages: [] })),
156156
preferences: {
@@ -161,7 +161,7 @@ describe('useAssistantActions', function () {
161161
},
162162
});
163163

164-
expect(result.current).to.deep.equal({});
164+
expect(result.current).to.have.keys(['getIsAssistantEnabled']);
165165
});
166166

167167
it('returns actions when both AI features and assistant flag are enabled', function () {

packages/compass-assistant/src/compass-assistant-provider.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ type AssistantActionsContextType = {
7474
type AssistantActionsType = Omit<
7575
AssistantActionsContextType,
7676
'ensureOptInAndSend' | 'clearChat'
77-
>;
77+
> & {
78+
getIsAssistantEnabled: () => boolean;
79+
};
7880

7981
export const AssistantActionsContext =
8082
createContext<AssistantActionsContextType>({
@@ -90,7 +92,9 @@ export function useAssistantActions(): AssistantActionsType {
9092
const isAIFeatureEnabled = useIsAIFeatureEnabled();
9193
const isAssistantFlagEnabled = usePreference('enableAIAssistant');
9294
if (!isAIFeatureEnabled || !isAssistantFlagEnabled) {
93-
return {};
95+
return {
96+
getIsAssistantEnabled: () => false,
97+
};
9498
}
9599

96100
const {
@@ -103,15 +107,37 @@ export function useAssistantActions(): AssistantActionsType {
103107
interpretExplainPlan,
104108
interpretConnectionError,
105109
tellMoreAboutInsight,
110+
getIsAssistantEnabled: () => true,
106111
};
107112
}
108113

109-
export const compassAssistantServiceLocator = createServiceLocator(function () {
114+
export const compassAssistantServiceLocator = createServiceLocator(() => {
110115
const actions = useAssistantActions();
111-
return actions;
116+
117+
const interpretConnectionErrorRef = useRef(actions.interpretConnectionError);
118+
interpretConnectionErrorRef.current = actions.interpretConnectionError;
119+
120+
const getIsAssistantEnabledRef = useRef(actions.getIsAssistantEnabled);
121+
getIsAssistantEnabledRef.current = actions.getIsAssistantEnabled;
122+
123+
return {
124+
interpretConnectionError: (options: {
125+
connectionInfo: ConnectionInfo;
126+
error: Error;
127+
}) => interpretConnectionErrorRef.current?.(options),
128+
getIsAssistantEnabled: () => {
129+
return getIsAssistantEnabledRef.current();
130+
},
131+
};
112132
}, 'compassAssistantLocator');
113133

114-
export type CompassAssistantService = AssistantActionsType;
134+
export type CompassAssistantService = {
135+
interpretConnectionError: (options: {
136+
connectionInfo: ConnectionInfo;
137+
error: Error;
138+
}) => void;
139+
getIsAssistantEnabled: () => boolean;
140+
};
115141

116142
export const AssistantProvider: React.FunctionComponent<
117143
PropsWithChildren<{
@@ -121,6 +147,7 @@ export const AssistantProvider: React.FunctionComponent<
121147
> = ({ chat, atlasAiService, children }) => {
122148
const { openDrawer } = useDrawerActions();
123149
const track = useTelemetry();
150+
124151
const createEntryPointHandler = useRef(function <T>(
125152
entryPointName:
126153
| 'explain plan'

packages/compass-connections/src/stores/connections-store-redux.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,9 @@ const connectionAttemptError = (
12841284
}
12851285
: undefined,
12861286
onDebugClick:
1287-
compassAssistant.interpretConnectionError && connectionInfo
1287+
compassAssistant.getIsAssistantEnabled() && connectionInfo
12881288
? () => {
1289-
compassAssistant.interpretConnectionError?.({
1289+
compassAssistant.interpretConnectionError({
12901290
connectionInfo,
12911291
error: err,
12921292
});

0 commit comments

Comments
 (0)