The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useGT } from "gt-react-native"; |
| 2 | import type { EnvironmentId, ServerSettingsPatch } from "@t3tools/contracts"; |
| 3 | import { |
| 4 | isAtomCommandInterrupted, |
| 5 | squashAtomCommandFailure, |
| 6 | } from "@t3tools/client-runtime/state/runtime"; |
| 7 | |
| 8 | import * as Dialog from "../../components/ui/Dialog"; |
| 9 | import { serverEnvironment } from "../../state/server"; |
| 10 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 11 | import type { ScopedMobileSettingsTarget } from "./settings-scoped-server"; |
| 12 | |
| 13 | export function useSettingsWriter() { |
| 14 | const gt = useGT(); |
| 15 | const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { |
| 16 | label: "environment settings update", |
| 17 | reportFailure: true, |
| 18 | }); |
| 19 | |
| 20 | return async ( |
| 21 | targets: readonly ScopedMobileSettingsTarget[], |
| 22 | writes: readonly { |
| 23 | readonly environmentId: EnvironmentId; |
| 24 | readonly patch: ServerSettingsPatch; |
| 25 | }[], |
| 26 | ) => { |
| 27 | const outcomes = await Promise.all( |
| 28 | writes.map(async (entry) => { |
| 29 | const result = await updateSettings({ |
| 30 | environmentId: entry.environmentId, |
| 31 | input: { patch: entry.patch }, |
| 32 | }); |
| 33 | if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return null; |
| 34 | const label = |
| 35 | targets.find((target) => target.environment.environmentId === entry.environmentId) |
| 36 | ?.environment.label ?? entry.environmentId; |
| 37 | const error = squashAtomCommandFailure(result); |
| 38 | return gt("{environment}: {error}", { |
| 39 | environment: label, |
| 40 | error: error instanceof Error ? error.message : String(error), |
| 41 | }); |
| 42 | }), |
| 43 | ); |
| 44 | const failures = outcomes.filter((outcome) => outcome !== null); |
| 45 | if (failures.length > 0) { |
| 46 | Dialog.alert(gt("Could not save settings"), failures.join("\n\n")); |
| 47 | } |
| 48 | }; |
| 49 | } |