@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 74 lines 2.62 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import type { EnvironmentId } from "@t3tools/contracts";
3 import * as Effect from "effect/Effect";
4 import * as Option from "effect/Option";
5 import * as Schema from "effect/Schema";
6 import { AsyncResult, Atom } from "effect/reactivity";
7 import { msg } from "gt-react-native";
8
9 import { m } from "../../i18n/messages";
10 import { findEnvironmentUpdate } from "./environment-maintenance";
11
12 export interface EnvironmentReleaseCheck {
13 readonly fromVersion: string;
14 readonly targetVersion: string | null;
15 }
16
17 export class EnvironmentUpdateCheckError extends Schema.TaggedError<EnvironmentUpdateCheckError>()(
18 "EnvironmentUpdateCheckError",
19 {
20 message: Schema.String,
21 cause: Schema.optional(Schema.Defect()),
22 },
23 ) {}
24
25 const asError = (cause: unknown) =>
26 new EnvironmentUpdateCheckError({
27 message:
28 cause instanceof Error
29 ? (errorDisplayMessage(cause, m) ?? cause.message)
30 : m(msg("Could not check for updates.")),
31 cause,
32 });
33
34 /**
35 * Looks up the newest T3 Code release for one environment. The atom is not
36 * kept alive, so leaving the screen interrupts the lookup and aborts its fetch.
37 * The argument reads the server version, which an outdated server only
38 * reports through a separate description request.
39 */
40 export const environmentReleaseCheckAtom = Atom.family((environmentId: EnvironmentId) =>
41 Atom.fn((readServerVersion: () => Promise<string>) =>
42 Effect.gen(function* () {
43 const fromVersion = yield* Effect.tryPromise({ try: readServerVersion, catch: asError });
44 const targetVersion = yield* Effect.tryPromise({
45 try: (signal) => findEnvironmentUpdate(fromVersion, signal),
46 catch: asError,
47 }).pipe(
48 Effect.timeoutOrElse({
49 duration: "20 seconds",
50 orElse: () =>
51 Effect.fail(
52 new EnvironmentUpdateCheckError({
53 message: m(msg("Checking for updates timed out. Try again.")),
54 }),
55 ),
56 }),
57 );
58 return { fromVersion, targetVersion } satisfies EnvironmentReleaseCheck;
59 }),
60 ).pipe(Atom.withLabel(`mobile:environment-release-check:${environmentId}`)),
61 );
62
63 export function releaseCheckValue(
64 result: AsyncResult.AsyncResult<EnvironmentReleaseCheck, Error>,
65 ): EnvironmentReleaseCheck | null {
66 return AsyncResult.getOrElse(result, () => null);
67 }
68
69 export function releaseCheckError(
70 result: AsyncResult.AsyncResult<EnvironmentReleaseCheck, Error>,
71 ): string | null {
72 if (!AsyncResult.isFailure(result) || result.waiting) return null;
73 return Option.getOrNull(AsyncResult.error(result))?.message ?? null;
74 }