The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type ServerConfig, ServerNpmReleaseError } from "@t3tools/contracts"; |
| 2 | import * as Cause from "effect/Cause"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import { Atom } from "effect/reactivity"; |
| 7 | import { msg } from "gt-react"; |
| 8 | |
| 9 | import { m, type MessageResolver } from "../i18n/messages"; |
| 10 | |
| 11 | import { environmentSummaries } from "./presentation"; |
| 12 | import { primaryEnvironmentIdAtom } from "./primaryEnvironment"; |
| 13 | import { serverEnvironment } from "./server"; |
| 14 | |
| 15 | const isServerNpmReleaseError = Schema.is(ServerNpmReleaseError); |
| 16 | |
| 17 | function unsupportedLookup(config: ServerConfig | null): ServerNpmReleaseError | null { |
| 18 | if (config === null) { |
| 19 | return new ServerNpmReleaseError({ |
| 20 | reason: m(msg("Connect to this server to check releases.")), |
| 21 | }); |
| 22 | } |
| 23 | return config.environment.capabilities.npmReleaseLookup === true |
| 24 | ? null |
| 25 | : new ServerNpmReleaseError({ |
| 26 | reason: m( |
| 27 | msg("This server does not support registry lookups. Update it before switching tracks."), |
| 28 | ), |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | export function npmReleaseFailureMessage( |
| 33 | cause: Cause.Cause<unknown>, |
| 34 | resolve: MessageResolver = m, |
| 35 | ): string { |
| 36 | const error = Option.getOrUndefined(Cause.findErrorOption(cause)); |
| 37 | return isServerNpmReleaseError(error) |
| 38 | ? error.message |
| 39 | : resolve( |
| 40 | msg( |
| 41 | "Could not check this environment's npm registry. Check the connection and registry settings.", |
| 42 | ), |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | export const npmReleaseAtom = Atom.family((spec: string) => |
| 47 | Atom.make((get) => { |
| 48 | const primary = get(primaryEnvironmentIdAtom); |
| 49 | // With the desktop's local server turned off there is no primary, so a connected server answers. |
| 50 | const candidates = |
| 51 | primary === null ? get(environmentSummaries.connectedEnvironmentIdsAtom) : [primary]; |
| 52 | const environmentId = |
| 53 | candidates.find( |
| 54 | (id) => unsupportedLookup(get(serverEnvironment.configValueAtom(id))) === null, |
| 55 | ) ?? candidates[0]; |
| 56 | if (environmentId === undefined) { |
| 57 | return Effect.fail( |
| 58 | new ServerNpmReleaseError({ reason: m(msg("Connect to a server to check releases.")) }), |
| 59 | ); |
| 60 | } |
| 61 | const unsupported = unsupportedLookup(get(serverEnvironment.configValueAtom(environmentId))); |
| 62 | if (unsupported !== null) return Effect.fail(unsupported); |
| 63 | return get |
| 64 | .result(serverEnvironment.npmRelease({ environmentId, input: { spec } })) |
| 65 | .pipe( |
| 66 | Effect.map((release) => |
| 67 | release === null |
| 68 | ? null |
| 69 | : { version: release.version, databaseMigration: release.databaseMigration }, |
| 70 | ), |
| 71 | ); |
| 72 | }).pipe(Atom.withLabel(`npm-release:${spec}`)), |
| 73 | ); |