Skip to content

Commit 7845bb0

Browse files
fix(mobile): report each rejected shared settings write
1 parent 5030a78 commit 7845bb0

3 files changed

Lines changed: 52 additions & 32 deletions

File tree

‎apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import { View } from "react-native";
1818
import { useSafeAreaInsets } from "react-native-safe-area-context";
1919

2020
import { RUNTIME_MODE_CHOICES } from "../threads/thread-settings-options";
21-
import { serverEnvironment } from "../../state/server";
22-
import { useAtomCommand } from "../../state/use-atom-command";
2321
import { SettingsScreen } from "./components/SettingsScreen";
2422
import {
2523
AndroidSettingsEnvironmentFilter,
@@ -32,6 +30,7 @@ import { SettingsSection } from "./components/SettingsSection";
3230
import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
3331
import { SettingsProjectOverridesSection } from "./components/SettingsProjectOverridesSection";
3432
import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
33+
import { useSettingsWriter } from "./useSettingsWriter";
3534
import {
3635
planMobileScopedSettingsClear,
3736
planMobileScopedSettingsPatch,
@@ -172,10 +171,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
172171
const isMixed = (key: keyof ServerSettings) =>
173172
reference === null ||
174173
displayTargets.some((entry) => entry.settings[key] !== reference.settings[key]);
175-
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
176-
label: "environment settings update",
177-
reportFailure: true,
178-
});
174+
const writeSettings = useSettingsWriter();
179175
const write = (patch: ServerSettingsPatch) => {
180176
if (writeInFlight.current) {
181177
const queued = queuedPatch.current;
@@ -197,11 +193,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
197193
writeInFlight.current = true;
198194
setPendingTargets(targets);
199195
setPendingWrites((count) => count + 1);
200-
void Promise.allSettled(
201-
writes.map((entry) =>
202-
updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
203-
),
204-
).finally(() => {
196+
void writeSettings(targets, writes).finally(() => {
205197
writeInFlight.current = false;
206198
setPendingTargets(null);
207199
setPendingWrites((count) => count - 1);
@@ -225,11 +217,7 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
225217
writeInFlight.current = true;
226218
setPendingTargets(targets);
227219
setPendingWrites((count) => count + 1);
228-
void Promise.allSettled(
229-
writes.map((entry) =>
230-
updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
231-
),
232-
).finally(() => {
220+
void writeSettings(targets, writes).finally(() => {
233221
writeInFlight.current = false;
234222
// Resending a draft over these pre-clear targets would undo the clear.
235223
queuedPatch.current = null;

‎apps/mobile/src/features/settings/SettingsThreadsRouteScreen.tsx‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
1212
import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
1313
import { AppText as Text } from "../../components/ui/AppText";
1414
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
15-
import { serverEnvironment } from "../../state/server";
16-
import { useAtomCommand } from "../../state/use-atom-command";
1715
import { SettingsSection } from "./components/SettingsSection";
1816
import { SettingsProjectOverridesSection } from "./components/SettingsProjectOverridesSection";
1917
import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
@@ -24,6 +22,7 @@ import {
2422
} from "./components/SettingsEnvironmentFilterHeader";
2523
import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
2624
import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
25+
import { useSettingsWriter } from "./useSettingsWriter";
2726
import {
2827
planMobileScopedSettingsClear,
2928
planMobileScopedSettingsPatch,
@@ -67,10 +66,7 @@ function AutoSettleSettingsRows() {
6766
const [pendingTargets, setPendingTargets] = useState<
6867
readonly ScopedMobileSettingsTarget[] | null
6968
>(null);
70-
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
71-
label: "server settings update",
72-
reportFailure: true,
73-
});
69+
const writeSettings = useSettingsWriter();
7470

7571
const syncEnvironments = selectedTargets.filter(supportsSharedSettingsSync);
7672
const syncTargets = resolveMobileSettingsTargets(
@@ -107,11 +103,7 @@ function AutoSettleSettingsRows() {
107103
writeInFlight.current = true;
108104
setPendingTargets(syncTargets);
109105
setPendingWrites((count) => count + 1);
110-
void Promise.allSettled(
111-
writes.map((entry) =>
112-
updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
113-
),
114-
).finally(() => {
106+
void writeSettings(syncTargets, writes).finally(() => {
115107
writeInFlight.current = false;
116108
setPendingTargets(null);
117109
setPendingWrites((count) => count - 1);
@@ -161,11 +153,7 @@ function AutoSettleSettingsRows() {
161153
writeInFlight.current = true;
162154
setPendingTargets(syncTargets);
163155
setPendingWrites((count) => count + 1);
164-
void Promise.allSettled(
165-
writes.map((entry) =>
166-
updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
167-
),
168-
).finally(() => {
156+
void writeSettings(syncTargets, writes).finally(() => {
169157
writeInFlight.current = false;
170158
setPendingTargets(null);
171159
setPendingWrites((count) => count - 1);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { EnvironmentId, ServerSettingsPatch } from "@t3tools/contracts";
2+
import {
3+
isAtomCommandInterrupted,
4+
squashAtomCommandFailure,
5+
} from "@t3tools/client-runtime/state/runtime";
6+
7+
import * as Dialog from "../../components/ui/Dialog";
8+
import { serverEnvironment } from "../../state/server";
9+
import { useAtomCommand } from "../../state/use-atom-command";
10+
import type { ScopedMobileSettingsTarget } from "./settings-scoped-server";
11+
12+
export function useSettingsWriter() {
13+
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
14+
label: "environment settings update",
15+
reportFailure: true,
16+
});
17+
18+
return async (
19+
targets: readonly ScopedMobileSettingsTarget[],
20+
writes: readonly {
21+
readonly environmentId: EnvironmentId;
22+
readonly patch: ServerSettingsPatch;
23+
}[],
24+
) => {
25+
const outcomes = await Promise.all(
26+
writes.map(async (entry) => {
27+
const result = await updateSettings({
28+
environmentId: entry.environmentId,
29+
input: { patch: entry.patch },
30+
});
31+
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return null;
32+
const label =
33+
targets.find((target) => target.environment.environmentId === entry.environmentId)
34+
?.environment.label ?? entry.environmentId;
35+
const error = squashAtomCommandFailure(result);
36+
return `${label}: ${error instanceof Error ? error.message : String(error)}`;
37+
}),
38+
);
39+
const failures = outcomes.filter((outcome) => outcome !== null);
40+
if (failures.length > 0) {
41+
Dialog.alert("Could not save settings", failures.join("\n\n"));
42+
}
43+
};
44+
}

0 commit comments

Comments
 (0)