Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId, PreviewRuntime, PreviewSessionSnapshot } from "@t3tools/contracts";
3
4import { isElectron } from "~/env";
5import { isPreviewSupportedInRuntime } from "~/previewStateStore";
6import { appAtomRegistry } from "~/rpc/atomRegistry";
7import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment";
8import {
9 readEnvironmentSupportsServerBrowser,
10 useEnvironmentSupportsServerBrowser,
11} from "~/state/entities";
12
13/**
14 * Where a tab the user opens should run. The desktop's own server drives
15 * isolated pages in its browser profile and streams them to clients. A remote
16 * environment's tabs would stream, so the desktop opens them in its own
17 * browser instead: no latency, and it reaches what this computer reaches. The
18 * user can move a tab to the environment when only it can reach the page.
19 */
20export function previewRuntimeFor(environmentId: EnvironmentId): PreviewRuntime | undefined {
21 if (!readEnvironmentSupportsServerBrowser(environmentId)) return undefined;
22 if (
23 isPreviewSupportedInRuntime() &&
24 environmentId !== appAtomRegistry.get(primaryEnvironmentIdAtom)
25 ) {
26 return undefined;
27 }
28 return "server";
29}
30
31/**
32 * The other browser a tab can move to, or null when it has none: a desktop
33 * tab of a remote environment can move to that environment's browser, and back.
34 */
35export function alternatePreviewRuntime(
36 environmentId: EnvironmentId,
37 primaryEnvironmentId: EnvironmentId | null,
38 serverBrowser: boolean,
39 snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
40): PreviewRuntime | null {
41 if (!snapshot || !serverBrowser || !isPreviewSupportedInRuntime()) return null;
42 if (environmentId === primaryEnvironmentId) return null;
43 return snapshot.runtime === "server" ? "desktop" : "server";
44}
45
46/** Electron hosts its own browser tabs; other clients need the environment to host them. */
47export function isPreviewAvailableFor(environmentId: EnvironmentId): boolean {
48 return isPreviewSupportedInRuntime() || readEnvironmentSupportsServerBrowser(environmentId);
49}
50
51export function usePreviewAvailable(environmentId: EnvironmentId | null): boolean {
52 const serverBrowser = useEnvironmentSupportsServerBrowser(environmentId);
53 return isPreviewSupportedInRuntime() || serverBrowser;
54}
55
56/**
57 * Whether the desktop hosts this server tab's browser profile. Its page is
58 * isolated from the app's native focus tree and streams like remote tabs.
59 */
60export function hostsServerTabProfile(
61 environmentId: EnvironmentId,
62 primaryEnvironmentId: EnvironmentId | null,
63 snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
64): boolean {
65 return (
66 isElectron &&
67 snapshot?.runtime === "server" &&
68 primaryEnvironmentId !== null &&
69 environmentId === primaryEnvironmentId
70 );
71}
72
73export function useHostsServerTabProfile(
74 environmentId: EnvironmentId,
75 snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
76): boolean {
77 return hostsServerTabProfile(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot);
78}