TypeScript
fix(mobile): composer keeps the keyboard when a queued edit ends (#31317)
shivam337c5858c33b
diff --git a/apps/mobile/src/components/ComposerEditor.ios.test.tsx b/apps/mobile/src/components/ComposerEditor.ios.test.tsxindex d5e99928ac7..d3b7fa11a43 100644--- a/apps/mobile/src/components/ComposerEditor.ios.test.tsx+++ b/apps/mobile/src/components/ComposerEditor.ios.test.tsx@@ -20,6 +20,7 @@ const native = vi.hoisted(() => ({text: "",eventCount: 0,resetGeneration: 0,+ mounts: 0,props: null as NativeProps | null,deliver: null as ((event: NativeEvent) => void) | null,}));@@ -28,6 +29,7 @@ function NativeView(props: NativeProps) {const propsRef = useRef(props);useLayoutEffect(() => {Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0 });+ native.mounts += 1;}, []);useLayoutEffect(() => {propsRef.current = props;@@ -173,13 +175,16 @@ it("keeps a pending event from the previous native editor out of the selected dr);await act(async () => renderDraft(first));const deliverPendingEvent = native.deliver!;+ const pending = { eventCount: native.eventCount + 1, resetGeneration: native.resetGeneration };+ const mounts = native.mounts;await act(async () => renderDraft(second));+ // Remounting the native view would drop the keyboard mid-edit.+ expect(native.mounts).toBe(mounts);await act(async () =>deliverPendingEvent({value: "Draft A corrected",selection: { start: 17, end: 17 },- eventCount: 1,- resetGeneration: 0,+ ...pending,}),);expect(getComposerDraftSnapshot(second).text).toBe("Draft B");diff --git a/apps/mobile/src/components/ComposerEditor.tsx b/apps/mobile/src/components/ComposerEditor.tsxindex 9a146487625..c925113901a 100644--- a/apps/mobile/src/components/ComposerEditor.tsx+++ b/apps/mobile/src/components/ComposerEditor.tsx@@ -177,7 +177,7 @@ export function ComposerEditor({<>{/* Reset revision history with the recreated iOS view without aborting context imports. */}<NativeComposerEditor- key={`${hidden ? "hidden" : "visible"}:${draftKey ?? "no-draft"}`}+ key={hidden ? "hidden" : "visible"}{...props}enterBehavior={props.enterBehavior ?? preferredEnterBehavior}onChangeText={changeText}diff --git a/apps/mobile/src/state/use-composer-drafts.ts b/apps/mobile/src/state/use-composer-drafts.tsindex 2a19a7a4f7b..07da494625e 100644--- a/apps/mobile/src/state/use-composer-drafts.ts+++ b/apps/mobile/src/state/use-composer-drafts.ts@@ -21,7 +21,7 @@ import {type ScopedThreadRef,} from "@t3tools/contracts";import * as Schema from "effect/Schema";-import { useEffect } from "react";+import { useEffect, useState } from "react";import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity";import * as Option from "effect/Option";import { parseScopedThreadKey, scopedThreadKey } from "@t3tools/client-runtime/environment";@@ -112,20 +112,31 @@ const composerDraftResetAtom = Atom.family((_draftKey: string) =>);const NO_COMPOSER_DRAFT_RESET_ATOM = Atom.make(0);-function markComposerDraftReset(draftKey: string): void {+function nextComposerDraftReset(): number {lastComposerDraftReset += 1;- appAtomRegistry.set(composerDraftResetAtom(draftKey), lastComposerDraftReset);+ return lastComposerDraftReset;+}++function markComposerDraftReset(draftKey: string): void {+ appAtomRegistry.set(composerDraftResetAtom(draftKey), nextComposerDraftReset());}/**- * Raised when a draft's text is replaced outright (sent, discarded). Native editor events read it- * synchronously, so an edit the editor made before applying the reset cannot restore the text.+ * Raised when a draft's text is replaced outright (sent, discarded), or when the editor switches+ * drafts. iOS editor events read it synchronously, so an edit the editor made before applying the+ * reset cannot restore the text or land in the next draft. Android ignores it.*/export function useComposerDraftResetGeneration(draftKey: string | null): () => number {const atom = draftKey === null ? NO_COMPOSER_DRAFT_RESET_ATOM : composerDraftResetAtom(draftKey);// Subscribing re-renders the editor so it sends the reset even when the text did not change.useAtomValue(atom);- return () => appAtomRegistry.get(atom);+ // The editor stays mounted across drafts (remounting drops the keyboard), so a switch takes a+ // fresh generation; the selected draft's own, possibly older, stamp would move it backwards.+ const [selected, setSelected] = useState({ draftKey, generation: 0 });+ if (selected.draftKey !== draftKey) {+ setSelected({ draftKey, generation: nextComposerDraftReset() });+ }+ return () => Math.max(selected.generation, appAtomRegistry.get(atom));}export function rememberComposerDraftSelection(