fix: retain session failures across recovery surfaces (#3669)
This commit is contained in:
@@ -45,7 +45,6 @@ const SCROLL_KEYS = new Set([
|
||||
"PageUp",
|
||||
" ",
|
||||
]);
|
||||
const RECOVERY_SCROLL_KEYS = new Set(["ArrowDown", "End", "PageDown", " "]);
|
||||
|
||||
/** Writes a clamped maximum so the browser resolves the native bottom without
|
||||
* forcing a synchronous scrollHeight layout read. */
|
||||
@@ -234,112 +233,31 @@ type PrependScrollState = {
|
||||
anchorTop: number | null;
|
||||
};
|
||||
|
||||
type RecoveryRevealState = {
|
||||
key: string | null;
|
||||
keepAtTop: boolean;
|
||||
};
|
||||
|
||||
/** Transfers recovery scroll ownership only after a real downward gesture. */
|
||||
function useRecoveryPrependScrollOwnership({
|
||||
/** Captures the reader's current position between older-history requests. */
|
||||
function usePrependScrollStateCapture({
|
||||
scrollRef,
|
||||
recoveryRevealStateRef,
|
||||
isLoadingMoreRef,
|
||||
olderLoadPendingRef,
|
||||
newestItemKeyRef,
|
||||
scrollState,
|
||||
}: {
|
||||
scrollRef: React.RefObject<HTMLDivElement | null>;
|
||||
recoveryRevealStateRef: React.MutableRefObject<RecoveryRevealState>;
|
||||
isLoadingMoreRef: React.MutableRefObject<boolean>;
|
||||
olderLoadPendingRef: React.MutableRefObject<boolean>;
|
||||
newestItemKeyRef: React.MutableRefObject<string | null>;
|
||||
scrollState: React.MutableRefObject<PrependScrollState>;
|
||||
}) {
|
||||
useEffect(() => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
let userScrollIntentUntil = 0;
|
||||
let pointerActive = false;
|
||||
let touchActive = false;
|
||||
let touchStartY: number | null = null;
|
||||
const markUserScrollIntent = () => {
|
||||
userScrollIntentUntil = Date.now() + USER_SCROLL_INTENT_WINDOW_MS;
|
||||
};
|
||||
const onWheel = (event: WheelEvent) => {
|
||||
if (event.deltaY > 0) markUserScrollIntent();
|
||||
};
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (RECOVERY_SCROLL_KEYS.has(event.key)) markUserScrollIntent();
|
||||
};
|
||||
const onPointerDown = () => {
|
||||
pointerActive = true;
|
||||
markUserScrollIntent();
|
||||
};
|
||||
const onPointerUp = () => {
|
||||
pointerActive = false;
|
||||
};
|
||||
const onTouchStart = (event: TouchEvent) => {
|
||||
touchActive = true;
|
||||
touchStartY = event.touches[0]?.clientY ?? null;
|
||||
};
|
||||
const onTouchMove = (event: TouchEvent) => {
|
||||
const nextY = event.touches[0]?.clientY;
|
||||
if (touchStartY !== null && nextY !== undefined && nextY < touchStartY) {
|
||||
markUserScrollIntent();
|
||||
}
|
||||
};
|
||||
const onTouchEnd = () => {
|
||||
touchActive = false;
|
||||
touchStartY = null;
|
||||
};
|
||||
const onScroll = () => {
|
||||
const recoveryScrolledAway =
|
||||
recoveryRevealStateRef.current.keepAtTop &&
|
||||
el.scrollTop > 0 &&
|
||||
(userScrollIntentUntil >= Date.now() || pointerActive || touchActive);
|
||||
if (recoveryScrolledAway) {
|
||||
recoveryRevealStateRef.current.keepAtTop = false;
|
||||
if (olderLoadPendingRef.current || isLoadingMoreRef.current) {
|
||||
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
|
||||
}
|
||||
}
|
||||
// Native overflow anchoring can adjust scrollTop while the older page is
|
||||
// being inserted. Keep the pre-request baseline until our layout effect
|
||||
// has restored the visual position explicitly.
|
||||
const element = scrollRef.current;
|
||||
if (!element) return;
|
||||
const capture = () => {
|
||||
if (olderLoadPendingRef.current || isLoadingMoreRef.current) return;
|
||||
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
|
||||
scrollState.current = capturePrependScrollState(element, newestItemKeyRef.current);
|
||||
};
|
||||
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
|
||||
el.addEventListener("wheel", onWheel, { passive: true });
|
||||
el.addEventListener("keydown", onKeyDown);
|
||||
el.addEventListener("pointerdown", onPointerDown, { passive: true });
|
||||
el.addEventListener("pointerup", onPointerUp, { passive: true });
|
||||
el.addEventListener("pointercancel", onPointerUp, { passive: true });
|
||||
el.addEventListener("touchstart", onTouchStart, { passive: true });
|
||||
el.addEventListener("touchmove", onTouchMove, { passive: true });
|
||||
el.addEventListener("touchend", onTouchEnd, { passive: true });
|
||||
el.addEventListener("touchcancel", onTouchEnd, { passive: true });
|
||||
el.addEventListener("scroll", onScroll, { passive: true });
|
||||
return () => {
|
||||
el.removeEventListener("wheel", onWheel);
|
||||
el.removeEventListener("keydown", onKeyDown);
|
||||
el.removeEventListener("pointerdown", onPointerDown);
|
||||
el.removeEventListener("pointerup", onPointerUp);
|
||||
el.removeEventListener("pointercancel", onPointerUp);
|
||||
el.removeEventListener("touchstart", onTouchStart);
|
||||
el.removeEventListener("touchmove", onTouchMove);
|
||||
el.removeEventListener("touchend", onTouchEnd);
|
||||
el.removeEventListener("touchcancel", onTouchEnd);
|
||||
el.removeEventListener("scroll", onScroll);
|
||||
};
|
||||
}, [
|
||||
isLoadingMoreRef,
|
||||
newestItemKeyRef,
|
||||
olderLoadPendingRef,
|
||||
recoveryRevealStateRef,
|
||||
scrollRef,
|
||||
scrollState,
|
||||
]);
|
||||
capture();
|
||||
element.addEventListener("scroll", capture, { passive: true });
|
||||
return () => element.removeEventListener("scroll", capture);
|
||||
}, [isLoadingMoreRef, newestItemKeyRef, olderLoadPendingRef, scrollRef, scrollState]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -359,7 +277,6 @@ function useScrollPositionOnPrepend(
|
||||
items: RenderItem[],
|
||||
isLoadingMore: boolean,
|
||||
isProgrammaticScrollLocked: () => boolean,
|
||||
recoveryRevealKey: string | null,
|
||||
): () => void {
|
||||
const scrollState = useRef<PrependScrollState>({
|
||||
scrollHeight: 0,
|
||||
@@ -370,22 +287,11 @@ function useScrollPositionOnPrepend(
|
||||
const isLoadingMoreRef = useRef(isLoadingMore);
|
||||
const olderLoadPendingRef = useRef(false);
|
||||
const newestItemKeyRef = useRef<string | null>(getNewestNonSyntheticItemKey(items));
|
||||
const recoveryRevealStateRef = useRef({
|
||||
key: recoveryRevealKey,
|
||||
keepAtTop: recoveryRevealKey !== null,
|
||||
});
|
||||
if (recoveryRevealStateRef.current.key !== recoveryRevealKey) {
|
||||
recoveryRevealStateRef.current = {
|
||||
key: recoveryRevealKey,
|
||||
keepAtTop: recoveryRevealKey !== null,
|
||||
};
|
||||
}
|
||||
isLoadingMoreRef.current = isLoadingMore;
|
||||
if (isLoadingMore) olderLoadPendingRef.current = true;
|
||||
newestItemKeyRef.current = getNewestNonSyntheticItemKey(items);
|
||||
useRecoveryPrependScrollOwnership({
|
||||
usePrependScrollStateCapture({
|
||||
scrollRef,
|
||||
recoveryRevealStateRef,
|
||||
isLoadingMoreRef,
|
||||
olderLoadPendingRef,
|
||||
newestItemKeyRef,
|
||||
@@ -417,11 +323,6 @@ function useScrollPositionOnPrepend(
|
||||
prevFirstKeyRef.current = nextFirstKey;
|
||||
if (olderLoadSettled) olderLoadPendingRef.current = false;
|
||||
if (!el || !prepend || isProgrammaticScrollLocked()) return;
|
||||
if (recoveryRevealStateRef.current.key !== null && recoveryRevealStateRef.current.keepAtTop) {
|
||||
if (el.scrollTop !== 0) el.scrollTop = 0;
|
||||
scrollState.current = capturePrependScrollState(el, newestItemKeyRef.current);
|
||||
return;
|
||||
}
|
||||
const prev = scrollState.current;
|
||||
const anchor = findMessageRow(el, prev.anchorKey);
|
||||
if (anchor && prev.anchorTop !== null) {
|
||||
@@ -1311,8 +1212,6 @@ type InitialScrollApplyParams = {
|
||||
isNearBottomRef: React.RefObject<boolean>;
|
||||
envSwitchPlacementToken: number | null;
|
||||
hasUnreadDivider: boolean;
|
||||
recoveryRevealKey: string | null;
|
||||
recoveryPlacementPendingRef: React.RefObject<boolean>;
|
||||
isProgrammaticScrollLocked: () => boolean;
|
||||
phase: "provisional" | "final";
|
||||
};
|
||||
@@ -1320,11 +1219,9 @@ type InitialScrollApplyParams = {
|
||||
function markInitialScrollConsumed(
|
||||
didInitialScroll: React.RefObject<boolean>,
|
||||
activationPendingRef: React.RefObject<boolean>,
|
||||
recoveryPlacementPendingRef: React.RefObject<boolean>,
|
||||
): void {
|
||||
didInitialScroll.current = true;
|
||||
activationPendingRef.current = false;
|
||||
recoveryPlacementPendingRef.current = false;
|
||||
}
|
||||
|
||||
function completeEnvSwitchPlacement(envSwitchPlacementToken: number | null): void {
|
||||
@@ -1348,30 +1245,23 @@ function isCurrentEnvSwitchPlacement(
|
||||
export type CompetingInitialScrollOwner =
|
||||
| "layout-restore"
|
||||
| "explicit-target"
|
||||
| "recovery"
|
||||
| "unread-divider"
|
||||
| "programmatic-scroll";
|
||||
|
||||
export function resolveCompetingInitialScrollOwner(params: {
|
||||
hasPendingLayoutRestore: boolean;
|
||||
hasExplicitScrollTarget: boolean;
|
||||
hasRecoveryReveal: boolean;
|
||||
hasUnreadDivider: boolean;
|
||||
isProgrammaticScrollLocked: () => boolean;
|
||||
}): CompetingInitialScrollOwner | null {
|
||||
if (params.hasPendingLayoutRestore) return "layout-restore";
|
||||
if (params.hasExplicitScrollTarget) return "explicit-target";
|
||||
if (params.hasRecoveryReveal) return "recovery";
|
||||
if (params.hasUnreadDivider) return "unread-divider";
|
||||
if (params.isProgrammaticScrollLocked()) return "programmatic-scroll";
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveInitialPlacementOwner(
|
||||
enabled: boolean,
|
||||
recoveryRevealKey: string | null,
|
||||
): "recovery" | "bottom" | "saved-position" {
|
||||
if (recoveryRevealKey !== null) return "recovery";
|
||||
function resolveInitialPlacementOwner(enabled: boolean): "bottom" | "saved-position" {
|
||||
return enabled ? "bottom" : "saved-position";
|
||||
}
|
||||
|
||||
@@ -1381,13 +1271,12 @@ function reportInitialPlacement(params: {
|
||||
sessionId: string | null;
|
||||
token: number | null;
|
||||
enabled: boolean;
|
||||
recoveryRevealKey: string | null;
|
||||
}): void {
|
||||
if (!isDebug()) return;
|
||||
placementDebug(`${params.phase} placement`, {
|
||||
sessionId: params.sessionId,
|
||||
token: params.token,
|
||||
owner: resolveInitialPlacementOwner(params.enabled, params.recoveryRevealKey),
|
||||
owner: resolveInitialPlacementOwner(params.enabled),
|
||||
scrollTop: params.element.scrollTop,
|
||||
scrollHeight: params.element.scrollHeight,
|
||||
clientHeight: params.element.clientHeight,
|
||||
@@ -1434,8 +1323,6 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
|
||||
isNearBottomRef,
|
||||
envSwitchPlacementToken,
|
||||
hasUnreadDivider,
|
||||
recoveryRevealKey,
|
||||
recoveryPlacementPendingRef,
|
||||
isProgrammaticScrollLocked,
|
||||
phase,
|
||||
} = params;
|
||||
@@ -1455,18 +1342,17 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
|
||||
const competingOwner = resolveCompetingInitialScrollOwner({
|
||||
hasPendingLayoutRestore,
|
||||
hasExplicitScrollTarget,
|
||||
hasRecoveryReveal: recoveryRevealKey !== null,
|
||||
hasUnreadDivider,
|
||||
isProgrammaticScrollLocked,
|
||||
});
|
||||
if (competingOwner && competingOwner !== "recovery") {
|
||||
if (competingOwner) {
|
||||
if (phase === "provisional") return;
|
||||
placementDebug(`${phase} placement delegated`, {
|
||||
sessionId,
|
||||
owner: competingOwner,
|
||||
token: envSwitchPlacementToken,
|
||||
});
|
||||
markInitialScrollConsumed(didInitialScroll, activationPendingRef, recoveryPlacementPendingRef);
|
||||
markInitialScrollConsumed(didInitialScroll, activationPendingRef);
|
||||
completeEnvSwitchPlacement(envSwitchPlacementToken);
|
||||
return;
|
||||
}
|
||||
@@ -1475,17 +1361,14 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
|
||||
getStoredAutoScrollTop(sessionId) ??
|
||||
undefined)
|
||||
: undefined;
|
||||
const scrollTop =
|
||||
competingOwner === "recovery"
|
||||
? 0
|
||||
: resolveNativeInitialScrollTop({
|
||||
enabled,
|
||||
hasPendingLayoutRestore,
|
||||
savedScrollTop,
|
||||
scrollHeight: element.scrollHeight,
|
||||
});
|
||||
const scrollTop = resolveNativeInitialScrollTop({
|
||||
enabled,
|
||||
hasPendingLayoutRestore,
|
||||
savedScrollTop,
|
||||
scrollHeight: element.scrollHeight,
|
||||
});
|
||||
if (phase === "final") {
|
||||
markInitialScrollConsumed(didInitialScroll, activationPendingRef, recoveryPlacementPendingRef);
|
||||
markInitialScrollConsumed(didInitialScroll, activationPendingRef);
|
||||
}
|
||||
if (scrollTop === null) {
|
||||
completeFinalEnvSwitchPlacement(phase, envSwitchPlacementToken);
|
||||
@@ -1507,7 +1390,6 @@ function applyInitialScrollPosition(params: InitialScrollApplyParams): void {
|
||||
sessionId,
|
||||
token: envSwitchPlacementToken,
|
||||
enabled,
|
||||
recoveryRevealKey,
|
||||
});
|
||||
finishAppliedInitialPlacement({
|
||||
phase,
|
||||
@@ -1528,31 +1410,16 @@ function shouldSkipProvisionalPlacement(
|
||||
return phase === "provisional" && (hasUnreadDivider || appliedToken === currentToken);
|
||||
}
|
||||
|
||||
function useInitialPlacementLatches(
|
||||
envSwitchPlacementToken: number | null,
|
||||
recoveryRevealKey: string | null,
|
||||
) {
|
||||
function useInitialPlacementLatches(envSwitchPlacementToken: number | null) {
|
||||
const didInitialScroll = useRef(false);
|
||||
const lastTokenRef = useRef<number | null>(null);
|
||||
const provisionalTokenRef = useRef<number | null>(null);
|
||||
const lastRecoveryRevealKeyRef = useRef<string | null>(null);
|
||||
const recoveryPlacementPendingRef = useRef(recoveryRevealKey !== null);
|
||||
if (envSwitchPlacementToken !== null && envSwitchPlacementToken !== lastTokenRef.current) {
|
||||
lastTokenRef.current = envSwitchPlacementToken;
|
||||
provisionalTokenRef.current = null;
|
||||
didInitialScroll.current = false;
|
||||
}
|
||||
if (recoveryRevealKey !== null && recoveryRevealKey !== lastRecoveryRevealKeyRef.current) {
|
||||
lastRecoveryRevealKeyRef.current = recoveryRevealKey;
|
||||
provisionalTokenRef.current = null;
|
||||
didInitialScroll.current = false;
|
||||
recoveryPlacementPendingRef.current = true;
|
||||
}
|
||||
if (recoveryRevealKey === null) {
|
||||
lastRecoveryRevealKeyRef.current = null;
|
||||
recoveryPlacementPendingRef.current = false;
|
||||
}
|
||||
return { didInitialScroll, provisionalTokenRef, recoveryPlacementPendingRef };
|
||||
return { didInitialScroll, provisionalTokenRef };
|
||||
}
|
||||
|
||||
type InitialPlacementLatches = ReturnType<typeof useInitialPlacementLatches>;
|
||||
@@ -1569,7 +1436,6 @@ type InitialScrollPositionParams = {
|
||||
envSwitchPlacementToken: number | null;
|
||||
isRestoringLayout: boolean;
|
||||
isProgrammaticScrollLocked: () => boolean;
|
||||
recoveryRevealKey: string | null;
|
||||
placementLatches: InitialPlacementLatches;
|
||||
};
|
||||
|
||||
@@ -1579,7 +1445,6 @@ type InitialScrollPositionEffectParams = Omit<InitialScrollPositionParams, "plac
|
||||
provisionalTokenRef: React.RefObject<number | null>;
|
||||
isVisibleRef: React.RefObject<boolean>;
|
||||
activationPendingRef: React.RefObject<boolean>;
|
||||
recoveryPlacementPendingRef: React.RefObject<boolean>;
|
||||
};
|
||||
|
||||
function runInitialScrollPositionEffect(
|
||||
@@ -1597,19 +1462,17 @@ function runInitialScrollPositionEffect(
|
||||
envSwitchPlacementToken,
|
||||
isRestoringLayout,
|
||||
isProgrammaticScrollLocked,
|
||||
recoveryRevealKey,
|
||||
storeApi,
|
||||
didInitialScroll,
|
||||
provisionalTokenRef,
|
||||
isVisibleRef,
|
||||
activationPendingRef,
|
||||
recoveryPlacementPendingRef,
|
||||
} = params;
|
||||
if (!isVisible || (envSwitchPlacementToken !== null && isRestoringLayout)) return;
|
||||
const hasEmptyTranscript = itemCount === 0 && recoveryRevealKey === null;
|
||||
const hasEmptyTranscript = itemCount === 0;
|
||||
if (envSwitchPlacementToken !== null && hasEmptyTranscript) {
|
||||
if (historyRefreshPending) return;
|
||||
markInitialScrollConsumed(didInitialScroll, activationPendingRef, recoveryPlacementPendingRef);
|
||||
markInitialScrollConsumed(didInitialScroll, activationPendingRef);
|
||||
completeEnvSwitchPlacement(envSwitchPlacementToken);
|
||||
return;
|
||||
}
|
||||
@@ -1640,8 +1503,6 @@ function runInitialScrollPositionEffect(
|
||||
isNearBottomRef,
|
||||
envSwitchPlacementToken,
|
||||
hasUnreadDivider,
|
||||
recoveryRevealKey,
|
||||
recoveryPlacementPendingRef,
|
||||
isProgrammaticScrollLocked,
|
||||
phase,
|
||||
});
|
||||
@@ -1664,11 +1525,10 @@ function useInitialScrollPosition({
|
||||
envSwitchPlacementToken,
|
||||
isRestoringLayout,
|
||||
isProgrammaticScrollLocked,
|
||||
recoveryRevealKey,
|
||||
placementLatches,
|
||||
}: InitialScrollPositionParams) {
|
||||
const storeApi = useAppStoreApi();
|
||||
const { didInitialScroll, provisionalTokenRef, recoveryPlacementPendingRef } = placementLatches;
|
||||
const { didInitialScroll, provisionalTokenRef } = placementLatches;
|
||||
const { isVisibleRef, activationPendingRef } = useActivationPending(
|
||||
isVisible,
|
||||
envSwitchPlacementToken,
|
||||
@@ -1687,13 +1547,11 @@ function useInitialScrollPosition({
|
||||
envSwitchPlacementToken,
|
||||
isRestoringLayout,
|
||||
isProgrammaticScrollLocked,
|
||||
recoveryRevealKey,
|
||||
storeApi,
|
||||
didInitialScroll,
|
||||
provisionalTokenRef,
|
||||
isVisibleRef,
|
||||
activationPendingRef,
|
||||
recoveryPlacementPendingRef,
|
||||
}),
|
||||
[
|
||||
itemCount,
|
||||
@@ -1707,7 +1565,6 @@ function useInitialScrollPosition({
|
||||
envSwitchPlacementToken,
|
||||
isRestoringLayout,
|
||||
isProgrammaticScrollLocked,
|
||||
recoveryRevealKey,
|
||||
scrollRef,
|
||||
],
|
||||
);
|
||||
@@ -1728,7 +1585,6 @@ type NativeScrollManagementParams = {
|
||||
isLoadingMore: boolean;
|
||||
loadMore: () => Promise<number>;
|
||||
isVisible: boolean;
|
||||
recoveryRevealKey?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1755,7 +1611,6 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
|
||||
isLoadingMore,
|
||||
loadMore,
|
||||
isVisible,
|
||||
recoveryRevealKey = null,
|
||||
} = params;
|
||||
const pendingChatInitialPlacement = useDockviewStore(
|
||||
(state) => state.pendingChatInitialPlacement,
|
||||
@@ -1763,9 +1618,8 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
|
||||
const isRestoringLayout = useDockviewStore((state) => state.isRestoringLayout);
|
||||
const envSwitchPlacementToken =
|
||||
pendingChatInitialPlacement?.sessionId === sessionId ? pendingChatInitialPlacement.token : null;
|
||||
const placementLatches = useInitialPlacementLatches(envSwitchPlacementToken, recoveryRevealKey);
|
||||
const initialPlacementPending =
|
||||
envSwitchPlacementToken !== null || placementLatches.recoveryPlacementPendingRef.current;
|
||||
const placementLatches = useInitialPlacementLatches(envSwitchPlacementToken);
|
||||
const initialPlacementPending = envSwitchPlacementToken !== null;
|
||||
const programmaticScrollLockRef = useRef(false);
|
||||
const isProgrammaticScrollLocked = useCallback(() => programmaticScrollLockRef.current, []);
|
||||
const { isNearBottomRef, resyncIsNearBottom, markNotNearBottom } = useAutoScroll({
|
||||
@@ -1790,7 +1644,6 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
|
||||
items,
|
||||
isLoadingMore,
|
||||
isProgrammaticScrollLocked,
|
||||
recoveryRevealKey,
|
||||
);
|
||||
const loadMoreWithPrependBaseline = useCallback(() => {
|
||||
beginOlderLoad();
|
||||
@@ -1825,7 +1678,6 @@ export function useNativeScrollManagement(params: NativeScrollManagementParams)
|
||||
envSwitchPlacementToken,
|
||||
isRestoringLayout,
|
||||
isProgrammaticScrollLocked,
|
||||
recoveryRevealKey,
|
||||
placementLatches,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user