Open History to see the last commit
1import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
2import type {
3 EnvironmentId,
4 PreviewCloseInput,
5 PreviewSessionSnapshot,
6 ScopedThreadRef,
7} from "@t3tools/contracts";
8
9import { useClosedViewStore } from "~/closedViewStore";
10import { beginPreviewSessionClose, cancelPreviewSessionClose } from "~/previewStateStore";
11
12interface ClosePreviewSessionInput<E> {
13 readonly closePreview: (input: {
14 readonly environmentId: EnvironmentId;
15 readonly input: PreviewCloseInput;
16 }) => Promise<AtomCommandResult<void, E>>;
17 readonly snapshot: PreviewSessionSnapshot | null;
18 readonly tabId: string;
19 readonly threadRef: ScopedThreadRef;
20}
21
22export async function closePreviewSession<E>(
23 input: ClosePreviewSessionInput<E>,
24): Promise<AtomCommandResult<void, E>> {
25 beginPreviewSessionClose(input.threadRef, input.tabId);
26 const result = await input.closePreview({
27 environmentId: input.threadRef.environmentId,
28 input: { threadId: input.threadRef.threadId, tabId: input.tabId },
29 });
30 if (result._tag === "Failure") {
31 cancelPreviewSessionClose(input.threadRef, input.snapshot, input.tabId);
32 } else if (input.snapshot) {
33 useClosedViewStore.getState().remember({
34 kind: "browser",
35 threadRef: input.threadRef,
36 snapshot: input.snapshot,
37 });
38 }
39 return result;
40}