Skip to content

Commit 2c72910

Browse files
committed
chore: wip: virtuoso update
1 parent 50ee29c commit 2c72910

File tree

5 files changed

+43
-41
lines changed

5 files changed

+43
-41
lines changed

examples/vite/yarn.lock

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2253,14 +2253,19 @@ react-textarea-autosize@^8.3.0:
22532253
use-composed-ref "^1.3.0"
22542254
use-latest "^1.2.1"
22552255

2256-
react-virtuoso@^2.10.2, react-virtuoso@^2.16.5:
2256+
react-virtuoso@^2.10.2:
22572257
version "2.19.1"
22582258
resolved "https://registry.yarnpkg.com/react-virtuoso/-/react-virtuoso-2.19.1.tgz#a660a5c3cafcc7a84b59dfc356e1916e632c1e3a"
22592259
integrity sha512-zF6MAwujNGy2nJWCx/Df92ay/RnV2Kj4glUZfdyadI4suAn0kAZHB1BeI7yPFVp2iSccLzFlszhakWyr+fJ4Dw==
22602260
dependencies:
22612261
"@virtuoso.dev/react-urx" "^0.2.12"
22622262
"@virtuoso.dev/urx" "^0.2.12"
22632263

2264+
react-virtuoso@^4.12.3:
2265+
version "4.12.3"
2266+
resolved "https://registry.yarnpkg.com/react-virtuoso/-/react-virtuoso-4.12.3.tgz#beecf0582b31058c5a6ed3ec58fc43fd780e5844"
2267+
integrity sha512-6X1p/sU7hecmjDZMAwN+r3go9EVjofKhwkUbVlL8lXhBZecPv9XVCkZ/kBPYOr0Mv0Vl5+Ziwgexg9Kh7+NNXQ==
2268+
22642269
"react@link:../../node_modules/react":
22652270
version "0.0.0"
22662271
uid ""
@@ -2663,6 +2668,11 @@ use-latest@^1.2.1:
26632668
dependencies:
26642669
use-isomorphic-layout-effect "^1.1.1"
26652670

2671+
use-sync-external-store@^1.4.0:
2672+
version "1.4.0"
2673+
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.4.0.tgz#adbc795d8eeb47029963016cefdf89dc799fcebc"
2674+
integrity sha512-9WXSPC5fMv61vaupRkCKCxsPxBocVnwakBEkMIHHpkTTg6icbJtg6jzgtLDm4bl3cSHAca52rYWih0k4K3PfHw==
2675+
26662676
uvu@^0.5.0:
26672677
version "0.5.6"
26682678
resolved "https://registry.yarnpkg.com/uvu/-/uvu-0.5.6.tgz#2754ca20bcb0bb59b64e9985e84d2e81058502df"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"react-player": "2.10.1",
127127
"react-popper": "^2.3.0",
128128
"react-textarea-autosize": "^8.3.0",
129-
"react-virtuoso": "^2.16.5",
129+
"react-virtuoso": "^4.12.3",
130130
"remark-gfm": "^4.0.1",
131131
"textarea-caret": "^3.1.0",
132132
"tslib": "^2.6.2",

src/components/MessageList/VirtualizedMessageList.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,21 @@ const VirtualizedMessageListWithContext = <
420420

421421
const computeItemKey = useCallback<
422422
ComputeItemKey<UnknownType, VirtuosoContext<StreamChatGenerics>>
423-
>(
424-
(index, _, { numItemsPrepended, processedMessages }) =>
425-
processedMessages[calculateItemIndex(index, numItemsPrepended)].id,
426-
[],
427-
);
423+
>((index, _, { numItemsPrepended, processedMessages }) => {
424+
// Due to a bug in Virtuoso (https://github.com/petyosi/react-virtuoso/issues/1157),
425+
// it's possible that an item key will be requested with an out-of-bounds index.
426+
// Thankfully, this is fixed in subsequent re-render, so we can ignore it when it happens.
427+
const prependedIndex = calculateItemIndex(index, numItemsPrepended);
428+
const clampedIndex = Math.min(Math.max(prependedIndex, 0), processedMessages.length - 1);
429+
const key = processedMessages[clampedIndex].id;
430+
431+
// To avoid duplicate keys
432+
if (prependedIndex !== clampedIndex) {
433+
return `${key}-${prependedIndex}`;
434+
}
435+
436+
return key;
437+
}, []);
428438

429439
const atBottomStateChange = (isAtBottom: boolean) => {
430440
atBottom.current = isAtBottom;

src/components/MessageList/VirtualizedMessageListComponents.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ export const Item = <
5454
>({
5555
context,
5656
...props
57-
}: ItemProps & CommonVirtuosoComponentProps<StreamChatGenerics>) => {
57+
}: ItemProps<unknown> & CommonVirtuosoComponentProps<StreamChatGenerics>) => {
5858
if (!context) return <></>;
5959

60-
const message =
61-
context.processedMessages[
62-
calculateItemIndex(props['data-item-index'], context.numItemsPrepended)
63-
];
60+
// Due to a bug in Virtuoso (https://github.com/petyosi/react-virtuoso/issues/1157),
61+
// it's possible that an item will be rendered with an out-of-bounds index.
62+
// Thankfully, this is fixed in subsequent re-render, so we can ignore it when it happens.
63+
let clampedIndex = calculateItemIndex(props['data-item-index'], context.numItemsPrepended);
64+
clampedIndex = Math.min(Math.max(clampedIndex, 0), context.processedMessages.length - 1);
65+
const message = context.processedMessages[clampedIndex];
6466
const groupStyles: GroupStyle = context.messageGroupStyles[message.id];
6567

6668
return (

yarn.lock

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11039,14 +11039,19 @@ react-textarea-autosize@^8.3.0:
1103911039
use-composed-ref "^1.0.0"
1104011040
use-latest "^1.0.0"
1104111041

11042-
react-virtuoso@^2.10.2, react-virtuoso@^2.16.5:
11042+
react-virtuoso@^2.10.2:
1104311043
version "2.16.5"
1104411044
resolved "https://registry.yarnpkg.com/react-virtuoso/-/react-virtuoso-2.16.5.tgz#807b79ed378b9aa5430d45a94034afef5ab5afab"
1104511045
integrity sha512-bedbW1eJX/s0/XdDVxYZG1kLwXbLkpASskRQ4+UuvZ3mU0LWqDtjPsEzylXbOGhRaWeiuiG768BGl4zxQUBczg==
1104611046
dependencies:
1104711047
"@virtuoso.dev/react-urx" "^0.2.12"
1104811048
"@virtuoso.dev/urx" "^0.2.12"
1104911049

11050+
react-virtuoso@^4.12.3:
11051+
version "4.12.5"
11052+
resolved "https://registry.yarnpkg.com/react-virtuoso/-/react-virtuoso-4.12.5.tgz#cf92efc2527e56d6df1d4d63c6e4dd3fac5a4030"
11053+
integrity sha512-YeCbRRsC9CLf0buD0Rct7WsDbzf+yBU1wGbo05/XjbcN2nJuhgh040m3y3+6HVogTZxEqVm45ac9Fpae4/MxRQ==
11054+
1105011055
react@^19.0.0:
1105111056
version "19.0.0"
1105211057
resolved "https://registry.yarnpkg.com/react/-/react-19.0.0.tgz#6e1969251b9f108870aa4bff37a0ce9ddfaaabdd"
@@ -12179,16 +12184,7 @@ string-length@^4.0.1:
1217912184
char-regex "^1.0.2"
1218012185
strip-ansi "^6.0.0"
1218112186

12182-
"string-width-cjs@npm:string-width@^4.2.0":
12183-
version "4.2.3"
12184-
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
12185-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
12186-
dependencies:
12187-
emoji-regex "^8.0.0"
12188-
is-fullwidth-code-point "^3.0.0"
12189-
strip-ansi "^6.0.1"
12190-
12191-
string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
12187+
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3:
1219212188
version "4.2.3"
1219312189
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
1219412190
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -12296,7 +12292,7 @@ stringify-entities@^4.0.0:
1229612292
character-entities-html4 "^2.0.0"
1229712293
character-entities-legacy "^3.0.0"
1229812294

12299-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
12295+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
1230012296
version "6.0.1"
1230112297
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
1230212298
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -12310,13 +12306,6 @@ strip-ansi@^3.0.0:
1231012306
dependencies:
1231112307
ansi-regex "^2.0.0"
1231212308

12313-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
12314-
version "6.0.1"
12315-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
12316-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
12317-
dependencies:
12318-
ansi-regex "^5.0.1"
12319-
1232012309
strip-ansi@^7.0.1, strip-ansi@^7.1.0:
1232112310
version "7.1.0"
1232212311
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -13591,16 +13580,7 @@ wordwrap@^1.0.0:
1359113580
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
1359213581
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==
1359313582

13594-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
13595-
version "7.0.0"
13596-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
13597-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
13598-
dependencies:
13599-
ansi-styles "^4.0.0"
13600-
string-width "^4.1.0"
13601-
strip-ansi "^6.0.0"
13602-
13603-
wrap-ansi@^7.0.0:
13583+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
1360413584
version "7.0.0"
1360513585
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1360613586
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==

0 commit comments

Comments
 (0)