The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { DesktopBridge, DesktopDiscoveredSshHost } from "@t3tools/contracts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import { Atom } from "effect/reactivity"; |
| 5 | import { msg } from "gt-react"; |
| 6 | |
| 7 | import { m } from "../i18n/messages"; |
| 8 | |
| 9 | type DesktopSshDiscoveryBridge = Pick<DesktopBridge, "discoverSshHosts">; |
| 10 | |
| 11 | export function filterDiscoveredSshHosts( |
| 12 | hosts: ReadonlyArray<DesktopDiscoveredSshHost>, |
| 13 | query: string, |
| 14 | ): ReadonlyArray<DesktopDiscoveredSshHost> { |
| 15 | const normalizedQuery = query.trim().toLowerCase(); |
| 16 | if (normalizedQuery.length === 0) return hosts; |
| 17 | |
| 18 | const prefixMatches: Array<DesktopDiscoveredSshHost> = []; |
| 19 | const substringMatches: Array<DesktopDiscoveredSshHost> = []; |
| 20 | for (const host of hosts) { |
| 21 | const alias = host.alias.toLowerCase(); |
| 22 | if (alias.startsWith(normalizedQuery)) { |
| 23 | prefixMatches.push(host); |
| 24 | } else if ( |
| 25 | alias.includes(normalizedQuery) || |
| 26 | host.hostname.toLowerCase().includes(normalizedQuery) |
| 27 | ) { |
| 28 | substringMatches.push(host); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return [...prefixMatches, ...substringMatches]; |
| 33 | } |
| 34 | |
| 35 | class DesktopSshDiscoveryUnavailableError extends Schema.TaggedError<DesktopSshDiscoveryUnavailableError>()( |
| 36 | "DesktopSshDiscoveryUnavailableError", |
| 37 | {}, |
| 38 | ) { |
| 39 | override get message(): string { |
| 40 | return m(msg("Desktop SSH host discovery is unavailable.")); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | class DesktopSshDiscoveryError extends Schema.TaggedError<DesktopSshDiscoveryError>()( |
| 45 | "DesktopSshDiscoveryError", |
| 46 | { cause: Schema.Defect() }, |
| 47 | ) { |
| 48 | override get message(): string { |
| 49 | return m(msg("Failed to discover SSH hosts.")); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function getDesktopSshDiscoveryBridge(): DesktopSshDiscoveryBridge | undefined { |
| 54 | return typeof window === "undefined" ? undefined : window.desktopBridge; |
| 55 | } |
| 56 | |
| 57 | function createDesktopSshHostsStateAtom(getBridge: () => DesktopSshDiscoveryBridge | undefined) { |
| 58 | const discoverDesktopSshHosts = Effect.fn("discoverDesktopSshHosts")(function* () { |
| 59 | const bridge = getBridge(); |
| 60 | if (!bridge) { |
| 61 | return yield* new DesktopSshDiscoveryUnavailableError(); |
| 62 | } |
| 63 | return yield* Effect.tryPromise({ |
| 64 | try: (): Promise<ReadonlyArray<DesktopDiscoveredSshHost>> => bridge.discoverSshHosts(), |
| 65 | catch: (cause) => new DesktopSshDiscoveryError({ cause }), |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | return Atom.make(discoverDesktopSshHosts()).pipe( |
| 70 | Atom.swr({ staleTime: 30_000, revalidateOnMount: true }), |
| 71 | Atom.keepAlive, |
| 72 | Atom.withLabel("desktop:ssh-hosts"), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | export const desktopSshHostsStateAtom = createDesktopSshHostsStateAtom( |
| 77 | getDesktopSshDiscoveryBridge, |
| 78 | ); |