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

openPreviewSession.ts

42 lines1.2 KB

1import type {
2 EnvironmentId,
3 PreviewOpenInput,
4 PreviewSessionSnapshot,
5 ScopedThreadRef,
6} from "@v12/contracts";
7import type { AtomCommandResult } from "@v12/client-runtime/state/runtime";
8​
9import { applyPreviewServerSnapshot, rememberPreviewUrl } from "~/previewStateStore";
10​
11interface OpenPreviewSessionInput<E> {
12 openPreview: (input: {
13 readonly environmentId: EnvironmentId;
14 readonly input: PreviewOpenInput;
15 }) => Promise<AtomCommandResult<PreviewSessionSnapshot, E>>;
16 threadRef: ScopedThreadRef;
17 url?: string;
19​
20export async function openPreviewSession<E>(
21 input: OpenPreviewSessionInput<E>,
22): Promise<AtomCommandResult<PreviewSessionSnapshot, E>> {
23 const result = await input.openPreview({
24 environmentId: input.threadRef.environmentId,
25 input: {
26 threadId: input.threadRef.threadId,
27 ...(input.url === undefined ? {} : { url: input.url }),
28 },
29 });
30 if (result._tag === "Failure") {
31 return result;
32 }
33 const snapshot = result.value;
34 applyPreviewServerSnapshot(input.threadRef, snapshot);
35 if (input.url !== undefined) {
36 rememberPreviewUrl(
37 input.threadRef,
38 snapshot.navStatus._tag === "Idle" ? input.url : snapshot.navStatus.url,
39 );
40 }
41 return result;