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 type { |
| 2 | AdvertisedEndpoint, |
| 3 | DesktopBridge, |
| 4 | DesktopServerExposureState, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { Atom } from "effect/reactivity"; |
| 9 | |
| 10 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 11 | import { msg } from "gt-react"; |
| 12 | |
| 13 | import { m } from "../i18n/messages"; |
| 14 | |
| 15 | const DESKTOP_NETWORK_ACCESS_STALE_TIME_MS = 30_000; |
| 16 | |
| 17 | type DesktopNetworkAccessBridge = Pick< |
| 18 | DesktopBridge, |
| 19 | "getAdvertisedEndpoints" | "getServerExposureState" |
| 20 | >; |
| 21 | |
| 22 | export interface DesktopNetworkAccessSnapshot { |
| 23 | readonly advertisedEndpoints: ReadonlyArray<AdvertisedEndpoint>; |
| 24 | readonly serverExposureState: DesktopServerExposureState; |
| 25 | } |
| 26 | |
| 27 | class DesktopNetworkAccessUnavailableError extends Schema.TaggedError<DesktopNetworkAccessUnavailableError>()( |
| 28 | "DesktopNetworkAccessUnavailableError", |
| 29 | {}, |
| 30 | ) { |
| 31 | override get message(): string { |
| 32 | return m(msg("Desktop network access is unavailable.")); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | class DesktopServerExposureStateLoadError extends Schema.TaggedError<DesktopServerExposureStateLoadError>()( |
| 37 | "DesktopServerExposureStateLoadError", |
| 38 | { cause: Schema.Defect() }, |
| 39 | ) { |
| 40 | override get message(): string { |
| 41 | return m(msg("Failed to load desktop server exposure state.")); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | class DesktopAdvertisedEndpointsLoadError extends Schema.TaggedError<DesktopAdvertisedEndpointsLoadError>()( |
| 46 | "DesktopAdvertisedEndpointsLoadError", |
| 47 | { cause: Schema.Defect() }, |
| 48 | ) { |
| 49 | override get message(): string { |
| 50 | return m(msg("Failed to load advertised desktop endpoints.")); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | function getDesktopNetworkAccessBridge(): DesktopNetworkAccessBridge | undefined { |
| 55 | return typeof window === "undefined" ? undefined : window.desktopBridge; |
| 56 | } |
| 57 | |
| 58 | function createDesktopNetworkAccessStateAtom( |
| 59 | getBridge: () => DesktopNetworkAccessBridge | undefined, |
| 60 | ) { |
| 61 | const loadDesktopNetworkAccess = Effect.fn("loadDesktopNetworkAccess")(function* () { |
| 62 | const bridge = getBridge(); |
| 63 | if (!bridge) { |
| 64 | return yield* new DesktopNetworkAccessUnavailableError(); |
| 65 | } |
| 66 | const [serverExposureState, advertisedEndpoints] = yield* Effect.all( |
| 67 | [ |
| 68 | Effect.tryPromise({ |
| 69 | try: () => bridge.getServerExposureState(), |
| 70 | catch: (cause) => new DesktopServerExposureStateLoadError({ cause }), |
| 71 | }), |
| 72 | Effect.tryPromise({ |
| 73 | try: () => bridge.getAdvertisedEndpoints(), |
| 74 | catch: (cause) => new DesktopAdvertisedEndpointsLoadError({ cause }), |
| 75 | }), |
| 76 | ], |
| 77 | { concurrency: "unbounded" }, |
| 78 | ); |
| 79 | return { |
| 80 | advertisedEndpoints, |
| 81 | serverExposureState, |
| 82 | } satisfies DesktopNetworkAccessSnapshot; |
| 83 | }); |
| 84 | |
| 85 | return Atom.make(loadDesktopNetworkAccess()).pipe( |
| 86 | // Retain the snapshot, but let the SWR wrapper revalidate when Settings remounts. |
| 87 | Atom.keepAlive, |
| 88 | Atom.swr({ |
| 89 | staleTime: DESKTOP_NETWORK_ACCESS_STALE_TIME_MS, |
| 90 | revalidateOnMount: true, |
| 91 | }), |
| 92 | Atom.withLabel("desktop:network-access"), |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | export const desktopNetworkAccessStateAtom = createDesktopNetworkAccessStateAtom( |
| 97 | getDesktopNetworkAccessBridge, |
| 98 | ); |
| 99 | |
| 100 | export function refreshDesktopNetworkAccessState(): void { |
| 101 | appAtomRegistry.refresh(desktopNetworkAccessStateAtom); |
| 102 | } |