Skip to content

Commit 2add256

Browse files
committed
fix(web): retain remote thread targets after borrowed rows disappear
Remember the selected remote reference for the current link and reacquire its family when another component releases its row. Preserve ambiguity handling and release the retained family when the link changes.
1 parent fa8b1b0 commit 2add256

2 files changed

Lines changed: 66 additions & 21 deletions

File tree

‎apps/web/src/components/chat/MarkdownThreadLink.browser.test.tsx‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,21 @@ const row = (id: string, title: string) =>
6565
({ id: ThreadId.make(id), title }) as OrchestrationV2ThreadShell;
6666

6767
function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map()) {
68-
appAtomRegistry.set(mocks.shells, shells);
68+
const baseShells = new Map(shells);
69+
appAtomRegistry.set(mocks.shells, baseShells);
6970
const mounted = new Map<string, OrchestrationV2ThreadFamily>();
7071
const counts = new Map<string, number>();
7172
const requested = new Set<string>();
73+
const publish = () => {
74+
const rows = new Map(baseShells);
75+
for (const [familyKey, family] of mounted) {
76+
const [environmentId] = JSON.parse(familyKey) as [EnvironmentId, ThreadId];
77+
for (const thread of family.threads) {
78+
rows.set(key({ environmentId, threadId: thread.id }), thread);
79+
}
80+
}
81+
appAtomRegistry.set(mocks.shells, rows);
82+
};
7283
const queries = vi
7384
.spyOn(threadFamilies, "familyAtom")
7485
.mockImplementation((environmentId, threadId) => {
@@ -85,16 +96,6 @@ function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map
8596
const id = key({ environmentId, threadId });
8697
const query = threadFamilies.familyAtom(environmentId, threadId);
8798
counts.set(id, (counts.get(id) ?? 0) + 1);
88-
const publish = () => {
89-
const rows = new Map(shells);
90-
for (const [familyKey, family] of mounted) {
91-
const [environmentId] = JSON.parse(familyKey) as [EnvironmentId, ThreadId];
92-
for (const thread of family.threads) {
93-
rows.set(key({ environmentId, threadId: thread.id }), thread);
94-
}
95-
}
96-
registry.set(mocks.shells, rows);
97-
};
9899
const unsubscribe = registry.subscribe(
99100
query,
100101
(result) => {
@@ -114,6 +115,12 @@ function setup(shells: ReadonlyMap<string, OrchestrationV2ThreadShell> = new Map
114115
});
115116
return {
116117
requested,
118+
setShell: (target: ScopedThreadRef, thread: OrchestrationV2ThreadShell | null) =>
119+
act(() => {
120+
if (thread === null) baseShells.delete(key(target));
121+
else baseShells.set(key(target), thread);
122+
publish();
123+
}),
117124
fail: (target: ScopedThreadRef, waiting = false) =>
118125
act(() =>
119126
appAtomRegistry.set(
@@ -211,6 +218,29 @@ describe("thread link family lookups", () => {
211218
await expect
212219
.element(screen.getByRole("link", { name: "Thread, Remote thread" }))
213220
.toBeInTheDocument();
221+
expect(fixture.requested.has(key(remote))).toBe(false);
222+
await fixture.setShell(remote, null);
223+
expect(fixture.requested.has(key(remote))).toBe(true);
224+
await fixture.resolve(remote, [row(remote.threadId, "Remote thread reloaded")]);
225+
await expect
226+
.element(screen.getByRole("link", { name: "Thread, Remote thread reloaded" }))
227+
.toBeInTheDocument();
228+
const duplicate = ref(remote.threadId, EnvironmentId.make("another-remote-environment"));
229+
await fixture.setShell(duplicate, row(duplicate.threadId, "Ambiguous remote thread"));
230+
await expect
231+
.element(screen.getByRole("link", { name: "Thread, Remote thread reloaded" }))
232+
.not.toBeInTheDocument();
233+
await fixture.setShell(duplicate, null);
234+
await expect
235+
.element(screen.getByRole("link", { name: "Thread, Remote thread reloaded" }))
236+
.toBeInTheDocument();
237+
const next = ref("different-thread");
238+
await screen.rerender(view(next, "markdown", true));
239+
await fixture.fail(next);
240+
await expect
241+
.element(screen.getByRole("link", { name: "Thread, Remote thread reloaded" }))
242+
.not.toBeInTheDocument();
243+
expect(appAtomRegistry.get(mocks.shells).has(key(remote))).toBe(false);
214244
},
215245
);
216246

‎apps/web/src/components/chat/MarkdownThreadLink.tsx‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import { threadFamilyRead } from "@t3tools/client-runtime/state/threads";
3-
import type { EnvironmentId } from "@t3tools/contracts";
3+
import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
44
import { AsyncResult } from "effect/reactivity";
5+
import { useState } from "react";
56
import {
67
formatThreadLink,
78
guessThreadLinkRef,
@@ -52,15 +53,29 @@ export function MarkdownThreadLink(props: {
5253
(lookupFailed || (writtenMissing && decodedMissing)) &&
5354
props.link.environmentId === null;
5455
const refs = useThreadRefs(guess);
55-
const remote = useThreadShell(
56-
guess
57-
? guessThreadLinkRef(
58-
props.link,
59-
refs.filter((ref) => ref.environmentId !== environmentId),
60-
)
61-
: null,
62-
);
63-
const thread = local ?? remote;
56+
const guessedRef = guess
57+
? guessThreadLinkRef(
58+
props.link,
59+
refs.filter((ref) => ref.environmentId !== environmentId),
60+
)
61+
: null;
62+
const sourceKey = JSON.stringify([environmentId, props.link.threadId]);
63+
const [remoteSelection, setRemoteSelection] = useState<{
64+
sourceKey: string;
65+
ref: ScopedThreadRef;
66+
} | null>(null);
67+
const rememberedRef = remoteSelection?.sourceKey === sourceKey ? remoteSelection.ref : null;
68+
if (
69+
guessedRef !== null &&
70+
(rememberedRef?.environmentId !== guessedRef.environmentId ||
71+
rememberedRef.threadId !== guessedRef.threadId)
72+
) {
73+
setRemoteSelection({ sourceKey, ref: guessedRef });
74+
}
75+
const remoteRef = guess ? (guessedRef ?? rememberedRef) : null;
76+
const remote = useThreadShell(remoteRef);
77+
useThreadFamilyIfMissing(remoteRef, remoteRef !== null && remote === null);
78+
const thread = local ?? (guessedRef === null ? null : remote);
6479
if (thread === null) {
6580
const unknown = unknownThreadLinkLabel(props.link, props.environmentId);
6681
return <UnresolvedChip label={props.label ?? unknown} tooltip={unknown} />;

0 commit comments

Comments
 (0)