Skip to content
TypeScript

fix(mobile): preserve Home row content across recycling (#14004)

AKolenda760cdecd05d5
diff --git a/apps/mobile/src/features/home/HomeScreen.tsx b/apps/mobile/src/features/home/HomeScreen.tsx
index e90fc1672bc..eb31946d1f5 100644
--- a/apps/mobile/src/features/home/HomeScreen.tsx
+++ b/apps/mobile/src/features/home/HomeScreen.tsx
@@ -137,7 +137,7 @@ interface HomeScreenProps {
}
const ESTIMATED_THREAD_LIST_V2_ROW_HEIGHT = 72;
-// Rows away from the viewport are cheap dormant frames (see
+// Rows away from the viewport skip their swipe actions (see
// swipe-row-activation), so render further ahead: a fast fling then reaches
// rows that are already built instead of rows still being rebuilt.
const THREAD_LIST_V2_DRAW_DISTANCE = 1_000;
diff --git a/apps/mobile/src/features/home/swipe-row-activation.ts b/apps/mobile/src/features/home/swipe-row-activation.ts
index 1d942294661..40da6157ec3 100644
--- a/apps/mobile/src/features/home/swipe-row-activation.ts
+++ b/apps/mobile/src/features/home/swipe-row-activation.ts
@@ -1,16 +1,13 @@
import { createContext, use, useSyncExternalStore } from "react";
/**
- * Full swipe rows (pan gesture, animated actions, hidden action buttons) only
- * exist around the viewport. Every other Home row renders a dormant frame that
- * paints the same content with a fraction of the native views, so a row the
- * list rebuilds while scrolling is cheap.
+ * Only rows around the viewport enable swipe gestures and render actions.
+ * Dormant rows keep the same child views so waking them preserves touches.
*/
export function createSwipeRowActivation() {
let activeKeys = new Set<string>();
- // Swapping a row's frame remounts it, which would cancel a press or long
- // press in progress, so changes wait until every finger that started on the
- // list has lifted.
+ // Turning a row's swipe gesture off ends a swipe in progress, so changes
+ // wait until every finger that started on the list has lifted.
const listTouches = new Set<string>();
let pendingKeys: ReadonlyArray<string> | null = null;
const listeners = new Set<() => void>();
diff --git a/apps/mobile/src/features/home/thread-swipe-actions.tsx b/apps/mobile/src/features/home/thread-swipe-actions.tsx
index dad92ab65be..a320e7b21e3 100644
--- a/apps/mobile/src/features/home/thread-swipe-actions.tsx
+++ b/apps/mobile/src/features/home/thread-swipe-actions.tsx
@@ -8,6 +8,7 @@ import {
useCallback,
useEffect,
useLayoutEffect,
+ useMemo,
useRef,
useState,
type ComponentProps,
@@ -249,31 +250,8 @@ interface ThreadSwipeableProps {
readonly threadTitle: string;
}
-const closeDormant = () => {};
-
export function ThreadSwipeable(props: ThreadSwipeableProps) {
- if (props.dormant) {
- return (
- <View
- style={[
- { overflow: "hidden", backgroundColor: props.backgroundColor },
- props.containerStyle,
- ]}
- >
- <View style={{ backgroundColor: props.backgroundColor }}>
- {props.children(closeDormant)}
- </View>
- </View>
- );
- }
- // Recycled content gets fresh native and animation state. Late callbacks
- // from the previous row retain its action, never the replacement's action.
- return <ThreadSwipeableRow key={props.resetKey} {...props} />;
-}
-
-function ThreadSwipeableRow(props: ThreadSwipeableProps) {
const swipeableRef = useRef<SwipeableMethods | null>(null);
- const fullSwipeArmedRef = useRef(false);
const hasSecondaryAction = props.secondaryAction !== null;
const actionsWidth = swipeActionsWidth(hasSecondaryAction);
const fullSwipeThreshold = Math.max(actionsWidth + 44, props.fullSwipeWidth * 0.58);
@@ -283,8 +261,14 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
const gateEnabled = use(SwipeableScrollGateContext);
const mountedRef = useRef(true);
const dismissalRef = useRef<{ finished: Promise<void>; restore: () => void } | null>(null);
- const pendingDismissRef = useRef<(() => void) | null>(null);
+ const pendingDismissRef = useRef<{ id: number; resolve: () => void } | null>(null);
+ const dismissalIdRef = useRef(0);
const activeTranslationRef = useRef<SharedValue<number> | null>(null);
+ const content = useMemo(
+ () => ({ resetKey: props.resetKey, threadKey: props.threadKey }),
+ [props.resetKey, props.threadKey],
+ );
+ const contentRef = useRef(content);
const [isDismissing, setIsDismissing] = useState(false);
const dismissing = useSharedValue(false);
const rowHeight = useSharedValue(0);
@@ -293,23 +277,31 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
const fallbackTranslation = useSharedValue(0);
const actionOpacity = useSharedValue(1);
const primaryAction = props.primaryAction;
+ const onDelete = props.onDelete;
const onSwipeableClose = props.onSwipeableClose;
const restoreRow = useCallback(() => {
if (!mountedRef.current) return;
dismissalRef.current = null;
- swipeableRef.current?.reset();
- fallbackTranslation.set(0);
- collapse.set(0);
- actionOpacity.set(1);
- dismissing.set(false);
+ // RN-to-UI jobs run in order, so this also resets a queued dismissal.
+ runOnUI((methods: SwipeableMethods | null) => {
+ "worklet";
+ methods?.reset();
+ // RNGH reset() leaves tap-to-close enabled; close() clears it.
+ methods?.close();
+ fallbackTranslation.set(0);
+ collapse.set(0);
+ actionOpacity.set(1);
+ dismissing.set(false);
+ })(swipeableRef.current);
setIsDismissing(false);
}, [actionOpacity, collapse, dismissing, fallbackTranslation]);
- const finishDismiss = useCallback(() => {
- const finish = pendingDismissRef.current;
+ const finishDismiss = useCallback((id?: number) => {
+ const pending = pendingDismissRef.current;
+ if (!pending || (id !== undefined && pending.id !== id)) return;
pendingDismissRef.current = null;
- finish?.();
+ pending.resolve();
}, []);
useLayoutEffect(() => {
@@ -324,8 +316,22 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
};
}, [actionOpacity, collapse, fallbackTranslation, finishDismiss]);
+ // Reset recycled rows in place so their child views stay mounted.
+ useLayoutEffect(() => {
+ if (contentRef.current === content) return;
+ contentRef.current = content;
+ finishDismiss();
+ restoreRow();
+ }, [content, finishDismiss, restoreRow]);
+
+ useLayoutEffect(() => {
+ if (props.dormant && activeTranslationRef.current && dismissalRef.current === null) {
+ restoreRow();
+ }
+ }, [props.dormant, restoreRow]);
+
const dismiss = useCallback(
- (translation: SharedValue<number>) => {
+ (translation: SharedValue<number>, id: number) => {
"worklet";
if (dismissing.value) return;
dismissing.set(true);
@@ -340,7 +346,7 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
if (!finished) return;
collapse.set(
withTiming(1, { ...timing, duration: 180 }, (collapsed) => {
- if (collapsed) runOnJS(finishDismiss)();
+ if (collapsed) runOnJS(finishDismiss)(id);
}),
);
}),
@@ -350,49 +356,75 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
);
useLayoutEffect(
() =>
- registerThreadDismissal(props.threadKey, () => {
- if (dismissalRef.current) return dismissalRef.current;
- const finished = new Promise<void>((resolve) => {
- pendingDismissRef.current = resolve;
- });
- fullSwipeArmedRef.current = false;
- setIsDismissing(true);
- if (swipeableRef.current) onSwipeableClose?.(swipeableRef.current);
- runOnUI(dismiss)(activeTranslationRef.current ?? fallbackTranslation);
- dismissalRef.current = { finished, restore: restoreRow };
- return dismissalRef.current;
- }),
- [dismiss, fallbackTranslation, onSwipeableClose, props.threadKey, restoreRow],
+ props.dormant
+ ? undefined
+ : registerThreadDismissal(props.threadKey, () => {
+ if (dismissalRef.current) return dismissalRef.current;
+ const id = ++dismissalIdRef.current;
+ const finished = new Promise<void>((resolve) => {
+ pendingDismissRef.current = { id, resolve };
+ });
+ setIsDismissing(true);
+ if (swipeableRef.current) onSwipeableClose?.(swipeableRef.current);
+ runOnUI(dismiss)(activeTranslationRef.current ?? fallbackTranslation, id);
+ const dismissal = {
+ finished,
+ restore: () => {
+ if (dismissalRef.current === dismissal) restoreRow();
+ },
+ };
+ dismissalRef.current = dismissal;
+ return dismissal;
+ }),
+ [dismiss, fallbackTranslation, onSwipeableClose, props.dormant, props.threadKey, restoreRow],
);
const dismissStyle = useAnimatedStyle(() => ({
- height: dismissing.value ? rowHeight.value * (1 - collapse.value) : undefined,
+ // Native prop updates omit undefined, leaving a collapsed height in place.
+ height: dismissing.value ? rowHeight.value * (1 - collapse.value) : "auto",
pointerEvents: dismissing.value ? "none" : "auto",
overflow: "hidden",
transform: [{ translateX: fallbackTranslation.value }],
}));
const actionStyle = useAnimatedStyle(() => ({ opacity: actionOpacity.value, height: "100%" }));
const commitPrimaryAction = useCallback(() => {
+ if (!mountedRef.current || contentRef.current !== content) return;
primaryAction.onPress();
if (!pendingDismissRef.current) swipeableRef.current?.close();
- }, [primaryAction]);
+ }, [content, primaryAction]);
+ // RNGH lifecycle events call the latest handler, so commit from this release.
+ const commitFullSwipe = useCallback(() => {
+ if (!mountedRef.current || contentRef.current !== content) return;
+ if (fullSwipeAction === "primary") commitPrimaryAction();
+ else {
+ swipeableRef.current?.close();
+ onDelete();
+ }
+ }, [commitPrimaryAction, content, fullSwipeAction, onDelete]);
const handleRelease = useCallback(
(translation: SharedValue<number>) => {
"worklet";
if (dismissing.value) return true;
- if (fullSwipeAction === "primary" && -translation.value >= fullSwipeThreshold) {
- runOnJS(commitPrimaryAction)();
+ if (-translation.value >= fullSwipeThreshold) {
+ runOnJS(commitFullSwipe)();
return true;
}
return false;
},
- [commitPrimaryAction, dismissing, fullSwipeAction, fullSwipeThreshold],
+ [commitFullSwipe, dismissing, fullSwipeThreshold],
+ );
+ const handleFullSwipeArmedChange = useCallback(
+ (armed: boolean) => {
+ if (mountedRef.current && contentRef.current === content && armed) {
+ void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
+ }
+ },
+ [content],
);
- const handleFullSwipeArmedChange = useCallback((armed: boolean) => {
- if (armed && !fullSwipeArmedRef.current) {
- void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
+ const handleWillOpen = () => {
+ if (!props.dormant && !dismissalRef.current && swipeableRef.current) {
+ props.onSwipeableWillOpen?.(swipeableRef.current);
}
- fullSwipeArmedRef.current = armed;
- }, []);
+ };
return (
<Animated.View style={dismissStyle}>
@@ -408,7 +440,7 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
childrenContainerStyle={{ backgroundColor: props.backgroundColor }}
containerStyle={[{ backgroundColor: props.backgroundColor }, props.containerStyle]}
dragOffsetFromRight={-8}
- enabled={!isDismissing && props.enabled !== false && gateEnabled}
+ enabled={!props.dormant && !isDismissing && props.enabled !== false && gateEnabled}
enableTrackpadTwoFingerGesture={props.enableTrackpadSwipe ?? true}
// Fail the swipe once the pan is vertically dominant (patched-in RNGH
// prop) — otherwise trackpad scrolls with ~8px of horizontal drift
@@ -417,59 +449,44 @@ function ThreadSwipeableRow(props: ThreadSwipeableProps) {
failOffsetY={[-10, 10]}
friction={1}
onSwipeableClose={() => {
- fullSwipeArmedRef.current = false;
if (swipeableRef.current) {
props.onSwipeableClose?.(swipeableRef.current);
}
}}
onSwipeableRelease={handleRelease}
- onSwipeableOpenStartDrag={() => {
- if (swipeableRef.current) {
- props.onSwipeableWillOpen?.(swipeableRef.current);
- }
- }}
- onSwipeableWillOpen={() => {
- const methods = swipeableRef.current;
- if (!methods) {
- return;
- }
-
- props.onSwipeableWillOpen?.(methods);
- if (fullSwipeArmedRef.current && fullSwipeAction !== "primary") {
- fullSwipeArmedRef.current = false;
- methods.close();
- props.onDelete();
- }
- }}
+ onSwipeableOpenStartDrag={handleWillOpen}
+ onSwipeableWillOpen={handleWillOpen}
overshootFriction={1}
overshootRight
- renderRightActions={(_progress, translation, methods) => (
- <Animated.View
- ref={() => {
- activeTranslationRef.current = translation;
- }}
- style={actionStyle}
- >
- <ThreadSwipeActions
- backgroundColor={props.backgroundColor}
- compact={props.compactActions === true}
- fullSwipeAction={fullSwipeAction}
- fullSwipeThreshold={fullSwipeThreshold}
- onFullSwipeArmedChange={handleFullSwipeArmedChange}
- primaryAction={{
- ...primaryAction,
- onPress: commitPrimaryAction,
+ renderRightActions={(_progress, translation, methods) =>
+ props.dormant ? null : (
+ <Animated.View
+ ref={() => {
+ activeTranslationRef.current = translation;
}}
- secondaryAction={resolveSecondaryAction({
- close: () => methods.close(),
- onDelete: props.onDelete,
- secondaryAction: props.secondaryAction,
- threadTitle: props.threadTitle,
- })}
- translation={translation}
- />
- </Animated.View>
- )}
+ style={actionStyle}
+ >
+ <ThreadSwipeActions
+ backgroundColor={props.backgroundColor}
+ compact={props.compactActions === true}
+ fullSwipeAction={fullSwipeAction}
+ fullSwipeThreshold={fullSwipeThreshold}
+ onFullSwipeArmedChange={handleFullSwipeArmedChange}
+ primaryAction={{
+ ...primaryAction,
+ onPress: commitPrimaryAction,
+ }}
+ secondaryAction={resolveSecondaryAction({
+ close: () => methods.close(),
+ onDelete: props.onDelete,
+ secondaryAction: props.secondaryAction,
+ threadTitle: props.threadTitle,
+ })}
+ translation={translation}
+ />
+ </Animated.View>
+ )
+ }
rightThreshold={actionsWidth * 0.42}
simultaneousWith={props.simultaneousWith}
>