1import type { ScopedThreadRef } from "@t3tools/contracts";
2
3import { browserMiniPlayerSource, usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore";
4import { whenPreviewTabKnown } from "~/previewStateStore";
5import { useRightPanelStore } from "~/rightPanelStore";
6
7/**
8 * Switches the viewer to a tab the page they drive just opened, as a browser
9 * does for a link click or `window.open`. A floating opener floats the popup;
10 * otherwise it becomes the panel's active tab. The opener stays open.
11 */
12export function showPreviewPopup(
13 threadRef: ScopedThreadRef,
14 popupTabId: string,
15 from: "panel" | "floating",
16): void {
17 whenPreviewTabKnown(threadRef, popupTabId, () => {
18 if (from === "floating") {
19 usePreviewMiniPlayerStore.getState().open(threadRef, browserMiniPlayerSource(popupTabId));
20 } else {
21 useRightPanelStore.getState().openBrowser(threadRef, popupTabId);
22 }
23 });
24}