37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
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("web-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: result._tag === "Failure" ? formatError(result.cause) : null,
|
|
isPending: atom !== null && result.waiting,
|
|
refresh,
|
|
};
|
|
}
|