Skip to content

Commit 9ea892e

Browse files
authored
fix(client): show thread state changes before remote replies (pingdotgg#11408)
1 parent a37b852 commit 9ea892e

8 files changed

Lines changed: 646 additions & 9 deletions

File tree

‎apps/mobile/src/state/threads.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ import { environmentCatalog } from "../connection/catalog";
1515
import { connectionAtomRuntime } from "../connection/runtime";
1616
import { environmentSnapshotAtom } from "./shell";
1717

18-
export const threadEnvironment = createThreadEnvironmentAtoms(connectionAtomRuntime);
18+
export const threadEnvironment = createThreadEnvironmentAtoms(
19+
connectionAtomRuntime,
20+
environmentSnapshotAtom,
21+
);
1922
export const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
2023
export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
2124
environmentThreads.stateAtom,
2225
);
2326
export const environmentThreadShells = createEnvironmentThreadShellAtoms({
2427
catalogValueAtom: environmentCatalog.catalogValueAtom,
25-
snapshotAtom: environmentSnapshotAtom,
28+
snapshotAtom: threadEnvironment.snapshotAtom,
2629
});
2730

2831
const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(

‎apps/web/src/components/Sidebar.logic.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
sortProjectsForSidebar,
4545
sortScopedProjectsForSidebar,
4646
shouldCreateNewThreadInCurrentProject,
47+
shouldNavigateAfterThreadPark,
4748
THREAD_JUMP_HINT_SHOW_DELAY_MS,
4849
type SidebarListItem,
4950
type SidebarListMarker,
@@ -2500,3 +2501,44 @@ describe("resolveSidebarDropVerb", () => {
25002501
expect(resolveSidebarDropVerb("active", "snoozed")).toBeNull();
25012502
});
25022503
});
2504+
2505+
describe("navigation after parking a thread", () => {
2506+
it.each([
2507+
["settle", "settled", null, "thread", true],
2508+
["settle", "active", null, "thread", false],
2509+
["settle", "settled", null, "other-thread", false],
2510+
["snooze", null, "2099-01-01T00:00:00.000Z", "thread", true],
2511+
["snooze", null, null, "thread", false],
2512+
["snooze", null, "2026-09-12T09:00:00.000Z", "thread", false],
2513+
["snooze", null, "2099-01-01T00:00:00.000Z", "thread", false, true],
2514+
["snooze", null, "2099-01-01T00:00:00.000Z", "other-thread", false],
2515+
] as const)(
2516+
"%s with state %s / %s on %s navigates: %s",
2517+
(
2518+
action,
2519+
settledOverride,
2520+
snoozedUntil,
2521+
currentThreadKey,
2522+
expected,
2523+
hasPendingApprovals: boolean = false,
2524+
) => {
2525+
expect(
2526+
shouldNavigateAfterThreadPark({
2527+
threadKey: "thread",
2528+
currentThreadKey,
2529+
action,
2530+
now: "2026-09-12T10:00:00.000Z",
2531+
thread: {
2532+
settledOverride,
2533+
snoozedUntil,
2534+
snoozedAt: null,
2535+
session: null,
2536+
latestTurn: null,
2537+
hasPendingApprovals,
2538+
hasPendingUserInput: false,
2539+
},
2540+
}),
2541+
).toBe(expected);
2542+
},
2543+
);
2544+
});

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import type { ContextMenuItem } from "@t3tools/contracts";
99
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
1010
import type { AsyncResult } from "effect/unstable/reactivity";
1111
import { planPinnedReorder } from "@t3tools/client-runtime/state/thread-sort";
12+
import {
13+
effectiveSnoozed,
14+
type ThreadSnoozeShell,
15+
} from "@t3tools/client-runtime/state/thread-settled";
1216
import {
1317
getThreadSortTimestamp,
1418
resolveSettledThreadTimestamp,
@@ -21,6 +25,22 @@ import type { SidebarThreadSummary, Thread } from "../types";
2125
import { cn } from "../lib/utils";
2226
import { isLatestTurnSettled } from "../session-logic";
2327

28+
export function shouldNavigateAfterThreadPark(input: {
29+
readonly threadKey: string;
30+
readonly currentThreadKey: string | null;
31+
readonly action: "settle" | "snooze";
32+
readonly now: string;
33+
readonly thread: (ThreadSnoozeShell & Pick<SidebarThreadSummary, "settledOverride">) | null;
34+
}): boolean {
35+
return (
36+
input.threadKey === input.currentThreadKey &&
37+
input.thread !== null &&
38+
(input.action === "settle"
39+
? input.thread.settledOverride === "settled"
40+
: effectiveSnoozed(input.thread, { now: input.now }))
41+
);
42+
}
43+
2444
const THREAD_SELECTION_SAFE_SELECTOR = "[data-thread-item], [data-thread-selection-safe]";
2545
export const THREAD_JUMP_HINT_SHOW_DELAY_MS = 200;
2646
// Visible sidebar rows are prewarmed into the thread-detail cache so opening a

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import {
169169
resolveSidebarThreadStatus,
170170
searchSidebarThreads,
171171
shouldCreateNewThreadInCurrentProject,
172+
shouldNavigateAfterThreadPark,
172173
shouldRecedeSidebarThread,
173174
resolveWorkingStartedAt,
174175
sidebarListItemId,
@@ -3053,7 +3054,15 @@ export default function Sidebar() {
30533054
}
30543055
// Only move forward if the user is still on the settled thread —
30553056
// a navigation made during the await wins over ours.
3056-
if (routeThreadKeyRef.current === threadKey) {
3057+
if (
3058+
shouldNavigateAfterThreadPark({
3059+
threadKey,
3060+
currentThreadKey: routeThreadKeyRef.current,
3061+
action: "settle",
3062+
now: new Date().toISOString(),
3063+
thread: readThreadShell(threadRef),
3064+
})
3065+
) {
30573066
navigateAfterSettle?.();
30583067
}
30593068
} finally {
@@ -3582,7 +3591,17 @@ export default function Sidebar() {
35823591
const settled = await run(settleThread(threadRef), "Failed to settle thread").finally(
35833592
() => settlingThreadKeysRef.current.delete(activeKey),
35843593
);
3585-
if (settled && routeThreadKeyRef.current === activeKey) navigateAfterSettle?.();
3594+
if (
3595+
settled &&
3596+
shouldNavigateAfterThreadPark({
3597+
threadKey: activeKey,
3598+
currentThreadKey: routeThreadKeyRef.current,
3599+
action: "settle",
3600+
now: new Date().toISOString(),
3601+
thread: readThreadShell(threadRef),
3602+
})
3603+
)
3604+
navigateAfterSettle?.();
35863605
return;
35873606
}
35883607
case "move-active":
@@ -3679,7 +3698,15 @@ export default function Sidebar() {
36793698
}
36803699
// Only move forward if the user is still on the snoozed thread —
36813700
// a navigation made during the await wins over ours.
3682-
if (routeThreadKeyRef.current === threadKey) {
3701+
if (
3702+
shouldNavigateAfterThreadPark({
3703+
threadKey,
3704+
currentThreadKey: routeThreadKeyRef.current,
3705+
action: "snooze",
3706+
now: new Date().toISOString(),
3707+
thread: readThreadShell(threadRef),
3708+
})
3709+
) {
36833710
navigateAfterSnooze?.();
36843711
}
36853712
return { status: "success" } as const;

‎apps/web/src/state/threads.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ import { environmentCatalog } from "../connection/catalog";
1515
import { connectionAtomRuntime } from "../connection/runtime";
1616
import { environmentSnapshotAtom } from "./shell";
1717

18-
export const threadEnvironment = createThreadEnvironmentAtoms(connectionAtomRuntime);
18+
export const threadEnvironment = createThreadEnvironmentAtoms(
19+
connectionAtomRuntime,
20+
environmentSnapshotAtom,
21+
);
1922
const environmentThreads = createEnvironmentThreadStateAtoms(connectionAtomRuntime);
2023
export const environmentThreadDetails = createEnvironmentThreadDetailAtoms(
2124
environmentThreads.stateAtom,
2225
);
2326
export const environmentThreadShells = createEnvironmentThreadShellAtoms({
2427
catalogValueAtom: environmentCatalog.catalogValueAtom,
25-
snapshotAtom: environmentSnapshotAtom,
28+
snapshotAtom: threadEnvironment.snapshotAtom,
2629
});
2730

2831
const EMPTY_THREAD_STATE_ATOM = Atom.make(AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE)).pipe(

0 commit comments

Comments
 (0)