sailfork / t3code-github / apps/ mobile/ src/ state/ use-atom-query-runner.ts

import { RegistryContext } from "@effect/atom-react";
import {
  executeAtomQuery,
  type AtomQueryOptions,
  type AtomCommandResult,
} from "@t3tools/client-runtime/state/runtime";
import { AsyncResult, type Atom } from "effect/reactivity";
import { useContext } from "react";

export function useAtomQueryRunner<T, A, E>(
  family: (target: T) => Atom.Atom<AsyncResult.AsyncResult<A, E>>,
  options?: string | AtomQueryOptions,
): (target: T) => Promise<AtomCommandResult<A, E>> {
  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);
  const refresh = typeof options === "string" ? false : (options?.refresh ?? false);

  return (target: T) => {
    const atom = family(target);
    return executeAtomQuery(registry, atom, {
      label: explicitLabel ?? atom.label?.[0] ?? "atom query",
      reportFailure,
      reportDefect,
      refresh,
    });
  };
}