Skip to content
Source
Sign inSign up

previewRuntime.ts

63 lines2.3 KB

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​
13export function previewRuntimeFor(environmentId: EnvironmentId): PreviewRuntime | undefined {
14 if (!readEnvironmentSupportsServerBrowser(environmentId)) return undefined;
15 if (
16 isPreviewSupportedInRuntime() &&
17 environmentId !== appAtomRegistry.get(primaryEnvironmentIdAtom)
18 ) {
19 return undefined;
20 }
21 return "server";
23​
24export function alternatePreviewRuntime(
25 environmentId: EnvironmentId,
26 primaryEnvironmentId: EnvironmentId | null,
27 serverBrowser: boolean,
28 snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
29): PreviewRuntime | null {
30 if (!snapshot || !serverBrowser || !isPreviewSupportedInRuntime()) return null;
31 if (environmentId === primaryEnvironmentId) return null;
32 return snapshot.runtime === "server" ? "desktop" : "server";
34​
35export function isPreviewAvailableFor(environmentId: EnvironmentId): boolean {
36 return isPreviewSupportedInRuntime() || readEnvironmentSupportsServerBrowser(environmentId);
38​
39export function usePreviewAvailable(environmentId: EnvironmentId | null): boolean {
40 const serverBrowser = useEnvironmentSupportsServerBrowser(environmentId);
41 return isPreviewSupportedInRuntime() || serverBrowser;
43​
44export function rendersServerTabNatively(
45 environmentId: EnvironmentId,
46 primaryEnvironmentId: EnvironmentId | null,
47 snapshot: Pick<PreviewSessionSnapshot, "runtime" | "headless"> | null | undefined,
48): boolean {
49 return (
50 isElectron &&
51 snapshot?.runtime === "server" &&
52 !snapshot.headless &&
53 primaryEnvironmentId !== null &&
54 environmentId === primaryEnvironmentId
55 );
57​
58export function useRendersServerTabNatively(
59 environmentId: EnvironmentId,
60 snapshot: Pick<PreviewSessionSnapshot, "runtime" | "headless"> | null | undefined,
61): boolean {
62 return rendersServerTabNatively(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot);