import { useAtomValue } from "@effect/atom-react"; import type { EnvironmentId, PreviewRuntime, PreviewSessionSnapshot } from "@t3tools/contracts"; import { isElectron } from "~/env"; import { isPreviewSupportedInRuntime } from "~/previewStateStore"; import { appAtomRegistry } from "~/rpc/atomRegistry"; import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment"; import { readEnvironmentSupportsServerBrowser, useEnvironmentSupportsServerBrowser, } from "~/state/entities"; /** * Where a tab the user opens should run. The desktop app draws its own * server's tabs natively, so those stay agent-drivable at no cost. A remote * environment's tabs would stream, so the desktop opens them in its own * browser instead: no latency, and it reaches what this computer reaches. The * user can move a tab to the environment when only it can reach the page. */ export function previewRuntimeFor(environmentId: EnvironmentId): PreviewRuntime | undefined { if (!readEnvironmentSupportsServerBrowser(environmentId)) return undefined; if ( isPreviewSupportedInRuntime() && environmentId !== appAtomRegistry.get(primaryEnvironmentIdAtom) ) { return undefined; } return "server"; } /** * The other browser a tab can move to, or null when it has none: a desktop * tab of a remote environment can move to that environment's browser, and back. */ export function alternatePreviewRuntime( environmentId: EnvironmentId, primaryEnvironmentId: EnvironmentId | null, serverBrowser: boolean, snapshot: Pick | null | undefined, ): PreviewRuntime | null { if (!snapshot || !serverBrowser || !isPreviewSupportedInRuntime()) return null; if (environmentId === primaryEnvironmentId) return null; return snapshot.runtime === "server" ? "desktop" : "server"; } /** Electron hosts its own browser tabs; other clients need the environment to host them. */ export function isPreviewAvailableFor(environmentId: EnvironmentId): boolean { return isPreviewSupportedInRuntime() || readEnvironmentSupportsServerBrowser(environmentId); } export function usePreviewAvailable(environmentId: EnvironmentId | null): boolean { const serverBrowser = useEnvironmentSupportsServerBrowser(environmentId); return isPreviewSupportedInRuntime() || serverBrowser; } /** * Whether this client draws a server tab with its own ``. The desktop * app renders tabs of the server it launched, which drives them over the * desktop browser channel; every other client and environment streams them. */ export function rendersServerTabNatively( environmentId: EnvironmentId, primaryEnvironmentId: EnvironmentId | null, snapshot: Pick | null | undefined, ): boolean { return ( isElectron && snapshot?.runtime === "server" && primaryEnvironmentId !== null && environmentId === primaryEnvironmentId ); } export function useRendersServerTabNatively( environmentId: EnvironmentId, snapshot: Pick | null | undefined, ): boolean { return rendersServerTabNatively(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot); }