Skip to content

Commit 0408cea

Browse files
Updated chat examples for UseSelector Storybook page (#5871)
1 parent cdd7039 commit 0408cea

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

packages/chat-component-bindings/src/handlers/createHandlers.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ describe('createHandlers', () => {
222222
await handlers.onSendMessage('test message');
223223
expect(mockChatThreadClient.sendMessage).toHaveBeenCalledWith(
224224
{
225-
content: 'test message',
226-
senderDisplayName: displayName
225+
content: 'test message'
227226
},
228-
undefined
227+
{
228+
senderDisplayName: displayName
229+
}
229230
);
230231
});
231232

@@ -240,16 +241,16 @@ describe('createHandlers', () => {
240241
await handlers.onSendMessage('test message', options);
241242
expect(mockChatThreadClient.sendMessage).toHaveBeenCalledWith(
242243
{
243-
content: 'test message',
244-
senderDisplayName: displayName
244+
content: 'test message'
245245
},
246246
{
247247
metadata: {
248248
key: 'value',
249249
fileSharingMetadata: JSON.stringify(options.attachments)
250250
},
251251
attachments: undefined,
252-
type: undefined
252+
type: undefined,
253+
senderDisplayName: displayName
253254
}
254255
);
255256
});
@@ -262,8 +263,7 @@ describe('createHandlers', () => {
262263
await handlers.onSendMessage(content);
263264
expect(mockChatThreadClient.sendMessage).toHaveBeenCalledWith(
264265
{
265-
content,
266-
senderDisplayName: displayName
266+
content
267267
},
268268
{
269269
attachments: [
@@ -275,7 +275,8 @@ describe('createHandlers', () => {
275275
metadata: {
276276
fileSharingMetadata: undefined
277277
},
278-
type: undefined
278+
type: undefined,
279+
senderDisplayName: displayName
279280
}
280281
);
281282
});
@@ -293,16 +294,16 @@ describe('createHandlers', () => {
293294
await handlers.onSendMessage(content, options);
294295
expect(mockChatThreadClient.sendMessage).toHaveBeenCalledWith(
295296
{
296-
content,
297-
senderDisplayName: displayName
297+
content
298298
},
299299
{
300300
metadata: {
301301
key: 'value',
302302
fileSharingMetadata: JSON.stringify(options.attachments)
303303
},
304304
attachments: [{ id: 'image1', attachmentType: 'image' }],
305-
type: undefined
305+
type: undefined,
306+
senderDisplayName: displayName
306307
}
307308
);
308309
});

packages/chat-component-bindings/src/handlers/createHandlers.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PagedAsyncIterableIterator } from '@azure/core-paging';
55
import { ReactElement } from 'react';
66
import { Common, fromFlatCommunicationIdentifier } from '@internal/acs-ui-common';
77
import { StatefulChatClient } from '@internal/chat-stateful-client';
8+
import { SendMessageRequest } from '@azure/communication-chat';
89
/* @conditional-compile-remove(file-sharing-acs) */
910
import { ChatAttachment } from '@azure/communication-chat';
1011
/* @conditional-compile-remove(rich-text-editor-image-upload) */
@@ -68,9 +69,8 @@ export const createDefaultChatHandlers = memoizeOne(
6869
content: string,
6970
options?: SendMessageOptions | /* @conditional-compile-remove(file-sharing-acs) */ MessageOptions
7071
) {
71-
const sendMessageRequest = {
72-
content,
73-
senderDisplayName: chatClient.getState().displayName
72+
const sendMessageRequest: SendMessageRequest = {
73+
content
7474
};
7575

7676
/* @conditional-compile-remove(rich-text-editor-image-upload) */
@@ -100,13 +100,17 @@ export const createDefaultChatHandlers = memoizeOne(
100100
},
101101
/* @conditional-compile-remove(rich-text-editor-image-upload) */
102102
attachments: imageAttachments,
103-
type: options?.type
103+
type: options?.type,
104+
senderDisplayName: chatClient.getState().displayName
104105
};
105106
await chatThreadClient.sendMessage(sendMessageRequest, chatSDKOptions);
106107
return;
107108
}
108109

109-
await chatThreadClient.sendMessage(sendMessageRequest, options as SendMessageOptions);
110+
await chatThreadClient.sendMessage(sendMessageRequest, {
111+
...(options as SendMessageOptions),
112+
senderDisplayName: chatClient.getState().displayName
113+
});
110114
},
111115
/* @conditional-compile-remove(rich-text-editor-image-upload) */
112116
onUploadImage: async function (image: Blob, imageFilename: string): Promise<UploadChatImageResult> {

packages/storybook8/stories/StatefulClient/ReactHooks/UseSelector.mdx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,24 @@ While we provide a set of default props(which includes data and handler function
103103
In some situations, you may want to override default behavior and place your own API call (like call another service, or call another API in our stateful SDK). Here is an example of how to change the message before using the stateful client to send it.
104104

105105
```tsx
106+
const threadId = '<Thread id from chat service>';
106107
const sendBoxProps = usePropsFor(SendBox);
107-
// This hook will get stateful client you created
108-
const chatThreadClient = useThreadClient();
108+
// Instantiate the statefulChatClient
109+
const statefulChatClient = createStatefulChatClient(...); // create a stateful chat client
110+
// Get the chat thread client
111+
const chatThreadClient = statefulChatClient.getChatThreadClient(threadId);
109112

110113
const onSendMessage = useCallback(
111114
(message: string) => {
112-
const sendMessageRequest = {
113-
`✉: ${content}`,
114-
senderDisplayName: chatClient.getState().displayName
115+
const sendMessageRequest: = {
116+
// Updated message content
117+
content: `✉: ${message}`
118+
};
119+
const sendMessageOptions = {
120+
senderDisplayName: statefulChatClient.getState().displayName
115121
};
116122
// directly call into stateful client
117-
await chatThreadClient.sendMessage(sendMessageRequest);
123+
await chatThreadClient.sendMessage(sendMessageRequest, sendMessageOptions);
118124
},
119125
[chatThreadClient]
120126
);
@@ -142,8 +148,8 @@ By calling `useSelector`/`usePropsFor`, you can get the right combination of def
142148
```tsx
143149
const customSelector = createSelector(....); // create your own selector
144150
const messageThreadProps = useSelector(customSelector);
145-
// This hook will get stateful client you created
146-
const chatThreadClient = useThreadClient();
151+
// Get the chat thread client
152+
const chatThreadClient = useChatThreadClient();
147153

148154
// Create your own handlers
149155
const onMessageSeen = useCallback((message: string) => {

0 commit comments

Comments
 (0)