Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

previewAutomationTarget.ts

37 lines1.2 KB

1import type { PreviewSessionSnapshot } from "@t3tools/contracts";
2​
3interface PreviewAutomationSessionIndex {
4 readonly snapshot: PreviewSessionSnapshot | null;
5 readonly sessions: Readonly<Record<string, PreviewSessionSnapshot>>;
6}
7​
8export function needsPreviewAutomationSessionSync(
9 state: PreviewAutomationSessionIndex,
10 requestedTabId: string | undefined,
11): boolean {
12 return (
13 Object.keys(state.sessions).length === 0 ||
14 requestedTabId === undefined ||
15 state.sessions[requestedTabId] === undefined
16 );
18​
19export function resolvePreviewAutomationTarget(
20 state: PreviewAutomationSessionIndex,
21 requestedTabId: string | null,
22): { readonly tabId: string | null; readonly snapshot: PreviewSessionSnapshot | null } {
23 const snapshot = requestedTabId ? (state.sessions[requestedTabId] ?? null) : state.snapshot;
24 return { tabId: snapshot?.tabId ?? null, snapshot };
26​
27export function resolvePreviewAutomationOpenTab(
28 state: PreviewAutomationSessionIndex,
29 requestedTabId: string | undefined,
30 reuseExistingTab: boolean,
31): string | null {
32 if (!reuseExistingTab) return null;
33 if (requestedTabId !== undefined) {
34 return state.sessions[requestedTabId]?.tabId ?? null;
35 }
36 return state.snapshot?.tabId ?? null;