Skip to content

Commit 342fd86

Browse files
saphidclaude
andcommitted
fix(mobile): sending on iOS no longer leaves an autocorrected draft behind
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 689763a commit 342fd86

9 files changed

Lines changed: 63 additions & 21 deletions

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorModule.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public class T3ComposerEditorModule: Module {
127127
AsyncFunction("setSelection") { (view: T3ComposerEditorView, start: Int, end: Int) in
128128
view.setSelection(start: start, end: end)
129129
}
130+
AsyncFunction("readState") { (view: T3ComposerEditorView) -> [String: Any] in
131+
view.readState()
132+
}
130133
}
131134
}
132135
}

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,15 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
773773
textView.resignFirstResponder()
774774
}
775775

776+
func readState() -> [String: Any] {
777+
let selection = sourceSelection()
778+
return [
779+
"value": value,
780+
"selection": ["start": selection.start, "end": selection.end],
781+
"eventCount": nativeEventCount,
782+
]
783+
}
784+
776785
func setSelection(start: Int, end: Int) {
777786
let previousSelection = sourceSelection()
778787
requestedSelection = ComposerSelectionPayload(start: start, end: end)

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
653653
const threadKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
654654
if (inFlightThreadIdsRef.current.has(threadKey)) return;
655655
inFlightThreadIdsRef.current.add(threadKey);
656+
await inputRef.current?.flushPendingInput();
656657
const draftAtSend = getComposerDraftSnapshot(composerDraftKey);
657658
const stillSendable = () => {
658659
const draft = getComposerDraftSnapshot(composerDraftKey);

‎apps/mobile/src/native/T3ComposerEditor.ios.tsx‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ interface NativeComposerEditorRef {
6868
focus: () => Promise<void>;
6969
blur: () => Promise<void>;
7070
setSelection: (start: number, end: number) => Promise<void>;
71+
readState: () => Promise<{
72+
readonly value: string;
73+
readonly selection: ComposerEditorSelection;
74+
readonly eventCount: number;
75+
}>;
7176
}
7277

7378
interface NativeComposerEditorProps extends ViewProps {
@@ -163,17 +168,6 @@ export function ComposerEditor({
163168
const theme = useUniwindTheme();
164169
const fontFamily = FONT_FAMILIES.regular;
165170

166-
useImperativeHandle(
167-
ref,
168-
() => ({
169-
focus: () => settleViewCommand(nativeRef.current?.focus()),
170-
blur: () => settleViewCommand(nativeRef.current?.blur()),
171-
setSelection: (nextSelection) =>
172-
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
173-
}),
174-
[],
175-
);
176-
177171
const skillLabels = useMemo(() => composerSkillLabels(skills), [skills]);
178172
const chipTextColor = createNativeComposerTheme(theme).text;
179173
const tokensJson = useMemo(() => {
@@ -280,6 +274,35 @@ export function ComposerEditor({
280274
},
281275
[nativeEventSnapshotsRef],
282276
);
277+
const applyNativeChange = (
278+
eventCount: number,
279+
value: string,
280+
nextSelection: ComposerEditorSelection,
281+
) => {
282+
const acknowledgedEventCount = acceptNativeEvent(eventCount, value, nextSelection);
283+
if (acknowledgedEventCount === false) return;
284+
onChangeText(value);
285+
onSelectionChange?.(nextSelection);
286+
setMostRecentEventCount(acknowledgedEventCount);
287+
forceNativeEventRender((sequence) => sequence + 1);
288+
};
289+
useImperativeHandle(ref, () => ({
290+
focus: () => settleViewCommand(nativeRef.current?.focus()),
291+
blur: () => settleViewCommand(nativeRef.current?.blur()),
292+
setSelection: (nextSelection) =>
293+
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
294+
// Tapping outside the editor makes iOS accept a pending autocorrection, and that edit's
295+
// event can reach JS after the tap's handler has already read the draft.
296+
flushPendingInput: async () => {
297+
const state = await Promise.resolve()
298+
.then(() => nativeRef.current?.readState())
299+
.catch(() => undefined);
300+
// Paste events advance the count without changing text; consuming one here would drop it.
301+
if (state && state.value !== props.value) {
302+
applyNativeChange(state.eventCount, state.value, state.selection);
303+
}
304+
},
305+
}));
283306
const themeJson = JSON.stringify(createNativeComposerTheme(theme));
284307
const resolvedTextStyle = StyleSheet.flatten(textStyle) ?? {};
285308
return (
@@ -315,18 +338,13 @@ export function ComposerEditor({
315338
textPasteThresholdBytes={onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0}
316339
maxInputChars={PROVIDER_SEND_TURN_MAX_INPUT_CHARS}
317340
style={style as StyleProp<ViewStyle>}
318-
onComposerChange={(event) => {
319-
const acknowledgedEventCount = acceptNativeEvent(
341+
onComposerChange={(event) =>
342+
applyNativeChange(
320343
event.nativeEvent.eventCount,
321344
event.nativeEvent.value,
322345
event.nativeEvent.selection,
323-
);
324-
if (acknowledgedEventCount === false) return;
325-
onChangeText(event.nativeEvent.value);
326-
onSelectionChange?.(event.nativeEvent.selection);
327-
setMostRecentEventCount(acknowledgedEventCount);
328-
forceNativeEventRender((sequence) => sequence + 1);
329-
}}
346+
)
347+
}
330348
onComposerSelectionChange={(event) => {
331349
const acknowledgedEventCount = acceptNativeEvent(
332350
event.nativeEvent.eventCount,

‎apps/mobile/src/native/T3ComposerEditor.native.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export function ComposerEditor({
167167
blur: () => settleViewCommand(nativeRef.current?.blur()),
168168
setSelection: (nextSelection) =>
169169
settleViewCommand(nativeRef.current?.setSelection(nextSelection.start, nextSelection.end)),
170+
flushPendingInput: async () => {},
170171
}),
171172
[],
172173
);

‎apps/mobile/src/native/T3ComposerEditor.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function ComposerEditor({
3434
blur: () => inputRef.current?.blur(),
3535
setSelection: (nextSelection) =>
3636
inputRef.current?.setSelection(nextSelection.start, nextSelection.end),
37+
flushPendingInput: async () => {},
3738
}),
3839
[],
3940
);

‎apps/mobile/src/native/T3ComposerEditor.types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export interface ComposerEditorHandle {
2222
focus: () => void;
2323
blur: () => void;
2424
setSelection: (selection: ComposerEditorSelection) => void;
25+
/** iOS: resolves once a native edit still in flight has reached `onChangeText`. */
26+
flushPendingInput: () => Promise<void>;
2527
}
2628

2729
export interface ComposerEditorProps {

‎apps/mobile/src/native/composerEditorRevision.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,10 @@ describe("typing immediately before an intercepted paste", () => {
164164
expect(acknowledgeComposerNativeEvent(2, 1)).toBeNull();
165165
});
166166
});
167+
168+
describe("acknowledgeComposerNativeEvent", () => {
169+
it("drops a native edit a send already read, so it cannot refill the cleared draft", () => {
170+
expect(acknowledgeComposerNativeEvent(76, 77)).toBe(77);
171+
expect(acknowledgeComposerNativeEvent(77, 77)).toBeNull();
172+
});
173+
});

‎apps/mobile/src/native/composerEditorRevision.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function acknowledgeComposerNativeEvent(
1313
mostRecentEventCount: number,
1414
incomingEventCount: number,
1515
): number | null {
16-
if (!Number.isSafeInteger(incomingEventCount) || incomingEventCount < mostRecentEventCount) {
16+
if (!Number.isSafeInteger(incomingEventCount) || incomingEventCount <= mostRecentEventCount) {
1717
return null;
1818
}
1919
return incomingEventCount;

0 commit comments

Comments
 (0)