Skip to content

Commit 30c11a3

Browse files
fix(web): the thread menu works for an older settled thread the sidebar hasn't loaded (pingdotgg#32507)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 047d492 commit 30c11a3

4 files changed

Lines changed: 79 additions & 12 deletions

File tree

‎apps/web/src/components/ChatView.browser.test.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ENVIRONMENT_ID,
1818
PROJECT_ID,
1919
installFakeEnvironment,
20+
threadWithHistory,
2021
type FakeDriver,
2122
} from "../../test/fakeEnvironment";
2223
import { render } from "../../test/render";
@@ -65,3 +66,28 @@ it("a new draft does not load its reserved thread before the server lists it", a
6566
// A 404 here marks the thread deleted, and the view never loads it once the send creates it.
6667
expect(threadRequests).toEqual([]);
6768
});
69+
70+
it("opens the thread actions menu for an older settled thread the shell leaves out", async () => {
71+
const thread = { ...threadWithHistory("older-settled", 1), olderSettled: true as const };
72+
using _environment = installFakeEnvironment([thread]);
73+
__setClientSettingsForTests({
74+
...DEFAULT_CLIENT_SETTINGS,
75+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
76+
});
77+
78+
const router = getRouter(
79+
createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
80+
);
81+
await router.load();
82+
await using _app = await render(<AppRoot router={router} />, {
83+
// AppRoot brings its own router.
84+
router: false,
85+
});
86+
await expect.element(page.getByText("older-settled: question 0")).toBeVisible();
87+
88+
await page.getByRole("button", { name: `Thread actions for ${thread.title}` }).click();
89+
// Without a bound the poll runs to the test deadline and reports a bare timeout.
90+
await expect
91+
.element(page.getByRole("menuitem", { name: "Rename thread" }), { timeout: 5_000 })
92+
.toBeVisible();
93+
});

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2+
import { parseScopedThreadKey, scopedThreadKey } from "@t3tools/client-runtime/environment";
33
import { parseThreadKey, threadKey } from "@t3tools/client-runtime/state/entities";
44
import { visibilityParentKey } from "@t3tools/client-runtime/state/threads";
5+
import type { ScopedThreadRef } from "@t3tools/contracts";
56
import * as Option from "effect/Option";
67
import { Navigate, useNavigate } from "@tanstack/react-router";
78
import { useEffect, useState } from "react";
@@ -16,15 +17,34 @@ import {
1617
useAnyEnvironmentHiding,
1718
useQuietThreadShell,
1819
} from "../state/entities";
20+
import { appAtomRegistry } from "../rpc/atomRegistry";
1921
import { useEnvironmentShellStatus } from "../state/shell";
20-
import { environmentThreadShells, useEnvironmentThread } from "../state/threads";
22+
import { environmentThreadShells, threadFamilies, useEnvironmentThread } from "../state/threads";
2123
import {
2224
buildThreadRouteParams,
2325
isThreadRouteSnapshotAuthoritative,
2426
resolveThreadRouteRenderState,
2527
type ThreadRouteTarget,
2628
} from "../threadRoutes";
2729

30+
/**
31+
* Keeps the family of an open thread the shell leaves out (an older settled thread) in the shell
32+
* rows until the route leaves it, so every reader of its shell row finds it.
33+
*/
34+
function useOpenThreadFamily(threadRef: ScopedThreadRef | null, missing: boolean) {
35+
const key = threadRef === null ? null : scopedThreadKey(threadRef);
36+
// Sticks to the route thread, so the row the family brings in does not release it.
37+
const [mountedKey, setMountedKey] = useState<string | null>(null);
38+
const nextKey = missing || mountedKey === key ? key : null;
39+
if (nextKey !== mountedKey) setMountedKey(nextKey);
40+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- holds the open thread's family in the thread family registry until the route leaves it
41+
useEffect(() => {
42+
const ref = mountedKey === null ? null : parseScopedThreadKey(mountedKey);
43+
if (ref === null) return;
44+
return threadFamilies.mount(appAtomRegistry, ref.environmentId, ref.threadId);
45+
}, [mountedKey]);
46+
}
47+
2848
/**
2949
* Unkeyed so navigating between server threads reuses one ChatView. Rendered by the `_chat`
3050
* layout, since an element only survives a route swap when the same parent renders it. Draft
@@ -88,6 +108,7 @@ export function ThreadRouteView({
88108
unlistedThreadRef?.environmentId ?? null,
89109
unlistedThreadRef?.threadId ?? null,
90110
);
111+
useOpenThreadFamily(serverThreadRef, bootstrapComplete && unlistedThreadRef !== null);
91112
const hiddenKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom);
92113
const unlistedThread = Option.getOrNull(Option.map(unlistedThreadDetail.data, (p) => p.thread));
93114
const unlistedParentKey =

‎apps/web/src/hooks/useThreadActionMenu.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ export function useThreadActionMenu(input: {
121121
const api = readLocalApi();
122122
if (!api) return;
123123
const thread = readThreadShell(threadRef);
124-
if (!thread) return;
124+
if (!thread) {
125+
failureToast("Thread actions unavailable", new Error("This thread has not loaded yet."));
126+
return;
127+
}
125128
const now = new Date();
126129
const configLoaded = readEnvironmentServerConfigLoaded(threadRef.environmentId);
127130
const supports = {

‎apps/web/test/fakeEnvironment.ts‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const auth = {
6868
sessionCookieName: "t3_session",
6969
};
7070

71-
const serverConfig: ServerConfig = {
71+
const baseServerConfig: ServerConfig = {
7272
...Schema.decodeUnknownSync(ServerConfig)({
7373
environment: descriptor,
7474
auth,
@@ -106,6 +106,11 @@ const serverConfig: ServerConfig = {
106106
}),
107107
keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
108108
};
109+
// A server only leaves threads out of the shell when it serves their families.
110+
const serverConfig = (): ServerConfig =>
111+
threads.some((thread) => thread.olderSettled)
112+
? { ...baseServerConfig, shellThreadFamilies: true }
113+
: baseServerConfig;
109114

110115
const project: OrchestrationProjectShell = {
111116
id: PROJECT_ID,
@@ -189,6 +194,8 @@ function run(threadId: ThreadId, id: RunId, ordinal: number, userMessageId: stri
189194
export interface FakeThread {
190195
/** Makes this a side chat of that thread. */
191196
readonly sideChatOf?: ThreadId;
197+
/** Makes this an older settled thread the shell leaves out; only its family read returns it. */
198+
readonly olderSettled?: true;
192199
readonly id: ThreadId;
193200
readonly title: string;
194201
readonly runs: ReadonlyArray<OrchestrationV2Run>;
@@ -253,8 +260,8 @@ function shellOf(thread: FakeThread): OrchestrationV2ThreadShell {
253260
createdAt: AT,
254261
updatedAt: AT,
255262
archivedAt: null,
256-
settledOverride: null,
257-
settledAt: null,
263+
settledOverride: thread.olderSettled ? "settled" : null,
264+
settledAt: thread.olderSettled ? AT : null,
258265
lastVisitedAt: AT,
259266
deletedAt: null,
260267
};
@@ -290,8 +297,8 @@ function projectionOf(thread: FakeThread): OrchestrationV2ThreadProjection {
290297
createdAt: AT,
291298
updatedAt: AT,
292299
archivedAt: null,
293-
settledOverride: null,
294-
settledAt: null,
300+
settledOverride: shell.settledOverride,
301+
settledAt: shell.settledAt,
295302
lastVisitedAt: AT,
296303
deletedAt: null,
297304
},
@@ -363,7 +370,7 @@ const handlers: Record<string, (input: never) => unknown> = {
363370
schemaVersion: 1,
364371
snapshotSequence: sequence,
365372
projects: [project],
366-
threads: threads.map(shellOf),
373+
threads: threads.filter((thread) => !thread.olderSettled).map(shellOf),
367374
archivedThreads: [],
368375
},
369376
},
@@ -387,6 +394,16 @@ const handlers: Record<string, (input: never) => unknown> = {
387394
},
388395
);
389396
},
397+
[ORCHESTRATION_V2_WS_METHODS.getThreadFamily]: (input: { readonly threadId: ThreadId }) => {
398+
const rootOf = (thread: FakeThread) => thread.sideChatOf ?? thread.id;
399+
const requested = threads.find((candidate) => candidate.id === input.threadId);
400+
return Effect.succeed({
401+
snapshotSequence: sequence,
402+
threads: requested
403+
? threads.filter((thread) => rootOf(thread) === rootOf(requested)).map(shellOf)
404+
: [],
405+
});
406+
},
390407
[WS_METHODS.subscribeVcsStatus]: () =>
391408
subscribe<VcsStatusStreamEvent>([], (queue) => {
392409
vcsStatusSubscriber = queue;
@@ -395,7 +412,7 @@ const handlers: Record<string, (input: never) => unknown> = {
395412
};
396413
}),
397414
[WS_METHODS.subscribeServerConfig]: () =>
398-
subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig }]),
415+
subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig() }]),
399416
};
400417

401418
// Unanswered calls stay pending, like a server that has not replied yet.
@@ -410,9 +427,9 @@ const client = new Proxy(handlers, {
410427

411428
const session: EnvironmentConnectionLease["session"] = {
412429
client: client as unknown as WsRpcProtocolClient,
413-
initialConfig: Effect.succeed(serverConfig),
430+
initialConfig: Effect.sync(serverConfig),
414431
subscribeServerConfig: () =>
415-
subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig }]),
432+
subscribe([{ version: 1 as const, type: "snapshot" as const, config: serverConfig() }]),
416433
ready: Effect.void,
417434
probe: Effect.void,
418435
inboundFrames: Effect.succeed(0),

0 commit comments

Comments
 (0)