Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { Predicate } from "effect";
import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
const EMPTY_ASYNC_RESULT_ATOM = Atom.make(AsyncResult.initial<never, never>(false)).pipe(
Atom.withLabel("mobile-environment-query:empty"),
);
export interface EnvironmentQueryView<A> {
readonly data: A | null;
readonly error: string | null;
readonly isPending: boolean;
readonly refresh: () => void;
}
function formatError(cause: Cause.Cause<unknown>): string {
const error = Cause.squash(cause);
return error instanceof Error && error.message.trim().length > 0
? error.message
: "The environment request failed.";
}
export function useEnvironmentQuery<A, E>(
atom: Atom.Atom<AsyncResult.AsyncResult<A, E>> | null,
): EnvironmentQueryView<A> {
const selectedAtom = atom ?? EMPTY_ASYNC_RESULT_ATOM;
const result = useAtomValue(selectedAtom);
const refresh = useAtomRefresh(selectedAtom);
return {
data: Option.getOrNull(AsyncResult.value(result)),
error: Predicate.isTagged(result, "Failure") ? formatError(result.cause) : null,
isPending: atom !== null && result.waiting,
refresh,
};
}