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 { DesktopBridge, DesktopCliCommandState } from "@t3tools/contracts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import { Atom } from "effect/reactivity"; |
| 5 | |
| 6 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 7 | import { msg } from "gt-react"; |
| 8 | |
| 9 | import { m } from "../i18n/messages"; |
| 10 | |
| 11 | type DesktopCliCommandBridge = DesktopBridge["cliCommand"]; |
| 12 | |
| 13 | class DesktopCliCommandUnavailableError extends Schema.TaggedError<DesktopCliCommandUnavailableError>()( |
| 14 | "DesktopCliCommandUnavailableError", |
| 15 | {}, |
| 16 | ) { |
| 17 | override get message(): string { |
| 18 | return m(msg("The t3 command is unavailable.")); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | class DesktopCliCommandLoadError extends Schema.TaggedError<DesktopCliCommandLoadError>()( |
| 23 | "DesktopCliCommandLoadError", |
| 24 | { cause: Schema.Defect() }, |
| 25 | ) { |
| 26 | override get message(): string { |
| 27 | return m(msg("Failed to load the t3 command state.")); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export function getDesktopCliCommandBridge(): DesktopCliCommandBridge | undefined { |
| 32 | return typeof window === "undefined" ? undefined : window.desktopBridge?.cliCommand; |
| 33 | } |
| 34 | |
| 35 | const loadDesktopCliCommandState = Effect.fn("loadDesktopCliCommandState")(function* () { |
| 36 | const bridge = getDesktopCliCommandBridge(); |
| 37 | if (!bridge) { |
| 38 | return yield* new DesktopCliCommandUnavailableError(); |
| 39 | } |
| 40 | return yield* Effect.tryPromise({ |
| 41 | try: (): Promise<DesktopCliCommandState> => bridge.getState(), |
| 42 | catch: (cause) => new DesktopCliCommandLoadError({ cause }), |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | export const desktopCliCommandStateAtom = Atom.make(loadDesktopCliCommandState()).pipe( |
| 47 | // Keep the loaded state, but let the SWR wrapper revalidate when Settings remounts. |
| 48 | Atom.keepAlive, |
| 49 | Atom.swr({ staleTime: 30_000, revalidateOnMount: true }), |
| 50 | Atom.withLabel("desktop:cli-command:load"), |
| 51 | ); |
| 52 | |
| 53 | export function refreshDesktopCliCommandState(): void { |
| 54 | appAtomRegistry.refresh(desktopCliCommandStateAtom); |
| 55 | } |