stream-chat-react 11.1.0 → 11.1.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
@@ -37186,7 +37186,7 @@ var UnMemoizedChannelList = function (props) {
37186
37186
  */
37187
37187
  var ChannelList = React__default["default"].memo(UnMemoizedChannelList);
37188
37188
 
37189
- var version = '11.1.0';
37189
+ var version = '11.1.1';
37190
37190
 
37191
37191
  var useChat = function (_a) {
37192
37192
  var _b, _c;
@@ -38756,11 +38756,14 @@ function useNewMessageNotification(messages, currentUserId, hasMoreNewer) {
38756
38756
  };
38757
38757
  }
38758
38758
 
38759
- var STATUSES_EXCLUDED_FROM_PREPEND = ['sending', 'failed'];
38759
+ var STATUSES_EXCLUDED_FROM_PREPEND = {
38760
+ failed: true,
38761
+ sending: true,
38762
+ };
38760
38763
  function usePrependedMessagesCount(messages, hasDateSeparator) {
38761
38764
  var firstRealMessageIndex = hasDateSeparator ? 1 : 0;
38762
- var firstMessageId = React.useRef();
38763
- var earliestMessageId = React.useRef();
38765
+ var firstMessageOnFirstLoadedPage = React.useRef();
38766
+ var previousFirstMessageOnFirstLoadedPage = React.useRef();
38764
38767
  var previousNumItemsPrepended = React.useRef(0);
38765
38768
  var numItemsPrepended = React.useMemo(function () {
38766
38769
  var _a, _b;
@@ -38768,47 +38771,43 @@ function usePrependedMessagesCount(messages, hasDateSeparator) {
38768
38771
  previousNumItemsPrepended.current = 0;
38769
38772
  return 0;
38770
38773
  }
38771
- var currentFirstMessageId = (_a = messages === null || messages === void 0 ? void 0 : messages[firstRealMessageIndex]) === null || _a === void 0 ? void 0 : _a.id;
38772
- // if no new messages were prepended, return early (same amount as before)
38773
- if (currentFirstMessageId === earliestMessageId.current) {
38774
+ var currentFirstMessage = messages === null || messages === void 0 ? void 0 : messages[firstRealMessageIndex];
38775
+ var noNewMessages = (currentFirstMessage === null || currentFirstMessage === void 0 ? void 0 : currentFirstMessage.id) === ((_a = previousFirstMessageOnFirstLoadedPage.current) === null || _a === void 0 ? void 0 : _a.id);
38776
+ // This is possible only, when sending messages very quickly (basically single char messages submitted like a crazy) in empty channel (first page)
38777
+ // Optimistic UI update, when sending messages, can lead to a situation, when
38778
+ // the order of the messages changes for a moment. This can happen, when a user
38779
+ // sends multiple messages withing few milliseconds. E.g. we send a message A
38780
+ // then message B. At first we have message array with both messages of status "sending"
38781
+ // then response for message A is received with a new - later - created_at timestamp
38782
+ // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageOnFirstLoadedPage.current
38783
+ // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
38784
+ // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
38785
+ // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
38786
+ // not considered as prepended messages.
38787
+ var firstMsgMovedAfterMessagesInExcludedStatus = (currentFirstMessage === null || currentFirstMessage === void 0 ? void 0 : currentFirstMessage.status) && STATUSES_EXCLUDED_FROM_PREPEND[currentFirstMessage.status];
38788
+ if (noNewMessages || firstMsgMovedAfterMessagesInExcludedStatus) {
38774
38789
  return previousNumItemsPrepended.current;
38775
38790
  }
38776
- if (!firstMessageId.current) {
38777
- firstMessageId.current = currentFirstMessageId;
38791
+ if (!firstMessageOnFirstLoadedPage.current) {
38792
+ firstMessageOnFirstLoadedPage.current = currentFirstMessage;
38778
38793
  }
38779
- earliestMessageId.current = currentFirstMessageId;
38794
+ previousFirstMessageOnFirstLoadedPage.current = currentFirstMessage;
38780
38795
  // if new messages were prepended, find out how many
38781
38796
  // start with this number because there cannot be fewer prepended items than before
38782
- var adjustPrependedMessageCount = 0;
38783
- for (var i = previousNumItemsPrepended.current; i < messages.length; i += 1) {
38784
- // Optimistic UI update, when sending messages, can lead to a situation, when
38785
- // the order of the messages changes for a moment. This can happen, when a user
38786
- // sends multiple messages withing few milliseconds. E.g. we send a message A
38787
- // then message B. At first we have message array with both messages of status "sending"
38788
- // then response for message A is received with a new - later - created_at timestamp
38789
- // this leads to rearrangement of 1.B ("sending"), 2.A ("received"). Still firstMessageId.current
38790
- // points to message A, but now this message has index 1 => previousNumItemsPrepended.current === 1
38791
- // That in turn leads to incorrect index calculation in VirtualizedMessageList trying to access a message
38792
- // at non-existent index. Therefore, we ignore messages of status "sending" / "failed" in order they are
38793
- // not considered as prepended messages.
38794
- if (((_b = messages[i]) === null || _b === void 0 ? void 0 : _b.status) &&
38795
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
38796
- STATUSES_EXCLUDED_FROM_PREPEND.includes(messages[i].status) &&
38797
- messages[i].id !== firstMessageId.current) {
38798
- adjustPrependedMessageCount++;
38799
- }
38800
- if (messages[i].id === firstMessageId.current) {
38801
- previousNumItemsPrepended.current = i - adjustPrependedMessageCount;
38802
- return previousNumItemsPrepended.current;
38797
+ for (var prependedMessageCount = previousNumItemsPrepended.current; prependedMessageCount < messages.length; prependedMessageCount += 1) {
38798
+ var messageIsFirstOnFirstLoadedPage = messages[prependedMessageCount].id === ((_b = firstMessageOnFirstLoadedPage.current) === null || _b === void 0 ? void 0 : _b.id);
38799
+ if (messageIsFirstOnFirstLoadedPage) {
38800
+ previousNumItemsPrepended.current = prependedMessageCount;
38801
+ return prependedMessageCount;
38803
38802
  }
38804
38803
  }
38805
38804
  // if no match has found, we have jumped - reset the prepended item count.
38806
- firstMessageId.current = currentFirstMessageId;
38805
+ firstMessageOnFirstLoadedPage.current = currentFirstMessage;
38807
38806
  previousNumItemsPrepended.current = 0;
38808
38807
  return 0;
38809
38808
  // TODO: there's a bug here, the messages prop is the same array instance (something mutates it)
38810
38809
  // that's why the second dependency is necessary
38811
- }, [messages, messages === null || messages === void 0 ? void 0 : messages.length]);
38810
+ }, [firstRealMessageIndex, messages, messages === null || messages === void 0 ? void 0 : messages.length]);
38812
38811
  return numItemsPrepended;
38813
38812
  }
38814
38813
 
package/dist/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const version = "11.1.0";
1
+ export declare const version = "11.1.1";
2
2
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export var version = '11.1.0';
1
+ export var version = '11.1.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stream-chat-react",
3
- "version": "11.1.0",
3
+ "version": "11.1.1",
4
4
  "description": "React components to create chat conversations or livestream style chat",
5
5
  "author": "GetStream",
6
6
  "homepage": "https://getstream.io/chat/",