Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit dee503f

Browse files
authored
fix(lib): fix fetchHistory not being called when a live message showe… (#113)
* fix(lib): fix fetchHistory not being called when a live message showed up * fix(lib): revert one condition
1 parent 8887253 commit dee503f

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

packages/common/src/message-list/message-list.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export const useMessageListCore = (props: CommonMessageListProps) => {
7171
const [fetchingMessages, setFetchingMessages] = useState(false);
7272
const [reactingToMessage, setReactingToMessage] = useState(null);
7373
const [emojiPickerShown, setEmojiPickerShown] = useState(false);
74+
// https://pubnub.atlassian.net/browse/UI-1843
75+
const [initMessagesLoaded, setInitMessagesLoaded] = useState({});
7476

7577
/*
7678
/* Helper functions
@@ -195,8 +197,12 @@ export const useMessageListCore = (props: CommonMessageListProps) => {
195197
useEffect(() => {
196198
if (!pubnub || !channel) return;
197199
if (channel === prevChannel) return;
198-
if (!messages?.length) fetchHistory();
199-
}, [channel, fetchHistory, messages?.length, prevChannel, pubnub]);
200+
if (!initMessagesLoaded[channel]) {
201+
fetchHistory().then(() => {
202+
setInitMessagesLoaded((curr) => ({ ...curr, [channel]: true }));
203+
});
204+
}
205+
}, [channel, fetchHistory, initMessagesLoaded, messages.length, prevChannel, pubnub]);
200206

201207
useEffect(() => {
202208
if (!messages?.length || scrolledBottom) return;
@@ -230,5 +236,6 @@ export const useMessageListCore = (props: CommonMessageListProps) => {
230236
theme,
231237
unreadMessages,
232238
users,
239+
initMessagesLoaded,
233240
};
234241
};

packages/common/src/state-atoms.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ export const CurrentChannelMessagesAtom = atom(
3939
);
4040

4141
export const CurrentChannelPaginationAtom = atom(
42-
(get) => get(PaginationAtom)[get(CurrentChannelAtom)] || false,
43-
(get, set, value) =>
44-
set(
42+
(get) => {
43+
return get(PaginationAtom)[get(CurrentChannelAtom)] || false;
44+
},
45+
(get, set, value) => {
46+
return set(
4547
PaginationAtom,
4648
Object.assign({}, get(PaginationAtom), { [get(CurrentChannelAtom)]: value })
47-
)
49+
);
50+
}
4851
);
4952

5053
export const CurrentChannelTypingIndicatorAtom = atom(

packages/react/src/message-list/message-list.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export const MessageList: FC<MessageListProps> = (props: MessageListProps) => {
6565
theme,
6666
unreadMessages,
6767
users,
68+
initMessagesLoaded,
6869
} = useMessageListCore(props);
6970

7071
const lastMessageUniqueReactions = Object.keys(messages.slice(-1)[0]?.actions?.reaction || {});
@@ -136,7 +137,8 @@ export const MessageList: FC<MessageListProps> = (props: MessageListProps) => {
136137
* Lifecycle
137138
*/
138139
useEffect(() => {
139-
if (!isSpinnerVisible || wasSpinnerVisible || !messages.length || fetchingMessages) return;
140+
if (!isSpinnerVisible || wasSpinnerVisible || !initMessagesLoaded[channel] || fetchingMessages)
141+
return;
140142
fetchMoreHistory();
141143

142144
async function fetchMoreHistory() {
@@ -147,11 +149,11 @@ export const MessageList: FC<MessageListProps> = (props: MessageListProps) => {
147149
}
148150
}, [
149151
fetchHistory,
150-
fetchingMessages,
151152
isSpinnerVisible,
152-
messages,
153-
paginationEnd,
154153
wasSpinnerVisible,
154+
initMessagesLoaded,
155+
channel,
156+
fetchingMessages,
155157
]);
156158

157159
useEffect(() => {

samples/react/group-chat/src/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import "./index.css";
1515
const hash = document.location.hash.split("?")[1];
1616
const params = new URLSearchParams(hash);
1717
const uuid = params.get("uuid");
18+
1819
const pubnubKeys = {
1920
publishKey: params.get("pubkey") || (import.meta.env.REACT_APP_PUB_KEY as string),
2021
subscribeKey: params.get("subkey") || (import.meta.env.REACT_APP_SUB_KEY as string),

0 commit comments

Comments
 (0)