Skip to content
Source
Sign inSign up

use-atom-query-runner.ts

29 lines1.1 KB

1import { RegistryContext } from "@effect/atom-react";
2import {
3 executeAtomQuery,
4 type AtomQueryOptions,
5 type AtomCommandResult,
6} from "@t3tools/client-runtime/state/runtime";
7import { AsyncResult, type Atom } from "effect/reactivity";
8import { useContext } from "react";
9​
10export function useAtomQueryRunner<T, A, E>(
11 family: (target: T) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
12 options?: string | AtomQueryOptions,
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 const refresh = typeof options === "string" ? false : (options?.refresh ?? false);
19​
20 return (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 refresh,
27 });
28 };