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 | connectionCatalogDisplayUrl, |
| 4 | hasRelayRoute, |
| 5 | type EnvironmentPresentation as BaseEnvironmentPresentation, |
| 6 | } from "@t3tools/client-runtime/connection"; |
| 7 | import { Discovery } from "@t3tools/client-runtime/relay"; |
| 8 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import { useState } from "react"; |
| 11 | |
| 12 | import { environmentCatalog } from "../connection/catalog"; |
| 13 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 14 | import { |
| 15 | environmentPresentations, |
| 16 | environmentSummaries, |
| 17 | useEnvironmentPresentation, |
| 18 | } from "./presentation"; |
| 19 | import { primaryEnvironmentIdAtom } from "./primaryEnvironment"; |
| 20 | import { relayEnvironmentDiscovery } from "./relay"; |
| 21 | import { usePreparedConnection } from "./session"; |
| 22 | |
| 23 | export interface EnvironmentPresentation extends BaseEnvironmentPresentation { |
| 24 | readonly environmentId: EnvironmentId; |
| 25 | readonly label: string; |
| 26 | readonly displayUrl: string | null; |
| 27 | readonly relayManaged: boolean; |
| 28 | } |
| 29 | |
| 30 | function projectEnvironmentPresentation( |
| 31 | environmentId: EnvironmentId, |
| 32 | presentation: BaseEnvironmentPresentation, |
| 33 | ): EnvironmentPresentation { |
| 34 | return { |
| 35 | ...presentation, |
| 36 | environmentId, |
| 37 | label: presentation.entry.target.label, |
| 38 | displayUrl: connectionCatalogDisplayUrl(presentation.entry), |
| 39 | relayManaged: hasRelayRoute(presentation.entry), |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | export function useEnvironments() { |
| 44 | const catalog = useAtomValue(environmentCatalog.catalogValueAtom); |
| 45 | const networkStatus = useAtomValue(environmentCatalog.networkStatusValueAtom); |
| 46 | const presentationById = useAtomValue(environmentPresentations.presentationsAtom); |
| 47 | |
| 48 | const environments = [...presentationById.entries()].map(([environmentId, presentation]) => |
| 49 | projectEnvironmentPresentation(environmentId, presentation), |
| 50 | ); |
| 51 | |
| 52 | return { |
| 53 | isReady: catalog.isReady, |
| 54 | networkStatus, |
| 55 | environments, |
| 56 | presentationById, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | /** Provider statuses change on every push, so they live apart from the presentations. */ |
| 61 | export function useEnvironmentProviders() { |
| 62 | return useAtomValue(environmentPresentations.providersByEnvironmentAtom); |
| 63 | } |
| 64 | |
| 65 | export function readEnvironmentProviders(environmentId: EnvironmentId) { |
| 66 | return appAtomRegistry |
| 67 | .get(environmentPresentations.providersByEnvironmentAtom) |
| 68 | .get(environmentId); |
| 69 | } |
| 70 | |
| 71 | export function usePrimaryEnvironmentId(): EnvironmentId | null { |
| 72 | return useAtomValue(primaryEnvironmentIdAtom); |
| 73 | } |
| 74 | |
| 75 | export function readPrimaryEnvironmentId(): EnvironmentId | null { |
| 76 | return appAtomRegistry.get(primaryEnvironmentIdAtom); |
| 77 | } |
| 78 | |
| 79 | /** The environment's state now, for handlers that should not re-render on every change. */ |
| 80 | export function readEnvironment(environmentId: EnvironmentId): EnvironmentPresentation | null { |
| 81 | const presentation = appAtomRegistry |
| 82 | .get(environmentPresentations.presentationsAtom) |
| 83 | .get(environmentId); |
| 84 | return presentation ? projectEnvironmentPresentation(environmentId, presentation) : null; |
| 85 | } |
| 86 | |
| 87 | export function useEnvironment( |
| 88 | environmentId: EnvironmentId | null, |
| 89 | ): EnvironmentPresentation | null { |
| 90 | const { presentation } = useEnvironmentPresentation(environmentId); |
| 91 | return environmentId === null || presentation === null |
| 92 | ? null |
| 93 | : projectEnvironmentPresentation(environmentId, presentation); |
| 94 | } |
| 95 | |
| 96 | export function usePrimaryEnvironment(): EnvironmentPresentation | null { |
| 97 | return useEnvironment(usePrimaryEnvironmentId()); |
| 98 | } |
| 99 | |
| 100 | export function useEnvironmentHttpBaseUrl(environmentId: EnvironmentId | null): string | null { |
| 101 | const prepared = usePreparedConnection(environmentId); |
| 102 | return Option.isSome(prepared) ? prepared.value.httpBaseUrl : null; |
| 103 | } |
| 104 | |
| 105 | export function useRelayEnvironmentDiscovery(): Discovery.RelayEnvironmentDiscoveryState { |
| 106 | return useAtomValue(relayEnvironmentDiscovery.stateValueAtom); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * The account's T3 Connect machines for a view that keeps one in front of the user. A refresh |
| 111 | * empties the list until it answers; the machines last seen stay meanwhile. |
| 112 | */ |
| 113 | export function useRetainedRelayEnvironments() { |
| 114 | const discovery = useRelayEnvironmentDiscovery(); |
| 115 | const [seen, setSeen] = useState(discovery.environments); |
| 116 | const emptied = discovery.refreshing && discovery.environments.size === 0; |
| 117 | if (!emptied && seen !== discovery.environments) setSeen(discovery.environments); |
| 118 | return emptied ? seen : discovery.environments; |
| 119 | } |
| 120 | |
| 121 | export function useEnvironmentIdentities() { |
| 122 | return useAtomValue(environmentSummaries.identitiesAtom); |
| 123 | } |
| 124 | |
| 125 | export function usePullRequestsSupported() { |
| 126 | return useAtomValue(environmentSummaries.pullRequestsSupportedAtom); |
| 127 | } |
| 128 | |
| 129 | export function useIssuesSupported() { |
| 130 | return useAtomValue(environmentSummaries.issuesSupportedAtom); |
| 131 | } |
| 132 | |
| 133 | export function useEnvironmentMachines() { |
| 134 | return useAtomValue(environmentSummaries.machineByIdAtom); |
| 135 | } |
| 136 | |
| 137 | export function useConnectedEnvironmentIds() { |
| 138 | return useAtomValue(environmentSummaries.connectedEnvironmentIdsAtom); |
| 139 | } |