Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: prevent Markdown stream replay when vlist remounts streaming items (#14086)

Innei50ffa5b100a8
diff --git a/src/features/Conversation/ChatList/components/VirtualizedList.tsx b/src/features/Conversation/ChatList/components/VirtualizedList.tsx
index 4913f4a6ba9..9b3980dbe37 100644
--- a/src/features/Conversation/ChatList/components/VirtualizedList.tsx
+++ b/src/features/Conversation/ChatList/components/VirtualizedList.tsx
@@ -2,17 +2,24 @@
import isEqual from 'fast-deep-equal';
import { type ReactElement, type ReactNode } from 'react';
-import { memo, useCallback, useEffect, useRef } from 'react';
+import { memo, useCallback, useEffect, useMemo, useRef } from 'react';
import { type VListHandle } from 'virtua';
import { VList } from 'virtua';
+import { useShallow } from 'zustand/react/shallow';
import WideScreenContainer from '../../../WideScreenContainer';
-import { dataSelectors, useConversationStore, virtuaListSelectors } from '../../store';
+import {
+ dataSelectors,
+ messageStateSelectors,
+ useConversationStore,
+ virtuaListSelectors,
+} from '../../store';
import {
CONVERSATION_SPACER_TRANSITION_MS,
useConversationSpacer,
} from '../hooks/useConversationSpacer';
import { useScrollToUserMessage } from '../hooks/useScrollToUserMessage';
+import { useSelectionMessageIds } from '../hooks/useSelectionMessageIds';
import AutoScroll from './AutoScroll';
import { AT_BOTTOM_THRESHOLD } from './AutoScroll/const';
import DebugInspector, { OPEN_DEV_INSPECTOR } from './AutoScroll/DebugInspector';
@@ -144,6 +151,35 @@ const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent })
const secondLastMessage = displayMessages.at(-2);
const isSecondLastMessageFromUser = secondLastMessage?.role === 'user';
+ // Keep currently-streaming items mounted so vlist recycling never triggers
+ // Markdown animation replay when the user scrolls them back into view.
+ const streamingIndices = useConversationStore(
+ useShallow((s) => {
+ const indices: number[] = [];
+ for (let i = 0; i < dataSource.length; i++) {
+ const id = dataSource[i];
+ if (!id) continue;
+ if (messageStateSelectors.isMessageGenerating(id)(s)) indices.push(i);
+ }
+ return indices;
+ }),
+ );
+
+ // Also keep items that host the active text selection — unmounting a node
+ // containing a Selection endpoint would silently drop the user's highlight.
+ const selectionMessageIds = useSelectionMessageIds();
+
+ const keepMountedIndices = useMemo(() => {
+ if (selectionMessageIds.size === 0) return streamingIndices;
+ const merged = new Set<number>(streamingIndices);
+ for (let i = 0; i < dataSource.length; i++) {
+ const id = dataSource[i];
+ if (id && selectionMessageIds.has(id)) merged.add(i);
+ }
+ if (merged.size === streamingIndices.length) return streamingIndices;
+ return [...merged].sort((a, b) => a - b);
+ }, [dataSource, streamingIndices, selectionMessageIds]);
+
// Auto scroll to user message when user sends a new message
// Only scroll when 2 new messages are added and second-to-last is from user
useScrollToUserMessage({
@@ -172,6 +208,7 @@ const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent })
<VList
bufferSize={typeof window !== 'undefined' ? window.innerHeight : 0}
data={listData}
+ keepMounted={keepMountedIndices}
ref={virtuaRef}
style={{ height: '100%', overflowAnchor: 'none', paddingBottom: 24 }}
onScroll={handleScroll}
diff --git a/src/features/Conversation/ChatList/hooks/useSelectionMessageIds.ts b/src/features/Conversation/ChatList/hooks/useSelectionMessageIds.ts
new file mode 100644
index 00000000000..1025f0aa8d3
--- /dev/null
+++ b/src/features/Conversation/ChatList/hooks/useSelectionMessageIds.ts
@@ -0,0 +1,56 @@
+import { useEffect, useState } from 'react';
+
+const MESSAGE_ID_ATTR = 'data-message-id';
+
+const findMessageIdFromNode = (node: Node | null): string | null => {
+ let cur: Node | null = node;
+ while (cur) {
+ if (cur.nodeType === Node.ELEMENT_NODE) {
+ const id = (cur as HTMLElement).getAttribute?.(MESSAGE_ID_ATTR);
+ if (id) return id;
+ }
+ cur = cur.parentNode;
+ }
+ return null;
+};
+
+const setsEqual = (a: ReadonlySet<string>, b: ReadonlySet<string>): boolean => {
+ if (a.size !== b.size) return false;
+ for (const v of a) if (!b.has(v)) return false;
+ return true;
+};
+
+/**
+ * Track which message items currently contain the active text selection so
+ * the virtualized list can pin them with `keepMounted`. Recycling a node
+ * that hosts a Selection endpoint would drop the selection entirely.
+ */
+export const useSelectionMessageIds = (): ReadonlySet<string> => {
+ const [ids, setIds] = useState<ReadonlySet<string>>(() => new Set());
+
+ useEffect(() => {
+ const handler = () => {
+ const sel = typeof window === 'undefined' ? null : window.getSelection();
+ if (!sel || sel.isCollapsed || sel.rangeCount === 0) {
+ setIds((prev) => (prev.size === 0 ? prev : new Set()));
+ return;
+ }
+
+ const next = new Set<string>();
+ for (let i = 0; i < sel.rangeCount; i++) {
+ const range = sel.getRangeAt(i);
+ const startId = findMessageIdFromNode(range.startContainer);
+ const endId = findMessageIdFromNode(range.endContainer);
+ if (startId) next.add(startId);
+ if (endId) next.add(endId);
+ }
+
+ setIds((prev) => (setsEqual(prev, next) ? prev : next));
+ };
+
+ document.addEventListener('selectionchange', handler);
+ return () => document.removeEventListener('selectionchange', handler);
+ }, []);
+
+ return ids;
+};