Open History to see the last commit
1import type { EnvironmentId, ServerSettingsPatch } from "@t3tools/contracts";
2import {
3 isAtomCommandInterrupted,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6
7import * as Dialog from "../../components/ui/Dialog";
8import { serverEnvironment } from "../../state/server";
9import { useAtomCommand } from "../../state/use-atom-command";
10import type { ScopedMobileSettingsTarget } from "./settings-scoped-server";
11
12export 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}