Open History to see the last commit
1import {
2 mapAtomCommandResult,
3 type AtomCommandResult,
4} from "@t3tools/client-runtime/state/runtime";
5import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts";
6
7import type { BrowserSettingsReadError, OpenPreviewMutation } from "~/browser/openFileInPreview";
8import { selectThreadRightPanelState, useRightPanelStore } from "~/rightPanelStore";
9
10import { openPreviewSession } from "./openPreviewSession";
11
12export async function addBrowserSurface<E>(input: {
13 readonly threadRef: ScopedThreadRef;
14 readonly openPreview: OpenPreviewMutation<E>;
15 readonly profileId?: string | undefined;
16 readonly duplicateFrom?: PreviewSessionSnapshot;
17}): Promise<AtomCommandResult<void, E | BrowserSettingsReadError>> {
18 const source = input.duplicateFrom;
19 const result = await openPreviewSession({
20 openPreview: input.openPreview,
21 threadRef: input.threadRef,
22 ...(input.profileId === undefined ? {} : { profileId: input.profileId }),
23 ...(source === undefined
24 ? {}
25 : {
26 ...(source.navStatus._tag === "Idle" ? {} : { url: source.navStatus.url }),
27 ...(source.profileId === undefined ? {} : { profileId: source.profileId }),
28 ...(source.viewport === undefined ? {} : { viewport: source.viewport }),
29 runtime: source.runtime ?? "desktop",
30 }),
31 });
32 return mapAtomCommandResult(result, (snapshot) => {
33 useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId);
34 if (source) {
35 const panels = useRightPanelStore.getState();
36 const { surfaces } = selectThreadRightPanelState(panels.byThreadKey, input.threadRef);
37 const sourceIndex = surfaces.findIndex(
38 (surface) => surface.kind === "preview" && surface.resourceId === source.tabId,
39 );
40 const duplicate = surfaces.find(
41 (surface) => surface.kind === "preview" && surface.resourceId === snapshot.tabId,
42 );
43 if (sourceIndex >= 0 && duplicate) {
44 panels.moveSurface(input.threadRef, duplicate.id, sourceIndex + 1);
45 }
46 }
47 });
48}