The best way to control Claude Code, Codex and more from any computer or phone
perf(mobile): composer draft no longer re-renders the thread screen when typing (#21261)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 18, 2026 at 14:33 UTC
d30899cb503dc453d35167826193939d84548baa
10 files changed
+144
-53
apps/mobile/src/features/threads/ThreadComposer.tsx
+14
-19
@@ -55,6 +55,7 @@ import {
55
setComposerDraftText,
56
composerContextImportsAtom,
57
countComposerDraftAttachmentsAfterSelection,
58
+ useComposerDraft,
59
} from "../../state/use-composer-drafts";
60
import { appAtomRegistry } from "../../state/atom-registry";
61
import type { ComposerDocumentAttachment } from "../../lib/composerContext";
@@ -79,7 +80,6 @@ import {
80
import { ProviderIcon } from "../../components/ProviderIcon";
81
import {
82
composerStripAttachments,
82
- type DraftComposerAttachment,
83
type DraftComposerFileAttachment,
84
} from "../../lib/composerImages";
85
import {
@@ -130,8 +130,6 @@ export const COMPOSER_EXPANDED_CHROME = 156;
130
131
export interface ThreadComposerProps {
132
readonly canOperateThread: boolean;
133
- readonly draftMessage: string;
134
- readonly draftAttachments: ReadonlyArray<DraftComposerAttachment>;
133
readonly placeholder: string;
134
readonly contentMaxWidth?: number;
135
readonly bottomInset?: number;
@@ -349,12 +347,15 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
347
348
const [previewFile, setPreviewFile] = useState<FilePreviewSource | null>(null);
349
const [previewVideo, setPreviewVideo] = useState<VideoPreviewSource | null>(null);
350
+ const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
351
+ const composerDraftKey = props.draftKey ?? composerOwnerKey;
352
+ const { text: draftMessage, attachments: draftAttachments } = useComposerDraft(composerDraftKey);
353
const queuedEdit = props.queuedEdit ?? null;
354
const hasContent =
354
- props.draftMessage.trim().length > 0 ||
355
- props.draftAttachments.length > 0 ||
355
+ draftMessage.trim().length > 0 ||
356
+ draftAttachments.length > 0 ||
357
(queuedEdit?.existingAttachments.length ?? 0) > 0;
357
- const stripAttachments = composerStripAttachments(props.draftAttachments);
358
+ const stripAttachments = composerStripAttachments(draftAttachments);
359
const showStopAction = !hasContent && props.canStopThread && queuedEdit === null;
360
361
const uploadStates = useAtomValue(composerAttachmentUploadsAtom);
@@ -362,7 +363,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
363
props.connectionState === "connected" &&
364
composerAttachmentsStillUploading({
365
environmentId: props.environmentId,
365
- attachments: props.draftAttachments,
366
+ attachments: draftAttachments,
367
serverConfig: props.serverConfig,
368
states: uploadStates,
369
});
@@ -392,8 +393,6 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
393
) ?? null
394
);
395
})();
395
- const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
396
- const composerDraftKey = props.draftKey ?? composerOwnerKey;
396
const openDraftDocument = (attachment: ComposerDocumentAttachment) => {
397
Keyboard.dismiss();
398
navigation.navigate("ThreadAttachment", {
@@ -429,7 +428,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
428
};
429
430
const composerMenu = useComposerCommandMenu({
432
- draftMessage: props.draftMessage,
431
+ draftMessage: draftMessage,
432
ownerKey: composerDraftKey,
433
environmentId: props.environmentId,
434
threadShells: useThreadShells(),
@@ -450,7 +449,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
449
: props.onUpdateInteractionMode,
450
offersUsageLimits: usageLimitsOffered,
451
onUsageLimits:
453
- usageLimitsOffered && props.draftAttachments.length === 0 ? openUsageLimits : undefined,
452
+ usageLimitsOffered && draftAttachments.length === 0 ? openUsageLimits : undefined,
453
});
454
const voiceInput = useVoiceInputController({
455
ownerKey: composerDraftKey,
@@ -471,7 +470,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
470
const isToolbarVisible = isExpanded || isVoiceInputPresented;
471
const attachmentBlockReason = composerAttachmentUploadBlockReason({
472
environmentId: props.environmentId,
474
- attachments: props.draftAttachments,
473
+ attachments: draftAttachments,
474
connected: props.connectionState === "connected",
475
serverConfig: props.serverConfig,
476
states: uploadStates,
@@ -501,7 +500,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
500
(current) =>
501
current ?? {
502
...source,
504
- gallery: filePreviewGallery(source, composerImageGallery(props.draftAttachments)),
503
+ gallery: filePreviewGallery(source, composerImageGallery(draftAttachments)),
504
},
505
);
506
};
@@ -538,11 +537,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
537
};
538
const handleSend = async (followUp?: ActiveTurnComposerAction) => {
539
if (voiceInput.blocksSubmission || pendingPastedTextAttachmentCountRef.current > 0) return;
541
- if (
542
- usageLimitsOffered &&
543
- isUsageLimitsCommand(props.draftMessage) &&
544
- props.draftAttachments.length === 0
545
- ) {
540
+ if (usageLimitsOffered && isUsageLimitsCommand(draftMessage) && draftAttachments.length === 0) {
541
if (openUsageLimits()) onChangeDraftMessage("");
542
return;
543
}
@@ -808,7 +803,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
803
onInertChipPress={() => inputRef.current?.focus()}
804
ref={inputRef}
805
multiline
811
- value={props.draftMessage}
806
+ value={draftMessage}
807
readOnly={voiceInput.freezesEditor}
808
skills={composerMenu.skills}
809
selection={composerMenu.selection}
apps/mobile/src/features/threads/ThreadDetailScreen.tsx
+5
-7
@@ -88,7 +88,7 @@ import { useAppearancePreferences } from "../settings/appearance/AppearancePrefe
88
import { collectProviderUsageLimits } from "@t3tools/shared/usageLimits";
89
import type { ComposerEditorHandle } from "../../components/ComposerEditor";
90
import type { StatusTone } from "../../components/StatusPill";
91
-import type { DraftComposerAttachment } from "../../lib/composerImages";
91
+import { getComposerDraftSnapshot } from "../../state/use-composer-drafts";
92
import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary";
93
import {
94
ANDROID_READING_MAX_WIDTH,
@@ -187,8 +187,6 @@ export interface ThreadDetailScreenProps {
187
readonly activePendingUserInputDrafts: Record<string, PendingUserInputDraftAnswer>;
188
readonly activePendingUserInputAnswers: Record<string, string | ReadonlyArray<string>> | null;
189
readonly respondingUserInputId: RuntimeRequestId | null;
190
- readonly draftMessage: string;
191
- readonly draftAttachments: ReadonlyArray<DraftComposerAttachment>;
190
readonly connectionStateLabel: EnvironmentConnectionPhase;
191
readonly threadSyncStatus?: EnvironmentThreadStatus;
192
readonly historyControls?: ThreadFeedHistoryControls;
@@ -949,8 +947,10 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
947
};
948
949
const handleUseArtifactTemplate = (template: CodexArtifactTemplate) => {
952
- const nextDraft = appendCodexArtifactTemplateUsePrompt(props.draftMessage, template);
953
- if (nextDraft !== props.draftMessage) props.onChangeDraftMessage(nextDraft);
950
+ const draftMessage =
951
+ props.composerDraftKey === null ? "" : getComposerDraftSnapshot(props.composerDraftKey).text;
952
+ const nextDraft = appendCodexArtifactTemplateUsePrompt(draftMessage, template);
953
+ if (nextDraft !== draftMessage) props.onChangeDraftMessage(nextDraft);
954
requestAnimationFrame(() => {
955
composerEditorRef.current?.focus();
956
composerEditorRef.current?.setSelection({ start: nextDraft.length, end: nextDraft.length });
@@ -1300,8 +1300,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
1300
canOperateThread={props.canOperateThread}
1301
reportedModelSelection={reportedModelSelection}
1302
editorRef={composerEditorRef}
1303
- draftMessage={props.draftMessage}
1304
- draftAttachments={props.draftAttachments}
1303
placeholder="Ask the repo agent, or run a command…"
1304
contentMaxWidth={contentMaxWidth}
1305
connectionState={props.connectionStateLabel}
apps/mobile/src/features/threads/ThreadRouteScreen.tsx
-2
@@ -926,8 +926,6 @@ function ThreadRouteContent(
926
activePendingUserInputDrafts={requests.activePendingUserInputDrafts}
927
activePendingUserInputAnswers={requests.activePendingUserInputAnswers}
928
respondingUserInputId={requests.respondingUserInputId}
929
- draftMessage={composer.draftMessage}
930
- draftAttachments={composer.draftAttachments}
929
connectionStateLabel={routeConnectionState}
930
threadSyncStatus={selectedThreadDetailState.status}
931
historyControls={historyControls}
apps/mobile/src/state/composer-attachment-uploads.ts
+2
-1
@@ -21,6 +21,7 @@ import { useThreadOutboxMessages } from "./use-thread-outbox";
21
import {
22
ComposerDraftPersistenceError,
23
composerDraftsAtom,
24
+ composerDraftUploadSourcesAtom,
25
ensureComposerDraftsLoaded,
26
flushComposerDrafts,
27
setComposerDraftAttachmentUpload,
@@ -56,7 +57,7 @@ export function retryComposerAttachmentUpload(environmentId: EnvironmentId, atta
57
}
58
59
export function useComposerAttachmentUploadWorker() {
59
- const drafts = useAtomValue(composerDraftsAtom);
60
+ const drafts = useAtomValue(composerDraftUploadSourcesAtom);
61
const queuedMessages = useThreadOutboxMessages();
62
const serverConfigs = useServerConfigs();
63
const { connectedEnvironments } = useRemoteConnectionStatus();
apps/mobile/src/state/thread-task-permissions.test.ts
+2
@@ -126,6 +126,8 @@ vi.mock("./use-composer-drafts", async () => {
126
mergeComposerDraftContent: vi.fn(),
127
removeComposerDraftAttachment: vi.fn(),
128
useComposerDraft: () => state.draft,
129
+ useComposerDraftSettings: () => ({}),
130
+ questionAttachmentDraftsAtom: () => Atom.make({}),
131
};
132
});
133
vi.mock("../lib/composerImages", () => ({
apps/mobile/src/state/use-composer-drafts.ts
+100
-8
@@ -171,16 +171,18 @@ function withReferencedContextFiles(
171
const retainedIds = new Set(
172
context?.records.flatMap((record) => ("attachmentId" in record ? [record.attachmentId] : [])),
173
);
174
+ const attachments = draft.attachments.filter(
175
+ (attachment) =>
176
+ attachment.type === "image" ||
177
+ !previousIds.has(attachment.id) ||
178
+ retainedIds.has(attachment.id),
179
+ );
180
+ // Keep the array identity on plain text edits so attachment readers see no change.
181
return {
182
...draft,
183
text,
184
context,
178
- attachments: draft.attachments.filter(
179
- (attachment) =>
180
- attachment.type === "image" ||
181
- !previousIds.has(attachment.id) ||
182
- retainedIds.has(attachment.id),
183
- ),
185
+ attachments: attachments.length === draft.attachments.length ? draft.attachments : attachments,
186
};
187
}
188
@@ -1885,12 +1887,102 @@ export async function clearComposerDraftsEnvironment(environmentId: EnvironmentI
1887
await releaseUnusedComposerAttachmentFiles(removedAttachments);
1888
}
1889
1890
+// One atom per draft key, so typing in one composer only notifies readers of
1891
+// that draft instead of every subscriber to the whole drafts map.
1892
+const composerDraftAtom = Atom.family((draftKey: string) =>
1893
+ Atom.make((get) => get(composerDraftsAtom)[draftKey] ?? EMPTY_DRAFT),
1894
+);
1895
+const EMPTY_DRAFT_ATOM = Atom.make(EMPTY_DRAFT);
1896
+
1897
+type ComposerDraftsByKey = Readonly<Record<string, ComposerDraft>>;
1898
+const EMPTY_DRAFTS: ComposerDraftsByKey = {};
1899
+function sameDrafts(a: ComposerDraftsByKey, b: ComposerDraftsByKey): boolean {
1900
+ const keys = Object.keys(a);
1901
+ return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
1902
+}
1903
+// The question drafts under one key prefix. Equal while only other drafts change
1904
+// (the open thread's composer text, for example), so typing does not notify readers.
1905
+export const questionAttachmentDraftsAtom = Atom.family((prefix: string) =>
1906
+ Atom.make((get): ComposerDraftsByKey => {
1907
+ if (prefix === "") return EMPTY_DRAFTS;
1908
+ const matching = Object.entries(get(composerDraftsAtom)).filter(([key]) =>
1909
+ key.startsWith(prefix),
1910
+ );
1911
+ return matching.length === 0 ? EMPTY_DRAFTS : Object.fromEntries(matching);
1912
+ }).pipe(Atom.withEquality(sameDrafts)),
1913
+);
1914
+
1915
+// New-task drafts only; equal while a thread composer's text changes.
1916
+export const newTaskComposerDraftsAtom = Atom.make((get): ComposerDraftsByKey => {
1917
+ const matching = Object.entries(get(composerDraftsAtom)).filter(([key]) =>
1918
+ isNewTaskDraftKey(key),
1919
+ );
1920
+ return matching.length === 0 ? EMPTY_DRAFTS : Object.fromEntries(matching);
1921
+}).pipe(Atom.withEquality(sameDrafts));
1922
+
1923
+type ComposerDraftUploadSource = Pick<ComposerDraft, "attachments" | "project">;
1924
+// What the upload worker reads: equal while only draft text changes.
1925
+type ComposerDraftUploadSources = Readonly<Record<string, ComposerDraftUploadSource>>;
1926
+export const composerDraftUploadSourcesAtom = Atom.make((get): ComposerDraftUploadSources =>
1927
+ Object.fromEntries(
1928
+ Object.entries(get(composerDraftsAtom)).map(([key, draft]) => [
1929
+ key,
1930
+ { attachments: draft.attachments, project: draft.project },
1931
+ ]),
1932
+ ),
1933
+).pipe(
1934
+ Atom.withEquality((a: ComposerDraftUploadSources, b: ComposerDraftUploadSources) => {
1935
+ const keys = Object.keys(a);
1936
+ return (
1937
+ keys.length === Object.keys(b).length &&
1938
+ keys.every(
1939
+ (key) => a[key]?.attachments === b[key]?.attachments && a[key]?.project === b[key]?.project,
1940
+ )
1941
+ );
1942
+ }),
1943
+);
1944
+
1945
+type ComposerDraftSettings = Pick<
1946
+ ComposerDraft,
1947
+ "modelSelection" | "runtimeMode" | "interactionMode"
1948
+>;
1949
+const EMPTY_DRAFT_SETTINGS: ComposerDraftSettings = {};
1950
+// Text edits copy the other fields by reference, so this stays equal while typing.
1951
+const composerDraftSettingsAtom = Atom.family((draftKey: string) =>
1952
+ Atom.make((get): ComposerDraftSettings => {
1953
+ const draft = get(composerDraftAtom(draftKey));
1954
+ return {
1955
+ modelSelection: draft.modelSelection,
1956
+ runtimeMode: draft.runtimeMode,
1957
+ interactionMode: draft.interactionMode,
1958
+ };
1959
+ }).pipe(
1960
+ Atom.withEquality(
1961
+ (a: ComposerDraftSettings, b: ComposerDraftSettings) =>
1962
+ a.modelSelection === b.modelSelection &&
1963
+ a.runtimeMode === b.runtimeMode &&
1964
+ a.interactionMode === b.interactionMode,
1965
+ ),
1966
+ ),
1967
+);
1968
+const EMPTY_DRAFT_SETTINGS_ATOM = Atom.make(EMPTY_DRAFT_SETTINGS);
1969
+
1970
+export function useComposerDraftSettings(draftKey: string | null): ComposerDraftSettings {
1971
+ const settings = useAtomValue(
1972
+ draftKey ? composerDraftSettingsAtom(draftKey) : EMPTY_DRAFT_SETTINGS_ATOM,
1973
+ );
1974
+ useEffect(() => {
1975
+ ensureComposerDraftsLoaded();
1976
+ }, []);
1977
+ return settings;
1978
+}
1979
+
1980
export function useComposerDraft(draftKey: string | null): ComposerDraft {
1889
- const drafts = useAtomValue(composerDraftsAtom);
1981
+ const draft = useAtomValue(draftKey ? composerDraftAtom(draftKey) : EMPTY_DRAFT_ATOM);
1982
useEffect(() => {
1983
ensureComposerDraftsLoaded();
1984
}, []);
1893
- return draftKey ? normalizeDraft(drafts[draftKey]) : EMPTY_DRAFT;
1985
+ return draft;
1986
}
1987
1988
export function useStickyComposerModelSelection(): ModelSelection | null {
apps/mobile/src/state/use-pending-new-tasks.ts
+2
-2
@@ -2,7 +2,7 @@ import { useAtomValue } from "@effect/atom-react";
2
import { useMemo } from "react";
3
import { buildPendingNewTasks, type PendingNewTask } from "./pending-new-tasks-model";
4
import { flattenQueuedThreadMessages } from "./thread-outbox-model";
5
-import { composerDraftsAtom } from "./use-composer-drafts";
5
+import { newTaskComposerDraftsAtom } from "./use-composer-drafts";
6
import { useThreadOutboxMessages } from "./use-thread-outbox";
7
8
export type {
@@ -13,7 +13,7 @@ export type {
13
14
export function usePendingNewTasks(): ReadonlyArray<PendingNewTask> {
15
const queuedMessagesByThreadKey = useThreadOutboxMessages();
16
- const drafts = useAtomValue(composerDraftsAtom);
16
+ const drafts = useAtomValue(newTaskComposerDraftsAtom);
17
return useMemo(
18
() =>
19
buildPendingNewTasks({
apps/mobile/src/state/use-selected-thread-requests.test.tsx
+2
-1
@@ -19,7 +19,8 @@ vi.mock("@effect/atom-react", () => ({
19
: {},
20
}));
21
vi.mock("./use-composer-drafts", () => ({
22
- composerDraftsAtom: "drafts",
22
+ composerDraftsAtom: "all-drafts",
23
+ questionAttachmentDraftsAtom: () => "drafts",
24
clearComposerDraft: vi.fn(),
25
}));
26
vi.mock("./composer-attachment-uploads", async () => ({
apps/mobile/src/state/use-selected-thread-requests.ts
+12
-2
@@ -5,7 +5,11 @@ import {
5
questionAttachmentDraftPrefix,
6
questionAttachmentPreparationAtom,
7
} from "./question-attachments";
8
-import { composerDraftsAtom, clearComposerDraft } from "./use-composer-drafts";
8
+import {
9
+ clearComposerDraft,
10
+ composerDraftsAtom,
11
+ questionAttachmentDraftsAtom,
12
+} from "./use-composer-drafts";
13
import {
14
composerAttachmentUploadsAtom,
15
composerAttachmentUploadBlockReason,
@@ -119,7 +123,13 @@ export function useSelectedThreadRequests() {
123
}
124
}, [activePendingUserInput, selectedThreadShell]);
125
const questionServerConfigs = useServerConfigs();
122
- const attachmentDrafts = useAtomValue(composerDraftsAtom);
126
+ const attachmentDrafts = useAtomValue(
127
+ questionAttachmentDraftsAtom(
128
+ selectedThreadShell
129
+ ? questionAttachmentDraftPrefix(selectedThreadShell.environmentId, selectedThreadShell.id)
130
+ : "",
131
+ ),
132
+ );
133
const preparationCounts = useAtomValue(questionAttachmentPreparationAtom);
134
const uploadStates = useAtomValue(composerAttachmentUploadsAtom);
135
useEffect(() => {
apps/mobile/src/state/use-thread-composer-state.ts
+5
-11
@@ -72,7 +72,6 @@ import {
72
insertComposerDraftText,
73
insertComposerDraftContext,
74
clearComposerDraftContent,
75
- composerDraftsAtom,
75
composerContextImportsAtom,
76
ensureComposerDraftsLoaded,
77
getComposerDraftSnapshot,
@@ -82,6 +81,7 @@ import {
81
setComposerDraftText,
82
updateComposerDraftSettings,
83
useComposerDraft,
84
+ useComposerDraftSettings,
85
} from "./use-composer-drafts";
86
import {
87
resolveComposerDispatchMode,
@@ -185,7 +185,6 @@ export function useThreadComposerState() {
185
} = useThreadSelection();
186
const selectedThreadProjection = useSelectedThreadProjection();
187
const selectedThreadVisibleTurnItems = useSelectedThreadVisibleTurnItems();
188
- const composerDrafts = useAtomValue(composerDraftsAtom);
188
const acknowledgedMessages = useAtomValue(acknowledgedThreadMessagesAtom);
189
const queuedMessagesByThreadKey = useThreadOutboxMessages();
190
const dispatchingQueuedMessageId = useAtomValue(dispatchingQueuedMessageIdAtom);
@@ -295,26 +294,23 @@ export function useThreadComposerState() {
294
: queuedRunEdit === null
295
? selectedThreadKey
296
: queuedEditDraftKey(selectedThreadKey, queuedRunEdit.runId);
298
- const editedDraft = composerDraftKey ? composerDrafts[composerDraftKey] : null;
299
- const selectedDraft = selectedThreadKey ? composerDrafts[selectedThreadKey] : null;
300
- const draftMessage = editedDraft?.text ?? "";
301
- const draftAttachments = editedDraft?.attachments ?? [];
297
+ const selectedDraft = useComposerDraftSettings(selectedThreadKey);
298
const selectedThreadQueueCount = selectedThreadQueuedMessages.length;
299
const selectedThread = selectedThreadShell;
304
- const modelSelection = selectedDraft?.modelSelection ?? selectedThread?.modelSelection ?? null;
300
+ const modelSelection = selectedDraft.modelSelection ?? selectedThread?.modelSelection ?? null;
301
const pendingHandoff = resolvePendingHandoff(
302
selectedThreadProjection?.projection,
303
modelSelection,
304
selectedEnvironmentRuntime?.serverConfig?.providers ?? [],
305
);
310
- const runtimeMode = selectedDraft?.runtimeMode ?? selectedThread?.runtimeMode ?? null;
306
+ const runtimeMode = selectedDraft.runtimeMode ?? selectedThread?.runtimeMode ?? null;
307
const selectedProvider = selectedEnvironmentRuntime?.serverConfig?.providers.find(
308
(provider) => provider.instanceId === modelSelection?.instanceId,
309
);
310
const interactionMode = selectedThread
311
? resolveProviderInteractionMode(
312
selectedProvider,
317
- selectedDraft?.interactionMode ?? selectedThread.interactionMode,
313
+ selectedDraft.interactionMode ?? selectedThread.interactionMode,
314
)
315
: null;
316
const canSwitchThreadProvider = threadAllowsProviderSwitch({
@@ -967,8 +963,6 @@ export function useThreadComposerState() {
963
runlessWorkActive,
964
providerSubagentStatus,
965
isCompacting,
970
- draftMessage,
971
- draftAttachments,
966
composerDraftKey,
967
followUpBehavior,
968
canSteerActiveTurn,