stream-chat-react 10.7.2 → 10.7.3

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/usePrependMessagesCount.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAE1E,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAEtE,wBAAgB,yBAAyB,CACvC,kBAAkB,SAAS,yBAAyB,GAAG,yBAAyB,EAChF,QAAQ,EAAE,aAAa,CAAC,kBAAkB,CAAC,EAAE,EAAE,gBAAgB,EAAE,OAAO,UAwCzE"}
1
+ {"version":3,"file":"usePrependMessagesCount.d.ts","sourceRoot":"","sources":["../../../../src/components/MessageList/hooks/usePrependMessagesCount.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAE1E,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAItE,wBAAgB,yBAAyB,CACvC,kBAAkB,SAAS,yBAAyB,GAAG,yBAAyB,EAChF,QAAQ,EAAE,aAAa,CAAC,kBAAkB,CAAC,EAAE,EAAE,gBAAgB,EAAE,OAAO,UA2DzE"}
@@ -1,11 +1,12 @@
1
1
  import { useMemo, useRef } from 'react';
2
+ var STATUSES_EXCLUDED_FROM_PREPEND = ['sending', 'failed'];
2
3
  export function usePrependedMessagesCount(messages, hasDateSeparator) {
3
4
  var firstRealMessageIndex = hasDateSeparator ? 1 : 0;
4
5
  var firstMessageId = useRef();
5
6
  var earliestMessageId = useRef();
6
7
  var previousNumItemsPrepended = useRef(0);
7
8
  var numItemsPrepended = useMemo(function () {
8
- var _a;
9
+ var _a, _b;
9
10
  if (!messages || !messages.length) {
10
11
  previousNumItemsPrepended.current = 0;
11
12
  return 0;
@@ -21,13 +22,30 @@ export function usePrependedMessagesCount(messages, hasDateSeparator) {
21
22
  earliestMessageId.current = currentFirstMessageId;
22
23
  // if new messages were prepended, find out how many
23
24
  // start with this number because there cannot be fewer prepended items than before
25
+ var adjustPrependedMessageCount = 0;
24
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
+ }
25
43
  if (messages[i].id === firstMessageId.current) {
26
- previousNumItemsPrepended.current = i;
27
- return i;
44
+ previousNumItemsPrepended.current = i - adjustPrependedMessageCount;
45
+ return previousNumItemsPrepended.current;
28
46
  }
29
47
  }
30
- // if no match has found, we have jumped - reset the prepend item count.
48
+ // if no match has found, we have jumped - reset the prepended item count.
31
49
  firstMessageId.current = currentFirstMessageId;
32
50
  previousNumItemsPrepended.current = 0;
33
51
  return 0;
package/dist/index.cjs.js CHANGED
@@ -33670,7 +33670,7 @@ var UnMemoizedChannelList = function (props) {
33670
33670
  */
33671
33671
  var ChannelList = React__default["default"].memo(UnMemoizedChannelList);
33672
33672
 
33673
- var version = '10.7.2';
33673
+ var version = '10.7.3';
33674
33674
 
33675
33675
  var useChat = function (_a) {
33676
33676
  var _b, _c;
@@ -35260,13 +35260,14 @@ function useNewMessageNotification(messages, currentUserId, hasMoreNewer) {
35260
35260
  };
35261
35261
  }
35262
35262
 
35263
+ var STATUSES_EXCLUDED_FROM_PREPEND = ['sending', 'failed'];
35263
35264
  function usePrependedMessagesCount(messages, hasDateSeparator) {
35264
35265
  var firstRealMessageIndex = hasDateSeparator ? 1 : 0;
35265
35266
  var firstMessageId = React.useRef();
35266
35267
  var earliestMessageId = React.useRef();
35267
35268
  var previousNumItemsPrepended = React.useRef(0);
35268
35269
  var numItemsPrepended = React.useMemo(function () {
35269
- var _a;
35270
+ var _a, _b;
35270
35271
  if (!messages || !messages.length) {
35271
35272
  previousNumItemsPrepended.current = 0;
35272
35273
  return 0;
@@ -35282,13 +35283,30 @@ function usePrependedMessagesCount(messages, hasDateSeparator) {
35282
35283
  earliestMessageId.current = currentFirstMessageId;
35283
35284
  // if new messages were prepended, find out how many
35284
35285
  // start with this number because there cannot be fewer prepended items than before
35286
+ var adjustPrependedMessageCount = 0;
35285
35287
  for (var i = previousNumItemsPrepended.current; i < messages.length; i += 1) {
35288
+ // Optimistic UI update, when sending messages, can lead to a situation, when
35289
+ // the order of the messages changes for a moment. This can happen, when a user
35290
+ // sends multiple messages withing few milliseconds. E.g. we send a message A
35291
+ // then message B. At first we have message array with both messages of status "sending"
35292
+ // then response for message A is received with a new - later - created_at timestamp
35293
+ // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageId.current
35294
+ // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
35295
+ // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
35296
+ // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
35297
+ // not considered as prepended messages.
35298
+ if (((_b = messages[i]) === null || _b === void 0 ? void 0 : _b.status) &&
35299
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
35300
+ STATUSES_EXCLUDED_FROM_PREPEND.includes(messages[i].status) &&
35301
+ messages[i].id !== firstMessageId.current) {
35302
+ adjustPrependedMessageCount++;
35303
+ }
35286
35304
  if (messages[i].id === firstMessageId.current) {
35287
- previousNumItemsPrepended.current = i;
35288
- return i;
35305
+ previousNumItemsPrepended.current = i - adjustPrependedMessageCount;
35306
+ return previousNumItemsPrepended.current;
35289
35307
  }
35290
35308
  }
35291
- // if no match has found, we have jumped - reset the prepend item count.
35309
+ // if no match has found, we have jumped - reset the prepended item count.
35292
35310
  firstMessageId.current = currentFirstMessageId;
35293
35311
  previousNumItemsPrepended.current = 0;
35294
35312
  return 0;