The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | |
| 3 | import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments"; |
| 4 | import { resolveAccessEnvironmentId } from "./ConnectionsSettings.logic"; |
| 5 | import { settingsScopeEnvironmentLabel } from "./settingsScopeAxis"; |
| 6 | |
| 7 | /** |
| 8 | * The environments Sharing & access manages, the one `requested` resolves to, and its name. |
| 9 | * The MCP wizard uses the same, so both name an environment alike. A desktop-local WSL backend |
| 10 | * runs its own server with its own access, so it is a candidate too. |
| 11 | */ |
| 12 | export function useAccessEnvironment(requested: EnvironmentId | null) { |
| 13 | const { environments } = useEnvironments(); |
| 14 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 15 | const candidates = environments.filter( |
| 16 | (environment) => |
| 17 | environment.environmentId === primaryEnvironmentId || |
| 18 | environment.environmentId === requested || |
| 19 | environment.entry.enabled, |
| 20 | ); |
| 21 | const environmentId = resolveAccessEnvironmentId({ |
| 22 | primaryEnvironmentId, |
| 23 | environments: candidates.map((environment) => ({ |
| 24 | environmentId: environment.environmentId, |
| 25 | connected: environment.connection.phase === "connected", |
| 26 | })), |
| 27 | selected: requested, |
| 28 | }); |
| 29 | const environment = |
| 30 | candidates.find((candidate) => candidate.environmentId === environmentId) ?? null; |
| 31 | return { |
| 32 | candidates, |
| 33 | environment, |
| 34 | label: environment ? settingsScopeEnvironmentLabel(environment, candidates) : null, |
| 35 | }; |
| 36 | } |