Open History to see the last commit
1import { RegistryContext } from "@effect/atom-react";
2import {
3 executeAtomQuery,
4 type AtomCommandOptions,
5 type AtomCommandResult,
6} from "@t3tools/client-runtime/state/runtime";
7import { AsyncResult, type Atom } from "effect/unstable/reactivity";
8import { useCallback, useContext } from "react";
9
10export function useAtomQueryRunner<T, A, E>(
11 family: (target: T) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
12 options?: string | AtomCommandOptions,
13): (target: T) => Promise<AtomCommandResult<A, E>> {
14 const registry = useContext(RegistryContext);
15 const explicitLabel = typeof options === "string" ? options : options?.label;
16 const reportFailure = typeof options === "string" ? true : (options?.reportFailure ?? true);
17 const reportDefect = typeof options === "string" ? true : (options?.reportDefect ?? true);
18
19 return useCallback(
20 (target: T) => {
21 const atom = family(target);
22 return executeAtomQuery(registry, atom, {
23 label: explicitLabel ?? atom.label?.[0] ?? "atom query",
24 reportFailure,
25 reportDefect,
26 });
27 },
28 [explicitLabel, family, registry, reportDefect, reportFailure],
29 );
30}