1import { getTerminalPlacement } from "~/hooks/useTerminalPlacement";
2import type { ScopedThreadRef } from "@t3tools/contracts";
3
4import { selectThreadRightPanelState, useRightPanelStore } from "../rightPanelStore";
5import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
6
7/** Reveal an existing terminal in the panel that owns it, without opening another PTY. */
8export function revealThreadTerminal(threadRef: ScopedThreadRef, terminalId: string) {
9 const panels = useRightPanelStore.getState();
10 const surface = selectThreadRightPanelState(panels.byThreadKey, threadRef).surfaces.find(
11 (surface) => surface.kind === "terminal" && surface.terminalIds.includes(terminalId),
12 );
13 if (surface) {
14 panels.activateTerminal(threadRef, surface.id, terminalId);
15 panels.activateSurface(threadRef, surface.id);
16 return;
17 }
18 const terminalUi = useTerminalUiStateStore.getState();
19 const drawer = selectThreadTerminalUiState(terminalUi.terminalUiStateByThreadKey, threadRef);
20 if (!drawer.terminalIds.includes(terminalId) && getTerminalPlacement() === "right") {
21 panels.openTerminal(threadRef, terminalId);
22 return;
23 }
24 terminalUi.ensureTerminal(threadRef, terminalId, {
25 open: true,
26 active: true,
27 });
28}