| 1 | import { useCommittedCommand } from "../lib/useCommittedCommand"; |
| 2 | import { useGlobalShortcut } from "../lib/keyboardShortcuts"; |
| 3 | import { useLayoutStore, saveTerminalPanelOpen } from "../store/layout"; |
| 4 | import { useTerminalStore } from "../store/terminal"; |
| 5 | |
| 6 | function showTerminal() { |
| 7 | useLayoutStore.getState().setTerminalPanelOpen(true); |
| 8 | saveTerminalPanelOpen(true); |
| 9 | } |
| 10 | |
| 11 | /** Commands and shortcuts share the same committed capability boundary. */ |
| 12 | export function useTerminalPanelCommands(input: { tabId?: string; enabled: boolean; shortcutsEnabled?: boolean }) { |
| 13 | const toggleTerminalPanel = useCommittedCommand(() => { |
| 14 | if (!input.enabled) return; |
| 15 | const next = !useLayoutStore.getState().terminalPanelOpen; |
| 16 | useLayoutStore.getState().setTerminalPanelOpen(next); |
| 17 | saveTerminalPanelOpen(next); |
| 18 | }); |
| 19 | const openTerminalForPath = useCommittedCommand((path = ".") => { |
| 20 | if (!input.enabled) return; |
| 21 | showTerminal(); |
| 22 | if (input.tabId) void useTerminalStore.getState().createSession(input.tabId, path || ".", "default").catch(() => {}); |
| 23 | }); |
| 24 | const newTerminalSession = useCommittedCommand(() => { |
| 25 | if (input.tabId) openTerminalForPath(); |
| 26 | }); |
| 27 | const closeTerminalPanel = useCommittedCommand(() => { |
| 28 | useLayoutStore.getState().setTerminalPanelOpen(false); |
| 29 | saveTerminalPanelOpen(false); |
| 30 | }); |
| 31 | useGlobalShortcut("terminal.toggle", toggleTerminalPanel, [toggleTerminalPanel], input.shortcutsEnabled !== false); |
| 32 | useGlobalShortcut("terminal.newSession", newTerminalSession, [newTerminalSession], input.shortcutsEnabled !== false); |
| 33 | return { toggleTerminalPanel, openTerminalForPath, closeTerminalPanel }; |
| 34 | } |
| 35 |