fix(webui): unblock earlier-history pagination and show retry states (#5934)
* fix(webui): show history pagination loading and retry states * fix(webui): load earlier history on backward input at top * fix(webui): use pull gestures and spinners for history loading * fix(webui): shorten history loading failure messages * refactor(webui): simplify history pagination guards and tests
This commit is contained in:
1 parent
db6ef4d674
commit
33dddaaa46
17 files changed
+533
-28
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Loader2, RotateCcw } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
interface ThreadHistoryStatusProps {
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
pullDistance?: number;
|
||||
onRetry: () => void;
|
||||
}
|
||||
|
||||
export function ThreadHistoryStatus({ loading, error, onRetry, pullDistance = 0 }: ThreadHistoryStatusProps) {
|
||||
const { t } = useTranslation();
|
||||
const [showLoading, setShowLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading) {
|
||||
setShowLoading(false);
|
||||
return;
|
||||
}
|
||||
const timer = window.setTimeout(() => setShowLoading(true), 180);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [loading]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="thread-message-row pointer-events-none absolute inset-x-0 top-0 z-10 flex items-center justify-center"
|
||||
style={{ height: 48 + pullDistance }}
|
||||
>
|
||||
<div role="status" aria-live="polite" aria-atomic="true">
|
||||
{(loading && showLoading) || pullDistance > 0 ? (
|
||||
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-background/95 text-muted-foreground">
|
||||
<Loader2
|
||||
aria-hidden="true"
|
||||
className={`h-4 w-4 ${loading ? "animate-spin motion-reduce:animate-none" : ""}`}
|
||||
style={!loading ? { transform: `rotate(${pullDistance * 6}deg)`, opacity: Math.min(1, pullDistance / 24) } : undefined}
|
||||
/>
|
||||
{loading ? <span className="sr-only">{t("thread.history.loading")}</span> : null}
|
||||
</div>
|
||||
) : !loading && error ? (
|
||||
<div className="pointer-events-auto flex items-center gap-1 rounded-control bg-background/95 pl-3 text-xs text-muted-foreground">
|
||||
<span>{t("thread.history.error")}</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-11 w-11 shrink-0 p-0 sm:h-8 sm:w-8"
|
||||
aria-label={t("thread.history.retry")}
|
||||
title={t("thread.history.retry")}
|
||||
onClick={onRetry}
|
||||
>
|
||||
<RotateCcw aria-hidden="true" className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -683,6 +683,7 @@ export function ThreadShell({
|
||||
loading,
|
||||
error: historyError,
|
||||
loadingOlder,
|
||||
olderError,
|
||||
loadOlder,
|
||||
hasMoreBefore,
|
||||
userMessageOffset,
|
||||
@@ -1922,6 +1923,7 @@ export function ThreadShell({
|
||||
forkBoundaryMessageCount={forkBoundaryMessageCount}
|
||||
hasMoreBefore={hasMoreBefore}
|
||||
loadingOlder={loadingOlder}
|
||||
olderError={olderError}
|
||||
userMessageOffset={userMessageOffset}
|
||||
onLoadOlder={loadOlder}
|
||||
traceDetailScope={historyKey}
|
||||
|
||||
@@ -14,6 +14,7 @@ import { useTranslation } from "react-i18next";
|
||||
|
||||
import { PromptRail } from "@/components/thread/PromptRail";
|
||||
import { ThreadMessages } from "@/components/thread/ThreadMessages";
|
||||
import { ThreadHistoryStatus } from "@/components/thread/ThreadHistoryStatus";
|
||||
import { isAgentActivityMember } from "@/components/thread/AgentActivityCluster";
|
||||
import { ThreadCameraController } from "@/components/thread/thread-camera";
|
||||
import {
|
||||
@@ -54,6 +55,7 @@ interface ThreadViewportProps {
|
||||
forkBoundaryMessageCount?: number | null;
|
||||
hasMoreBefore?: boolean;
|
||||
loadingOlder?: boolean;
|
||||
olderError?: string | null;
|
||||
userMessageOffset?: number;
|
||||
onLoadOlder?: () => Promise<void> | void;
|
||||
traceDetailScope?: string | null;
|
||||
@@ -63,6 +65,7 @@ interface ThreadViewportProps {
|
||||
onQuoteSelection?: (text: string) => void;
|
||||
}
|
||||
|
||||
const HISTORY_PULL_THRESHOLD_PX = 48;
|
||||
const NEAR_BOTTOM_PX = 48;
|
||||
const PROMPT_TOP_INSET_PX = 48;
|
||||
const HISTORY_PREFETCH_MIN_PX = 160;
|
||||
@@ -245,6 +248,7 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
forkBoundaryMessageCount = null,
|
||||
hasMoreBefore = false,
|
||||
loadingOlder = false,
|
||||
olderError = null,
|
||||
userMessageOffset = 0,
|
||||
onLoadOlder,
|
||||
traceDetailScope = null,
|
||||
@@ -273,6 +277,22 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
const composerInputScrollTopRef = useRef<number | null>(null);
|
||||
const composerDockHeightRef = useRef(0);
|
||||
const [atBottom, setAtBottom] = useState(true);
|
||||
const [nearHistoryTop, setNearHistoryTop] = useState(false);
|
||||
const [historyPull, setHistoryPull] = useState(0);
|
||||
const historyInputRef = useRef<"touch" | "other">("other");
|
||||
const historyTouchRef = useRef<{
|
||||
lastY: number; startX: number; startY: number | null; triggered: boolean;
|
||||
} | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!loadingOlder || olderError || !hasMoreBefore) setHistoryPull(0);
|
||||
}, [loadingOlder, olderError, hasMoreBefore]);
|
||||
|
||||
useEffect(() => {
|
||||
historyTouchRef.current = null;
|
||||
historyInputRef.current = "other";
|
||||
setHistoryPull(0);
|
||||
}, [conversationKey]);
|
||||
const [composerDockHeight, setComposerDockHeight] = useState(0);
|
||||
const [keyboardInsetBottom, setKeyboardInsetBottom] = useState(0);
|
||||
const [hasVerticalOverflow, setHasVerticalOverflow] = useState(false);
|
||||
@@ -469,9 +489,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
if (!el || !hasMessages || pendingConversationScrollRef.current) return;
|
||||
if (!threadMotionRef.current?.isBrowsingHistory()) return;
|
||||
if (el.scrollTop > historyPrefetchDistance(el)) return;
|
||||
if (hiddenMessageCount <= 0 && !hasMoreBefore) return;
|
||||
if (hiddenMessageCount <= 0 && (historyInputRef.current === "touch" || el.scrollTop > 1)) return;
|
||||
if (hiddenMessageCount <= 0 && (!hasMoreBefore || olderError)) return;
|
||||
loadEarlierMessages();
|
||||
}, [hasMessages, hasMoreBefore, hiddenMessageCount, loadEarlierMessages]);
|
||||
}, [hasMessages, hasMoreBefore, hiddenMessageCount, loadEarlierMessages, olderError]);
|
||||
|
||||
const navigateToVisiblePrompt = useCallback((promptId: string) => {
|
||||
const scrollEl = scrollRef.current;
|
||||
@@ -738,6 +759,8 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
if (!el) return;
|
||||
|
||||
const onScroll = (allowHistoryLoad = true) => {
|
||||
setNearHistoryTop(el.scrollTop <= historyPrefetchDistance(el));
|
||||
if (el.scrollTop > 1) setHistoryPull(0);
|
||||
const distance = el.scrollHeight - el.scrollTop - el.clientHeight;
|
||||
const near = distance < NEAR_BOTTOM_PX;
|
||||
const owner = threadMotionRef.current?.observeScroll(near) ?? "automatic";
|
||||
@@ -755,10 +778,19 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
|
||||
onScroll(false);
|
||||
const handleScroll = () => onScroll(true);
|
||||
const canLoadAtTop = () => el.scrollTop <= 0 && hasMessages && conversationReady
|
||||
&& !pendingConversationScrollRef.current && !loadingOlder
|
||||
&& (hiddenMessageCount > 0 || (hasMoreBefore && !olderError));
|
||||
const handleDirectionalInput = (
|
||||
direction: ThreadScrollDirection | null,
|
||||
) => {
|
||||
if (!direction) return;
|
||||
// At the top (including a page too short to scroll), backward intent
|
||||
// produces no scroll event. It must still be able to reveal history.
|
||||
if (direction === "backward" && canLoadAtTop()) {
|
||||
loadEarlierMessages();
|
||||
return;
|
||||
}
|
||||
threadMotionRef.current?.handleUserScrollIntent(
|
||||
canScrollInDirection(el, direction),
|
||||
direction === "forward",
|
||||
@@ -772,6 +804,8 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
) {
|
||||
return;
|
||||
}
|
||||
historyInputRef.current = "other";
|
||||
setHistoryPull(0);
|
||||
handleDirectionalInput(directionFromDelta(event.deltaY));
|
||||
};
|
||||
const handlePointerDown = (event: PointerEvent) => {
|
||||
@@ -782,21 +816,64 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
yieldCameraToUser();
|
||||
}
|
||||
};
|
||||
let lastTouchY: number | null = null;
|
||||
const handleTouchCancel = () => {
|
||||
historyTouchRef.current = null;
|
||||
setHistoryPull(0);
|
||||
};
|
||||
const handleTouchStart = (event: TouchEvent) => {
|
||||
lastTouchY = event.touches[0]?.clientY ?? null;
|
||||
historyInputRef.current = "touch";
|
||||
const touch = event.touches[0];
|
||||
historyTouchRef.current = event.touches.length === 1 && touch
|
||||
? {
|
||||
lastY: touch.clientY,
|
||||
startX: touch.clientX,
|
||||
startY: el.scrollTop <= 0 ? touch.clientY : null,
|
||||
triggered: false,
|
||||
}
|
||||
: null;
|
||||
};
|
||||
const handleTouchMove = (event: TouchEvent) => {
|
||||
const currentY = event.touches[0]?.clientY;
|
||||
const scrollDeltaY =
|
||||
lastTouchY !== null && currentY !== undefined
|
||||
? lastTouchY - currentY
|
||||
: 0;
|
||||
lastTouchY = currentY ?? null;
|
||||
handleDirectionalInput(directionFromDelta(scrollDeltaY));
|
||||
const gesture = historyTouchRef.current;
|
||||
const touch = event.touches[0];
|
||||
if (event.touches.length !== 1) {
|
||||
handleTouchCancel();
|
||||
return;
|
||||
}
|
||||
if (!gesture || !touch || event.defaultPrevented) return;
|
||||
const delta = gesture.lastY - touch.clientY;
|
||||
gesture.lastY = touch.clientY;
|
||||
if (gesture.triggered) {
|
||||
if (el.scrollTop <= 0 && delta < 0 && event.cancelable) event.preventDefault();
|
||||
return;
|
||||
}
|
||||
if (el.scrollTop > 0) gesture.startY = null;
|
||||
if (canLoadAtTop() && hiddenMessageCount === 0) {
|
||||
gesture.startY ??= touch.clientY;
|
||||
const distance = touch.clientY - gesture.startY;
|
||||
if (distance > 0 && distance > Math.abs(touch.clientX - gesture.startX)) {
|
||||
if (event.cancelable) event.preventDefault();
|
||||
yieldCameraToUser();
|
||||
// Resistance exposes a small loading pocket without stretching the transcript.
|
||||
const pull = Math.min(HISTORY_PULL_THRESHOLD_PX, distance * 0.5);
|
||||
setHistoryPull(pull);
|
||||
setNearHistoryTop(true);
|
||||
if (pull >= HISTORY_PULL_THRESHOLD_PX) {
|
||||
gesture.triggered = true;
|
||||
loadEarlierMessages();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
setHistoryPull(0);
|
||||
// Locally windowed messages need no network gesture or loading feedback.
|
||||
if (hiddenMessageCount > 0) handleDirectionalInput(directionFromDelta(delta));
|
||||
else threadMotionRef.current?.handleUserScrollIntent(
|
||||
canScrollInDirection(el, directionFromDelta(delta)), delta > 0,
|
||||
);
|
||||
};
|
||||
const handleTouchEnd = () => {
|
||||
lastTouchY = null;
|
||||
historyTouchRef.current = null;
|
||||
if (!loadingOlder) setHistoryPull(0);
|
||||
};
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (
|
||||
@@ -816,14 +893,16 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
return;
|
||||
}
|
||||
if (isKeyboardControl(event.target as Element | null)) return;
|
||||
historyInputRef.current = "other";
|
||||
setHistoryPull(0);
|
||||
handleDirectionalInput(keyboardScrollDirection(event));
|
||||
};
|
||||
el.addEventListener("scroll", handleScroll, { passive: true });
|
||||
el.addEventListener("wheel", handleWheel, { passive: true });
|
||||
el.addEventListener("touchstart", handleTouchStart, { passive: true });
|
||||
el.addEventListener("touchmove", handleTouchMove, { passive: true });
|
||||
el.addEventListener("touchmove", handleTouchMove, { passive: false });
|
||||
el.addEventListener("touchend", handleTouchEnd, { passive: true });
|
||||
el.addEventListener("touchcancel", handleTouchEnd, { passive: true });
|
||||
el.addEventListener("touchcancel", handleTouchCancel, { passive: true });
|
||||
el.addEventListener("pointerdown", handlePointerDown);
|
||||
el.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
@@ -832,14 +911,20 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
el.removeEventListener("touchstart", handleTouchStart);
|
||||
el.removeEventListener("touchmove", handleTouchMove);
|
||||
el.removeEventListener("touchend", handleTouchEnd);
|
||||
el.removeEventListener("touchcancel", handleTouchEnd);
|
||||
el.removeEventListener("touchcancel", handleTouchCancel);
|
||||
el.removeEventListener("pointerdown", handlePointerDown);
|
||||
el.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [
|
||||
captureHistoryScrollAnchor,
|
||||
conversationReady,
|
||||
hasMessages,
|
||||
hasMoreBefore,
|
||||
hiddenMessageCount,
|
||||
loadingOlder,
|
||||
loadEarlierMessages,
|
||||
maybeLoadEarlierFromScroll,
|
||||
olderError,
|
||||
yieldCameraToUser,
|
||||
]);
|
||||
|
||||
@@ -875,9 +960,10 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
<div
|
||||
ref={messageRegionRef}
|
||||
data-testid="thread-message-region"
|
||||
style={{ transform: historyPull > 0 ? `translateY(${historyPull}px)` : undefined }}
|
||||
className={cn(
|
||||
"thread-message-viewport thread-viewport-scrollbar row-start-1 flex min-h-0 min-w-0 flex-col",
|
||||
"scroll-auto justify-start overflow-x-hidden pb-0 pt-[var(--thread-prompt-inset,3rem)]",
|
||||
"scroll-auto justify-start overflow-x-hidden overscroll-y-contain pb-0 pt-12",
|
||||
"[overflow-anchor:none] [scrollbar-width:none]",
|
||||
"[&::-webkit-scrollbar]:hidden",
|
||||
hasVerticalOverflow ? "overflow-y-auto" : "overflow-hidden",
|
||||
@@ -972,6 +1058,15 @@ export const ThreadViewport = forwardRef<ThreadViewportHandle, ThreadViewportPro
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
{hasMessages && conversationReady && nearHistoryTop && hasMoreBefore ? (
|
||||
<ThreadHistoryStatus
|
||||
key={conversationKey}
|
||||
loading={loadingOlder}
|
||||
pullDistance={historyPull}
|
||||
error={olderError}
|
||||
onRetry={loadEarlierMessages}
|
||||
/>
|
||||
) : null}
|
||||
{!hasMessages ? <div ref={bottomRef} aria-hidden className="h-px" /> : null}
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user