import type { EnvironmentId, PreviewOpenInput, PreviewSessionSnapshot, ScopedThreadRef, } from "@t3tools/contracts"; import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime"; import { applyPreviewServerSnapshot, rememberPreviewUrl } from "~/previewStateStore"; interface OpenPreviewSessionInput { openPreview: (input: { readonly environmentId: EnvironmentId; readonly input: PreviewOpenInput; }) => Promise>; threadRef: ScopedThreadRef; url?: string; } export async function openPreviewSession( input: OpenPreviewSessionInput, ): Promise> { const result = await input.openPreview({ environmentId: input.threadRef.environmentId, input: { threadId: input.threadRef.threadId, ...(input.url === undefined ? {} : { url: input.url }), }, }); if (result._tag === "Failure") { return result; } const snapshot = result.value; applyPreviewServerSnapshot(input.threadRef, snapshot); if (input.url !== undefined) { rememberPreviewUrl( input.threadRef, snapshot.navStatus._tag === "Idle" ? input.url : snapshot.navStatus.url, ); } return result; }