@devries-cameron20 / t3code-1 / commits / 1a9572a4d8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): branch naming text saves when you leave the screen (#18381)

Co-authored-by: Claude <noreply@anthropic.com>

Aditya Vardhan Sharma committed Oct 18, 2026 at 20:32 UTC 1a9572a4d899a5b54c6e9fa5e65000729a14c2f5
4 files changed +326 -33
apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.test.tsx new
+102
@@ -0,0 +1,102 @@
1 +// @vitest-environment jsdom
2 +import { DEFAULT_SERVER_SETTINGS, type ServerSettingsPatch } from "@t3tools/contracts";
3 +import type { ReactNode } from "react";
4 +import { act } from "react";
5 +import { createRoot, type Root } from "react-dom/client";
6 +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
7 +
8 +const { Children, mocks } = vi.hoisted(() => ({
9 + Children: ({ children }: { children?: ReactNode }) => children,
10 + mocks: {
11 + branchNaming: { onChange: (_patch: ServerSettingsPatch) => {} },
12 + updateSettings: vi.fn(),
13 + },
14 +}));
15 +
16 +vi.mock("react-native", () => ({ View: Children }));
17 +vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
18 +vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
19 +vi.mock("../../state/session", () => ({
20 + readEnvironmentScope: () => true,
21 + useEnvironmentsWithScope: () => new Set(["local"]),
22 +}));
23 +vi.mock("../../state/server", () => ({ serverEnvironment: { updateSettings: {} } }));
24 +vi.mock("../../state/use-atom-command", () => ({
25 + useAtomCommand: () => mocks.updateSettings,
26 +}));
27 +vi.mock("../threads/thread-settings-options", () => ({ RUNTIME_MODE_CHOICES: [] }));
28 +vi.mock("../../components/ScreenScrollView", () => ({ ScreenScrollView: Children }));
29 +vi.mock("../../components/AppText", () => ({ AppText: Children }));
30 +vi.mock("./components/SettingsScreen", () => ({ SettingsScreen: Children }));
31 +vi.mock("./components/SettingsEnvironmentFilterHeader", () => ({
32 + AndroidSettingsEnvironmentFilter: () => null,
33 + SettingsEnvironmentFilterHeader: () => null,
34 +}));
35 +vi.mock("./components/SettingsSection", () => ({ SettingsSection: Children }));
36 +vi.mock("./components/SettingsChoiceRow", () => ({ SettingsChoiceRow: () => null }));
37 +vi.mock("./components/SettingsSwitchRow", () => ({ SettingsSwitchRow: () => null }));
38 +vi.mock("./components/SettingsRow", () => ({ SettingsRow: () => null }));
39 +vi.mock("./components/SettingsProjectOverridesSection", () => ({
40 + SettingsProjectOverridesSection: () => null,
41 +}));
42 +vi.mock("./components/BranchNamingSettings", () => ({
43 + BranchNamingSettings: (props: { onChange: (patch: ServerSettingsPatch) => void }) => {
44 + mocks.branchNaming.onChange = props.onChange;
45 + return null;
46 + },
47 +}));
48 +vi.mock("./settings-environment-filter", () => ({
49 + useSettingsEnvironmentFilter: () => ({
50 + selectedTargets: [
51 + {
52 + environmentId: "local",
53 + serverConfig: {
54 + settings: DEFAULT_SERVER_SETTINGS,
55 + environment: { capabilities: { projectSettingsOverrides: true } },
56 + },
57 + },
58 + ],
59 + projectGroups: [],
60 + selectedProjectKey: null,
61 + }),
62 +}));
63 +
64 +import { SettingsEnvironmentSourceControlRouteScreen } from "./SettingsServerControlsRouteScreen";
65 +
66 +let root: Root;
67 +
68 +beforeEach(() => {
69 + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
70 + root = createRoot(document.createElement("div"));
71 + mocks.updateSettings.mockReset();
72 +});
73 +
74 +afterEach(() => {
75 + act(() => root.unmount());
76 +});
77 +
78 +describe("SettingsServerControlsRouteScreen", () => {
79 + it("sends a draft saved during a pending write once that write settles", async () => {
80 + let settle = () => {};
81 + mocks.updateSettings.mockImplementationOnce(
82 + () => new Promise<void>((resolve) => (settle = resolve)),
83 + );
84 + mocks.updateSettings.mockResolvedValue(undefined);
85 + act(() => root.render(<SettingsEnvironmentSourceControlRouteScreen />));
86 +
87 + act(() => mocks.branchNaming.onChange({ branchNamingMode: "custom" }));
88 + // The branch naming view closes with a draft while the mode write is pending.
89 + act(() => mocks.branchNaming.onChange({ branchNameInstructions: "Use issue IDs." }));
90 + expect(mocks.updateSettings).toHaveBeenCalledOnce();
91 +
92 + await act(async () => settle());
93 +
94 + expect(mocks.updateSettings).toHaveBeenCalledTimes(2);
95 + expect(mocks.updateSettings).toHaveBeenLastCalledWith({
96 + environmentId: "local",
97 + input: {
98 + patch: { branchNamingMode: "custom", branchNameInstructions: "Use issue IDs." },
99 + },
100 + });
101 + });
102 +});
apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx
+22 -4
@@ -151,6 +151,12 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
151 );
152 const [pendingWrites, setPendingWrites] = useState(0);
153 const writeInFlight = useRef(false);
154 + // A closing view can still save its drafts while a write is pending, after
155 + // the controls disabled. Those patches run once the current write settles.
156 + const queuedPatch = useRef<{ key: string; patch: ServerSettingsPatch } | null>(null);
157 + const targetsKey = targets
158 + .map((target) => `${target.environment.environmentId}:${target.projectId}`)
159 + .join(",");
160 const [pendingTargets, setPendingTargets] = useState<
161 readonly ScopedMobileSettingsTarget[] | null
162 >(null);
@@ -171,8 +177,15 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
177 reportFailure: true,
178 });
179 const write = (patch: ServerSettingsPatch) => {
180 + if (writeInFlight.current) {
181 + const queued = queuedPatch.current;
182 + queuedPatch.current = {
183 + key: targetsKey,
184 + patch: queued?.key === targetsKey ? { ...queued.patch, ...patch } : patch,
185 + };
186 + return;
187 + }
188 if (
175 - writeInFlight.current ||
189 !hasConnectedSelection ||
190 !targets.every((target) =>
191 readEnvironmentScope(target.environment.environmentId, AuthSettingsWriteScope),
@@ -192,6 +205,11 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
205 writeInFlight.current = false;
206 setPendingTargets(null);
207 setPendingWrites((count) => count - 1);
208 + const queued = queuedPatch.current;
209 + queuedPatch.current = null;
210 + // These targets may not show this write yet, so send it again with the
211 + // queued patch; project overrides are replaced whole.
212 + if (queued?.key === targetsKey) write({ ...patch, ...queued.patch });
213 });
214 };
215 const clearProjectOverrides = () => {
@@ -213,6 +231,8 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
231 ),
232 ).finally(() => {
233 writeInFlight.current = false;
234 + // Resending a draft over these pre-clear targets would undo the clear.
235 + queuedPatch.current = null;
236 setPendingTargets(null);
237 setPendingWrites((count) => count - 1);
238 });
@@ -349,9 +369,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
369 {props.page === "source-control" ? (
370 <>
371 <BranchNamingSettings
352 - key={targets
353 - .map((target) => `${target.environment.environmentId}:${target.projectId}`)
354 - .join(",")}
372 + key={targetsKey}
373 mode={uniform("branchNamingMode")}
374 prefix={uniform("branchNamePrefix")}
375 instructions={uniform("branchNameInstructions")}
apps/mobile/src/features/settings/components/BranchNamingSettings.test.tsx new
+167
@@ -0,0 +1,167 @@
1 +// @vitest-environment jsdom
2 +import type { BranchNamingMode } from "@t3tools/contracts";
3 +import type { ReactNode } from "react";
4 +import { act } from "react";
5 +import { createRoot, type Root } from "react-dom/client";
6 +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
7 +
8 +type InputProps = {
9 + accessibilityLabel: string;
10 + defaultValue: string;
11 + onChangeText: (text: string) => void;
12 + onEndEditing: (event: { nativeEvent: { text: string } }) => void;
13 +};
14 +
15 +const inputs = vi.hoisted(() => new Map<string, InputProps>());
16 +
17 +vi.mock("react-native", () => ({
18 + View: ({ children }: { children: ReactNode }) => <div>{children}</div>,
19 +}));
20 +vi.mock("../../../components/AppText", () => ({
21 + AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>,
22 + AppTextInput: (props: InputProps) => {
23 + inputs.set(props.accessibilityLabel, props);
24 + return null;
25 + },
26 +}));
27 +vi.mock("./SettingsChoiceRow", () => ({ SettingsChoiceRow: () => null }));
28 +vi.mock("./SettingsSection", () => ({
29 + SettingsSection: ({ children }: { children: ReactNode }) => <div>{children}</div>,
30 +}));
31 +
32 +import { BranchNamingSettings } from "./BranchNamingSettings";
33 +
34 +const PREFIX = "Branch prefix";
35 +const INSTRUCTIONS = "Branch naming instructions";
36 +
37 +let root: Root;
38 +const onChange = vi.fn();
39 +
40 +function render(
41 + mode: BranchNamingMode,
42 + saved: { prefix?: string; instructions?: string } = {},
43 + disabled = false,
44 +) {
45 + act(() =>
46 + root.render(
47 + <BranchNamingSettings
48 + mode={mode}
49 + prefix={saved.prefix ?? "t3"}
50 + instructions={saved.instructions ?? "Use issue IDs."}
51 + disabled={disabled}
52 + onChange={onChange}
53 + />,
54 + ),
55 + );
56 +}
57 +
58 +// Android Back hides the keyboard but never ends editing, so leaving the
59 +// screen is just an unmount with the text still in the field.
60 +function leaveScreen() {
61 + act(() => root.unmount());
62 + root = createRoot(document.createElement("div"));
63 +}
64 +
65 +const typed = (text: string) => ({ nativeEvent: { text } });
66 +
67 +function input(label: string) {
68 + const props = inputs.get(label);
69 + if (!props) throw new Error(`${label} input not rendered`);
70 + return props;
71 +}
72 +
73 +beforeEach(() => {
74 + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
75 + root = createRoot(document.createElement("div"));
76 + inputs.clear();
77 + onChange.mockClear();
78 +});
79 +
80 +afterEach(() => {
81 + act(() => root.unmount());
82 +});
83 +
84 +describe("BranchNamingSettings", () => {
85 + it("saves typed instructions when the screen closes before editing ends", () => {
86 + render("custom");
87 + act(() => input(INSTRUCTIONS).onChangeText(" Use julius/ prefixes. "));
88 + leaveScreen();
89 +
90 + expect(onChange).toHaveBeenCalledExactlyOnceWith({
91 + branchNameInstructions: "Use julius/ prefixes.",
92 + });
93 + });
94 +
95 + it("saves typed prefix when the screen closes before editing ends", () => {
96 + render("static");
97 + act(() => input(PREFIX).onChangeText("feat/"));
98 + leaveScreen();
99 +
100 + expect(onChange).toHaveBeenCalledExactlyOnceWith({ branchNamePrefix: "feat/" });
101 + });
102 +
103 + it.each([
104 + { mode: "static", label: PREFIX, key: "prefix", text: "feat/" },
105 + { mode: "custom", label: INSTRUCTIONS, key: "instructions", text: "Use julius/ prefixes." },
106 + ] as const)("shows the $key typed before Back when the screen reopens", (field) => {
107 + render(field.mode);
108 + act(() => input(field.label).onChangeText(field.text));
109 + leaveScreen();
110 +
111 + const [patch] = onChange.mock.lastCall ?? [];
112 + inputs.clear();
113 + render(field.mode, {
114 + prefix: patch?.branchNamePrefix,
115 + instructions: patch?.branchNameInstructions,
116 + });
117 + expect(input(field.label).defaultValue).toBe(field.text);
118 + });
119 +
120 + it("saves both drafts in one patch when the screen closes", () => {
121 + render("static");
122 + act(() => input(PREFIX).onChangeText("feat/"));
123 + render("custom");
124 + act(() => input(INSTRUCTIONS).onChangeText("New rules."));
125 + leaveScreen();
126 +
127 + expect(onChange).toHaveBeenCalledExactlyOnceWith({
128 + branchNamePrefix: "feat/",
129 + branchNameInstructions: "New rules.",
130 + });
131 + });
132 +
133 + it("still saves a draft when a pending write disables the field", () => {
134 + render("custom");
135 + act(() => input(INSTRUCTIONS).onChangeText("New rules."));
136 + render("custom", {}, true);
137 + leaveScreen();
138 +
139 + expect(onChange).toHaveBeenCalledExactlyOnceWith({ branchNameInstructions: "New rules." });
140 + });
141 +
142 + it("drops a draft once a newer saved value replaces the field", () => {
143 + render("custom");
144 + act(() => input(INSTRUCTIONS).onChangeText("Old draft."));
145 + render("custom", { instructions: "Saved elsewhere." });
146 + leaveScreen();
147 +
148 + expect(onChange).not.toHaveBeenCalled();
149 + });
150 +
151 + it("does not save again on close after editing ended", () => {
152 + render("custom");
153 + act(() => input(INSTRUCTIONS).onChangeText("New rules."));
154 + act(() => input(INSTRUCTIONS).onEndEditing(typed("New rules.")));
155 + leaveScreen();
156 +
157 + expect(onChange).toHaveBeenCalledExactlyOnceWith({ branchNameInstructions: "New rules." });
158 + });
159 +
160 + it("does not save unchanged text", () => {
161 + render("custom");
162 + act(() => input(INSTRUCTIONS).onChangeText("Use issue IDs."));
163 + act(() => input(INSTRUCTIONS).onEndEditing(typed("Use issue IDs.")));
164 +
165 + expect(onChange).not.toHaveBeenCalled();
166 + });
167 +});
apps/mobile/src/features/settings/components/BranchNamingSettings.tsx
+35 -29
@@ -1,5 +1,5 @@
1 import { BranchNamingMode, type ServerSettingsPatch } from "@t3tools/contracts";
2 -import { useRef } from "react";
2 +import { useEffect, useEffectEvent, useRef } from "react";
3 import { View } from "react-native";
4
5 import { AppText as Text, AppTextInput } from "../../../components/ui/AppText";
@@ -25,8 +25,36 @@ export function BranchNamingSettings(props: {
25 disabled: boolean;
26 onChange: (patch: ServerSettingsPatch) => void;
27 }) {
28 - const prefixEdited = useRef(false);
29 - const instructionsEdited = useRef(false);
28 + // Typed text waits here until it saves. Android's Back closes the keyboard
29 + // without ending editing, so leaving the screen saves it too.
30 + type Field = "branchNamePrefix" | "branchNameInstructions";
31 + const drafts = useRef<Partial<Record<Field, { text: string; over: string | null }>>>({});
32 + const savedValue = (field: Field) =>
33 + field === "branchNamePrefix" ? props.prefix : props.instructions;
34 + const edit = (field: Field, text: string) => {
35 + drafts.current[field] = { text, over: savedValue(field) };
36 + };
37 + // Takes a field's draft as a patch, or {} when there is nothing new to save.
38 + // A newer saved value remounts the input, so a draft typed over an older one
39 + // is no longer on screen and is dropped.
40 + const take = (field: Field): ServerSettingsPatch => {
41 + const draft = drafts.current[field];
42 + delete drafts.current[field];
43 + const saved = savedValue(field);
44 + // Not gated on `disabled`: a pending write disables the inputs, and the
45 + // route queues this save behind it and checks write access itself.
46 + if (draft === undefined || draft.over !== saved) return {};
47 + const value = draft.text.trim();
48 + return saved === null || value !== saved ? { [field]: value } : {};
49 + };
50 + const send = (patch: ServerSettingsPatch) => {
51 + if (Object.keys(patch).length > 0) props.onChange(patch);
52 + };
53 + // One patch, because the screen drops writes while another is in flight.
54 + const saveDrafts = useEffectEvent(() =>
55 + send({ ...take("branchNamePrefix"), ...take("branchNameInstructions") }),
56 + );
57 + useEffect(() => () => saveDrafts(), []);
58 return (
59 <SettingsSection
60 title="Worktree branch naming"
@@ -53,25 +81,14 @@ export function BranchNamingSettings(props: {
81 <AppTextInput
82 key={props.prefix}
83 accessibilityLabel="Branch prefix"
56 - onChangeText={() => {
57 - prefixEdited.current = true;
58 - }}
84 + onChangeText={(text) => edit("branchNamePrefix", text)}
85 defaultValue={props.prefix ?? ""}
86 placeholder={props.prefix === null ? "Mixed" : "No prefix"}
87 editable={!props.disabled}
88 autoCapitalize="none"
89 autoCorrect={false}
90 className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground"
65 - onEndEditing={(event) => {
66 - const value = event.nativeEvent.text.trim();
67 - if (
68 - !props.disabled &&
69 - prefixEdited.current &&
70 - (props.prefix === null || value !== props.prefix)
71 - )
72 - props.onChange({ branchNamePrefix: value });
73 - prefixEdited.current = false;
74 - }}
91 + onEndEditing={() => send(take("branchNamePrefix"))}
92 />
93 </View>
94 ) : null}
@@ -83,9 +100,7 @@ export function BranchNamingSettings(props: {
100 <AppTextInput
101 key={props.instructions}
102 accessibilityLabel="Branch naming instructions"
86 - onChangeText={() => {
87 - instructionsEdited.current = true;
88 - }}
103 + onChangeText={(text) => edit("branchNameInstructions", text)}
104 defaultValue={props.instructions ?? ""}
105 placeholder={
106 props.instructions === null
@@ -96,16 +111,7 @@ export function BranchNamingSettings(props: {
111 multiline
112 autoCapitalize="sentences"
113 className="min-h-24 rounded-xl px-3 py-2 text-base text-foreground"
99 - onEndEditing={(event) => {
100 - const value = event.nativeEvent.text.trim();
101 - if (
102 - !props.disabled &&
103 - instructionsEdited.current &&
104 - (props.instructions === null || value !== props.instructions)
105 - )
106 - props.onChange({ branchNameInstructions: value });
107 - instructionsEdited.current = false;
108 - }}
114 + onEndEditing={() => send(take("branchNameInstructions"))}
115 />
116 </View>
117 ) : null}