diff --git a/src/features/chat/ChatPanel.test.tsx b/src/features/chat/ChatPanel.test.tsx index f02435e..eb9cab9 100644 --- a/src/features/chat/ChatPanel.test.tsx +++ b/src/features/chat/ChatPanel.test.tsx @@ -136,6 +136,44 @@ it("releases every listener on unmount", async () => { }); }); +it("follows the newest message, and stops once the reader scrolls up", async () => { + // jsdom has no scrollTo, which is also why the component falls back to + // scrollTop. Install a spy so the intent is observable either way. + const scrollTo = vi.fn(); + Object.defineProperty(Element.prototype, "scrollTo", { + configurable: true, + writable: true, + value: scrollTo, + }); + + render(); + await flushListen(); + scrollTo.mockClear(); + + emitTauriEvent("connection-runtime-event", deltaEvent("第一段")); + await waitFor(() => expect(scrollTo).toHaveBeenCalled()); + + // Scrolling away from the bottom must stop the view from being yanked back. + const list = document.querySelector(".chat-message-list") as HTMLElement; + Object.defineProperty(list, "scrollHeight", { + value: 1000, + configurable: true, + }); + Object.defineProperty(list, "clientHeight", { + value: 200, + configurable: true, + }); + list.scrollTop = 0; + fireEvent.scroll(list); + + scrollTo.mockClear(); + emitTauriEvent("connection-runtime-event", deltaEvent("第二段")); + await waitFor(() => expect(screen.getByText(/第二段/)).toBeInTheDocument()); + expect(scrollTo).not.toHaveBeenCalled(); + + delete (Element.prototype as unknown as Record).scrollTo; +}); + it("keeps drafts on the slot they were typed in", async () => { await renderWithTabs(); diff --git a/src/features/chat/ChatPanel.tsx b/src/features/chat/ChatPanel.tsx index 4a2b500..00e26cb 100644 --- a/src/features/chat/ChatPanel.tsx +++ b/src/features/chat/ChatPanel.tsx @@ -102,6 +102,13 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { companion: false, operations: false, }); + const messageListRef = useRef(null); + /// Whether the view should follow new content. + /// + /// Starts true so opening a conversation lands on the newest message, and + /// stays true until the reader scrolls up — following unconditionally would + /// yank someone out of the history they were reading. + const followTail = useRef(true); const speechRecognition = useRef<{ start: () => void; stop: () => void; @@ -164,6 +171,38 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { setSlotError((current) => ({ ...current, [slot]: message })); }; + /// Distance from the bottom that still counts as "reading the newest". + const TAIL_THRESHOLD_PX = 80; + + const scrollToTail = useCallback((behavior: ScrollBehavior) => { + const list = messageListRef.current; + if (!list) return; + // `scrollTo` carries the smooth behaviour, but it is not universally + // available — jsdom has no implementation at all — so fall back to the + // property, which every engine supports. + if (typeof list.scrollTo === "function") { + list.scrollTo({ top: list.scrollHeight, behavior }); + } else { + list.scrollTop = list.scrollHeight; + } + }, []); + + // Follow the tail whenever the rendered messages change: a sent message, a + // streamed delta, or a freshly loaded history. The list had no scroll code at + // all, so every send left the new message below the fold. + useEffect(() => { + if (!followTail.current) return; + scrollToTail("smooth"); + }, [visibleMessages, scrollToTail]); + + // Opening a conversation, or switching slot or session, starts at the newest + // message rather than at the top. + useEffect(() => { + followTail.current = true; + const frame = window.requestAnimationFrame(() => scrollToTail("auto")); + return () => window.cancelAnimationFrame(frame); + }, [selectedSlot, historySessionId, scrollToTail]); + useEffect(() => { // `listen()` resolves after the effect may already have been cleaned up. // Assigning the unregister function unconditionally dropped it in that @@ -761,6 +800,13 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) {
{ + const list = event.currentTarget; + const distanceFromTail = + list.scrollHeight - list.scrollTop - list.clientHeight; + followTail.current = distanceFromTail <= TAIL_THRESHOLD_PX; + }} role="tabpanel" aria-label={`${selectedSlot} messages`} aria-busy={sending[selectedSlot]}