Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions apps/mobile/src/features/home/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSi
import type { ThreadMoveDestination } from "../threads/threadOrder";
import type { LegendListRef } from "@legendapp/list/react-native";
import { AnimatedLegendList } from "@legendapp/list/reanimated";
import { THREAD_SIDEBAR_ROW_LAYOUT } from "../threads/threadListAnimations";
import {
threadListShelfExpansionKey,
useThreadListShelfLayoutAnimation,
} from "../threads/threadListAnimations";
import {
type EnvironmentProject,
type EnvironmentThreadShell,
Expand Down Expand Up @@ -442,6 +445,13 @@ export function HomeScreen(props: HomeScreenProps) {
toggleSnoozedShelf,
toggleWorkingShelf,
} = useThreadListV2ShelfPreferences();
const shelfLayoutAnimation = useThreadListShelfLayoutAnimation(
threadListShelfExpansionKey({
workingShelfExpanded,
snoozedShelfExpanded,
settledShelfExpanded,
}),
);
const now = useThreadListNow(props.threads);
const listEnvironments = useAtomValue(threadListEnvironmentsAtom);
const {
Expand Down Expand Up @@ -794,7 +804,7 @@ export function HomeScreen(props: HomeScreenProps) {
<SwipeableScrollGateProvider enabled={swipeEnabled} activation={swipeRowActivation}>
<GestureDetector gesture={homeScrollGesture}>
<AnimatedLegendList
itemLayoutAnimation={sidebarSurface ? THREAD_SIDEBAR_ROW_LAYOUT : undefined}
itemLayoutAnimation={shelfLayoutAnimation}
ref={listRef}
onLoad={() => activateVisibleRows(threadListV2Items)}
onTouchStart={(event) => trackListTouches(event, true)}
Expand Down
16 changes: 13 additions & 3 deletions apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import type {
} from "@t3tools/client-runtime/state/shell";
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
import { AnimatedLegendList } from "@legendapp/list/reanimated";
import { THREAD_SIDEBAR_ROW_LAYOUT } from "./threadListAnimations";
import {
threadListShelfExpansionKey,
useThreadListShelfLayoutAnimation,
} from "./threadListAnimations";
import { useAtomValue } from "@effect/atom-react";
import { useMemo, useRef, useState } from "react";
import { Platform, StyleSheet, View } from "react-native";
Expand Down Expand Up @@ -247,6 +250,13 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) {
toggleSnoozedShelf,
toggleWorkingShelf,
} = useThreadListV2ShelfPreferences();
const shelfLayoutAnimation = useThreadListShelfLayoutAnimation(
threadListShelfExpansionKey({
workingShelfExpanded,
snoozedShelfExpanded,
settledShelfExpanded,
}),
);
const now = useThreadListNow(threads);
const listEnvironments = useAtomValue(threadListEnvironmentsAtom);
const {
Expand Down Expand Up @@ -587,7 +597,7 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) {
<GestureDetector gesture={sidebarScrollGesture}>
<AnimatedLegendList
renderScrollComponent={NativeScrollView}
itemLayoutAnimation={THREAD_SIDEBAR_ROW_LAYOUT}
itemLayoutAnimation={shelfLayoutAnimation}
data={listItems}
drawDistance={500}
estimatedItemSize={64}
Expand Down Expand Up @@ -644,7 +654,7 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) {
<GestureDetector gesture={sidebarScrollGesture}>
{/* oxlint-disable-next-line t3code/require-registered-list-scroll -- The Android sidebar has no native header to register with. */}
<AnimatedLegendList
itemLayoutAnimation={THREAD_SIDEBAR_ROW_LAYOUT}
itemLayoutAnimation={shelfLayoutAnimation}
data={listItems}
drawDistance={500}
estimatedItemSize={64}
Expand Down
29 changes: 28 additions & 1 deletion apps/mobile/src/features/threads/threadListAnimations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { useLayoutEffect, useState } from "react";
import { LinearTransition, ReduceMotion } from "react-native-reanimated";

export const THREAD_SIDEBAR_ROW_LAYOUT = LinearTransition.duration(180).reduceMotion(
export const THREAD_LIST_ROW_LAYOUT = LinearTransition.duration(180).reduceMotion(
ReduceMotion.System,
);

export function threadListShelfExpansionKey(input: {
readonly workingShelfExpanded: boolean;
readonly snoozedShelfExpanded: boolean;
readonly settledShelfExpanded: boolean;
}): string {
return `${input.workingShelfExpanded}:${input.snoozedShelfExpanded}:${input.settledShelfExpanded}`;
}

/**
* Shelf inserts would run a layout transition on every revealed row; skip the transition for that
* commit (same idea as the web sidebar), then restore it for ordinary list moves such as leaving
* Working.
*/
export function useThreadListShelfLayoutAnimation(
shelfExpansionKey: string,
animation: typeof THREAD_LIST_ROW_LAYOUT = THREAD_LIST_ROW_LAYOUT,
) {
const [committedKey, setCommittedKey] = useState(shelfExpansionKey);
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- re-enables row layout animation after a shelf expand/collapse commit that must not animate
useLayoutEffect(() => {
// oxlint-disable-next-line react/set-state-in-effect -- Marks the expand/collapse commit as done so later list moves can animate again.
setCommittedKey(shelfExpansionKey);
}, [shelfExpansionKey]);
return committedKey === shelfExpansionKey ? animation : undefined;
}
12 changes: 5 additions & 7 deletions apps/mobile/src/features/threads/threadListV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
import { createInboxReturnTracker } from "@t3tools/client-runtime/state/thread-inbox";
import { presentThreadShell } from "@t3tools/client-runtime/state/models";
import * as DateTime from "effect/DateTime";
import * as Schema from "effect/Schema";
Expand Down Expand Up @@ -42,6 +41,7 @@ import {
resolveThreadListV2SnoozePresets,
resolveThreadListV2Status,
resolveThreadListV2WokeAt,
threadListInboxReturns,
threadListV2ListItemsAreEqual,
threadHasUnseenCompletion,
type ThreadListV2ListItem,
Expand Down Expand Up @@ -1691,10 +1691,10 @@ describe("Working section beta", () => {
expect(off.workingCount).toBe(0);
});

it("keeps an inactive row in Working during the shared grace and returns it on expiry", () => {
it("leaves Working immediately when the mobile tracker has no exit grace", () => {
vi.useFakeTimers();
vi.setSystemTime(new Date(NOW));
const tracker = createInboxReturnTracker();
const tracker = threadListInboxReturns;
const working = makeThread({ id: ThreadId.make("grace"), title: "grace", runtime: running });
const inactive = { ...working, runtime: null };
const render = () =>
Expand All @@ -1707,12 +1707,10 @@ describe("Working section beta", () => {
try {
tracker.observe([working]);
tracker.observe([inactive]);
expect(render().workingCount).toBe(1);
expect(render().items[0]?.thread.runtime).toBeNull();
vi.advanceTimersByTime(2_000);
expect(render().workingCount).toBe(0);
expect(ids(render())).toEqual(["grace"]);
expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW) + 2_000);
expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW));
expect(vi.getTimerCount()).toBe(0);
} finally {
tracker.observe(null);
vi.useRealTimers();
Expand Down
3 changes: 2 additions & 1 deletion apps/mobile/src/features/threads/threadListV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import {
type PendingThreadOrder,
} from "./threadOrder";

export const threadListInboxReturns = createInboxReturnTracker();
/** No exit hold: the list animates the move instead of parking a finished row in Working. */
export const threadListInboxReturns = createInboxReturnTracker({ graceMs: 0 });

export function resolveThreadListV2ProviderDrivers(
thread: Pick<EnvironmentThreadShell, "providerInstanceHistory" | "modelSelection" | "runtime">,
Expand Down
1 change: 0 additions & 1 deletion apps/mobile/src/features/threads/useThreadListV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export function useThreadListV2(input: {
() => threadListV2LayoutTime({ threads, now: input.now, searchQuery: input.searchQuery }),
[threads, input.now, input.searchQuery],
);
// A thread can stop working when the threads change, or when its exit grace expires.
const inboxVersion = useSyncExternalStore(
threadListInboxReturns.subscribe,
threadListInboxReturns.getSnapshot,
Expand Down
12 changes: 12 additions & 0 deletions packages/client-runtime/src/state/threadInbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ describe("createInboxReturnTracker", () => {
expect(notify).not.toHaveBeenCalled();
expect(tracker.returnedAt(thread("a", false))).toBeUndefined();
});

it("leaves Working immediately when graceMs is 0", () => {
const tracker = createInboxReturnTracker({ graceMs: 0 });
const notify = vi.fn();
tracker.subscribe(notify);
tracker.observe([thread("a", true)]);
tracker.observe([thread("a", false)]);
expect(tracker.isWorking(thread("a", false))).toBe(false);
expect(tracker.returnedAt(thread("a", false))).toBe(10_000);
expect(vi.getTimerCount()).toBe(0);
expect(notify).not.toHaveBeenCalled();
});
});

describe("sortWorkingThreadsBySend", () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/client-runtime/src/state/threadInbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ type TrackedInboxThread = WorkingThreadInput &
type ThreadKeyInput = Pick<EnvironmentThreadShell, "environmentId" | "id">;

/**
* How long a thread that stopped working keeps reading as working. An orchestrator
* run can end moments before its continuation starts, so clients wait this long
* before treating the thread as done.
* Default hold for clients that animate shelf moves (web). Mobile passes `graceMs: 0` and
* animates the list instead: a finished row should leave Working immediately, like Mail.
* A non-zero hold also lets auto-settle land as Working → Settled without flashing Active.
*/
const WORKING_EXIT_GRACE_MS = 2_000;

Expand All @@ -138,7 +138,8 @@ const WORKING_EXIT_GRACE_MS = 2_000;
* `observe` when the shells or the snapshot version change, or with null while the beta
* is off. Its result stays fixed, so a list built from it can be cached.
*/
export function createInboxReturnTracker() {
export function createInboxReturnTracker(options?: { readonly graceMs?: number }) {
const graceMs = options?.graceMs ?? WORKING_EXIT_GRACE_MS;
const keyOf = (thread: ThreadKeyInput) => `${thread.environmentId}:${thread.id}`;
const states = new Map<
string,
Expand Down Expand Up @@ -235,8 +236,8 @@ export function createInboxReturnTracker() {
effectiveSnoozed(thread, { now: observedAt });
// Only shelf placement is held: execution state and attention requests stay authoritative.
const deadline =
!actual && previous?.working && !actionable && !moved && !excluded
? (previous.deadline ?? at + WORKING_EXIT_GRACE_MS)
graceMs > 0 && !actual && previous?.working && !actionable && !moved && !excluded
? (previous.deadline ?? at + graceMs)
: null;
const working = actual || (deadline !== null && deadline > at);
if (previous?.working && !working) returns.set(key, at);
Expand Down
4 changes: 2 additions & 2 deletions scripts/mobile-behavior/scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ export const SCENARIOS = {
);
yield* ctx.env.release(key);
yield* ctx.env.settled(threadId);
// The shelf keeps a finished row for WORKING_EXIT_GRACE_MS (2 s) on purpose.
yield* Effect.sleep("5 seconds");
// Mobile leaves Working immediately; wait only for the list and shell to settle.
yield* Effect.sleep("2 seconds");
const after = yield* ctx.device.state;
return {
shelfShownWhileWorking: shelfCount(working) > 0,
Expand Down
Loading