MCPcopy Create free account
hub / github.com/coder/mux / useOpenTerminal

Function useOpenTerminal

src/browser/hooks/useOpenTerminal.ts:23–64  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21 * For local workspaces in browser: Opens a web-based xterm.js terminal in a popup window.
22 */
23export function useOpenTerminal() {
24 const { api } = useAPI();
25
26 return useCallback(
27 async (
28 workspaceId: string,
29 runtimeConfig?: RuntimeConfig,
30 options?: TerminalSessionCreateOptions
31 ) => {
32 if (!api) return;
33
34 // Check if running in browser mode
35 // window.api is only available in Electron (set by preload.ts)
36 // If window.api exists, we're in Electron; if not, we're in browser mode
37 const isBrowser = !window.api;
38 const isSSH = isSSHRuntime(runtimeConfig);
39 const isDevcontainer = isDevcontainerRuntime(runtimeConfig);
40
41 // SSH/Devcontainer workspaces always use web terminal (in browser popup or Electron window)
42 // because the PTY service handles the SSH/container connection.
43 //
44 // Callers (e.g. WorkspaceMenuBar, the markdown Run button's mobile path) discard the
45 // returned promise via `void`, so we must catch rejections here to avoid an unhandled
46 // promise rejection that the user perceives as the app silently freezing/crashing.
47 try {
48 if (isBrowser || isSSH || isDevcontainer) {
49 // Create terminal session first - window needs sessionId to connect.
50 const session = await createTerminalSession(api, workspaceId, options);
51 // Awaited so a rejected `terminal.openWindow` (e.g., Electron
52 // terminalWindowManager failure) is observed by this try/catch instead of
53 // becoming an unhandled rejection that looks like a silent freeze.
54 await openTerminalPopout(api, workspaceId, session.sessionId);
55 } else {
56 await api.terminal.openNative({ workspaceId });
57 }
58 } catch (err) {
59 console.error("[useOpenTerminal] Failed to open terminal:", err);
60 }
61 },
62 [api]
63 );
64}

Callers 3

AppInnerFunction · 0.90
WorkspaceShellFunction · 0.90
WorkspaceMenuBarFunction · 0.90

Calls 6

useAPIFunction · 0.90
isSSHRuntimeFunction · 0.90
isDevcontainerRuntimeFunction · 0.90
createTerminalSessionFunction · 0.90
openTerminalPopoutFunction · 0.90
openNativeMethod · 0.80

Tested by

no test coverage detected