Files
t3code/apps/web/src/state/use-atom-command.ts
T
Julius Marmingeandcodex 841bbd9a17 Harden connection state handling across app
- adopt atom command results for connection and relay flows
- preserve relay error trace IDs and improve failure handling
- add coverage for auth, registration, and environment updates

Co-authored-by: codex <[email protected]>
2026-06-16 23:47:55 -07:00

25 lines
1012 B
TypeScript

import { useAtomSet } from "@effect/atom-react";
import {
executeAtomCommand,
type AtomCommandOptions,
type AtomCommandResult,
} from "@t3tools/client-runtime/state/runtime";
import { AsyncResult, type Atom } from "effect/unstable/reactivity";
import { useCallback } from "react";
export function useAtomCommand<A, E, W>(
atom: Atom.Writable<AsyncResult.AsyncResult<A, E>, W>,
options?: string | AtomCommandOptions,
): (value: W) => Promise<AtomCommandResult<A, E>> {
const set = useAtomSet(atom, { mode: "promiseExit" });
const label =
typeof options === "string" ? options : (options?.label ?? atom.label?.[0] ?? "atom command");
const reportFailure = typeof options === "string" ? true : (options?.reportFailure ?? true);
const reportDefect = typeof options === "string" ? true : (options?.reportDefect ?? true);
return useCallback(
(value: W) => executeAtomCommand(() => set(value), { label, reportFailure, reportDefect }),
[label, reportDefect, reportFailure, set],
);
}