import { RegistryContext } from "@effect/atom-react"; import { executeAtomQuery, type AtomCommandOptions, type AtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; import { AsyncResult, type Atom } from "effect/unstable/reactivity"; import { useCallback, useContext } from "react"; export function useAtomQueryRunner( family: (target: T) => Atom.Atom>, options?: string | AtomCommandOptions, ): (target: T) => Promise> { const registry = useContext(RegistryContext); const explicitLabel = typeof options === "string" ? options : options?.label; const reportFailure = typeof options === "string" ? true : (options?.reportFailure ?? true); const reportDefect = typeof options === "string" ? true : (options?.reportDefect ?? true); return useCallback( (target: T) => { const atom = family(target); return executeAtomQuery(registry, atom, { label: explicitLabel ?? atom.label?.[0] ?? "atom query", reportFailure, reportDefect, }); }, [explicitLabel, family, registry, reportDefect, reportFailure], ); }