Skip to content

Commit ecf29cc

Browse files
fix(mobile): typing in a question's custom answer no longer drops or repeats text
The answer field rendered the draft that comes back through the thread screen. The input bumps its native event count as soon as a keystroke arrives, but the draft only reaches it a render later, and iOS applies any text paired with the current count. That stale echo overwrote newer typing, so characters went missing or runs of text were inserted twice, most often right after focusing the field. The field now renders its own text, committed together with the event count, and adopts the draft only when it changes from outside the field, such as an option tap clearing the custom answer.
1 parent 36d4d71 commit ecf29cc

3 files changed

Lines changed: 145 additions & 2 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import type { UserInputQuestion } from "@t3tools/contracts";
2+
import type { ReactNode } from "react";
3+
import { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6+
7+
type InputProps = { value: string; onChangeText: (text: string) => void };
8+
9+
const field = vi.hoisted(() => ({ props: null as InputProps | null }));
10+
11+
vi.mock("react-native", () => ({
12+
View: ({ children }: { children: ReactNode }) => <div>{children}</div>,
13+
}));
14+
vi.mock("../../components/ui/AppText", () => ({
15+
AppTextInput: (props: InputProps) => {
16+
field.props = props;
17+
return null;
18+
},
19+
}));
20+
vi.mock("expo-paste-input", () => ({
21+
TextInputWrapper: ({ children }: { children: ReactNode }) => <>{children}</>,
22+
}));
23+
vi.mock("@effect/atom-react", () => ({ useAtomValue: () => ({}) }));
24+
vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ navigate: vi.fn() }) }));
25+
vi.mock("@t3tools/client-runtime/state/runtime", () => ({
26+
settlePromise: vi.fn(),
27+
squashAtomCommandFailure: vi.fn(),
28+
}));
29+
vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() }));
30+
vi.mock("../../lib/useNativePaste", () => ({ useNativePaste: () => vi.fn() }));
31+
vi.mock("../../lib/composerImages", () => ({
32+
convertPastedImagesToAttachments: vi.fn(),
33+
pickComposerFiles: vi.fn(),
34+
pickComposerMedia: vi.fn(),
35+
}));
36+
vi.mock("./ComposerAttachmentButton", () => ({ ComposerAttachmentButton: () => null }));
37+
vi.mock("../../components/ComposerAttachmentStrip", () => ({
38+
ComposerAttachmentStrip: () => null,
39+
}));
40+
vi.mock("../../components/FilePreviewModal", () => ({ FilePreviewModal: () => null }));
41+
vi.mock("../../components/VideoPreviewModal", () => ({ VideoPreviewModal: () => null }));
42+
vi.mock("../../state/use-thread-selection", () => ({
43+
useThreadSelection: () => ({ selectedThread: { environmentId: "env-1", id: "thread-1" } }),
44+
}));
45+
vi.mock("../../state/entities", () => ({ useServerConfigs: () => new Map() }));
46+
vi.mock("../../state/atom-registry", () => ({ appAtomRegistry: { get: () => ({}) } }));
47+
vi.mock("../../state/use-composer-drafts", () => ({
48+
appendComposerDraftAttachments: vi.fn(),
49+
composerDraftsAtom: {},
50+
removeComposerDraftAttachment: vi.fn(),
51+
releaseUnusedComposerAttachmentFiles: vi.fn(),
52+
}));
53+
vi.mock("../../state/question-attachments", () => ({
54+
changeQuestionAttachmentPreparation: vi.fn(),
55+
questionAttachmentDraftKey: () => "draft-key",
56+
questionAttachmentPreparationAtom: {},
57+
}));
58+
59+
import { QuestionAttachments } from "./QuestionAttachments";
60+
61+
const question = {
62+
id: "colour",
63+
header: "Colour",
64+
question: "Which colour?",
65+
options: [{ label: "Blue", description: "Blue" }],
66+
multiSelect: false,
67+
} as unknown as UserInputQuestion;
68+
69+
let root: Root;
70+
const onChangeText = vi.fn();
71+
72+
// `draft` is what the thread screen passes back; it lags the input until that screen re-renders.
73+
function render(draft: string) {
74+
act(() =>
75+
root.render(
76+
<QuestionAttachments
77+
requestId="request-1"
78+
question={question}
79+
questions={[question]}
80+
disabled={false}
81+
value={draft}
82+
onChangeText={onChangeText}
83+
/>,
84+
),
85+
);
86+
}
87+
88+
function input() {
89+
if (!field.props) throw new Error("answer field not rendered");
90+
return field.props;
91+
}
92+
93+
function type(text: string) {
94+
act(() => input().onChangeText(text));
95+
}
96+
97+
beforeEach(() => {
98+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
99+
root = createRoot(document.createElement("div"));
100+
field.props = null;
101+
onChangeText.mockClear();
102+
});
103+
104+
afterEach(() => {
105+
act(() => root.unmount());
106+
});
107+
108+
describe("QuestionAttachments answer field", () => {
109+
it("keeps newer typing while the thread screen still echoes the previous draft", () => {
110+
render("de");
111+
type("de ");
112+
113+
expect(onChangeText).toHaveBeenLastCalledWith("de ");
114+
expect(input().value).toBe("de ");
115+
116+
render("de ");
117+
expect(input().value).toBe("de ");
118+
});
119+
120+
it("shows the draft an option tap cleared", () => {
121+
render("");
122+
type("my own colour");
123+
render("my own colour");
124+
125+
render("");
126+
127+
expect(input().value).toBe("");
128+
});
129+
});

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ export function QuestionAttachments(props: {
6262
if (focusedRef.current) onInputFocusChange?.(false);
6363
});
6464
useEffect(() => () => releaseFocus(), []);
65+
// The draft returns through the thread screen a render after the input counts the keystroke;
66+
// iOS applies text paired with the current count, so that stale echo overwrote newer typing.
67+
// Local text commits with the count. Only outside changes, like an option tap, are adopted.
68+
const [text, setText] = useState(props.value);
69+
const [previousValue, setPreviousValue] = useState(props.value);
70+
if (props.value !== previousValue) {
71+
setPreviousValue(props.value);
72+
if (props.value !== text) setText(props.value);
73+
}
74+
const changeText = (value: string) => {
75+
setText(value);
76+
props.onChangeText(value);
77+
};
6578
const append = (
6679
key: string,
6780
attachments: Parameters<typeof appendComposerDraftAttachments>[1],
@@ -198,11 +211,11 @@ export function QuestionAttachments(props: {
198211
<VideoPreviewModal source={previewVideo} onRequestClose={() => setPreviewVideo(null)} />
199212
<TextInputWrapper onPaste={paste}>
200213
<TextInput
201-
value={props.value}
214+
value={text}
202215
multiline={props.question.initialAnswer !== undefined}
203216
submitBehavior={props.question.initialAnswer !== undefined ? "newline" : undefined}
204217
editable={!props.disabled}
205-
onChangeText={props.onChangeText}
218+
onChangeText={changeText}
206219
onFocus={() => {
207220
focusedRef.current = true;
208221
onInputFocusChange?.(true);

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const reactTests = [
1010
"src/features/pull-requests/usePullRequestEnvironmentFallback.test.tsx",
1111
"src/lib/filePreview.test.ts",
1212
"src/features/threads/thread-inspector-navigation.test.tsx",
13+
"src/features/threads/QuestionAttachments.test.tsx",
1314
"src/features/diffs/nativeReviewDiffSurface.test.tsx",
1415
"src/features/pull-requests/usePullRequestDiff.test.tsx",
1516
"src/components/ui/Dialog.android.test.tsx",

0 commit comments

Comments
 (0)