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 { revalidateOnMount } from "@t3tools/client-runtime/state/runtime"; |
| 2 | import type { DesktopBridge, DesktopWslState } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { Atom } from "effect/reactivity"; |
| 6 | |
| 7 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 8 | import { msg } from "gt-react"; |
| 9 | |
| 10 | import { m } from "../i18n/messages"; |
| 11 | |
| 12 | type DesktopWslStateBridge = Pick<DesktopBridge, "getWslState">; |
| 13 | const WSL_SEARCH_STALE_TIME_MS = 30_000; |
| 14 | |
| 15 | class DesktopWslStateUnavailableError extends Schema.TaggedError<DesktopWslStateUnavailableError>()( |
| 16 | "DesktopWslStateUnavailableError", |
| 17 | {}, |
| 18 | ) { |
| 19 | override get message(): string { |
| 20 | return m(msg("Desktop WSL state is unavailable.")); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | class DesktopWslStateLoadError extends Schema.TaggedError<DesktopWslStateLoadError>()( |
| 25 | "DesktopWslStateLoadError", |
| 26 | { cause: Schema.Defect() }, |
| 27 | ) { |
| 28 | override get message(): string { |
| 29 | return m(msg("Failed to load WSL state.")); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function getDesktopWslStateBridge(): DesktopWslStateBridge | undefined { |
| 34 | return typeof window === "undefined" ? undefined : window.desktopBridge; |
| 35 | } |
| 36 | |
| 37 | function createDesktopWslStateAtom(getBridge: () => DesktopWslStateBridge | undefined) { |
| 38 | const loadDesktopWslState = Effect.fn("loadDesktopWslState")(function* () { |
| 39 | const bridge = getBridge(); |
| 40 | if (!bridge) { |
| 41 | return yield* new DesktopWslStateUnavailableError(); |
| 42 | } |
| 43 | return yield* Effect.tryPromise({ |
| 44 | try: (): Promise<DesktopWslState> => bridge.getWslState(), |
| 45 | catch: (cause) => new DesktopWslStateLoadError({ cause }), |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | return Atom.make(loadDesktopWslState()).pipe( |
| 50 | Atom.keepAlive, |
| 51 | Atom.withLabel("desktop:wsl-state:load"), |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | const desktopWslStateCache = createDesktopWslStateAtom(getDesktopWslStateBridge); |
| 56 | |
| 57 | export const desktopWslStateAtom = desktopWslStateCache.pipe( |
| 58 | revalidateOnMount(WSL_SEARCH_STALE_TIME_MS), |
| 59 | ); |
| 60 | |
| 61 | export const desktopWslAccessStateAtom = desktopWslStateCache.pipe(revalidateOnMount(0)); |
| 62 | |
| 63 | export function refreshDesktopWslState(): void { |
| 64 | appAtomRegistry.refresh(desktopWslStateCache); |
| 65 | } |