Skip to content

Commit c9783d1

Browse files
committed
fix(web): edge-trigger older history auto-load
1 parent 2c17c8b commit c9783d1

3 files changed

Lines changed: 89 additions & 5 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.logic.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,47 @@ import {
44
computeMessageDurationStart,
55
deriveMessagesTimelineRows,
66
normalizeCompactToolLabel,
7+
resolveOlderHistoryAutoLoad,
78
resolveAssistantMessageCopyState,
89
} from "./MessagesTimeline.logic";
910

11+
describe("resolveOlderHistoryAutoLoad", () => {
12+
it("does not retry continuously while a failed request leaves the viewport at the start", () => {
13+
const first = resolveOlderHistoryAutoLoad({
14+
armed: true,
15+
hasMore: true,
16+
isAtStart: true,
17+
loading: false,
18+
});
19+
expect(first).toEqual({ armed: false, shouldLoad: true });
20+
21+
const afterFailure = resolveOlderHistoryAutoLoad({
22+
armed: first.armed,
23+
hasMore: true,
24+
isAtStart: true,
25+
loading: false,
26+
});
27+
expect(afterFailure).toEqual({ armed: false, shouldLoad: false });
28+
29+
const afterLeavingStart = resolveOlderHistoryAutoLoad({
30+
armed: afterFailure.armed,
31+
hasMore: true,
32+
isAtStart: false,
33+
loading: false,
34+
});
35+
expect(afterLeavingStart).toEqual({ armed: true, shouldLoad: false });
36+
37+
expect(
38+
resolveOlderHistoryAutoLoad({
39+
armed: afterLeavingStart.armed,
40+
hasMore: true,
41+
isAtStart: true,
42+
loading: false,
43+
}),
44+
).toEqual({ armed: false, shouldLoad: true });
45+
});
46+
});
47+
1048
describe("computeMessageDurationStart", () => {
1149
it("returns message createdAt when there is no preceding user message", () => {
1250
const result = computeMessageDurationStart([

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,33 @@ export interface TimelineEndState {
2121
readonly isNearEnd?: boolean;
2222
}
2323

24+
export interface OlderHistoryAutoLoadDecision {
25+
readonly armed: boolean;
26+
readonly shouldLoad: boolean;
27+
}
28+
29+
/**
30+
* Treat reaching the start as an edge, not a continuously-true condition.
31+
* A failed request leaves the viewport at the start, so level-triggered loading
32+
* would immediately retry on every render. Leaving the start rearms one future
33+
* automatic request; the visible header control remains available for explicit
34+
* retries while the edge is disarmed.
35+
*/
36+
export function resolveOlderHistoryAutoLoad(input: {
37+
readonly armed: boolean;
38+
readonly hasMore: boolean;
39+
readonly isAtStart: boolean;
40+
readonly loading: boolean;
41+
}): OlderHistoryAutoLoadDecision {
42+
if (!input.isAtStart) {
43+
return { armed: true, shouldLoad: false };
44+
}
45+
if (!input.armed || !input.hasMore || input.loading) {
46+
return { armed: input.armed, shouldLoad: false };
47+
}
48+
return { armed: false, shouldLoad: true };
49+
}
50+
2451
export function resolveTimelineIsAtEnd(state: TimelineEndState | undefined): boolean | undefined {
2552
return state?.isNearEnd ?? state?.isAtEnd;
2653
}

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
computeStableMessagesTimelineRows,
7070
deriveMessagesTimelineRows,
7171
normalizeCompactToolLabel,
72+
resolveOlderHistoryAutoLoad,
7273
resolveAssistantMessageCopyState,
7374
resolveTimelineIsAtEnd,
7475
resolveTimelineMinimapHasPersistentGutter,
@@ -329,6 +330,17 @@ export const MessagesTimeline = memo(function MessagesTimeline({
329330
null,
330331
);
331332
const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false);
333+
const olderHistoryAutoLoadArmedRef = useRef(true);
334+
const requestOlderHistory = useCallback(() => {
335+
// Disarm before both automatic and explicit requests. If a request fails,
336+
// prop changes while the viewport remains at the start must not trigger an
337+
// immediate retry loop; the header button still permits a deliberate retry.
338+
olderHistoryAutoLoadArmedRef.current = false;
339+
onLoadOlder?.();
340+
}, [onLoadOlder]);
341+
useEffect(() => {
342+
olderHistoryAutoLoadArmedRef.current = true;
343+
}, [routeThreadKey]);
332344
const handleAnchorReady = useCallback(
333345
(info: { anchorIndex: number | undefined }) => {
334346
if (anchorMessageId !== null && info.anchorIndex !== undefined) {
@@ -362,8 +374,15 @@ export const MessagesTimeline = memo(function MessagesTimeline({
362374
}
363375
// Reaching the top lazy-loads older history; maintainVisibleContentPosition
364376
// (set on the list) keeps the viewport anchored when rows prepend.
365-
if (state?.isAtStart && hasMoreOlder && !loadingOlder) {
366-
onLoadOlder?.();
377+
const olderHistoryDecision = resolveOlderHistoryAutoLoad({
378+
armed: olderHistoryAutoLoadArmedRef.current,
379+
hasMore: hasMoreOlder,
380+
isAtStart: state?.isAtStart ?? false,
381+
loading: loadingOlder,
382+
});
383+
olderHistoryAutoLoadArmedRef.current = olderHistoryDecision.armed;
384+
if (olderHistoryDecision.shouldLoad) {
385+
requestOlderHistory();
367386
}
368387
if (!state || minimapItems.length === 0) {
369388
return;
@@ -394,7 +413,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
394413
onIsAtEndChange,
395414
hasMoreOlder,
396415
loadingOlder,
397-
onLoadOlder,
416+
requestOlderHistory,
398417
]);
399418

400419
useEffect(() => {
@@ -438,15 +457,15 @@ export const MessagesTimeline = memo(function MessagesTimeline({
438457
return (
439458
<button
440459
type="button"
441-
onClick={onLoadOlder}
460+
onClick={requestOlderHistory}
442461
className="flex w-full cursor-pointer items-center justify-center py-2 text-xs text-muted-foreground transition-colors hover:text-foreground"
443462
>
444463
Load older history
445464
</button>
446465
);
447466
}
448467
return TIMELINE_LIST_HEADER;
449-
}, [loadingOlder, hasMoreOlder, onLoadOlder]);
468+
}, [loadingOlder, hasMoreOlder, requestOlderHistory]);
450469

451470
const sharedState = useMemo<TimelineRowSharedState>(
452471
() => ({

0 commit comments

Comments
 (0)