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 | allowsPreviewTunnel, |
| 4 | type EnvironmentId, |
| 5 | type PreviewRuntime, |
| 6 | type PreviewSessionSnapshot, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { Atom } from "effect/reactivity"; |
| 9 | |
| 10 | import { environmentCatalog } from "~/connection/catalog"; |
| 11 | import { isElectron } from "~/env"; |
| 12 | import { isPreviewSupportedInRuntime } from "~/previewStateStore"; |
| 13 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 14 | import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment"; |
| 15 | import { |
| 16 | readEnvironmentSupportsServerBrowser, |
| 17 | useEnvironmentSupportsServerBrowser, |
| 18 | } from "~/state/entities"; |
| 19 | import { environmentServerConfigsAtom } from "~/state/server"; |
| 20 | |
| 21 | import { tunnelsEnvironmentLoopback } from "./browserTargetResolver"; |
| 22 | |
| 23 | /** Whether this desktop loads the environment's localhost itself: it is local, or its tunnel is on. */ |
| 24 | const desktopLoadsLoopbackAtom = Atom.family((environmentId: EnvironmentId) => |
| 25 | Atom.make((get) => { |
| 26 | const target = get(environmentCatalog.catalogValueAtom).entries.get(environmentId)?.target; |
| 27 | if (!tunnelsEnvironmentLoopback(target)) return true; |
| 28 | const config = get(environmentServerConfigsAtom).get(environmentId); |
| 29 | return config !== undefined && allowsPreviewTunnel(config); |
| 30 | }).pipe(Atom.withLabel(`preview:desktop-loads-loopback:${environmentId}`)), |
| 31 | ); |
| 32 | |
| 33 | export function previewRuntimeFor(environmentId: EnvironmentId): PreviewRuntime | undefined { |
| 34 | if (!readEnvironmentSupportsServerBrowser(environmentId)) return undefined; |
| 35 | // Without its tunnel, another environment's tabs stream from it, as on the web. |
| 36 | if ( |
| 37 | isPreviewSupportedInRuntime() && |
| 38 | environmentId !== appAtomRegistry.get(primaryEnvironmentIdAtom) && |
| 39 | appAtomRegistry.get(desktopLoadsLoopbackAtom(environmentId)) |
| 40 | ) { |
| 41 | return undefined; |
| 42 | } |
| 43 | return "server"; |
| 44 | } |
| 45 | |
| 46 | export function alternatePreviewRuntime( |
| 47 | environmentId: EnvironmentId, |
| 48 | primaryEnvironmentId: EnvironmentId | null, |
| 49 | serverBrowser: boolean, |
| 50 | desktopLoadsLoopback: boolean, |
| 51 | snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined, |
| 52 | ): PreviewRuntime | null { |
| 53 | if (!snapshot || !serverBrowser || !isPreviewSupportedInRuntime()) return null; |
| 54 | if (environmentId === primaryEnvironmentId) return null; |
| 55 | if (snapshot.runtime === "server") return desktopLoadsLoopback ? "desktop" : null; |
| 56 | return "server"; |
| 57 | } |
| 58 | |
| 59 | export function useDesktopLoadsLoopback(environmentId: EnvironmentId): boolean { |
| 60 | return useAtomValue(desktopLoadsLoopbackAtom(environmentId)); |
| 61 | } |
| 62 | |
| 63 | export function isPreviewAvailableFor(environmentId: EnvironmentId): boolean { |
| 64 | return isPreviewSupportedInRuntime() || readEnvironmentSupportsServerBrowser(environmentId); |
| 65 | } |
| 66 | |
| 67 | export function usePreviewAvailable(environmentId: EnvironmentId | null): boolean { |
| 68 | const serverBrowser = useEnvironmentSupportsServerBrowser(environmentId); |
| 69 | return isPreviewSupportedInRuntime() || serverBrowser; |
| 70 | } |
| 71 | |
| 72 | export function rendersServerTabNatively( |
| 73 | environmentId: EnvironmentId, |
| 74 | primaryEnvironmentId: EnvironmentId | null, |
| 75 | snapshot: Pick<PreviewSessionSnapshot, "runtime" | "headless"> | null | undefined, |
| 76 | ): boolean { |
| 77 | return ( |
| 78 | isElectron && |
| 79 | snapshot?.runtime === "server" && |
| 80 | !snapshot.headless && |
| 81 | primaryEnvironmentId !== null && |
| 82 | environmentId === primaryEnvironmentId |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | export function useRendersServerTabNatively( |
| 87 | environmentId: EnvironmentId, |
| 88 | snapshot: Pick<PreviewSessionSnapshot, "runtime" | "headless"> | null | undefined, |
| 89 | ): boolean { |
| 90 | return rendersServerTabNatively(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot); |
| 91 | } |