Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3031dfd122 | ||
|
|
0cb4e75728 | ||
|
|
01accc005e | ||
|
|
0d9c26810f |
No files matched your search
@@ -1,11 +1,11 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { AUTO_SCROLL_BOTTOM_THRESHOLD_PX, isScrollContainerNearBottom } from "./chat-scroll";
|
||||
import { AUTO_SCROLL_BOTTOM_EPSILON_PX, isScrollContainerAtBottom } from "./chat-scroll";
|
||||
|
||||
describe("isScrollContainerNearBottom", () => {
|
||||
describe("isScrollContainerAtBottom", () => {
|
||||
it("returns true when already at bottom", () => {
|
||||
expect(
|
||||
isScrollContainerNearBottom({
|
||||
isScrollContainerAtBottom({
|
||||
scrollTop: 600,
|
||||
clientHeight: 400,
|
||||
scrollHeight: 1_000,
|
||||
@@ -13,31 +13,31 @@ describe("isScrollContainerNearBottom", () => {
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true when within the auto-scroll threshold", () => {
|
||||
it("returns true when within the bottom epsilon", () => {
|
||||
expect(
|
||||
isScrollContainerNearBottom({
|
||||
scrollTop: 540,
|
||||
isScrollContainerAtBottom({
|
||||
scrollTop: 599,
|
||||
clientHeight: 400,
|
||||
scrollHeight: 1_000,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when the user is meaningfully above the bottom", () => {
|
||||
it("returns false when the user is above the epsilon", () => {
|
||||
expect(
|
||||
isScrollContainerNearBottom({
|
||||
scrollTop: 520,
|
||||
isScrollContainerAtBottom({
|
||||
scrollTop: 597,
|
||||
clientHeight: 400,
|
||||
scrollHeight: 1_000,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("clamps negative thresholds to zero", () => {
|
||||
it("clamps negative epsilons to zero", () => {
|
||||
expect(
|
||||
isScrollContainerNearBottom(
|
||||
isScrollContainerAtBottom(
|
||||
{
|
||||
scrollTop: 539,
|
||||
scrollTop: 599,
|
||||
clientHeight: 400,
|
||||
scrollHeight: 1_000,
|
||||
},
|
||||
@@ -46,17 +46,27 @@ describe("isScrollContainerNearBottom", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("falls back to the default threshold for non-finite values", () => {
|
||||
it("falls back to the default epsilon for non-finite values", () => {
|
||||
expect(
|
||||
isScrollContainerNearBottom(
|
||||
isScrollContainerAtBottom(
|
||||
{
|
||||
scrollTop: 540,
|
||||
scrollTop: 599,
|
||||
clientHeight: 400,
|
||||
scrollHeight: 1_000,
|
||||
},
|
||||
Number.NaN,
|
||||
),
|
||||
).toBe(true);
|
||||
expect(AUTO_SCROLL_BOTTOM_THRESHOLD_PX).toBe(64);
|
||||
expect(AUTO_SCROLL_BOTTOM_EPSILON_PX).toBe(2);
|
||||
});
|
||||
|
||||
it("treats overscrolled values as bottom", () => {
|
||||
expect(
|
||||
isScrollContainerAtBottom({
|
||||
scrollTop: 610,
|
||||
clientHeight: 400,
|
||||
scrollHeight: 1_000,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
export const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 64;
|
||||
export const AUTO_SCROLL_BOTTOM_EPSILON_PX = 2;
|
||||
|
||||
interface ScrollPosition {
|
||||
scrollTop: number;
|
||||
@@ -6,19 +6,19 @@ interface ScrollPosition {
|
||||
scrollHeight: number;
|
||||
}
|
||||
|
||||
export function isScrollContainerNearBottom(
|
||||
export function isScrollContainerAtBottom(
|
||||
position: ScrollPosition,
|
||||
thresholdPx = AUTO_SCROLL_BOTTOM_THRESHOLD_PX,
|
||||
epsilonPx = AUTO_SCROLL_BOTTOM_EPSILON_PX,
|
||||
): boolean {
|
||||
const threshold = Number.isFinite(thresholdPx)
|
||||
? Math.max(0, thresholdPx)
|
||||
: AUTO_SCROLL_BOTTOM_THRESHOLD_PX;
|
||||
const epsilon = Number.isFinite(epsilonPx)
|
||||
? Math.max(0, epsilonPx)
|
||||
: AUTO_SCROLL_BOTTOM_EPSILON_PX;
|
||||
|
||||
const { scrollTop, clientHeight, scrollHeight } = position;
|
||||
if (![scrollTop, clientHeight, scrollHeight].every(Number.isFinite)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const distanceFromBottom = scrollHeight - clientHeight - scrollTop;
|
||||
return distanceFromBottom <= threshold;
|
||||
const distanceFromBottom = Math.max(0, scrollHeight - clientHeight - scrollTop);
|
||||
return distanceFromBottom <= epsilon;
|
||||
}
|
||||
@@ -17,6 +17,9 @@ import {
|
||||
type DragEvent,
|
||||
type FormEvent,
|
||||
type KeyboardEvent,
|
||||
type PointerEvent,
|
||||
type TouchEvent,
|
||||
type WheelEvent,
|
||||
memo,
|
||||
type RefObject,
|
||||
useCallback,
|
||||
@@ -61,7 +64,7 @@ import {
|
||||
formatElapsed,
|
||||
formatTimestamp,
|
||||
} from "../session-logic";
|
||||
import { isScrollContainerNearBottom } from "../chat-scroll";
|
||||
import { isScrollContainerAtBottom } from "../chat-scroll";
|
||||
import { useStore } from "../store";
|
||||
import { truncateTitle } from "../truncateTitle";
|
||||
import {
|
||||
@@ -402,6 +405,11 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
>(() => readLastInvokedScriptByProjectFromStorage());
|
||||
const messagesScrollRef = useRef<HTMLDivElement>(null);
|
||||
const shouldAutoScrollRef = useRef(true);
|
||||
const lastKnownScrollTopRef = useRef(0);
|
||||
const isPointerScrollActiveRef = useRef(false);
|
||||
const lastTouchClientYRef = useRef<number | null>(null);
|
||||
const pendingUserScrollUpIntentRef = useRef(false);
|
||||
const pendingAutoScrollFrameRef = useRef<number | null>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
const composerCommandInputRef = useRef<HTMLInputElement>(null);
|
||||
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
|
||||
@@ -1094,33 +1102,97 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
}
|
||||
}, [lastInvokedScriptByProjectId]);
|
||||
|
||||
// Auto-scroll on new messages
|
||||
const messageCount = activeThread?.messages.length ?? 0;
|
||||
// Auto-tail while the user stays at the bottom.
|
||||
const workLogCount = workLogEntries.length;
|
||||
const scrollMessagesToBottom = useCallback((behavior: ScrollBehavior = "auto") => {
|
||||
const scrollContainer = messagesScrollRef.current;
|
||||
if (!scrollContainer) return;
|
||||
scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior });
|
||||
lastKnownScrollTopRef.current = scrollContainer.scrollTop;
|
||||
shouldAutoScrollRef.current = true;
|
||||
}, []);
|
||||
const requestAutoTailToBottom = useCallback(() => {
|
||||
if (pendingAutoScrollFrameRef.current !== null) return;
|
||||
pendingAutoScrollFrameRef.current = window.requestAnimationFrame(() => {
|
||||
pendingAutoScrollFrameRef.current = null;
|
||||
if (!shouldAutoScrollRef.current) return;
|
||||
scrollMessagesToBottom();
|
||||
});
|
||||
}, [scrollMessagesToBottom]);
|
||||
const onMessagesScroll = useCallback(() => {
|
||||
const scrollContainer = messagesScrollRef.current;
|
||||
if (!scrollContainer) return;
|
||||
shouldAutoScrollRef.current = isScrollContainerNearBottom(scrollContainer);
|
||||
const currentScrollTop = scrollContainer.scrollTop;
|
||||
const atBottom = isScrollContainerAtBottom(scrollContainer);
|
||||
|
||||
if (!shouldAutoScrollRef.current && atBottom) {
|
||||
shouldAutoScrollRef.current = true;
|
||||
pendingUserScrollUpIntentRef.current = false;
|
||||
} else if (shouldAutoScrollRef.current && pendingUserScrollUpIntentRef.current) {
|
||||
const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1;
|
||||
if (scrolledUp) {
|
||||
shouldAutoScrollRef.current = false;
|
||||
}
|
||||
pendingUserScrollUpIntentRef.current = false;
|
||||
} else if (shouldAutoScrollRef.current && isPointerScrollActiveRef.current) {
|
||||
// Pointer-driven upward scroll means user takes control.
|
||||
const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1;
|
||||
if (scrolledUp) {
|
||||
shouldAutoScrollRef.current = false;
|
||||
}
|
||||
}
|
||||
|
||||
lastKnownScrollTopRef.current = currentScrollTop;
|
||||
}, []);
|
||||
const onMessagesWheel = useCallback((event: WheelEvent<HTMLDivElement>) => {
|
||||
// Wheel-up indicates user intent to move away from bottom.
|
||||
if (event.deltaY < 0) {
|
||||
pendingUserScrollUpIntentRef.current = true;
|
||||
}
|
||||
}, []);
|
||||
const onMessagesPointerDown = useCallback((_event: PointerEvent<HTMLDivElement>) => {
|
||||
isPointerScrollActiveRef.current = true;
|
||||
}, []);
|
||||
const onMessagesPointerUp = useCallback((_event: PointerEvent<HTMLDivElement>) => {
|
||||
isPointerScrollActiveRef.current = false;
|
||||
}, []);
|
||||
const onMessagesPointerCancel = useCallback((_event: PointerEvent<HTMLDivElement>) => {
|
||||
isPointerScrollActiveRef.current = false;
|
||||
}, []);
|
||||
const onMessagesTouchStart = useCallback((event: TouchEvent<HTMLDivElement>) => {
|
||||
const touch = event.touches[0];
|
||||
if (!touch) return;
|
||||
lastTouchClientYRef.current = touch.clientY;
|
||||
}, []);
|
||||
const onMessagesTouchMove = useCallback((event: TouchEvent<HTMLDivElement>) => {
|
||||
const touch = event.touches[0];
|
||||
if (!touch) return;
|
||||
const previousTouchY = lastTouchClientYRef.current;
|
||||
if (previousTouchY !== null && touch.clientY > previousTouchY + 1) {
|
||||
// Finger moved downward => user intends to scroll content upward.
|
||||
pendingUserScrollUpIntentRef.current = true;
|
||||
}
|
||||
lastTouchClientYRef.current = touch.clientY;
|
||||
}, []);
|
||||
const onMessagesTouchEnd = useCallback((_event: TouchEvent<HTMLDivElement>) => {
|
||||
lastTouchClientYRef.current = null;
|
||||
}, []);
|
||||
useLayoutEffect(() => {
|
||||
if (!activeThread?.id) return;
|
||||
scrollMessagesToBottom();
|
||||
}, [activeThread?.id, scrollMessagesToBottom]);
|
||||
useEffect(() => {
|
||||
useLayoutEffect(() => {
|
||||
if (!shouldAutoScrollRef.current) return;
|
||||
scrollMessagesToBottom("smooth");
|
||||
}, [messageCount, scrollMessagesToBottom]);
|
||||
useEffect(() => {
|
||||
if (phase !== "running") return;
|
||||
if (!shouldAutoScrollRef.current) return;
|
||||
scrollMessagesToBottom("smooth");
|
||||
}, [phase, workLogCount, scrollMessagesToBottom]);
|
||||
requestAutoTailToBottom();
|
||||
}, [activeThread?.messages, workLogCount, requestAutoTailToBottom]);
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (pendingAutoScrollFrameRef.current === null) return;
|
||||
window.cancelAnimationFrame(pendingAutoScrollFrameRef.current);
|
||||
pendingAutoScrollFrameRef.current = null;
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setExpandedWorkGroups({});
|
||||
@@ -2050,6 +2122,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
ref={messagesScrollRef}
|
||||
className="min-h-0 flex-1 overflow-y-auto px-5 py-4"
|
||||
onScroll={onMessagesScroll}
|
||||
onWheel={onMessagesWheel}
|
||||
onPointerDown={onMessagesPointerDown}
|
||||
onPointerUp={onMessagesPointerUp}
|
||||
onPointerCancel={onMessagesPointerCancel}
|
||||
onTouchStart={onMessagesTouchStart}
|
||||
onTouchMove={onMessagesTouchMove}
|
||||
onTouchEnd={onMessagesTouchEnd}
|
||||
onTouchCancel={onMessagesTouchEnd}
|
||||
>
|
||||
<MessagesTimeline
|
||||
hasMessages={activeThread.messages.length > 0}
|
||||
|
||||
Reference in new issue
Block a user