Skip to content

Commit 8bdbf3e

Browse files
fix(ai-sidebar): Add callback for selected agent (#4115)
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 9bf5853 commit 8bdbf3e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/elements/content-sidebar/BoxAISidebarContent.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as React from 'react';
66
import flow from 'lodash/flow';
77
import { useIntl } from 'react-intl';
88
import classNames from 'classnames';
9-
import { BoxAiAgentSelectorWithApi, useAgents } from '@box/box-ai-agent-selector';
9+
import { BoxAiAgentSelectorWithApi, useAgents, type AgentType } from '@box/box-ai-agent-selector';
1010
import { IconButton, Tooltip } from '@box/blueprint-web';
1111
import { ArrowsExpand } from '@box/blueprint-web-assets/icons/Fill';
1212
import {
@@ -34,11 +34,17 @@ const MARK_NAME_JS_READY: string = `${ORIGIN_BOXAI_SIDEBAR}_${EVENT_JS_READY}`;
3434

3535
mark(MARK_NAME_JS_READY);
3636

37-
function BoxAISidebarContent(props: ApiWrapperWithInjectedProps & { shouldShowLandingPage: boolean }) {
37+
function BoxAISidebarContent(
38+
props: ApiWrapperWithInjectedProps & {
39+
onSelectedAgentCallback: (selectedAgent: AgentType) => void;
40+
shouldShowLandingPage: boolean;
41+
},
42+
) {
3843
const {
3944
createSession,
4045
encodedSession,
4146
onClearAction,
47+
onSelectedAgentCallback,
4248
getAIStudioAgents,
4349
hasRequestInProgress,
4450
hostAppName,
@@ -155,6 +161,11 @@ function BoxAISidebarContent(props: ApiWrapperWithInjectedProps & { shouldShowLa
155161
// eslint-disable-next-line react-hooks/exhaustive-deps
156162
}, [encodedSession]);
157163

164+
React.useEffect(() => {
165+
onSelectedAgentCallback?.(selectedAgent);
166+
// eslint-disable-next-line react-hooks/exhaustive-deps
167+
}, [selectedAgent?.id]);
168+
158169
const renderBoxAISidebarTitle = () => {
159170
return (
160171
<div className="bcs-BoxAISidebar-title-part">

src/elements/content-sidebar/__tests__/BoxAISidebar.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import { render, screen } from '../../../test-utils/testing-library';
44
import BoxAISidebar, { BoxAISidebarProps } from '../BoxAISidebar';
55

66
let MockBoxAiAgentSelectorWithApi: jest.Mock;
7+
let mockUseAgents: jest.Mock;
8+
79
jest.mock('@box/box-ai-agent-selector', () => {
810
MockBoxAiAgentSelectorWithApi = jest.fn();
11+
mockUseAgents = jest.fn();
912
return {
1013
...jest.requireActual('@box/box-ai-agent-selector'),
1114
BoxAiAgentSelectorWithApi: MockBoxAiAgentSelectorWithApi,
15+
useAgents: mockUseAgents,
1216
};
1317
});
1418

@@ -39,6 +43,7 @@ jest.mock('@box/box-ai-content-answers', () => ({
3943
onClearAction={mockOnClearAction}
4044
onCloseModal={jest.fn()}
4145
onSelectAgent={jest.fn()}
46+
onSelectedAgentCallback={props.onSelectedAgentCallback}
4247
onSuggestedQuestionsFetched={props.onSuggestedQuestionsFetched}
4348
onAgentEditorToggle={jest.fn()}
4449
questions={props.restoredQuestions}
@@ -145,6 +150,12 @@ describe('elements/content-sidebar/BoxAISidebar', () => {
145150

146151
beforeEach(() => {
147152
MockBoxAiAgentSelectorWithApi.mockImplementation(() => <div data-testid="sidebar-agent-selector" />);
153+
mockUseAgents.mockReturnValue({
154+
agents: [],
155+
selectedAgent: { id: '1', config: {}, name: 'Test Agent' },
156+
setSelectedAgent: jest.fn(),
157+
requestState: 'success',
158+
});
148159
});
149160

150161
afterEach(() => {
@@ -465,4 +476,14 @@ describe('elements/content-sidebar/BoxAISidebar', () => {
465476

466477
expect(mockProps.onUserInteraction).toHaveBeenCalled();
467478
});
479+
480+
test('Should call onSelectedAgentCallback on agent selected change', async () => {
481+
const mockOnSelectedAgentCallback = jest.fn();
482+
483+
await renderComponent({
484+
onSelectedAgentCallback: mockOnSelectedAgentCallback,
485+
});
486+
487+
expect(mockOnSelectedAgentCallback).toHaveBeenCalled();
488+
});
468489
});

0 commit comments

Comments
 (0)