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 { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | RelayConnectionRegistration, |
| 4 | RelayConnectionTarget, |
| 5 | } from "@t3tools/client-runtime/connection"; |
| 6 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 7 | import type { |
| 8 | RelayClientEnvironmentRecord, |
| 9 | RelayEnvironmentStatusResponse, |
| 10 | } from "@t3tools/contracts/relay"; |
| 11 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 12 | import { useMessages } from "gt-react-native"; |
| 13 | import * as Option from "effect/Option"; |
| 14 | import { useMemo } from "react"; |
| 15 | |
| 16 | import { environmentCatalog } from "../../connection/catalog"; |
| 17 | import { |
| 18 | connectPairingUrl as connectPairingUrlAtom, |
| 19 | updateBearerConnection, |
| 20 | } from "../../connection/onboarding"; |
| 21 | import { useWorkspaceEnvironments } from "../../state/workspace"; |
| 22 | import { relayEnvironmentDiscovery } from "../../state/relay"; |
| 23 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 24 | import { relayManagedEnvironmentIds } from "./environmentSections"; |
| 25 | |
| 26 | export interface RelayEnvironmentView { |
| 27 | readonly environment: RelayClientEnvironmentRecord; |
| 28 | readonly availability: "checking" | "online" | "offline" | "error"; |
| 29 | readonly status: RelayEnvironmentStatusResponse | null; |
| 30 | readonly error: string | null; |
| 31 | readonly traceId: string | null; |
| 32 | } |
| 33 | |
| 34 | export function useConnectionController() { |
| 35 | const messages = useMessages(); |
| 36 | const connectedEnvironments = useWorkspaceEnvironments(); |
| 37 | const discovery = useAtomValue(relayEnvironmentDiscovery.stateValueAtom); |
| 38 | const connectPairingUrlMutation = useAtomCommand(connectPairingUrlAtom, { |
| 39 | reportFailure: false, |
| 40 | }); |
| 41 | const updateBearer = useAtomCommand(updateBearerConnection, { reportFailure: false }); |
| 42 | const registerEnvironment = useAtomCommand(environmentCatalog.register, "environment register"); |
| 43 | const removeEnvironmentMutation = useAtomCommand(environmentCatalog.remove, "environment remove"); |
| 44 | const setEnvironmentEnabledMutation = useAtomCommand( |
| 45 | environmentCatalog.setEnabled, |
| 46 | "environment toggle", |
| 47 | ); |
| 48 | const refreshRelayEnvironments = useAtomCommand( |
| 49 | relayEnvironmentDiscovery.refresh, |
| 50 | "relay environment refresh", |
| 51 | ); |
| 52 | |
| 53 | const registeredIds = useMemo( |
| 54 | () => relayManagedEnvironmentIds(connectedEnvironments), |
| 55 | [connectedEnvironments], |
| 56 | ); |
| 57 | const relayEnvironments = useMemo<ReadonlyArray<RelayEnvironmentView>>( |
| 58 | () => |
| 59 | [...discovery.environments.values()].map((entry) => ({ |
| 60 | environment: entry.environment, |
| 61 | availability: entry.availability, |
| 62 | status: Option.getOrNull(entry.status), |
| 63 | error: errorDisplayMessage(Option.getOrNull(entry.error), messages), |
| 64 | traceId: Option.getOrNull(entry.error)?.traceId ?? null, |
| 65 | })), |
| 66 | [discovery.environments, messages], |
| 67 | ); |
| 68 | const availableRelayEnvironments = useMemo( |
| 69 | () => relayEnvironments.filter((entry) => !registeredIds.has(entry.environment.environmentId)), |
| 70 | [registeredIds, relayEnvironments], |
| 71 | ); |
| 72 | |
| 73 | const connectPairingUrl = (pairingUrl: string, expectedEnvironmentId?: EnvironmentId) => |
| 74 | connectPairingUrlMutation({ |
| 75 | pairingUrl, |
| 76 | ...(expectedEnvironmentId === undefined ? {} : { expectedEnvironmentId }), |
| 77 | }); |
| 78 | const connectRelayEnvironment = (environment: RelayClientEnvironmentRecord) => |
| 79 | registerEnvironment( |
| 80 | new RelayConnectionRegistration({ |
| 81 | target: new RelayConnectionTarget({ |
| 82 | environmentId: environment.environmentId, |
| 83 | label: environment.label, |
| 84 | }), |
| 85 | }), |
| 86 | ); |
| 87 | const removeEnvironment = (environmentId: EnvironmentId) => |
| 88 | removeEnvironmentMutation(environmentId); |
| 89 | const setEnvironmentEnabled = (environmentId: EnvironmentId, enabled: boolean) => |
| 90 | setEnvironmentEnabledMutation({ environmentId, enabled }); |
| 91 | const updateEnvironment = ( |
| 92 | environmentId: EnvironmentId, |
| 93 | updates: { readonly label: string; readonly displayUrl: string }, |
| 94 | ) => |
| 95 | updateBearer({ |
| 96 | environmentId, |
| 97 | label: updates.label, |
| 98 | httpBaseUrl: updates.displayUrl, |
| 99 | }); |
| 100 | |
| 101 | return { |
| 102 | connectedEnvironments, |
| 103 | relayEnvironments, |
| 104 | availableRelayEnvironments, |
| 105 | relayDiscovery: { |
| 106 | isRefreshing: discovery.refreshing, |
| 107 | isOffline: discovery.offline, |
| 108 | error: errorDisplayMessage(Option.getOrNull(discovery.error), messages), |
| 109 | errorTraceId: Option.getOrNull(discovery.error)?.traceId ?? null, |
| 110 | }, |
| 111 | connectPairingUrl, |
| 112 | connectRelayEnvironment, |
| 113 | removeEnvironment, |
| 114 | setEnvironmentEnabled, |
| 115 | updateEnvironment, |
| 116 | refreshRelayEnvironments, |
| 117 | }; |
| 118 | } |