The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | connectionCatalogDisplayUrl, |
| 4 | hasRelayRoute, |
| 5 | type EnvironmentPresentation as BaseEnvironmentPresentation, |
| 6 | } from "@t3tools/client-runtime/connection"; |
| 7 | import type { ComposerEnvironmentCandidate } from "@t3tools/client-runtime/composerThreadItems"; |
| 8 | import { type EnvironmentId, resolveEnvironmentMachineKind } from "@t3tools/contracts"; |
| 9 | |
| 10 | import { environmentCatalog } from "../connection/catalog"; |
| 11 | import { environmentPresentations } from "./presentation"; |
| 12 | |
| 13 | export interface EnvironmentPresentation extends BaseEnvironmentPresentation { |
| 14 | readonly environmentId: EnvironmentId; |
| 15 | readonly label: string; |
| 16 | readonly displayUrl: string | null; |
| 17 | readonly relayManaged: boolean; |
| 18 | } |
| 19 | |
| 20 | export function projectEnvironmentPresentation( |
| 21 | environmentId: EnvironmentId, |
| 22 | presentation: BaseEnvironmentPresentation, |
| 23 | ): EnvironmentPresentation { |
| 24 | return { |
| 25 | ...presentation, |
| 26 | environmentId, |
| 27 | label: presentation.entry.target.label, |
| 28 | displayUrl: connectionCatalogDisplayUrl(presentation.entry), |
| 29 | relayManaged: hasRelayRoute(presentation.entry), |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | export function useEnvironments() { |
| 34 | const catalog = useAtomValue(environmentCatalog.catalogValueAtom); |
| 35 | const networkStatus = useAtomValue(environmentCatalog.networkStatusValueAtom); |
| 36 | const presentationById = useAtomValue(environmentPresentations.presentationsAtom); |
| 37 | |
| 38 | const environments = [...presentationById.entries()].map(([environmentId, presentation]) => |
| 39 | projectEnvironmentPresentation(environmentId, presentation), |
| 40 | ); |
| 41 | |
| 42 | return { |
| 43 | isReady: catalog.isReady, |
| 44 | networkStatus, |
| 45 | environments, |
| 46 | presentationById, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | /** The user's machines as `@` mention candidates, drawn with their machine kind. */ |
| 51 | export function useMentionableEnvironments(): ReadonlyArray<ComposerEnvironmentCandidate> { |
| 52 | const { environments } = useEnvironments(); |
| 53 | return environments.map((environment) => ({ |
| 54 | environmentId: environment.environmentId, |
| 55 | label: environment.label, |
| 56 | machine: resolveEnvironmentMachineKind(environment.serverConfig), |
| 57 | })); |
| 58 | } |