|
| 1 | +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | +import { parseUaControlCenterMenuAction } from "@t3tools/shared/uaControlCenter"; |
| 3 | +import { useEffect, useMemo } from "react"; |
| 4 | + |
| 5 | +import { openUrlInPreview } from "~/browser/openFileInPreview"; |
| 6 | +import { useHandleNewThread } from "~/hooks/useHandleNewThread"; |
| 7 | +import { isPreviewSupportedInRuntime } from "~/previewStateStore"; |
| 8 | +import { previewEnvironment } from "~/state/preview"; |
| 9 | +import { useAtomCommand } from "~/state/use-atom-command"; |
| 10 | +import { stackedThreadToast, toastManager } from "~/components/ui/toast"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Opens the Mac hub Control Center in the existing desktop preview pane when |
| 14 | + * the application menu fires `open-ua-control-center:<url>`. |
| 15 | + * |
| 16 | + * Auth is the hub's `ua_ui` cookie in the shared BrowserSession partition — |
| 17 | + * never an MCP bearer in the renderer. |
| 18 | + */ |
| 19 | +export function UaControlCenterMenuHandler() { |
| 20 | + const { activeDraftThread, routeThreadRef } = useHandleNewThread(); |
| 21 | + const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false }); |
| 22 | + |
| 23 | + const threadRef = useMemo(() => { |
| 24 | + if (routeThreadRef) { |
| 25 | + return routeThreadRef; |
| 26 | + } |
| 27 | + if (activeDraftThread) { |
| 28 | + return scopeThreadRef(activeDraftThread.environmentId, activeDraftThread.threadId); |
| 29 | + } |
| 30 | + return null; |
| 31 | + }, [activeDraftThread, routeThreadRef]); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + const onMenuAction = window.desktopBridge?.onMenuAction; |
| 35 | + if (typeof onMenuAction !== "function") { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const unsubscribe = onMenuAction((action) => { |
| 40 | + const url = parseUaControlCenterMenuAction(action); |
| 41 | + if (url === null) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (!isPreviewSupportedInRuntime()) { |
| 46 | + toastManager.add( |
| 47 | + stackedThreadToast({ |
| 48 | + type: "info", |
| 49 | + title: "Control Center needs desktop preview", |
| 50 | + description: "Open T3 Code in the desktop app to embed Unified Agent Control Center.", |
| 51 | + }), |
| 52 | + ); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (threadRef === null) { |
| 57 | + toastManager.add( |
| 58 | + stackedThreadToast({ |
| 59 | + type: "info", |
| 60 | + title: "Open a thread first", |
| 61 | + description: "Unified Agent Control Center opens in the active thread's preview pane.", |
| 62 | + }), |
| 63 | + ); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + void openUrlInPreview({ |
| 68 | + threadRef, |
| 69 | + url, |
| 70 | + openPreview, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + return () => { |
| 75 | + unsubscribe?.(); |
| 76 | + }; |
| 77 | + }, [openPreview, threadRef]); |
| 78 | + |
| 79 | + return null; |
| 80 | +} |
0 commit comments