Repository navigation
Expand file tree
/
Copy pathpreviewRuntime.ts
More file actions
79 lines (72 loc) · 3.11 KB
/
Copy pathpreviewRuntime.ts
File metadata and controls
79 lines (72 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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<PreviewSessionSnapshot, "runtime"> | 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 `<webview>`. 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<PreviewSessionSnapshot, "runtime"> | null | undefined,
): boolean {
return (
isElectron &&
snapshot?.runtime === "server" &&
primaryEnvironmentId !== null &&
environmentId === primaryEnvironmentId
);
}
export function useRendersServerTabNatively(
environmentId: EnvironmentId,
snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
): boolean {
return rendersServerTabNatively(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot);
}