Skip to content

Commit 7555a5f

Browse files
Haroenvclaude
andauthored
fix(chat): keep chatInstance getter on the widget (#7296)
Spreading the connector widget copied the getter's value (undefined before init), so widget.chatInstance was always undefined. Forward to the getter lazily instead. Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent d5a8379 commit 7555a5f

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

‎packages/instantsearch.js/src/widgets/chat/__tests__/chat.test.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ describe('chat', () => {
2222
warning.cache = {};
2323
});
2424

25+
describe('chatInstance', () => {
26+
test('is exposed on the widget once it has been initialized', async () => {
27+
const container = document.createElement('div');
28+
document.body.appendChild(container);
29+
30+
const search = instantsearch({
31+
indexName: 'indexName',
32+
searchClient: createSearchClient(),
33+
});
34+
35+
const widget = chat({ container, agentId: 'test-agent-id' });
36+
37+
expect(widget.chatInstance).toBeUndefined();
38+
39+
search.addWidgets([widget]);
40+
search.start();
41+
await wait(0);
42+
43+
expect(widget.chatInstance).toBeDefined();
44+
expect(widget.chatInstance.messages).toEqual([]);
45+
});
46+
});
47+
2548
describe('options', () => {
2649
test('throws without a `container`', () => {
2750
expect(() => {

‎packages/instantsearch.js/src/widgets/chat/chat.tsx‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,14 +1349,21 @@ export default (function chat<
13491349
render(null, containerNode)
13501350
);
13511351

1352+
const widget = makeWidget({
1353+
resume,
1354+
tools,
1355+
disableTriggerValidation: effectiveDisableTriggerValidation,
1356+
...options,
1357+
});
1358+
13521359
return {
1353-
...makeWidget({
1354-
resume,
1355-
tools,
1356-
disableTriggerValidation: effectiveDisableTriggerValidation,
1357-
...options,
1358-
}),
1360+
...widget,
13591361
$$widgetType: 'ais.chat',
1362+
// Spreading would copy the getter's value (still `undefined` before `init`),
1363+
// so we forward to it lazily instead.
1364+
get chatInstance() {
1365+
return widget.chatInstance;
1366+
},
13601367
};
13611368
} satisfies ChatWidget);
13621369

0 commit comments

Comments
 (0)