stream-chat-react 10.22.0 → 10.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"usePrependMessagesCount.d.ts","sourceRoot":"","sources":["../../../../../src/components/MessageList/hooks/VirtualizedMessageList/usePrependMessagesCount.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAE7E,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AAIzE,wBAAgB,yBAAyB,CACvC,kBAAkB,SAAS,yBAAyB,GAAG,yBAAyB,EAChF,QAAQ,EAAE,aAAa,CAAC,kBAAkB,CAAC,EAAE,EAAE,gBAAgB,EAAE,OAAO,UA2DzE"}
1
+ {"version":3,"file":"usePrependMessagesCount.d.ts","sourceRoot":"","sources":["../../../../../src/components/MessageList/hooks/VirtualizedMessageList/usePrependMessagesCount.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAE7E,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AAOzE,wBAAgB,yBAAyB,CACvC,kBAAkB,SAAS,yBAAyB,GAAG,yBAAyB,EAChF,QAAQ,EAAE,aAAa,CAAC,kBAAkB,CAAC,EAAE,EAAE,gBAAgB,EAAE,OAAO,UAgEzE"}
@@ -1,9 +1,12 @@
1
1
  import { useMemo, useRef } from 'react';
2
- var STATUSES_EXCLUDED_FROM_PREPEND = ['sending', 'failed'];
2
+ var STATUSES_EXCLUDED_FROM_PREPEND = {
3
+ failed: true,
4
+ sending: true,
5
+ };
3
6
  export function usePrependedMessagesCount(messages, hasDateSeparator) {
4
7
  var firstRealMessageIndex = hasDateSeparator ? 1 : 0;
5
- var firstMessageId = useRef();
6
- var earliestMessageId = useRef();
8
+ var firstMessageOnFirstLoadedPage = useRef();
9
+ var previousFirstMessageOnFirstLoadedPage = useRef();
7
10
  var previousNumItemsPrepended = useRef(0);
8
11
  var numItemsPrepended = useMemo(function () {
9
12
  var _a, _b;
@@ -11,46 +14,42 @@ export function usePrependedMessagesCount(messages, hasDateSeparator) {
11
14
  previousNumItemsPrepended.current = 0;
12
15
  return 0;
13
16
  }
14
- var currentFirstMessageId = (_a = messages === null || messages === void 0 ? void 0 : messages[firstRealMessageIndex]) === null || _a === void 0 ? void 0 : _a.id;
15
- // if no new messages were prepended, return early (same amount as before)
16
- if (currentFirstMessageId === earliestMessageId.current) {
17
+ var currentFirstMessage = messages === null || messages === void 0 ? void 0 : messages[firstRealMessageIndex];
18
+ var noNewMessages = (currentFirstMessage === null || currentFirstMessage === void 0 ? void 0 : currentFirstMessage.id) === ((_a = previousFirstMessageOnFirstLoadedPage.current) === null || _a === void 0 ? void 0 : _a.id);
19
+ // This is possible only, when sending messages very quickly (basically single char messages submitted like a crazy) in empty channel (first page)
20
+ // Optimistic UI update, when sending messages, can lead to a situation, when
21
+ // the order of the messages changes for a moment. This can happen, when a user
22
+ // sends multiple messages withing few milliseconds. E.g. we send a message A
23
+ // then message B. At first we have message array with both messages of status "sending"
24
+ // then response for message A is received with a new - later - created_at timestamp
25
+ // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageOnFirstLoadedPage.current
26
+ // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
27
+ // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
28
+ // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
29
+ // not considered as prepended messages.
30
+ var firstMsgMovedAfterMessagesInExcludedStatus = (currentFirstMessage === null || currentFirstMessage === void 0 ? void 0 : currentFirstMessage.status) && STATUSES_EXCLUDED_FROM_PREPEND[currentFirstMessage.status];
31
+ if (noNewMessages || firstMsgMovedAfterMessagesInExcludedStatus) {
17
32
  return previousNumItemsPrepended.current;
18
33
  }
19
- if (!firstMessageId.current) {
20
- firstMessageId.current = currentFirstMessageId;
34
+ if (!firstMessageOnFirstLoadedPage.current) {
35
+ firstMessageOnFirstLoadedPage.current = currentFirstMessage;
21
36
  }
22
- earliestMessageId.current = currentFirstMessageId;
37
+ previousFirstMessageOnFirstLoadedPage.current = currentFirstMessage;
23
38
  // if new messages were prepended, find out how many
24
39
  // start with this number because there cannot be fewer prepended items than before
25
- var adjustPrependedMessageCount = 0;
26
- for (var i = previousNumItemsPrepended.current; i < messages.length; i += 1) {
27
- // Optimistic UI update, when sending messages, can lead to a situation, when
28
- // the order of the messages changes for a moment. This can happen, when a user
29
- // sends multiple messages withing few milliseconds. E.g. we send a message A
30
- // then message B. At first we have message array with both messages of status "sending"
31
- // then response for message A is received with a new - later - created_at timestamp
32
- // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageId.current
33
- // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
34
- // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
35
- // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
36
- // not considered as prepended messages.
37
- if (((_b = messages[i]) === null || _b === void 0 ? void 0 : _b.status) &&
38
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
39
- STATUSES_EXCLUDED_FROM_PREPEND.includes(messages[i].status) &&
40
- messages[i].id !== firstMessageId.current) {
41
- adjustPrependedMessageCount++;
42
- }
43
- if (messages[i].id === firstMessageId.current) {
44
- previousNumItemsPrepended.current = i - adjustPrependedMessageCount;
45
- return previousNumItemsPrepended.current;
40
+ for (var prependedMessageCount = previousNumItemsPrepended.current; prependedMessageCount < messages.length; prependedMessageCount += 1) {
41
+ var messageIsFirstOnFirstLoadedPage = messages[prependedMessageCount].id === ((_b = firstMessageOnFirstLoadedPage.current) === null || _b === void 0 ? void 0 : _b.id);
42
+ if (messageIsFirstOnFirstLoadedPage) {
43
+ previousNumItemsPrepended.current = prependedMessageCount;
44
+ return prependedMessageCount;
46
45
  }
47
46
  }
48
47
  // if no match has found, we have jumped - reset the prepended item count.
49
- firstMessageId.current = currentFirstMessageId;
48
+ firstMessageOnFirstLoadedPage.current = currentFirstMessage;
50
49
  previousNumItemsPrepended.current = 0;
51
50
  return 0;
52
51
  // TODO: there's a bug here, the messages prop is the same array instance (something mutates it)
53
52
  // that's why the second dependency is necessary
54
- }, [messages, messages === null || messages === void 0 ? void 0 : messages.length]);
53
+ }, [firstRealMessageIndex, messages, messages === null || messages === void 0 ? void 0 : messages.length]);
55
54
  return numItemsPrepended;
56
55
  }
package/dist/index.cjs.js CHANGED
@@ -35160,7 +35160,7 @@ var UnMemoizedChannelList = function (props) {
35160
35160
  */
35161
35161
  var ChannelList = React__default["default"].memo(UnMemoizedChannelList);
35162
35162
 
35163
- var version = '10.22.0';
35163
+ var version = '10.22.1';
35164
35164
 
35165
35165
  var useChat = function (_a) {
35166
35166
  var _b, _c;
@@ -36730,11 +36730,14 @@ function useNewMessageNotification(messages, currentUserId, hasMoreNewer) {
36730
36730
  };
36731
36731
  }
36732
36732
 
36733
- var STATUSES_EXCLUDED_FROM_PREPEND = ['sending', 'failed'];
36733
+ var STATUSES_EXCLUDED_FROM_PREPEND = {
36734
+ failed: true,
36735
+ sending: true,
36736
+ };
36734
36737
  function usePrependedMessagesCount(messages, hasDateSeparator) {
36735
36738
  var firstRealMessageIndex = hasDateSeparator ? 1 : 0;
36736
- var firstMessageId = React.useRef();
36737
- var earliestMessageId = React.useRef();
36739
+ var firstMessageOnFirstLoadedPage = React.useRef();
36740
+ var previousFirstMessageOnFirstLoadedPage = React.useRef();
36738
36741
  var previousNumItemsPrepended = React.useRef(0);
36739
36742
  var numItemsPrepended = React.useMemo(function () {
36740
36743
  var _a, _b;
@@ -36742,47 +36745,43 @@ function usePrependedMessagesCount(messages, hasDateSeparator) {
36742
36745
  previousNumItemsPrepended.current = 0;
36743
36746
  return 0;
36744
36747
  }
36745
- var currentFirstMessageId = (_a = messages === null || messages === void 0 ? void 0 : messages[firstRealMessageIndex]) === null || _a === void 0 ? void 0 : _a.id;
36746
- // if no new messages were prepended, return early (same amount as before)
36747
- if (currentFirstMessageId === earliestMessageId.current) {
36748
+ var currentFirstMessage = messages === null || messages === void 0 ? void 0 : messages[firstRealMessageIndex];
36749
+ var noNewMessages = (currentFirstMessage === null || currentFirstMessage === void 0 ? void 0 : currentFirstMessage.id) === ((_a = previousFirstMessageOnFirstLoadedPage.current) === null || _a === void 0 ? void 0 : _a.id);
36750
+ // This is possible only, when sending messages very quickly (basically single char messages submitted like a crazy) in empty channel (first page)
36751
+ // Optimistic UI update, when sending messages, can lead to a situation, when
36752
+ // the order of the messages changes for a moment. This can happen, when a user
36753
+ // sends multiple messages withing few milliseconds. E.g. we send a message A
36754
+ // then message B. At first we have message array with both messages of status "sending"
36755
+ // then response for message A is received with a new - later - created_at timestamp
36756
+ // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageOnFirstLoadedPage.current
36757
+ // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
36758
+ // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
36759
+ // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
36760
+ // not considered as prepended messages.
36761
+ var firstMsgMovedAfterMessagesInExcludedStatus = (currentFirstMessage === null || currentFirstMessage === void 0 ? void 0 : currentFirstMessage.status) && STATUSES_EXCLUDED_FROM_PREPEND[currentFirstMessage.status];
36762
+ if (noNewMessages || firstMsgMovedAfterMessagesInExcludedStatus) {
36748
36763
  return previousNumItemsPrepended.current;
36749
36764
  }
36750
- if (!firstMessageId.current) {
36751
- firstMessageId.current = currentFirstMessageId;
36765
+ if (!firstMessageOnFirstLoadedPage.current) {
36766
+ firstMessageOnFirstLoadedPage.current = currentFirstMessage;
36752
36767
  }
36753
- earliestMessageId.current = currentFirstMessageId;
36768
+ previousFirstMessageOnFirstLoadedPage.current = currentFirstMessage;
36754
36769
  // if new messages were prepended, find out how many
36755
36770
  // start with this number because there cannot be fewer prepended items than before
36756
- var adjustPrependedMessageCount = 0;
36757
- for (var i = previousNumItemsPrepended.current; i < messages.length; i += 1) {
36758
- // Optimistic UI update, when sending messages, can lead to a situation, when
36759
- // the order of the messages changes for a moment. This can happen, when a user
36760
- // sends multiple messages withing few milliseconds. E.g. we send a message A
36761
- // then message B. At first we have message array with both messages of status "sending"
36762
- // then response for message A is received with a new - later - created_at timestamp
36763
- // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageId.current
36764
- // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
36765
- // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
36766
- // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
36767
- // not considered as prepended messages.
36768
- if (((_b = messages[i]) === null || _b === void 0 ? void 0 : _b.status) &&
36769
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
36770
- STATUSES_EXCLUDED_FROM_PREPEND.includes(messages[i].status) &&
36771
- messages[i].id !== firstMessageId.current) {
36772
- adjustPrependedMessageCount++;
36773
- }
36774
- if (messages[i].id === firstMessageId.current) {
36775
- previousNumItemsPrepended.current = i - adjustPrependedMessageCount;
36776
- return previousNumItemsPrepended.current;
36771
+ for (var prependedMessageCount = previousNumItemsPrepended.current; prependedMessageCount < messages.length; prependedMessageCount += 1) {
36772
+ var messageIsFirstOnFirstLoadedPage = messages[prependedMessageCount].id === ((_b = firstMessageOnFirstLoadedPage.current) === null || _b === void 0 ? void 0 : _b.id);
36773
+ if (messageIsFirstOnFirstLoadedPage) {
36774
+ previousNumItemsPrepended.current = prependedMessageCount;
36775
+ return prependedMessageCount;
36777
36776
  }
36778
36777
  }
36779
36778
  // if no match has found, we have jumped - reset the prepended item count.
36780
- firstMessageId.current = currentFirstMessageId;
36779
+ firstMessageOnFirstLoadedPage.current = currentFirstMessage;
36781
36780
  previousNumItemsPrepended.current = 0;
36782
36781
  return 0;
36783
36782
  // TODO: there's a bug here, the messages prop is the same array instance (something mutates it)
36784
36783
  // that's why the second dependency is necessary
36785
- }, [messages, messages === null || messages === void 0 ? void 0 : messages.length]);
36784
+ }, [firstRealMessageIndex, messages, messages === null || messages === void 0 ? void 0 : messages.length]);
36786
36785
  return numItemsPrepended;
36787
36786
  }
36788
36787