Skip to content

Commit e263b27

Browse files
committed
fix(mobile): prepare each submission once and retain editor focus
1 parent 7480dfe commit e263b27

5 files changed

Lines changed: 31 additions & 51 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,13 +775,14 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
775775

776776
func prepareForSubmit() -> [String: Any] {
777777
// Ending editing accepts UIKit's pending autocorrection synchronously.
778-
// Return the committed document rather than relying on JS receiving the
779-
// change event before the send button reads its draft.
778+
let wasEditing = textView.isFirstResponder
780779
textView.resignFirstResponder()
781780
value = textView.serializedText()
781+
if wasEditing {
782+
textView.becomeFirstResponder()
783+
}
782784
let selection = sourceSelection()
783-
// This snapshot is newer than every event emitted while ending editing.
784-
// Delayed change/selection events must not restore the submitted draft.
785+
// Delayed editing events must not restore the submitted draft.
785786
nativeEventCount += 1
786787
return [
787788
"value": value,

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

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime
2020
import {
2121
CommonActions,
2222
StackActions,
23-
useIsFocused,
2423
useNavigation,
2524
usePreventRemove,
2625
} from "@react-navigation/native";
27-
import { useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from "react";
26+
import { useEffect, useEffectEvent, useRef, useState } from "react";
2827
import { Platform, Pressable, View } from "react-native";
2928
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3029
import { KeyboardController } from "react-native-keyboard-controller";
@@ -36,7 +35,6 @@ import {
3635
} from "@t3tools/contracts";
3736

3837
import { ComposerEditor, type ComposerEditorHandle } from "../../components/ComposerEditor";
39-
import { submitComposer } from "../../state/submit-composer";
4038
import { composerContextImportsAtom } from "../../state/use-composer-drafts";
4139
import { abortComposerDraftOperations } from "../../state/composerDraftOperations";
4240
import { composerContextSendBlockReason } from "../../lib/composerContext";
@@ -160,7 +158,6 @@ export function NewTaskDraftScreen(props: {
160158
const projects = useProjects();
161159
const flow = useNewTaskFlow();
162160
const navigation = useNavigation();
163-
const isScreenFocused = useIsFocused();
164161
const goBack = useWorkspaceBackNavigation("threads");
165162
const {
166163
consumeShare,
@@ -808,48 +805,7 @@ export function NewTaskDraftScreen(props: {
808805
locked: isComposerInteractionLocked,
809806
});
810807

811-
const submissionOwnerRef = useRef<{ preparing: boolean } | null>(null);
812-
// Replacing the draft/edit or leaving the screen invalidates pending preparation.
813-
useLayoutEffect(() => {
814-
submissionOwnerRef.current = isScreenFocused ? { preparing: false } : null;
815-
return () => {
816-
submissionOwnerRef.current = null;
817-
};
818-
}, [
819-
flow.draftKey,
820-
selectedProject?.environmentId,
821-
selectedProject?.id,
822-
flow.editingPendingTask,
823-
isScreenFocused,
824-
]);
825-
826808
async function handleStart(): Promise<import("@t3tools/contracts").MessageId | null> {
827-
if (isComposerInteractionLocked) return null;
828-
const owner = submissionOwnerRef.current;
829-
const draftKey = flow.draftKey;
830-
if (!owner || owner.preparing || !draftKey || !navigation.isFocused()) return null;
831-
owner.preparing = true;
832-
const started = await settlePromise(() =>
833-
submitComposer(
834-
draftKey,
835-
promptInputRef.current,
836-
// Preparation can publish corrected text before React replaces the flow callbacks.
837-
() => startPreparedDraftRef.current(),
838-
() => submissionOwnerRef.current === owner && navigation.isFocused(),
839-
),
840-
);
841-
owner.preparing = false;
842-
if (AsyncResult.isFailure(started)) {
843-
const error = squashAtomCommandFailure(started);
844-
Dialog.alert(
845-
"Could not queue task",
846-
error instanceof Error ? error.message : "The task could not be saved to the outbox.",
847-
);
848-
}
849-
return AsyncResult.isSuccess(started) ? started.value : null;
850-
}
851-
852-
async function startPreparedDraft(): Promise<import("@t3tools/contracts").MessageId | null> {
853809
const selectedProject = flow.selectedProject;
854810
const draftKey = flow.draftKey;
855811
if (

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,13 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
532532
})();
533533
const activeDraftRef = useRef<symbol | null>(null);
534534
useLayoutEffect(() => {
535-
activeDraftRef.current = isScreenFocused ? Symbol(composerDraftKey) : null;
535+
activeDraftRef.current = isScreenFocused
536+
? Symbol(`${composerOwnerKey}:${props.selectedThread.projectId}:${composerDraftKey}`)
537+
: null;
536538
return () => {
537539
activeDraftRef.current = null;
538540
};
539-
}, [composerDraftKey, isScreenFocused]);
541+
}, [composerDraftKey, composerOwnerKey, props.selectedThread.projectId, isScreenFocused]);
540542
const openDraftDocument = (attachment: ComposerDocumentAttachment) => {
541543
Keyboard.dismiss();
542544
navigation.navigate("ThreadAttachment", {

‎apps/mobile/src/state/queued-run-edit.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ describe("submitting with a pending native correction", () => {
170170

171171
afterEach(() => endQueuedRunEdit(threadKey));
172172

173+
it("does not prepare an inactive draft", async () => {
174+
const prepareForSubmit = vi.fn(async () => true);
175+
const send = vi.fn(async () => "sent");
176+
expect(
177+
await submitComposer(
178+
threadKey,
179+
{
180+
focus() {},
181+
blur() {},
182+
setSelection() {},
183+
prepareForSubmit,
184+
},
185+
send,
186+
() => false,
187+
),
188+
).toBeNull();
189+
expect(prepareForSubmit).not.toHaveBeenCalled();
190+
expect(send).not.toHaveBeenCalled();
191+
});
192+
173193
it.each(["draft", "queued edit"])(
174194
"reads the corrected %s only after preparation finishes",
175195
async (kind) => {

‎apps/mobile/src/state/submit-composer.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function submitComposer<Result>(
1010
): Promise<Result | null> {
1111
const edit = getQueuedRunEdit(threadKey);
1212
const isCurrent = () => isActive() && getQueuedRunEdit(threadKey) === edit;
13+
if (!isCurrent()) return null;
1314
if ((await editor?.prepareForSubmit?.(isCurrent)) === false || !isCurrent()) return null;
1415
return send();
1516
}

0 commit comments

Comments
 (0)