From e1ed96a569c69261946e235f321b1c7d48ecb70d Mon Sep 17 00:00:00 2001 From: someone13574 <81528246+someone13574@users.noreply.github.com> Date: Fri, 16 Jun 2023 00:38:16 -0400 Subject: [PATCH] Fix out of bound index in chat paging (#3480) closes #3478 `useIsomorphicLayoutEffect` wasn't being called until the first render meaning if the index could be out of bounds if the parent message was changed to a tree with less siblings than the previous. A simple min against the number of siblings prevents the out of bound index and then `useIsomorphicLayoutEffect ` is called the next frame, setting the correct thread. --- website/src/components/Chat/ChatConversationTree.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/components/Chat/ChatConversationTree.tsx b/website/src/components/Chat/ChatConversationTree.tsx index afaffe2509..c83ec1f133 100644 --- a/website/src/components/Chat/ChatConversationTree.tsx +++ b/website/src/components/Chat/ChatConversationTree.tsx @@ -56,7 +56,7 @@ const TreeChildren = ({ } & StrictOmit) => { const sortedTrees = useMemo(() => trees.sort((a, b) => Date.parse(a.created_at) - Date.parse(b.created_at)), [trees]); const [index, setIndex] = useState(0); - const currentTree = sortedTrees[index]; + const currentTree = sortedTrees[Math.min(index, trees.length - 1)]; const length = trees.length; const hasSibling = length > 1;