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 | linkableMachines, |
| 4 | linkedEnvironmentNames, |
| 5 | offersLinkedEnvironments, |
| 6 | } from "@t3tools/client-runtime/link-request"; |
| 7 | import { createPeerLinkEnvironmentAtoms } from "@t3tools/client-runtime/state/peerLinks"; |
| 8 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 9 | import { useEffect, useEffectEvent, useMemo } from "react"; |
| 10 | |
| 11 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 12 | import { useEnvironments } from "./environments"; |
| 13 | import { relayEnvironmentDiscovery } from "./relay"; |
| 14 | import { useEnvironmentQuery } from "./query"; |
| 15 | import { useAtomCommand } from "./use-atom-command"; |
| 16 | |
| 17 | export const peerLinkEnvironment = createPeerLinkEnvironmentAtoms(connectionAtomRuntime); |
| 18 | |
| 19 | /** |
| 20 | * The machines `environmentId` links to, by name, while it offers links, and none otherwise. |
| 21 | * Its agents can read threads there, and so can a client allowed to read threads on it. |
| 22 | */ |
| 23 | export function useLinkedEnvironments( |
| 24 | environmentId: EnvironmentId | null, |
| 25 | ): ReadonlyMap<EnvironmentId, string> { |
| 26 | const { environments } = useEnvironments(); |
| 27 | const offered = offersLinkedEnvironments( |
| 28 | environments.find((environment) => environment.environmentId === environmentId)?.serverConfig, |
| 29 | ); |
| 30 | const links = useEnvironmentQuery( |
| 31 | offered && environmentId !== null |
| 32 | ? peerLinkEnvironment.names({ environmentId, input: {} }) |
| 33 | : null, |
| 34 | ); |
| 35 | return useMemo( |
| 36 | () => linkedEnvironmentNames({ links: offered ? (links.data?.links ?? []) : [], environments }), |
| 37 | [environments, links.data, offered], |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * The user's other machines, for picking one to link `environmentId` to, |
| 43 | * including those only on their T3 Connect account. Reads that environment's |
| 44 | * links only while `enabled`, since listing them asks every linked machine |
| 45 | * whether it answers. |
| 46 | */ |
| 47 | export function useLinkableMachines(environmentId: EnvironmentId, enabled: boolean) { |
| 48 | const { environments } = useEnvironments(); |
| 49 | const discovery = useAtomValue(relayEnvironmentDiscovery.stateValueAtom); |
| 50 | const refreshDiscovery = useAtomCommand(relayEnvironmentDiscovery.refresh, { |
| 51 | reportFailure: false, |
| 52 | }); |
| 53 | // Connect hosts are listed only once something asked for them, so ask on open. |
| 54 | const discoverIfUnasked = useEffectEvent(() => { |
| 55 | if (discovery.environments.size === 0 && !discovery.refreshing) void refreshDiscovery(); |
| 56 | }); |
| 57 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- discovery follows the picker's visibility, which a card's mount or a dialog's open sets |
| 58 | useEffect(() => { |
| 59 | if (enabled) discoverIfUnasked(); |
| 60 | }, [enabled]); |
| 61 | const links = useEnvironmentQuery( |
| 62 | enabled ? peerLinkEnvironment.list({ environmentId, input: {} }) : null, |
| 63 | ); |
| 64 | return linkableMachines({ |
| 65 | environments, |
| 66 | threadEnvironmentId: environmentId, |
| 67 | links: links.data?.links ?? [], |
| 68 | connect: discovery.environments, |
| 69 | }); |
| 70 | } |