Skip to content

Commit 87fd0ae

Browse files
committed
chore: fix test, combine preference to hook
1 parent 96f137f commit 87fd0ae

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { atlasServiceLocator } from '@mongodb-js/atlas-service/provider';
77
import { DocsProviderTransport } from './docs-provider-transport';
88
import { useDrawerActions } from '@mongodb-js/compass-components';
99
import { buildExplainPlanPrompt } from './prompts';
10+
import { usePreference } from 'compass-preferences-model/provider';
1011

1112
export const ASSISTANT_DRAWER_ID = 'compass-assistant-drawer';
1213

@@ -31,8 +32,15 @@ export const AssistantActionsContext =
3132
interpretExplainPlan: () => {},
3233
});
3334

34-
export function useAssistantActions(): AssistantActionsContextType {
35-
return useContext(AssistantActionsContext);
35+
export function useAssistantActions(): AssistantActionsContextType & {
36+
isAssistantEnabled: boolean;
37+
} {
38+
const isAssistantEnabled = usePreference('enableAIAssistant');
39+
40+
return {
41+
...useContext(AssistantActionsContext),
42+
isAssistantEnabled,
43+
};
3644
}
3745

3846
export const AssistantProvider: React.FunctionComponent<

packages/compass-explain-plan/src/components/explain-plan-modal.spec.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function render(
2121
{
2222
dataService: {},
2323
localAppRegistry: {},
24-
preferences: { enableAIAssistant: preferences?.enableAIAssistant },
2524
} as any,
2625
{ on() {}, cleanup() {} } as any
2726
);
@@ -35,7 +34,8 @@ function render(
3534
onModalClose={() => {}}
3635
{...props}
3736
></ExplainPlanModal>
38-
</Provider>
37+
</Provider>,
38+
{ preferences: { enableAIAssistant: preferences?.enableAIAssistant } }
3939
);
4040
}
4141

@@ -70,4 +70,12 @@ describe('ExplainPlanModal', function () {
7070
render({ status: 'ready' }, { preferences: { enableAIAssistant: false } });
7171
expect(screen.queryByTestId('interpret-for-me-button')).to.not.exist;
7272
});
73+
74+
it('should disable the "Interpret for me" button when the status is not ready', function () {
75+
render({ status: 'loading' }, { preferences: { enableAIAssistant: true } });
76+
expect(screen.getByTestId('interpret-for-me-button')).to.have.attr(
77+
'aria-disabled',
78+
'true'
79+
);
80+
});
7381
});

packages/compass-explain-plan/src/components/explain-plan-modal.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { closeExplainPlanModal } from '../stores/explain-plan-modal-store';
1818
import { ExplainPlanView } from './explain-plan-view';
1919
import type { CollectionTabPluginMetadata } from '@mongodb-js/compass-collection';
2020
import { useAssistantActions } from '@mongodb-js/compass-assistant';
21-
import { usePreference } from 'compass-preferences-model/provider';
2221

2322
export type ExplainPlanModalProps = Partial<
2423
Pick<
@@ -105,8 +104,7 @@ export const ExplainPlanModal: React.FunctionComponent<
105104
error,
106105
onModalClose,
107106
}) => {
108-
const isAiAssistantEnabled = usePreference('enableAIAssistant');
109-
const { interpretExplainPlan } = useAssistantActions();
107+
const { interpretExplainPlan, isAssistantEnabled } = useAssistantActions();
110108

111109
return (
112110
<Modal
@@ -134,7 +132,7 @@ export const ExplainPlanModal: React.FunctionComponent<
134132
}
135133
/>
136134
</div>
137-
{isAiAssistantEnabled && (
135+
{isAssistantEnabled && (
138136
<div className={headerButtonSectionStyles}>
139137
<Button
140138
size="small"
@@ -148,6 +146,7 @@ export const ExplainPlanModal: React.FunctionComponent<
148146
onModalClose();
149147
interpretExplainPlan(JSON.stringify(explainPlan));
150148
}}
149+
disabled={status !== 'ready'}
151150
>
152151
Interpret for me
153152
</Button>

0 commit comments

Comments
 (0)