| 1 | import type { DiscoveredLocalServer, ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | mapAtomCommandResult, |
| 4 | type AtomCommandResult, |
| 5 | } from "@t3tools/client-runtime/state/runtime"; |
| 6 | |
| 7 | import { resolveDiscoveredServerUrl } from "~/browser/browserTargetResolver"; |
| 8 | import type { BrowserSettingsReadError, OpenPreviewMutation } from "~/browser/openFileInPreview"; |
| 9 | import { recordVisitForThread } from "~/browserHistoryStore"; |
| 10 | import { useRightPanelStore } from "~/rightPanelStore"; |
| 11 | import { openPreviewSession } from "./openPreviewSession"; |
| 12 | import { previewRuntimeFor } from "~/browser/previewRuntime"; |
| 13 | |
| 14 | export async function openDiscoveredPort<E>(input: { |
| 15 | readonly threadRef: ScopedThreadRef; |
| 16 | readonly port: DiscoveredLocalServer; |
| 17 | readonly openPreview: OpenPreviewMutation<E>; |
| 18 | }): Promise<AtomCommandResult<void, E | BrowserSettingsReadError>> { |
| 19 | // A server tab runs on the environment, where loopback is already right. |
| 20 | const resolvedUrl = |
| 21 | previewRuntimeFor(input.threadRef.environmentId) === "server" |
| 22 | ? input.port.url |
| 23 | : resolveDiscoveredServerUrl(input.threadRef.environmentId, input.port.url); |
| 24 | const result = await openPreviewSession({ |
| 25 | openPreview: input.openPreview, |
| 26 | threadRef: input.threadRef, |
| 27 | url: resolvedUrl, |
| 28 | }); |
| 29 | return mapAtomCommandResult(result, (snapshot) => { |
| 30 | recordVisitForThread(input.threadRef, input.port.url); |
| 31 | useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId); |
| 32 | }); |
| 33 | } |