Skip to content

Commit 1038537

Browse files
Storybook messages loading issue fix (#2784)
1 parent 5e46898 commit 1038537

5 files changed

Lines changed: 52 additions & 12 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Fix messages thread loading issue when an adapter is updated",
4+
"packageName": "@azure/communication-react",
5+
"email": "98852890+vhuseinova-msft@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Fix messages thread loading issue when an adapter is updated",
4+
"packageName": "@azure/communication-react",
5+
"email": "98852890+vhuseinova-msft@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

packages/react-components/src/components/MessageThread.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,13 @@ export const MessageThread = (props: MessageThreadProps): JSX.Element => {
753753
const [readCountForHoveredIndicator, setReadCountForHoveredIndicator] = useState<number | undefined>(undefined);
754754

755755
const isAllChatMessagesLoadedRef = useRef(false);
756+
// isAllChatMessagesLoadedRef needs to be updated every time when a new adapter is set in order to display correct data
757+
// onLoadPreviousChatMessages is updated when a new adapter is set
758+
useEffect(() => {
759+
if (onLoadPreviousChatMessages) {
760+
isAllChatMessagesLoadedRef.current = false;
761+
}
762+
}, [onLoadPreviousChatMessages]);
756763

757764
const previousTopRef = useRef<number>(-1);
758765
const previousHeightRef = useRef<number>(-1);
@@ -851,16 +858,20 @@ export const MessageThread = (props: MessageThreadProps): JSX.Element => {
851858
if (!isLoadingChatMessagesRef.current) {
852859
if (onLoadPreviousChatMessages) {
853860
isLoadingChatMessagesRef.current = true;
854-
// Fetch message until scrollTop reach the threshold for fetching new message
855-
while (
856-
!isAllChatMessagesLoadedRef.current &&
857-
chatScrollDivRef.current &&
858-
chatScrollDivRef.current.scrollTop <= 500
859-
) {
860-
isAllChatMessagesLoadedRef.current = await onLoadPreviousChatMessages(numberOfChatMessagesToReload);
861-
await delay(200);
861+
try {
862+
// Fetch message until scrollTop reach the threshold for fetching new message
863+
while (
864+
!isAllChatMessagesLoadedRef.current &&
865+
chatScrollDivRef.current &&
866+
chatScrollDivRef.current.scrollTop <= 500
867+
) {
868+
isAllChatMessagesLoadedRef.current = await onLoadPreviousChatMessages(numberOfChatMessagesToReload);
869+
await delay(200);
870+
}
871+
} finally {
872+
// Set isLoadingChatMessagesRef to false after messages are fetched
873+
isLoadingChatMessagesRef.current = false;
862874
}
863-
isLoadingChatMessagesRef.current = false;
864875
}
865876
}
866877
}, [numberOfChatMessagesToReload, onLoadPreviousChatMessages]);

packages/react-composites/src/composites/ChatComposite/ChatScreen.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ export const ChatScreen = (props: ChatScreenProps): JSX.Element => {
127127

128128
useEffect(() => {
129129
// Initial data should be always fetched by the composite(or external caller) instead of the adapter
130-
adapter.fetchInitialData();
130+
const fetchData: () => Promise<void> = async () => {
131+
// Fetch initial data for adapter
132+
await adapter.fetchInitialData();
133+
// Fetch initial set of messages. Without fetching messages here, if the Composite's adapter is changed the message thread does not load new messages.
134+
await adapter.loadPreviousChatMessages(defaultNumberOfChatMessagesToReload);
135+
};
136+
fetchData();
131137
}, [adapter]);
132138

133139
const messageThreadProps = usePropsFor(MessageThread);

packages/storybook/stories/ChatComposite/snippets/Container.snippet.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
useAzureCommunicationChatAdapter
77
} from '@azure/communication-react';
88
import { PartialTheme, Theme } from '@fluentui/react';
9-
import React, { useMemo } from 'react';
9+
import React, { useMemo, useEffect, useState } from 'react';
1010

1111
export type ContainerProps = {
1212
/** UserIdentifier is of type CommunicationUserIdentifier see below how to construct it from a string input */
@@ -38,10 +38,19 @@ export const ContosoChatContainer = (props: ContainerProps): JSX.Element => {
3838
[props.userIdentifier]
3939
);
4040

41+
// Add throttling for setting display name during typing
42+
const [displayName, setDisplayName] = useState<string | undefined>(undefined);
43+
useEffect(() => {
44+
const handle = setTimeout(() => {
45+
setDisplayName(props.displayName);
46+
}, 500);
47+
return () => clearTimeout(handle);
48+
}, [props.displayName]);
49+
4150
const adapter = useAzureCommunicationChatAdapter({
4251
endpoint: props.endpointUrl,
4352
userId,
44-
displayName: props.displayName,
53+
displayName,
4554
credential,
4655
threadId: props.threadId
4756
});

0 commit comments

Comments
 (0)