diff --git a/apps/mobile/src/features/home/thread-swipe-actions.tsx b/apps/mobile/src/features/home/thread-swipe-actions.tsx index 05f12f773c..103c8746f2 100644 --- a/apps/mobile/src/features/home/thread-swipe-actions.tsx +++ b/apps/mobile/src/features/home/thread-swipe-actions.tsx @@ -58,6 +58,46 @@ interface ThreadSwipeSecondaryAction extends ThreadSwipeAction { readonly backgroundColor: string; } +/** + * Rows that opt out of a secondary action reveal a single-action tray, so the + * open width (and every animation range derived from it) shrinks to one item + * instead of leaving an empty slot where Delete used to sit. + */ +function swipeActionsWidth(hasSecondaryAction: boolean) { + return hasSecondaryAction ? THREAD_SWIPE_ACTIONS_WIDTH : ACTION_ITEM_WIDTH; +} + +/** `undefined` = v1 default (Delete), `null` = no secondary action at all. */ +function resolveSecondaryAction(input: { + readonly close: () => void; + readonly onDelete: () => void; + readonly secondaryAction: ThreadSwipeAction | null | undefined; + readonly threadTitle: string; +}): ThreadSwipeSecondaryAction | null { + if (input.secondaryAction === null) return null; + if (input.secondaryAction === undefined) { + return { + accessibilityLabel: `Delete ${input.threadTitle}`, + backgroundColor: "#ff2d55", + icon: "trash", + label: "Delete", + onPress: () => { + input.close(); + input.onDelete(); + }, + }; + } + const action = input.secondaryAction; + return { + ...action, + backgroundColor: "#5856d6", + onPress: () => { + input.close(); + action.onPress(); + }, + }; +} + /** * Delivers the scroll gate to swipeables via context so that flipping it does * NOT re-render whole rows: putting the flag in list extraData/renderItem deps @@ -188,8 +228,14 @@ export function ThreadSwipeable(props: { readonly onSwipeableClose?: (methods: SwipeableMethods) => void; readonly onSwipeableWillOpen?: (methods: SwipeableMethods) => void; readonly primaryAction: ThreadSwipeAction; - /** Optional second non-destructive action revealed nearest the row edge. */ - readonly secondaryAction?: ThreadSwipeAction; + /** + * Second action revealed nearest the row edge. Omitted (`undefined`) keeps + * the v1 default of a destructive Delete; explicit `null` opts out of a + * secondary action entirely and reveals a single-action tray — the two must + * stay distinguishable so gating an optional action can never fall back to + * an unadvertised Delete. + */ + readonly secondaryAction?: ThreadSwipeAction | null; /** * Identity of the content being wrapped. When a recycled list reuses this * component for a different item, the swipeable snaps back to closed so an @@ -203,7 +249,12 @@ export function ThreadSwipeable(props: { }) { const swipeableRef = useRef(null); const fullSwipeArmedRef = useRef(false); - const fullSwipeThreshold = Math.max(THREAD_SWIPE_ACTIONS_WIDTH + 44, props.fullSwipeWidth * 0.58); + const hasSecondaryAction = props.secondaryAction !== null; + const actionsWidth = swipeActionsWidth(hasSecondaryAction); + const fullSwipeThreshold = Math.max(actionsWidth + 44, props.fullSwipeWidth * 0.58); + // With no secondary action there is no Delete button to stretch or commit, + // so a full swipe can only mean the primary action. + const fullSwipeAction = hasSecondaryAction ? (props.fullSwipeAction ?? "delete") : "primary"; const close = useCallback(() => swipeableRef.current?.close(), []); const gateEnabled = use(SwipeableScrollGateContext); const resetKey = props.resetKey; @@ -257,7 +308,7 @@ export function ThreadSwipeable(props: { if (fullSwipeArmedRef.current) { fullSwipeArmedRef.current = false; methods.close(); - if (props.fullSwipeAction === "primary") { + if (fullSwipeAction === "primary") { props.primaryAction.onPress(); } else { props.onDelete(); @@ -270,7 +321,7 @@ export function ThreadSwipeable(props: { { - methods.close(); - props.onDelete(); - }, - } - : { - ...props.secondaryAction, - backgroundColor: "#5856d6", - onPress: () => { - methods.close(); - props.secondaryAction?.onPress(); - }, - } - } + secondaryAction={resolveSecondaryAction({ + close: () => methods.close(), + onDelete: props.onDelete, + secondaryAction: props.secondaryAction, + threadTitle: props.threadTitle, + })} translation={translation} /> )} - rightThreshold={THREAD_SWIPE_ACTIONS_WIDTH * 0.42} + rightThreshold={actionsWidth * 0.42} simultaneousWithExternalGesture={props.simultaneousWithExternalGesture} > {props.children(close)} @@ -314,6 +350,9 @@ export function ThreadSwipeable(props: { function SwipeActionButton(props: { readonly accessibilityLabel: string; + /** Width of the whole action tray — the point past which a swipe stretches + * the committing button instead of revealing more of it. */ + readonly actionsWidth: number; readonly backgroundColor: string; readonly compact: boolean; readonly entryRange: readonly [number, number]; @@ -329,10 +368,10 @@ function SwipeActionButton(props: { const actionStyle = useAnimatedStyle(() => { const reveal = Math.max(-props.translation.value, 0); const entryProgress = interpolate(reveal, props.entryRange, [0, 1], Extrapolation.CLAMP); - const stretch = Math.max(reveal - THREAD_SWIPE_ACTIONS_WIDTH, 0); + const stretch = Math.max(reveal - props.actionsWidth, 0); const fullSwipeProgress = interpolate( reveal, - [THREAD_SWIPE_ACTIONS_WIDTH, props.fullSwipeThreshold + 20], + [props.actionsWidth, props.fullSwipeThreshold + 20], [0, 1], Extrapolation.CLAMP, ); @@ -351,9 +390,7 @@ function SwipeActionButton(props: { }); const circleStyle = useAnimatedStyle(() => { const reveal = Math.max(-props.translation.value, 0); - const stretch = props.stretchesOnFullSwipe - ? Math.max(reveal - THREAD_SWIPE_ACTIONS_WIDTH, 0) - : 0; + const stretch = props.stretchesOnFullSwipe ? Math.max(reveal - props.actionsWidth, 0) : 0; return { transform: [{ translateX: -stretch }], @@ -362,9 +399,7 @@ function SwipeActionButton(props: { }); const iconStyle = useAnimatedStyle(() => { const reveal = Math.max(-props.translation.value, 0); - const stretch = props.stretchesOnFullSwipe - ? Math.max(reveal - THREAD_SWIPE_ACTIONS_WIDTH, 0) - : 0; + const stretch = props.stretchesOnFullSwipe ? Math.max(reveal - props.actionsWidth, 0) : 0; const armedProgress = interpolate( reveal, [props.fullSwipeThreshold, props.fullSwipeThreshold + 20], @@ -382,7 +417,7 @@ function SwipeActionButton(props: { } const reveal = Math.max(-props.translation.value, 0); - const stretch = Math.max(reveal - THREAD_SWIPE_ACTIONS_WIDTH, 0); + const stretch = Math.max(reveal - props.actionsWidth, 0); return { opacity: interpolate( reveal, @@ -472,10 +507,15 @@ export function ThreadSwipeActions(props: { readonly fullSwipeThreshold: number; readonly onFullSwipeArmedChange: (armed: boolean) => void; readonly primaryAction: ThreadSwipeAction; - readonly secondaryAction: ThreadSwipeSecondaryAction; + /** Null renders a single-action tray — no second button, no Delete. */ + readonly secondaryAction: ThreadSwipeSecondaryAction | null; readonly translation: SharedValue; }) { - const fullSwipeIsPrimary = props.fullSwipeAction === "primary"; + const secondaryAction = props.secondaryAction; + // Without a secondary action the primary is the only thing that can stretch + // and commit on full swipe, whatever the caller asked for. + const fullSwipeIsPrimary = props.fullSwipeAction === "primary" || secondaryAction === null; + const actionsWidth = swipeActionsWidth(secondaryAction !== null); useAnimatedReaction( () => -props.translation.value >= props.fullSwipeThreshold, (armed, previous) => { @@ -492,14 +532,21 @@ export function ThreadSwipeActions(props: { backgroundColor: props.backgroundColor, flexDirection: "row", height: "100%", - width: THREAD_SWIPE_ACTIONS_WIDTH, + width: actionsWidth, }} > - + {secondaryAction === null ? null : ( + + )} ); } diff --git a/apps/mobile/src/features/threads/thread-list-v2-items.tsx b/apps/mobile/src/features/threads/thread-list-v2-items.tsx index 0c50f467b7..99dbe5b38b 100644 --- a/apps/mobile/src/features/threads/thread-list-v2-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-v2-items.tsx @@ -4,7 +4,7 @@ import type { } from "@t3tools/client-runtime/state/shell"; import { canSnooze } from "@t3tools/client-runtime/state/thread-settled"; import type { MenuAction } from "@react-native-menu/menu"; -import { memo, useCallback, useEffect, useMemo, type ComponentProps } from "react"; +import { memo, useCallback, useEffect, useMemo, useState, type ComponentProps } from "react"; import { Platform, Pressable, useWindowDimensions, View } from "react-native"; import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable"; @@ -18,6 +18,7 @@ import { useThemeColor } from "../../lib/useThemeColor"; import { useThreadPr } from "../../state/use-thread-pr"; import { ThreadSwipeable } from "../home/thread-swipe-actions"; import { + resolveThreadListV2SnoozeGateExpiryMs, resolveThreadListV2Status, resolveThreadListV2SwipeActions, type ThreadListV2Status, @@ -187,6 +188,22 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { // row can un-settle — explicit settles clear the override, auto-settled // rows get pinned active until real activity clears the pin. const canUnsettle = variant === "slim"; + // Snooze eligibility is time-gated (a user message no turn has adopted yet + // blocks it for the grace window), so the row re-renders itself the moment + // that window lapses — otherwise Snooze would stay hidden until unrelated + // props changed. + const [snoozeGateTick, bumpSnoozeGateTick] = useState(0); + const snoozeGateExpiryMs = props.snoozeSupported + ? resolveThreadListV2SnoozeGateExpiryMs(thread, { now: new Date().toISOString() }) + : null; + useEffect(() => { + if (snoozeGateExpiryMs === null) return; + const delayMs = Math.min(Math.max(0, snoozeGateExpiryMs - Date.now()) + 50, 2_147_483_647); + const id = setTimeout(() => bumpSnoozeGateTick((tick) => tick + 1), delayMs); + return () => clearTimeout(id); + // snoozeGateTick re-arms after a clamped fire: for a far-future expiry the + // boundary is unchanged, so without it the timer chain would stop. + }, [snoozeGateExpiryMs, snoozeGateTick]); const swipeActions = resolveThreadListV2SwipeActions({ variant, settlementSupported: props.settlementSupported, @@ -228,11 +245,11 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { label: "Snooze 1h", onPress: handleSnooze, } - : undefined, + : null, [handleSnooze, swipeActions.secondary, thread.title], ); const swipeAccessibilityHint = - secondaryAction === undefined + secondaryAction === null ? `Opens the thread. Swipe left to ${primaryAction.label.toLowerCase()}.` : `Opens the thread. Swipe left for ${primaryAction.label.toLowerCase()} and snooze actions.`; diff --git a/apps/mobile/src/features/threads/threadListV2.test.ts b/apps/mobile/src/features/threads/threadListV2.test.ts index 441f395fbe..5e9f922a95 100644 --- a/apps/mobile/src/features/threads/threadListV2.test.ts +++ b/apps/mobile/src/features/threads/threadListV2.test.ts @@ -4,6 +4,7 @@ import { describe, expect, it } from "vite-plus/test"; import { buildThreadListV2Items, + resolveThreadListV2SnoozeGateExpiryMs, resolveThreadListV2SwipeActions, resolveThreadListV2Status, sortThreadsForListV2, @@ -120,6 +121,34 @@ describe("resolveThreadListV2SwipeActions", () => { }); }); +describe("resolveThreadListV2SnoozeGateExpiryMs", () => { + it("reports when an unadopted turn's grace window lapses", () => { + const thread = makeThread({ + id: ThreadId.make("t"), + title: "t", + latestUserMessageAt: "2026-06-02T00:00:30.000Z", + }); + expect(resolveThreadListV2SnoozeGateExpiryMs(thread, { now: "2026-06-02T00:01:00.000Z" })).toBe( + Date.parse("2026-06-02T00:02:30.000Z"), + ); + }); + + it("returns null once the thread is already snoozable", () => { + const thread = makeThread({ id: ThreadId.make("t"), title: "t" }); + expect(resolveThreadListV2SnoozeGateExpiryMs(thread, { now: NOW })).toBe(null); + }); + + it("returns null for threads blocked on the user — only new data clears those", () => { + const thread = makeThread({ + id: ThreadId.make("t"), + title: "t", + hasPendingApprovals: true, + latestUserMessageAt: NOW, + }); + expect(resolveThreadListV2SnoozeGateExpiryMs(thread, { now: NOW })).toBe(null); + }); +}); + describe("sortThreadsForListV2", () => { it("orders by creation time, newest first, ignoring activity", () => { const sorted = sortThreadsForListV2([ diff --git a/apps/mobile/src/features/threads/threadListV2.ts b/apps/mobile/src/features/threads/threadListV2.ts index def3d66dec..fe1683e845 100644 --- a/apps/mobile/src/features/threads/threadListV2.ts +++ b/apps/mobile/src/features/threads/threadListV2.ts @@ -1,4 +1,9 @@ -import { effectiveSettled, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled"; +import { + effectiveSettled, + effectiveSnoozed, + hasQueuedTurnStart, + QUEUED_TURN_START_GRACE_MS, +} from "@t3tools/client-runtime/state/thread-settled"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; import type { EnvironmentId, ProjectId } from "@t3tools/contracts"; @@ -33,6 +38,31 @@ export function resolveThreadListV2SwipeActions(input: { }; } +/** + * When a thread's snooze eligibility can flip on its own, as epoch ms — the + * moment the queued-turn-start grace window expires and `canSnooze` starts + * returning true. Null when nothing time-based gates it: either the thread is + * already snoozable, or it is blocked on the user and only fresh server data + * can clear that. Rows arm a one-shot timer on this so an unadopted turn's + * Snooze action appears when the grace period lapses instead of waiting for + * an unrelated re-render. + */ +export function resolveThreadListV2SnoozeGateExpiryMs( + thread: Pick< + EnvironmentThreadShell, + "hasPendingApprovals" | "hasPendingUserInput" | "latestUserMessageAt" | "latestTurn" | "session" + >, + options: { readonly now: string }, +): number | null { + // Blocked-on-user threads are not on a clock — leave them to data updates. + if (thread.hasPendingApprovals || thread.hasPendingUserInput) return null; + if (!hasQueuedTurnStart(thread, options)) return null; + // hasQueuedTurnStart already rejected a null/unparseable message time. + const messageAtMs = Date.parse(thread.latestUserMessageAt ?? ""); + if (Number.isNaN(messageAtMs)) return null; + return messageAtMs + QUEUED_TURN_START_GRACE_MS; +} + // Settled-tail paging: recent history is the common lookup; the deep tail // stays behind an explicit Show more. Shared by the compact Home list and // the iPad sidebar so both page identically.