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 { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment";
7import {
8 readEnvironmentSupportsServerBrowser,
9 useEnvironmentSupportsServerBrowser,
10} from "~/state/entities";
11
12export function previewRuntimeFor(environmentId: EnvironmentId): PreviewRuntime | undefined {
13 return readEnvironmentSupportsServerBrowser(environmentId) ? "server" : undefined;
14}
15
16/** Electron hosts its own browser tabs; other clients need the environment to host them. */
17export function isPreviewAvailableFor(environmentId: EnvironmentId): boolean {
18 return isPreviewSupportedInRuntime() || readEnvironmentSupportsServerBrowser(environmentId);
19}
20
21export function usePreviewAvailable(environmentId: EnvironmentId | null): boolean {
22 const serverBrowser = useEnvironmentSupportsServerBrowser(environmentId);
23 return isPreviewSupportedInRuntime() || serverBrowser;
24}
25
26/**
27 * Whether this client draws a server tab with its own `<webview>`. The desktop
28 * app renders tabs of the server it launched, which drives them over the
29 * desktop browser channel; every other client and environment streams them.
30 */
31export function rendersServerTabNatively(
32 environmentId: EnvironmentId,
33 primaryEnvironmentId: EnvironmentId | null,
34 snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
35): boolean {
36 return (
37 isElectron &&
38 snapshot?.runtime === "server" &&
39 primaryEnvironmentId !== null &&
40 environmentId === primaryEnvironmentId
41 );
42}
43
44export function useRendersServerTabNatively(
45 environmentId: EnvironmentId,
46 snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
47): boolean {
48 return rendersServerTabNatively(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot);
49}