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

addBrowserSurface.ts

50 lines2.2 KB

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 ...(source.zoomFactor === undefined ? {} : { zoomFactor: source.zoomFactor }),
30 ...(source.colorScheme === undefined ? {} : { colorScheme: source.colorScheme }),
31 runtime: source.runtime ?? "desktop",
32 }),
33 });
34 return mapAtomCommandResult(result, (snapshot) => {
35 useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId);
36 if (source) {
37 const panels = useRightPanelStore.getState();
38 const { surfaces } = selectThreadRightPanelState(panels.byThreadKey, input.threadRef);
39 const sourceIndex = surfaces.findIndex(
40 (surface) => surface.kind === "preview" && surface.resourceId === source.tabId,
41 );
42 const duplicate = surfaces.find(
43 (surface) => surface.kind === "preview" && surface.resourceId === snapshot.tabId,
44 );
45 if (sourceIndex >= 0 && duplicate) {
46 panels.moveSurface(input.threadRef, duplicate.id, sourceIndex + 1);
47 }
48 }
49 });