@cometchat/chat-uikit-react-native 5.2.4 → 5.2.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cometchat/chat-uikit-react-native",
3
- "version": "5.2.4",
3
+ "version": "5.2.5",
4
4
  "description": "Ready-to-use Chat UI Components for React Native",
5
5
  "main": "src/index",
6
6
  "module": "src/index",
@@ -823,6 +823,11 @@ export const CometChatMessageList = memo(
823
823
  const [loadingMessages, setLoadingMessages] = useState(false);
824
824
  /** this is required to prevent duplicate api calls. Cannot use state for this since this is being used in scrollHandler **/
825
825
  const loadingMessagesRef = useRef(false);
826
+ const loadingPrevMessagesRef = useRef(false);
827
+ // State for showing loading when navigating to far messages
828
+ const [navigationLoading, setNavigationLoading] = useState(false);
829
+ // State for showing bottom loading indicator when fetching next messages
830
+ const [bottomLoading, setBottomLoading] = useState(false);
826
831
  const [unreadCount, setUnreadCount] = useState(0);
827
832
  const [showMessageOptions, setShowMessageOptions] = useState<any[]>([]);
828
833
  const [ExtensionsComponent, setExtensionsComponent] = useState<JSX.Element | null>(null);
@@ -847,8 +852,8 @@ export const CometChatMessageList = memo(
847
852
  const bottomSheetRef = useRef<any>(null);
848
853
  const conversationId = useRef<string | null>(null);
849
854
  const streamSubscriptionRef = useRef<any>(null);
850
- let lastID = useRef(0);
851
- let reachedFirstMessage = useRef<boolean>(false);
855
+ const lastID = useRef(0);
856
+ const reachedFirstMessage = useRef<boolean>(false);
852
857
  const agenticParentMessageIdRef = useRef<string | undefined>(undefined);
853
858
  const [hasTargetMessageId, setHasTargetMessageId] = useState(false);
854
859
  const hasReachedBottomRef = useRef(true);
@@ -860,6 +865,21 @@ export const CometChatMessageList = memo(
860
865
  CometChat.markAsRead(messagesContentListRef.current[0]);
861
866
  setUnreadCount(0);
862
867
  };
868
+ const prevMsgListLengthRef = useRef<number>(0);
869
+ const aMessageWasSentByMeRef = useRef<boolean>(false);
870
+ const latestMessageRef = useRef<any | null>(null);
871
+
872
+ useEffect(() => {
873
+ if (messagesList.length === prevMsgListLengthRef.current + 1 && aMessageWasSentByMeRef.current) {
874
+ aMessageWasSentByMeRef.current = false;
875
+ setTimeout(() => {
876
+ messageListRef.current?.scrollToOffset({ offset: 0, animated: true });
877
+ }, 100);
878
+ }
879
+ prevMsgListLengthRef.current = messagesList.length;
880
+ }, [messagesList]);
881
+
882
+
863
883
 
864
884
  const newMsgIndicatorPressed = useCallback(async () => {
865
885
  if (hasTargetMessageId) {
@@ -868,6 +888,9 @@ export const CometChatMessageList = memo(
868
888
  messagesContentListRef.current = [];
869
889
  setMessagesList([]);
870
890
  setListState("loading");
891
+ reachedFirstMessage.current = false;
892
+ msgRequestBuilder.current?.setMessageId(0);
893
+ messageRequest.current = msgRequestBuilder.current?.build() || null;
871
894
  await getPreviousMessages();
872
895
  setTimeout(() => {
873
896
  scrollToBottom();
@@ -891,6 +914,10 @@ export const CometChatMessageList = memo(
891
914
  }, [onLoad, hasTargetMessageId]);
892
915
 
893
916
  const getPreviousMessages = async () => {
917
+ if (loadingPrevMessagesRef.current) {
918
+ return;
919
+ }
920
+ loadingPrevMessagesRef.current = true;
894
921
  // For agent chats (non-thread), don't fetch previous messages
895
922
  if (isAgenticUser() && !parentMessageId) {
896
923
  return;
@@ -963,18 +990,11 @@ export const CometChatMessageList = memo(
963
990
  }
964
991
  );
965
992
  if (previousMessagesFetched.length > 0) {
966
- const existingIds = new Set(messagesContentListRef.current.map(m => m.getId()));
967
- const newMessages = previousMessagesFetched.filter(m => !existingIds.has(m.getId()));
968
-
969
993
  messagesContentListRef.current = [
970
994
  ...messagesContentListRef.current,
971
- ...newMessages,
995
+ ...previousMessagesFetched,
972
996
  ];
973
- setMessagesList((prev) => {
974
- const prevIds = new Set(prev.map(m => m.getId()));
975
- const uniqueNew = previousMessagesFetched.filter(m => !prevIds.has(m.getId()));
976
- return [...prev, ...uniqueNew];
977
- });
997
+ setMessagesList(messagesContentListRef.current);
978
998
  }
979
999
  if (messagesContentListRef.current.length == 0) {
980
1000
  onEmpty && onEmpty();
@@ -993,6 +1013,8 @@ export const CometChatMessageList = memo(
993
1013
  loadingMessagesRef.current = false;
994
1014
  }
995
1015
  onError && onError(e);
1016
+ }).finally(() => {
1017
+ loadingPrevMessagesRef.current = false;
996
1018
  });
997
1019
  };
998
1020
 
@@ -1026,11 +1048,24 @@ export const CometChatMessageList = memo(
1026
1048
  }
1027
1049
 
1028
1050
  fetchingAroundIdRef.current = messageId; // Mark this message as being fetched
1029
- setListState("loading");
1030
- setHasTargetMessageId(true); // Track that we're navigating to a specific message
1051
+
1052
+ // Check if message exists in current list
1053
+ const messageExists = messagesContentListRef.current.find(msg =>
1054
+ String(msg.getId()) === String(messageId)
1055
+ );
1056
+
1057
+ if (messageExists) {
1058
+ setHasTargetMessageId(true);
1059
+ } else {
1060
+ // Message is far, show navigation loading
1061
+ setNavigationLoading(true);
1062
+ setHasTargetMessageId(true);
1063
+ }
1031
1064
 
1032
1065
  try {
1033
1066
  const targetMessageId = Number(messageId);
1067
+ msgRequestBuilder.current?.setMessageId(targetMessageId);
1068
+ messageRequest.current = msgRequestBuilder.current?.build() || null;
1034
1069
 
1035
1070
  // Get the target message first to ensure it exists
1036
1071
  const targetMessage = await CometChat.getMessageDetails(targetMessageId);
@@ -1039,48 +1074,39 @@ export const CometChatMessageList = memo(
1039
1074
  return;
1040
1075
  }
1041
1076
 
1042
- // Create request builder for previous messages (messages before the target)
1043
- let prevMessagesRequest = new CometChat.MessagesRequestBuilder()
1044
- .setLimit(15)
1045
- .setMessageId(targetMessageId);
1046
-
1047
1077
  // Create request builder for next messages (messages after the target)
1048
1078
  let nextMessagesRequest = new CometChat.MessagesRequestBuilder()
1049
1079
  .setLimit(15)
1050
1080
  .setMessageId(targetMessageId);
1051
1081
 
1052
1082
  if (user) {
1053
- prevMessagesRequest = prevMessagesRequest.setUID(user.getUid());
1054
1083
  nextMessagesRequest = nextMessagesRequest.setUID(user.getUid());
1055
1084
  }
1056
1085
 
1057
1086
  if (group) {
1058
- prevMessagesRequest = prevMessagesRequest.setGUID(group.getGuid());
1059
1087
  nextMessagesRequest = nextMessagesRequest.setGUID(group.getGuid());
1060
1088
  }
1061
1089
 
1062
1090
  if (parentMessageId) {
1063
- prevMessagesRequest = prevMessagesRequest.setParentMessageId(Number(parentMessageId));
1064
1091
  nextMessagesRequest = nextMessagesRequest.setParentMessageId(Number(parentMessageId));
1065
1092
  }
1066
1093
 
1067
1094
  // Fetch messages before and after the target message
1068
- const previousMessages = await prevMessagesRequest.build().fetchPrevious();
1069
1095
  const nextMessages = await nextMessagesRequest.build().fetchNext();
1070
1096
 
1071
1097
  // Combine messages: previous (reversed) + target + next
1072
1098
  const allMessages = [
1073
- ...previousMessages.reverse(),
1099
+ // ...previousMessages,
1074
1100
  targetMessage,
1075
1101
  ...nextMessages
1076
- ];
1077
-
1102
+ ].reverse();
1103
+
1078
1104
  // Remove duplicates and sort by timestamp (newest at top for FlatList)
1079
1105
  const uniqueMessages = allMessages
1080
- .filter((message, index, array) =>
1081
- array.findIndex(m => m.getId() === message.getId()) === index
1082
- )
1083
- .sort((a, b) => b.getSentAt() - a.getSentAt());
1106
+ // .filter((message, index, array) =>
1107
+ // array.findIndex(m => m.getId() === message.getId()) === index
1108
+ // )
1109
+ // .sort((a, b) => b.getSentAt() - a.getSentAt());
1084
1110
 
1085
1111
  if (uniqueMessages.length > 0) {
1086
1112
  messagesContentListRef.current = uniqueMessages;
@@ -1097,6 +1123,7 @@ export const CometChatMessageList = memo(
1097
1123
  setListState("error");
1098
1124
  onError && onError(error as CometChat.CometChatException);
1099
1125
  } finally {
1126
+ setNavigationLoading(false);
1100
1127
  fetchingAroundIdRef.current = null;
1101
1128
  }
1102
1129
  };
@@ -1591,7 +1618,7 @@ export const CometChatMessageList = memo(
1591
1618
  }
1592
1619
  };
1593
1620
 
1594
- const addToMessageList = (newMessage: CometChat.BaseMessage) => {
1621
+ const addToMessageList = async (newMessage: CometChat.BaseMessage) => {
1595
1622
  if (isAgenticUser() && !conversationId.current) {
1596
1623
  if (newMessage.getConversationId()) {
1597
1624
  conversationId.current = newMessage.getConversationId();
@@ -1610,28 +1637,30 @@ export const CometChatMessageList = memo(
1610
1637
  // ----------------------------------------------------------------------------------
1611
1638
  // Fix: Before inserting, check for an existing message with matching id OR muid.
1612
1639
  // If found, update it in place instead of inserting again.
1613
- try {
1614
- const incomingId = (newMessage as any)?.getId?.();
1615
- const incomingMuid = (newMessage as any)?.muid || (newMessage as any)?.getMuid?.();
1616
- const existingIndex = messagesContentListRef.current.findIndex((m: any) => {
1617
- const mid = m?.getId?.();
1618
- const mmuid = m?.muid || m?.getMuid?.();
1619
- return (
1620
- (incomingId && mid && String(mid) === String(incomingId)) ||
1621
- (incomingMuid && mmuid && String(mmuid) === String(incomingMuid))
1622
- );
1623
- });
1624
- if (existingIndex !== -1) {
1625
- // Update existing message instead of duplicating.
1626
- const updatedList = [...messagesContentListRef.current];
1627
- updatedList[existingIndex] = CommonUtils.clone(newMessage);
1628
- messagesContentListRef.current = updatedList;
1629
- onLoad && onLoad([...messagesContentListRef.current].reverse());
1630
- setMessagesList(updatedList);
1631
- return; // prevent duplicate insert
1640
+ if (!isAgenticUser) {
1641
+ try {
1642
+ const incomingId = (newMessage as any)?.getId?.();
1643
+ const incomingMuid = (newMessage as any)?.muid || (newMessage as any)?.getMuid?.();
1644
+ const existingIndex = messagesContentListRef.current.findIndex((m: any) => {
1645
+ const mid = m?.getId?.();
1646
+ const mmuid = m?.muid || m?.getMuid?.();
1647
+ return (
1648
+ (incomingId && mid && String(mid) === String(incomingId)) ||
1649
+ (incomingMuid && mmuid && String(mmuid) === String(incomingMuid))
1650
+ );
1651
+ });
1652
+ if (existingIndex !== -1) {
1653
+ // Update existing message instead of duplicating.
1654
+ const updatedList = [...messagesContentListRef.current];
1655
+ updatedList[existingIndex] = CommonUtils.clone(newMessage);
1656
+ messagesContentListRef.current = updatedList;
1657
+ onLoad && onLoad([...messagesContentListRef.current].reverse());
1658
+ setMessagesList(updatedList);
1659
+ return; // prevent duplicate insert
1660
+ }
1661
+ } catch (e) {
1662
+ console.log("Error in message de-duplication:", e);
1632
1663
  }
1633
- } catch (e) {
1634
- console.log('Error in message de-duplication:', e);
1635
1664
  }
1636
1665
 
1637
1666
  const isStreamMessage = (newMessage as any).isStreamMessage;
@@ -1658,8 +1687,20 @@ export const CometChatMessageList = memo(
1658
1687
  return;
1659
1688
  }
1660
1689
  }
1690
+ if (latestMessageRef.current !== null &&
1691
+ messagesContentListRef.current[0]?.id !== latestMessageRef.current.id
1692
+ && messagesContentListRef.current[0]?.muid !== latestMessageRef.current.muid
1693
+ ) {
1694
+ reachedFirstMessage.current = false;
1695
+ msgRequestBuilder.current?.setMessageId(0);
1696
+ messageRequest.current = msgRequestBuilder.current?.build() || null;
1697
+ messagesContentListRef.current = [];
1698
+ await getPreviousMessages();
1699
+ }
1661
1700
  messagesContentListRef.current = [newMessage, ...messagesContentListRef.current];
1662
1701
  onLoad && onLoad([...messagesContentListRef.current].reverse());
1702
+ latestMessageRef.current = newMessage;
1703
+ aMessageWasSentByMeRef.current = true;
1663
1704
  setMessagesList((prev) => [newMessage, ...prev]);
1664
1705
  };
1665
1706
 
@@ -1811,6 +1852,7 @@ export const CometChatMessageList = memo(
1811
1852
  } else {
1812
1853
  getPreviousMessages();
1813
1854
  }
1855
+ fetchLatestMessage();
1814
1856
  }
1815
1857
  })
1816
1858
  .catch((e: any) => {
@@ -1827,6 +1869,28 @@ export const CometChatMessageList = memo(
1827
1869
  };
1828
1870
  }, []);
1829
1871
 
1872
+
1873
+ const fetchLatestMessage = useCallback(() => {
1874
+ const messageRequestBuilder = new CometChat.MessagesRequestBuilder()
1875
+ .setLimit(1)
1876
+ .setMessageId(0);
1877
+
1878
+ if (user) {
1879
+ messageRequestBuilder.setUID(user.getUid());
1880
+ }
1881
+ if (group) {
1882
+ messageRequestBuilder.setGUID(group.getGuid());
1883
+ }
1884
+ messageRequestBuilder
1885
+ .build()
1886
+ .fetchPrevious()
1887
+ .then((messages) => {
1888
+ if (messages[0]) {
1889
+ latestMessageRef.current = messages[0];
1890
+ }
1891
+ });
1892
+ }, []);
1893
+
1830
1894
  // Callback to add stream message and register queue completion callback
1831
1895
  const createStreamMessage = useCallback(
1832
1896
  (userMessage: CometChat.BaseMessage) => {
@@ -2057,7 +2121,7 @@ export const CometChatMessageList = memo(
2057
2121
  messageEdited(editedMessage);
2058
2122
  },
2059
2123
  onMessageModerated: (moderatedMessage: any) => {
2060
- messageEdited(moderatedMessage);
2124
+ !isAgenticUser() && messageEdited(moderatedMessage);
2061
2125
  },
2062
2126
  onFormMessageReceived: (formMessage: any) => {
2063
2127
  newMessage(formMessage);
@@ -2673,8 +2737,8 @@ export const CometChatMessageList = memo(
2673
2737
  let _style = getCurrentBubbleStyle(item);
2674
2738
  const moderationStatus = getModerationStatus(item);
2675
2739
  const isOutgoing = item.getSender()?.getUid() === loggedInUser.current?.getUid();
2676
- if (isOutgoing && moderationStatus && moderationStatus !== "unmoderated") {
2677
- return undefined; // hide thread view if moderated
2740
+ if (isOutgoing && moderationStatus === "disapproved") {
2741
+ return undefined; // hide thread view if disapproved
2678
2742
  }
2679
2743
  return !isThreaded ? undefined : (
2680
2744
  <TouchableOpacity
@@ -2907,7 +2971,7 @@ export const CometChatMessageList = memo(
2907
2971
  if (effectiveHideModeration) {
2908
2972
  return hasTemplate?.BottomView && hasTemplate?.BottomView(message, bubbleAlignment);
2909
2973
  } else {
2910
- if (isOutgoing && moderationStatus === "disapproved") {
2974
+ if (isOutgoing && moderationStatus === "disapproved" && !isAgenticUser()) {
2911
2975
  return (
2912
2976
  <ModerationBottomView
2913
2977
  status={moderationStatus}
@@ -3004,7 +3068,7 @@ export const CometChatMessageList = memo(
3004
3068
 
3005
3069
  const moderationStatus = getModerationStatus(message);
3006
3070
  const isOutgoing = message.getSender()?.getUid() === loggedInUser.current?.getUid();
3007
- if (isOutgoing && moderationStatus && moderationStatus !== "approved") return;
3071
+ if (isOutgoing && moderationStatus && moderationStatus !== "approved" && moderationStatus !== "unmoderated") return;
3008
3072
 
3009
3073
  // Trigger reply if threshold is met
3010
3074
  if (translationX > swipeThreshold || velocityX > 500) {
@@ -3198,30 +3262,6 @@ export const CometChatMessageList = memo(
3198
3262
  });
3199
3263
  }, [messagesList]);
3200
3264
 
3201
- const showDateSeparatorAtIndex = useMemo(() => {
3202
- const n = messagesList.length;
3203
- const flags = new Array<boolean>(n).fill(false);
3204
- // helper to find next non-null key to the right
3205
- const nextNonNullKey = (startIdx: number) => {
3206
- for (let j = startIdx; j < n; j++) {
3207
- if (messageDayKeys[j] != null) return messageDayKeys[j];
3208
- }
3209
- return null;
3210
- };
3211
-
3212
- for (let i = 0; i < n; i++) {
3213
- const currKey = messageDayKeys[i] ?? nextNonNullKey(i);
3214
- const nextKey = i + 1 < n ? (messageDayKeys[i + 1] ?? nextNonNullKey(i + 1)) : null;
3215
- if (currKey != null && currKey !== nextKey) {
3216
- flags[i] = true;
3217
- } else if (i === n - 1 && currKey != null) {
3218
- // last message in list show header
3219
- flags[i] = true;
3220
- }
3221
- }
3222
- return flags;
3223
- }, [messageDayKeys, messagesList.length]);
3224
-
3225
3265
  const isSameLocalDay = (ms?: number) => {
3226
3266
  if (!ms) return false;
3227
3267
  const a = new Date(ms);
@@ -3261,10 +3301,13 @@ export const CometChatMessageList = memo(
3261
3301
  [mergedTheme, isAgenticUser, messagesList, getSentAtTimestamp, dateSeparatorPattern]
3262
3302
  );
3263
3303
 
3264
- const keyExtractor = useCallback((item: any, index: number) => {
3304
+ const keyExtractor = useCallback((item: any) => {
3265
3305
  const id = item?.getId();
3266
3306
  const muid = item?.getMuid();
3267
- return muid ? `${id}_${muid}` : `${id}_${index}`;
3307
+ if (!id && !muid) {
3308
+ console.log("item has no id or muid", item);
3309
+ }
3310
+ return `${id}_${muid}`;
3268
3311
  }, []);
3269
3312
 
3270
3313
  const itemSeparator = useCallback(() => <View style={{ height: 8 }} />, []);
@@ -3495,7 +3538,7 @@ export const CometChatMessageList = memo(
3495
3538
 
3496
3539
  const newestMessage = messagesContentListRef.current[0];
3497
3540
  const newestMessageId = newestMessage.getId();
3498
- setLoadingMessages(true);
3541
+ setBottomLoading(true);
3499
3542
  loadingMessagesRef.current = true;
3500
3543
 
3501
3544
  try {
@@ -3519,7 +3562,7 @@ export const CometChatMessageList = memo(
3519
3562
  const uniqueNewMessages = nextMessages.filter(m => !existingIds.has(m.getId()));
3520
3563
 
3521
3564
  if (uniqueNewMessages.length > 0) {
3522
- const reversedNewMessages = [...uniqueNewMessages].reverse();
3565
+ const reversedNewMessages = uniqueNewMessages.reverse();
3523
3566
  messagesContentListRef.current = [
3524
3567
  ...reversedNewMessages,
3525
3568
  ...messagesContentListRef.current
@@ -3535,11 +3578,20 @@ export const CometChatMessageList = memo(
3535
3578
  } catch (error) {
3536
3579
  console.error('[fetchNextMessages] Error:', error);
3537
3580
  } finally {
3538
- setLoadingMessages(false);
3581
+ setBottomLoading(false);
3539
3582
  loadingMessagesRef.current = false;
3540
3583
  }
3541
3584
  };
3542
3585
 
3586
+ const renderFooter = () => {
3587
+ if (!bottomLoading) return null;
3588
+ return (
3589
+ <View style={{ padding: 10, alignItems: "center" }}>
3590
+ <ActivityIndicator size='small' color={mergedTheme.color.primary} />
3591
+ </View>
3592
+ );
3593
+ };
3594
+
3543
3595
  const handleScroll = (event: any) => {
3544
3596
  if (loadingMessagesRef.current) {
3545
3597
  return;
@@ -3618,7 +3670,18 @@ export const CometChatMessageList = memo(
3618
3670
  const memoizedRenderItem = useCallback(
3619
3671
  ({ item, index, separators }: any) => {
3620
3672
  // index here is FlatList's index in messagesList (newest-first)
3621
- const showSeparator = showDateSeparatorAtIndex[index];
3673
+ let showSeparator = false;
3674
+ const nextItem = messagesContentListRef.current[index + 1];
3675
+ {
3676
+ const currentItemMs = sentAtToMs(item);
3677
+ const nextItemMs = sentAtToMs(nextItem);
3678
+ if (currentItemMs && nextItemMs) {
3679
+ showSeparator = new Date(currentItemMs).getDate() !== new Date(nextItemMs).getDate();
3680
+ }
3681
+ if (index === messagesContentListRef.current.length - 1 && messagesContentListRef.current.length < 30) {
3682
+ showSeparator = true;
3683
+ }
3684
+ }
3622
3685
  const ms = sentAtToMs(item) ?? Date.now();
3623
3686
  const isHighlighted = highlightedMessageId === String(item.getId());
3624
3687
 
@@ -3659,7 +3722,7 @@ export const CometChatMessageList = memo(
3659
3722
  </View>
3660
3723
  );
3661
3724
  },
3662
- [mergedTheme, showDateSeparatorAtIndex, dateSeparatorPattern, messageDayKeys, highlightedMessageId, highlightAnimatedValue]
3725
+ [mergedTheme, highlightedMessageId]
3663
3726
  );
3664
3727
 
3665
3728
  return (
@@ -3691,6 +3754,9 @@ export const CometChatMessageList = memo(
3691
3754
  <ActivityIndicator size='small' color={mergedTheme.color.primary} />
3692
3755
  </View>
3693
3756
  )}
3757
+
3758
+ {/* Navigation loading for far message fetching */}
3759
+ {navigationLoading && getLoadingStateView()}
3694
3760
 
3695
3761
  <FlatList
3696
3762
  showsVerticalScrollIndicator={false}
@@ -3698,6 +3764,13 @@ export const CometChatMessageList = memo(
3698
3764
  onMomentumScrollEnd={handleScroll}
3699
3765
  onScroll={shouldShowScrollToBottomButton}
3700
3766
  inverted={true}
3767
+ maintainVisibleContentPosition={{
3768
+ minIndexForVisible: 0,
3769
+ }}
3770
+ onEndReached={getPreviousMessages}
3771
+ onEndReachedThreshold={0.5}
3772
+ onStartReached={fetchNextMessages}
3773
+ onStartReachedThreshold={0.5}
3701
3774
  scrollEventThrottle={16}
3702
3775
  keyboardShouldPersistTaps={Platform.OS === "ios" ? "handled" : "always"}
3703
3776
  data={messagesList}
@@ -3706,6 +3779,7 @@ export const CometChatMessageList = memo(
3706
3779
  onLayout={() => {
3707
3780
  setIsListLayoutComplete(true);
3708
3781
  }}
3782
+ ListHeaderComponent={renderFooter}
3709
3783
  onScrollToIndexFailed={(error) => {
3710
3784
  const averageItemLength = error.averageItemLength || 120;
3711
3785
  const targetOffset = error.index * averageItemLength;