The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 6 | import { msg } from "gt-react"; |
| 7 | import { useContext } from "react"; |
| 8 | |
| 9 | import { m, type MessageResolver } from "../i18n/messages"; |
| 10 | |
| 11 | const EMPTY_ASYNC_RESULT_ATOM = Atom.make(AsyncResult.initial<never, never>(false)).pipe( |
| 12 | Atom.withLabel("web-environment-query:empty"), |
| 13 | ); |
| 14 | |
| 15 | export interface EnvironmentQueryView<A, E = unknown> { |
| 16 | readonly data: A | null; |
| 17 | readonly dataUpdatedAt: number; |
| 18 | readonly error: string | null; |
| 19 | readonly failure: E | null; |
| 20 | readonly isPending: boolean; |
| 21 | readonly isSuccess: boolean; |
| 22 | readonly refresh: () => void; |
| 23 | } |
| 24 | |
| 25 | export function formatEnvironmentQueryError( |
| 26 | cause: Cause.Cause<unknown>, |
| 27 | resolve: MessageResolver = m, |
| 28 | ): string { |
| 29 | const error = Cause.squash(cause); |
| 30 | return ( |
| 31 | (error instanceof Error ? errorDisplayMessage(error, resolve) : null) ?? |
| 32 | resolve(msg("The environment request failed.")) |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | function queryUpdatedAt(result: AsyncResult.AsyncResult<unknown, unknown>): number { |
| 37 | return result._tag === "Success" |
| 38 | ? result.timestamp |
| 39 | : result._tag === "Failure" |
| 40 | ? (Option.getOrNull(result.previousSuccess)?.timestamp ?? 0) |
| 41 | : 0; |
| 42 | } |
| 43 | |
| 44 | export function useEnvironmentQuery<A, E>( |
| 45 | atom: Atom.Atom<AsyncResult.AsyncResult<A, E>> | null, |
| 46 | resolve: MessageResolver = m, |
| 47 | ): EnvironmentQueryView<A, E> { |
| 48 | const selectedAtom = atom ?? EMPTY_ASYNC_RESULT_ATOM; |
| 49 | const result = useAtomValue(selectedAtom); |
| 50 | const refresh = useAtomRefresh(selectedAtom); |
| 51 | return { |
| 52 | data: AsyncResult.getOrElse(result, () => null), |
| 53 | dataUpdatedAt: queryUpdatedAt(result), |
| 54 | error: result._tag === "Failure" ? formatEnvironmentQueryError(result.cause, resolve) : null, |
| 55 | failure: |
| 56 | result._tag === "Failure" ? Option.getOrNull(Cause.findErrorOption(result.cause)) : null, |
| 57 | isPending: atom !== null && result.waiting, |
| 58 | isSuccess: result._tag === "Success", |
| 59 | refresh, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | /** Keep select outside the component; a new function rebuilds the derived atom on every render. */ |
| 64 | export function useEnvironmentQuerySelector<A, E, B>( |
| 65 | atom: Atom.Atom<AsyncResult.AsyncResult<A, E>> | null, |
| 66 | select: (result: AsyncResult.AsyncResult<A, E>) => B, |
| 67 | ): B { |
| 68 | return useAtomValue(atom ?? EMPTY_ASYNC_RESULT_ATOM, select); |
| 69 | } |
| 70 | |
| 71 | type QueryResultAtom = Atom.Atom<AsyncResult.AsyncResult<unknown, unknown>>; |
| 72 | const queryIsNewerAtoms = new WeakMap< |
| 73 | QueryResultAtom, |
| 74 | WeakMap<QueryResultAtom, Atom.Atom<boolean>> |
| 75 | >(); |
| 76 | function queryIsNewerAtom(query: QueryResultAtom, than: QueryResultAtom): Atom.Atom<boolean> { |
| 77 | let byThan = queryIsNewerAtoms.get(query); |
| 78 | if (byThan === undefined) { |
| 79 | byThan = new WeakMap(); |
| 80 | queryIsNewerAtoms.set(query, byThan); |
| 81 | } |
| 82 | let atom = byThan.get(than); |
| 83 | if (atom === undefined) { |
| 84 | atom = Atom.make((get) => queryUpdatedAt(get(query)) >= queryUpdatedAt(get(than))); |
| 85 | byThan.set(than, atom); |
| 86 | } |
| 87 | return atom; |
| 88 | } |
| 89 | |
| 90 | export function useEnvironmentQueryIsNewer( |
| 91 | query: QueryResultAtom | null, |
| 92 | than: QueryResultAtom | null, |
| 93 | ): boolean { |
| 94 | return useAtomValue( |
| 95 | queryIsNewerAtom(query ?? EMPTY_ASYNC_RESULT_ATOM, than ?? EMPTY_ASYNC_RESULT_ATOM), |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** Returns refresh and call-time pending state without subscribing to it. */ |
| 100 | export function useEnvironmentQueryRefresh<A, E>( |
| 101 | atom: Atom.Atom<AsyncResult.AsyncResult<A, E>> | null, |
| 102 | ): { readonly refresh: () => void; readonly isPending: () => boolean } { |
| 103 | const registry = useContext(RegistryContext); |
| 104 | const selectedAtom = atom ?? EMPTY_ASYNC_RESULT_ATOM; |
| 105 | const refresh = useAtomRefresh(selectedAtom); |
| 106 | return { refresh, isPending: () => atom !== null && registry.get(selectedAtom).waiting }; |
| 107 | } |