The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 15:45 UTC
fix(mobile): a caret move in one composer no longer moves another draft's insert point (#25681)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 26, 2026 at 16:19 UTC
78e93fd9d15ccecb2009804ee308fce2bd7b21dc
4 files changed
+146
-43
apps/mobile/src/features/threads/use-composer-command-menu.test.ts
+37
-1
@@ -9,8 +9,8 @@ vi.mock("../../state/queries", () => ({
9
}));
10
vi.mock("../../state/use-composer-drafts", () => ({
11
getComposerDraftSnapshot: vi.fn(),
12
- readComposerDraftSelection: vi.fn(),
12
setComposerDraftContext: vi.fn(),
13
+ useComposerDraftSelection: vi.fn(),
14
}));
15
vi.mock("../../lib/uuid", () => ({ uuidv4: () => "context-id" }));
16
vi.mock("../../state/server", () => ({
@@ -23,8 +23,44 @@ vi.mock("../../state/server", () => ({
23
import {
24
buildComposerSlashCommandItems,
25
resolveComposerCommandSelection,
26
+ resolveComposerMenuSelection,
27
} from "./use-composer-command-menu";
28
29
+describe("resolveComposerMenuSelection", () => {
30
+ it("prefers the caret an insertion placed in the current draft text", () => {
31
+ expect(
32
+ resolveComposerMenuSelection({
33
+ draftMessage: "hello @chip ",
34
+ ownerKey: "draft-a",
35
+ inserted: { start: 12, end: 12 },
36
+ local: { ownerKey: "draft-a", start: 2, end: 2 },
37
+ }),
38
+ ).toEqual({ start: 12, end: 12 });
39
+ });
40
+
41
+ it("clamps the composer's own caret when the draft shrinks", () => {
42
+ expect(
43
+ resolveComposerMenuSelection({
44
+ draftMessage: "hi",
45
+ ownerKey: "draft-a",
46
+ inserted: null,
47
+ local: { ownerKey: "draft-a", start: 5, end: 9 },
48
+ }),
49
+ ).toEqual({ start: 2, end: 2 });
50
+ });
51
+
52
+ it("starts at the end of the draft after switching owners", () => {
53
+ expect(
54
+ resolveComposerMenuSelection({
55
+ draftMessage: "other draft",
56
+ ownerKey: "draft-b",
57
+ inserted: null,
58
+ local: { ownerKey: "draft-a", start: 1, end: 1 },
59
+ }),
60
+ ).toEqual({ start: 11, end: 11 });
61
+ });
62
+});
63
+
64
describe("mobile slash commands", () => {
65
const antigravity = {
66
driver: ProviderDriverKind.make("antigravity"),
apps/mobile/src/features/threads/use-composer-command-menu.ts
+36
-28
@@ -17,8 +17,8 @@ import { pullRequestComposerContext, threadComposerContext } from "../../lib/com
17
import { uuidv4 } from "../../lib/uuid";
18
import {
19
getComposerDraftSnapshot,
20
- readComposerDraftSelection,
20
setComposerDraftContext,
21
+ useComposerDraftSelection,
22
} from "../../state/use-composer-drafts";
23
import { USAGE_LIMITS_COMMAND } from "@t3tools/shared/usageLimits";
24
import {
@@ -43,7 +43,7 @@ import {
43
} from "@t3tools/client-runtime/providerSkills";
44
import { useAtomRefresh } from "@effect/atom-react";
45
import { Atom } from "effect/reactivity";
46
-import { useEffect, useRef, useState } from "react";
46
+import { useEffect, useState } from "react";
47
48
import type { ComposerEditorSelection } from "../../components/ComposerEditor";
49
import { serverEnvironment } from "../../state/server";
@@ -68,6 +68,27 @@ function composerSelectionAtEnd(draftMessage: string): ComposerEditorSelection {
68
return { start: draftMessage.length, end: draftMessage.length };
69
}
70
71
+/**
72
+ * A caret placed by an insertion (chips, pasted context) wins while the draft
73
+ * still has the text it was placed in. Otherwise the composer's own caret is
74
+ * clamped to the draft, and a caret from another owner starts at the end.
75
+ */
76
+export function resolveComposerMenuSelection(input: {
77
+ readonly draftMessage: string;
78
+ readonly ownerKey: string | null;
79
+ readonly inserted: ComposerEditorSelection | null;
80
+ readonly local: {
81
+ readonly ownerKey: string | null;
82
+ readonly start: number;
83
+ readonly end: number;
84
+ };
85
+}): ComposerEditorSelection {
86
+ if (input.inserted) return input.inserted;
87
+ if (input.local.ownerKey !== input.ownerKey) return composerSelectionAtEnd(input.draftMessage);
88
+ const length = input.draftMessage.length;
89
+ return { start: Math.min(input.local.start, length), end: Math.min(input.local.end, length) };
90
+}
91
+
92
export function buildComposerSlashCommandItems(input: {
93
readonly query: string;
94
readonly atMessageStart: boolean;
@@ -212,34 +233,21 @@ export function useComposerCommandMenu({
233
readonly onUpdateInteractionMode?: (mode: ProviderInteractionMode) => void;
234
readonly onUsageLimits?: () => void;
235
}) {
215
- const [selection, setSelection] = useState(() => composerSelectionAtEnd(draftMessage));
236
+ const [localSelection, setLocalSelection] = useState(() => ({
237
+ ownerKey,
238
+ ...composerSelectionAtEnd(draftMessage),
239
+ }));
240
// Thread mentions rank by recency as of when the composer mounted.
241
const [now] = useState(() => Date.now());
218
- const previousOwnerKeyRef = useRef(ownerKey);
219
- useEffect(() => {
220
- const inserted = ownerKey ? readComposerDraftSelection(ownerKey, draftMessage) : null;
221
- if (inserted) {
222
- // oxlint-disable-next-line react/set-state-in-effect -- Applies a selection inserted into the draft store by another surface.
223
- setSelection((current) =>
224
- current.start === inserted.start && current.end === inserted.end ? current : inserted,
225
- );
226
- return;
227
- }
228
- const end = draftMessage.length;
229
- setSelection((current) => {
230
- const start = Math.min(current.start, end);
231
- const selectionEnd = Math.min(current.end, end);
232
- if (start === current.start && selectionEnd === current.end) {
233
- return current;
234
- }
235
- return { start, end: selectionEnd };
236
- });
237
- }, [draftMessage, ownerKey]);
238
- useEffect(() => {
239
- if (previousOwnerKeyRef.current === ownerKey) return;
240
- previousOwnerKeyRef.current = ownerKey;
241
- setSelection(composerSelectionAtEnd(draftMessage));
242
- }, [draftMessage, ownerKey]);
242
+ const insertedSelection = useComposerDraftSelection(ownerKey, draftMessage);
243
+ const selection = resolveComposerMenuSelection({
244
+ draftMessage,
245
+ ownerKey,
246
+ inserted: insertedSelection,
247
+ local: localSelection,
248
+ });
249
+ const setSelection = (next: ComposerEditorSelection) =>
250
+ setLocalSelection({ ownerKey, start: next.start, end: next.end });
251
252
const skills = selectedProviderStatus
253
? resolveProviderSkillsForCwd(selectedProviderStatus, projectCwd)
apps/mobile/src/state/use-composer-drafts.test.ts
+31
@@ -981,6 +981,37 @@ describe("mobile composer drafts", () => {
981
expect(getComposerDraftSnapshot(draftKey).context).toBeUndefined();
982
});
983
984
+ it("keeps each draft's caret when another composer moves its own caret", () => {
985
+ const threadKey = "caret-environment:caret-thread";
986
+ const newTaskKey = "caret-environment:new-task:caret-draft";
987
+ setComposerDraftText(threadKey, "Fix this next");
988
+ setComposerDraftText(newTaskKey, "Another task");
989
+ rememberComposerDraftSelection(threadKey, "Fix this next", { start: 4, end: 8 });
990
+ rememberComposerDraftSelection(newTaskKey, "Another task", { start: 2, end: 2 });
991
+ insertComposerDraftContext(threadKey, {
992
+ text: "[Build output](t3-context://v1/terminal/caret-terminal)",
993
+ context: {
994
+ version: 1,
995
+ records: [
996
+ {
997
+ version: 1,
998
+ kind: "terminal",
999
+ contextId: ComposerContextId.make("caret-terminal"),
1000
+ label: "Build output",
1001
+ terminalId: "main",
1002
+ terminalLabel: "Terminal",
1003
+ lineStart: 1,
1004
+ lineEnd: 1,
1005
+ text: "Build failed",
1006
+ },
1007
+ ],
1008
+ },
1009
+ });
1010
+ expect(getComposerDraftSnapshot(threadKey).text).toBe(
1011
+ "Fix [Build output](t3-context://v1/terminal/caret-terminal) next",
1012
+ );
1013
+ });
1014
+
1015
it("releases videos rejected by the live draft limit and keeps accepted files", async () => {
1016
const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true);
1017
onTestFinished(() => outboxLoad.mockRestore());
apps/mobile/src/state/use-composer-drafts.ts
+42
-14
@@ -88,26 +88,57 @@ export function setComposerContextImporting(draftKey: string, importing: boolean
88
appAtomRegistry.set(composerContextImportsAtom, next);
89
}
90
91
-let lastComposerSelection: { draftKey: string; text: string; start: number; end: number } | null =
92
- null;
91
+interface ComposerDraftSelection {
92
+ readonly text: string;
93
+ readonly start: number;
94
+ readonly end: number;
95
+}
96
+
97
+// Keyed by draft so a caret move in one composer neither replaces another draft's caret
98
+// nor notifies its readers. Kept alive so the caret survives the composer unmounting.
99
+const composerDraftSelectionAtom = Atom.family((_draftKey: string) =>
100
+ Atom.make<ComposerDraftSelection | null>(null).pipe(
101
+ Atom.keepAlive,
102
+ Atom.withLabel("mobile:composer-draft-selection"),
103
+ ),
104
+);
105
+const NO_COMPOSER_DRAFT_SELECTION_ATOM = Atom.make<ComposerDraftSelection | null>(null);
106
107
export function rememberComposerDraftSelection(
108
draftKey: string,
109
text: string,
110
selection: { start: number; end: number },
111
): void {
99
- lastComposerSelection = { draftKey, text, ...selection };
112
+ appAtomRegistry.set(composerDraftSelectionAtom(draftKey), { text, ...selection });
113
+}
114
+
115
+function matchingComposerDraftSelection(
116
+ selection: ComposerDraftSelection | null,
117
+ text: string,
118
+): { start: number; end: number } | null {
119
+ if (selection?.text !== text) return null;
120
+ return { start: selection.start, end: selection.end };
121
}
122
123
/** Read the post-chip-insertion selection or the editor restores the pre-insert caret before the chip. */
103
-export function readComposerDraftSelection(
124
+function readComposerDraftSelection(
125
draftKey: string,
126
text: string,
127
): { start: number; end: number } | null {
107
- if (lastComposerSelection?.draftKey !== draftKey || lastComposerSelection.text !== text) {
108
- return null;
109
- }
110
- return { start: lastComposerSelection.start, end: lastComposerSelection.end };
128
+ return matchingComposerDraftSelection(
129
+ appAtomRegistry.get(composerDraftSelectionAtom(draftKey)),
130
+ text,
131
+ );
132
+}
133
+
134
+export function useComposerDraftSelection(
135
+ draftKey: string | null,
136
+ text: string,
137
+): { start: number; end: number } | null {
138
+ const selection = useAtomValue(
139
+ draftKey === null ? NO_COMPOSER_DRAFT_SELECTION_ATOM : composerDraftSelectionAtom(draftKey),
140
+ );
141
+ return draftKey === null ? null : matchingComposerDraftSelection(selection, text);
142
}
143
144
export interface ComposerDraftInsertion {
@@ -133,8 +164,7 @@ function contextInsertionRange(
164
draft: ComposerDraft,
165
target?: ComposerDraftInsertion,
166
) {
136
- const captured =
137
- target ?? (lastComposerSelection?.draftKey === draftKey ? lastComposerSelection : null);
167
+ const captured = target ?? appAtomRegistry.get(composerDraftSelectionAtom(draftKey));
168
// Append after text edits during writes; caret-only moves preserve the captured insertion range.
169
const selection = captured?.text === draft.text ? captured : null;
170
const start = Math.max(0, Math.min(selection?.start ?? draft.text.length, draft.text.length));
@@ -307,12 +337,10 @@ function draftWithInsertedContext(
337
for (const record of content.context.records) records.set(record.contextId, record);
338
const context = referencedComposerContext(text, { version: 1, records: [...records.values()] });
339
if ((context?.records.length ?? 0) > COMPOSER_CONTEXT_MAX_RECORDS) return null;
310
- lastComposerSelection = {
311
- draftKey,
312
- text,
340
+ rememberComposerDraftSelection(draftKey, text, {
341
start: start + insertion.length,
342
end: start + insertion.length,
315
- };
343
+ });
344
return withReferencedContextFiles(draft, text, context);
345
}
346