@devries-cameron20 / t3code Mirror · read-only / commits / 6bb9b7b323
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 17:15 UTC

fix(web): auto-settle days save when the edit is finished, not on every keystroke (#36303)

Julius Marminge committed Nov 13, 2026 at 12:31 UTC 6bb9b7b3238079a09173903e793cfc70ce441342
3 files changed +193 -20
apps/web/src/components/settings/SettingsPanels.logic.test.ts
+84
@@ -2,9 +2,16 @@ import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts";
2 import * as Duration from "effect/Duration";
3 import { describe, expect, it } from "vite-plus/test";
4 import {
5 + autoSettleDaysField,
6 + type AutoSettleDaysField,
7 backgroundActivitySharedPolicySettings,
8 + cancelAutoSettleDaysField,
9 + commitAutoSettleDaysField,
10 + editAutoSettleDaysField,
11 + failAutoSettleDaysField,
12 getChangedBrowserSettingLabels,
13 hasChangedBackgroundActivitySettings,
14 + syncAutoSettleDaysField,
15 } from "./SettingsPanels.logic";
16
17 describe("background activity settings restore", () => {
@@ -88,3 +95,80 @@ describe("getChangedBrowserSettingLabels", () => {
95 ).toEqual(["Search engine"]);
96 });
97 });
98 +
99 +describe("auto-settle days field", () => {
100 + const type = (field: AutoSettleDaysField, ...drafts: string[]) =>
101 + drafts.reduce(editAutoSettleDaysField, field);
102 +
103 + it("saves only the finished edit, never a value typed on the way", () => {
104 + const typed = type(autoSettleDaysField(14), "1", "", "3", "30");
105 + expect(typed.pending).toBeNull();
106 +
107 + const committed = commitAutoSettleDaysField(typed);
108 + expect(committed.submit).toBe(30);
109 + expect(commitAutoSettleDaysField(committed.field).submit).toBeNull();
110 +
111 + const confirmed = syncAutoSettleDaysField(committed.field, 30);
112 + expect(confirmed).toMatchObject({ saved: 30, draft: "30", pending: null });
113 + });
114 +
115 + it.each(["", "0", "91", "1.5", "abc"])("restores the saved value for %j", (draft) => {
116 + const committed = commitAutoSettleDaysField(type(autoSettleDaysField(14), draft));
117 + expect(committed.submit).toBeNull();
118 + expect(committed.field.draft).toBe("14");
119 + });
120 +
121 + it("a rejected save falls back to the saved value and can be retried", () => {
122 + const submitted = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field;
123 + expect(failAutoSettleDaysField(submitted, submitted.revision).draft).toBe("14");
124 +
125 + const rejectedWhileCleared = failAutoSettleDaysField(type(submitted, ""), submitted.revision);
126 + expect(rejectedWhileCleared.draft).toBe("");
127 + expect(commitAutoSettleDaysField(rejectedWhileCleared).field.draft).toBe("14");
128 + expect(commitAutoSettleDaysField(type(rejectedWhileCleared, "30")).submit).toBe(30);
129 + });
130 +
131 + it("retyping a value that saved on only some environments sends it again", () => {
132 + const submitted = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field;
133 + const partlySaved = failAutoSettleDaysField(
134 + syncAutoSettleDaysField(submitted, 30),
135 + submitted.revision,
136 + );
137 + expect(commitAutoSettleDaysField(partlySaved).submit).toBeNull();
138 + expect(commitAutoSettleDaysField(type(partlySaved, "3", "30")).submit).toBe(30);
139 + });
140 +
141 + it("returning to the saved value while another save is in flight saves it again", () => {
142 + const submitted = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field;
143 + const back = commitAutoSettleDaysField(type(submitted, "14"));
144 + expect(back.submit).toBe(14);
145 + expect(failAutoSettleDaysField(back.field, submitted.revision)).toBe(back.field);
146 +
147 + const settled = syncAutoSettleDaysField(back.field, 14);
148 + expect(settled.pending).toBeNull();
149 + expect(syncAutoSettleDaysField(settled, 14)).toBe(settled);
150 + expect(syncAutoSettleDaysField(settled, 20).draft).toBe("20");
151 + });
152 +
153 + it("keeps a later save in flight when an earlier one is confirmed", () => {
154 + const first = commitAutoSettleDaysField(type(autoSettleDaysField(14), "30")).field;
155 + const second = commitAutoSettleDaysField(type(first, "60")).field;
156 + const firstConfirmed = syncAutoSettleDaysField(second, 30);
157 + expect(firstConfirmed).toMatchObject({ saved: 30, draft: "60", pending: 60 });
158 + expect(failAutoSettleDaysField(firstConfirmed, second.revision)).toMatchObject({
159 + draft: "30",
160 + pending: null,
161 + });
162 + });
163 +
164 + it("a cancelled edit restores the saved value and saves nothing", () => {
165 + const cancelled = cancelAutoSettleDaysField(type(autoSettleDaysField(14), "1"));
166 + expect(cancelled).toMatchObject({ draft: "14", dirty: false });
167 + expect(commitAutoSettleDaysField(cancelled).submit).toBeNull();
168 + });
169 +
170 + it("keeps a draft in progress when the saved value changes underneath it", () => {
171 + const typing = type(autoSettleDaysField(14), "4");
172 + expect(syncAutoSettleDaysField(typing, 30)).toMatchObject({ saved: 30, draft: "4" });
173 + });
174 +});
apps/web/src/components/settings/SettingsPanels.logic.ts
+75 -1
@@ -8,7 +8,11 @@ import type {
8 SidebarProjectGroupingMode,
9 UnifiedSettings,
10 } from "@t3tools/contracts";
11 -import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
11 +import {
12 + DEFAULT_UNIFIED_SETTINGS,
13 + MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
14 + MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
15 +} from "@t3tools/contracts/settings";
16 import {
17 getBackgroundActivityBaseProfile,
18 normalizeBackgroundActivitySettings,
@@ -271,3 +275,73 @@ export function backgroundActivityOverrideSettings(
275 },
276 };
277 }
278 +
279 +/**
280 + * Saving a shorter age settles threads at once, so typing stays in `draft` until the edit is
281 + * finished. `pending` is the submission in flight; `saved` is the only value a draft falls back to.
282 + */
283 +export interface AutoSettleDaysField {
284 + readonly saved: number;
285 + readonly draft: string;
286 + readonly dirty: boolean;
287 + readonly pending: number | null;
288 + readonly revision: number;
289 +}
290 +
291 +export function autoSettleDaysField(saved: number): AutoSettleDaysField {
292 + return { saved, draft: String(saved), dirty: false, pending: null, revision: 0 };
293 +}
294 +
295 +export function syncAutoSettleDaysField(
296 + field: AutoSettleDaysField,
297 + saved: number,
298 +): AutoSettleDaysField {
299 + // An earlier save can be confirmed while a later one is still in flight.
300 + const pending = field.pending === saved ? null : field.pending;
301 + if (field.saved === saved && pending === field.pending) return field;
302 + return {
303 + ...field,
304 + saved,
305 + pending,
306 + draft: field.dirty ? field.draft : String(pending ?? saved),
307 + };
308 +}
309 +
310 +export function editAutoSettleDaysField(
311 + field: AutoSettleDaysField,
312 + draft: string,
313 +): AutoSettleDaysField {
314 + return { ...field, draft, dirty: true };
315 +}
316 +
317 +export function cancelAutoSettleDaysField(field: AutoSettleDaysField): AutoSettleDaysField {
318 + return { ...field, draft: String(field.pending ?? field.saved), dirty: false };
319 +}
320 +
321 +export function commitAutoSettleDaysField(field: AutoSettleDaysField): {
322 + readonly field: AutoSettleDaysField;
323 + readonly submit: number | null;
324 +} {
325 + const current = field.pending ?? field.saved;
326 + const days = field.draft.trim() === "" ? Number.NaN : Number(field.draft);
327 + if (
328 + !Number.isInteger(days) ||
329 + days < MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS ||
330 + days > MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS
331 + ) {
332 + return { field: { ...field, draft: String(current), dirty: false }, submit: null };
333 + }
334 + const settled = { ...field, draft: String(days), dirty: false };
335 + // An edit ending on the saved value is sent again: a save can land on the shown environment
336 + // and fail on another, and retyping the number is how it is retried.
337 + if (!field.dirty || days === field.pending) return { field: settled, submit: null };
338 + return { field: { ...settled, pending: days, revision: field.revision + 1 }, submit: days };
339 +}
340 +
341 +export function failAutoSettleDaysField(
342 + field: AutoSettleDaysField,
343 + revision: number,
344 +): AutoSettleDaysField {
345 + if (field.revision !== revision) return field;
346 + return { ...field, pending: null, draft: field.dirty ? field.draft : String(field.saved) };
347 +}
apps/web/src/components/settings/SettingsPanels.tsx
+34 -19
@@ -89,6 +89,11 @@ import { ScopedSwitch } from "./ScopedSwitch";
89 import { stackedThreadToast, toastManager } from "../ui/toast";
90 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
91 import {
92 + autoSettleDaysField,
93 + cancelAutoSettleDaysField,
94 + commitAutoSettleDaysField,
95 + editAutoSettleDaysField,
96 + failAutoSettleDaysField,
97 getChangedBrowserSettingLabels,
98 getChangedTypographySettingLabels,
99 hasChangedBackgroundActivitySettings,
@@ -96,6 +101,7 @@ import {
101 projectGroupingModeFromToggle,
102 readLastEnabledProjectGroupingMode,
103 rememberEnabledProjectGroupingMode,
104 + syncAutoSettleDaysField,
105 } from "./SettingsPanels.logic";
106 import {
107 SETTINGS_PICKER_TRIGGER_CLASSNAME,
@@ -732,14 +738,21 @@ function AutoSettleDaysInput({
738 onCommit,
739 }: {
740 value: number;
735 - onCommit: (days: number) => void;
741 + onCommit: (days: number, options: { onFailed: () => void }) => void;
742 }) {
737 - const [draft, setDraft] = useState(String(value));
738 - const [draftSource, setDraftSource] = useState(value);
739 - if (draftSource !== value) {
740 - setDraftSource(value);
741 - setDraft(String(value));
742 - }
743 + const [stored, setField] = useState(() => autoSettleDaysField(value));
744 + const field = syncAutoSettleDaysField(stored, value);
745 + if (field !== stored) setField(field);
746 +
747 + const commit = () => {
748 + const next = commitAutoSettleDaysField(field);
749 + setField(next.field);
750 + if (next.submit === null) return;
751 + const { revision } = next.field;
752 + onCommit(next.submit, {
753 + onFailed: () => setField((current) => failAutoSettleDaysField(current, revision)),
754 + });
755 + };
756
757 return (
758 <Input
@@ -748,19 +761,19 @@ function AutoSettleDaysInput({
761 min={MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS}
762 max={MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS}
763 className="w-full sm:w-24"
751 - value={draft}
752 - onChange={(event) => {
753 - setDraft(event.target.value);
754 - const parsed = Number(event.target.value);
755 - if (
756 - Number.isInteger(parsed) &&
757 - parsed >= MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS &&
758 - parsed <= MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS
759 - ) {
760 - onCommit(parsed);
764 + value={field.draft}
765 + onChange={(event) => setField(editAutoSettleDaysField(field, event.target.value))}
766 + onBlur={commit}
767 + onKeyDown={(event) => {
768 + if (event.nativeEvent.isComposing) return;
769 + if (event.key === "Enter") commit();
770 + if (event.key === "Escape" && field.dirty) {
771 + // An unhandled Escape closes the settings page, and the blur that follows would save.
772 + event.preventDefault();
773 + event.stopPropagation();
774 + setField(cancelAutoSettleDaysField(field));
775 }
776 }}
763 - onBlur={() => setDraft(String(value))}
777 aria-label="Days of inactivity before auto-settle"
778 />
779 );
@@ -1941,7 +1954,9 @@ export function ThreadsSettingsPanel() {
1954 control={
1955 <AutoSettleDaysInput
1956 value={settings.sidebarAutoSettleAfterDays}
1944 - onCommit={(days) => updateSettings({ sidebarAutoSettleAfterDays: days })}
1957 + onCommit={(days, options) =>
1958 + updateSettings({ sidebarAutoSettleAfterDays: days }, options)
1959 + }
1960 />
1961 }
1962 />