Files
t3code/apps/mobile/src/state/use-atom-command.ts
T

24 lines
937 B
TypeScript

import { RegistryContext } from "@effect/atom-react";
import {
type AtomCommand,
type AtomCommandOptions,
type AtomCommandResult,
runAtomCommand,
} from "@t3tools/client-runtime/state/runtime";
import { useCallback, useContext } from "react";
export function useAtomCommand<A, E, W>(
command: AtomCommand<W, A, E>,
options?: string | AtomCommandOptions,
): (value: W) => Promise<AtomCommandResult<A, E>> {
const registry = useContext(RegistryContext);
const label = typeof options === "string" ? options : (options?.label ?? command.label);
const reportFailure = typeof options === "string" ? true : (options?.reportFailure ?? true);
const reportDefect = typeof options === "string" ? true : (options?.reportDefect ?? true);
return useCallback(
(value: W) => runAtomCommand(registry, command, value, { label, reportFailure, reportDefect }),
[command, label, registry, reportDefect, reportFailure],
);
}