diff --git a/apps/web/components/task/bottom-terminal-panel.tsx b/apps/web/components/task/bottom-terminal-panel.tsx new file mode 100644 index 0000000000..b1245a6c08 --- /dev/null +++ b/apps/web/components/task/bottom-terminal-panel.tsx @@ -0,0 +1,103 @@ +"use client"; + +import { useRef, useState, useCallback, useEffect } from "react"; +import { IconMinus } from "@tabler/icons-react"; +import { useAppStore, useAppStoreApi } from "@/components/state-provider"; +import { PassthroughTerminal } from "./passthrough-terminal"; +import { Button } from "@kandev/ui/button"; +import { cn } from "@/lib/utils"; + +const MIN_HEIGHT = 150; +const MAX_HEIGHT_RATIO = 0.6; +const DEFAULT_HEIGHT = 300; +const STORAGE_KEY_HEIGHT = "bottom-terminal-height"; +const STORAGE_KEY_OPEN = "bottom-terminal-open"; + +type Props = { + sessionId: string | null; +}; + +export function BottomTerminalPanel({ sessionId }: Props) { + const visible = useAppStore((s) => s.bottomTerminal.isOpen); + const storeApi = useAppStoreApi(); + const toggle = useCallback(() => storeApi.getState().toggleBottomTerminal(), [storeApi]); + + // Restore visibility state from localStorage on mount + useEffect(() => { + const saved = localStorage.getItem(STORAGE_KEY_OPEN); + if (saved === "true" && !storeApi.getState().bottomTerminal.isOpen) { + toggle(); + } + }, [storeApi, toggle]); + const panelRef = useRef(null); + const [height, setHeight] = useState(() => { + if (typeof window === "undefined") return DEFAULT_HEIGHT; + const saved = localStorage.getItem(STORAGE_KEY_HEIGHT); + return saved ? Math.max(MIN_HEIGHT, parseInt(saved, 10) || DEFAULT_HEIGHT) : DEFAULT_HEIGHT; + }); + const isDragging = useRef(false); + + const handleMouseDown = useCallback( + (e: React.MouseEvent) => { + e.preventDefault(); + isDragging.current = true; + const startY = e.clientY; + const startHeight = height; + + const onMouseMove = (moveEvent: MouseEvent) => { + if (!isDragging.current) return; + const delta = startY - moveEvent.clientY; + const maxHeight = window.innerHeight * MAX_HEIGHT_RATIO; + const newHeight = Math.min(maxHeight, Math.max(MIN_HEIGHT, startHeight + delta)); + if (panelRef.current) panelRef.current.style.height = `${newHeight}px`; + }; + + const onMouseUp = () => { + isDragging.current = false; + if (panelRef.current) { + const finalHeight = panelRef.current.offsetHeight; + setHeight(finalHeight); + localStorage.setItem(STORAGE_KEY_HEIGHT, String(finalHeight)); + } + document.removeEventListener("mousemove", onMouseMove); + document.removeEventListener("mouseup", onMouseUp); + }; + + document.addEventListener("mousemove", onMouseMove); + document.addEventListener("mouseup", onMouseUp); + }, + [height], + ); + + if (!visible) return null; + + return ( +
+ {/* Resize handle */} +
+ {/* Header */} +
+ Terminal + +
+ {/* Terminal content - pb-2 prevents last line from being cut off */} +
+ +
+
+ ); +} diff --git a/apps/web/components/task/dockview-desktop-layout.tsx b/apps/web/components/task/dockview-desktop-layout.tsx index 9c05ae2667..0f1723b37a 100644 --- a/apps/web/components/task/dockview-desktop-layout.tsx +++ b/apps/web/components/task/dockview-desktop-layout.tsx @@ -40,6 +40,7 @@ import { CommitDetailPanel } from "./commit-detail-panel"; import { PRDetailPanelComponent } from "@/components/github/pr-detail-panel"; import { PreviewController } from "./preview/preview-controller"; import { ReviewDialog } from "@/components/review/review-dialog"; +import { BottomTerminalPanel } from "./bottom-terminal-panel"; import { stopVscode } from "@/lib/api/domains/vscode-api"; import { stopUserShell } from "@/lib/api/domains/user-shell-api"; import { useReviewDialog } from "./use-review-dialog"; @@ -705,7 +706,7 @@ export const DockviewDesktopLayout = memo(function DockviewDesktopLayout({ return (
- - +
+ + +
+ {effectiveSessionId && ( { try { - const backendUrl = getBackendConfig().apiBaseUrl; - const url = new URL(backendUrl); - const protocol = url.protocol === "https:" ? "wss:" : "ws:"; - return `${protocol}//${url.host}`; + const url = new URL(getBackendConfig().apiBaseUrl); + return `${url.protocol === "https:" ? "wss:" : "ws:"}//${url.host}`; } catch { return "ws://localhost:8080"; } @@ -86,9 +85,9 @@ export function PassthroughTerminal(props: PassthroughTerminalProps) { const [connectedSessionId, setConnectedSessionId] = useState(null); const isConnected = sessionId != null && connectedSessionId === sessionId; const onConnected = useCallback(() => { - log("WebSocket connected — hiding loading overlay for session", sessionId); setConnectedSessionId(sessionId ?? null); - }, [sessionId]); + if (autoFocus) xtermRef.current?.focus(); + }, [sessionId, autoFocus]); const linkHandler = useTerminalLinkHandler(); const sendResize = useSendResize(wsRef); @@ -100,6 +99,7 @@ export function PassthroughTerminal(props: PassthroughTerminalProps) { sendResize, }); + const toggleBottomTerminal = useAppStore((s) => s.toggleBottomTerminal); useTerminalInit({ terminalRef, xtermRef, @@ -111,6 +111,7 @@ export function PassthroughTerminal(props: PassthroughTerminalProps) { fitAndResize, onReady: onTerminalReady, linkHandler, + onToggleBottomTerminal: toggleBottomTerminal, }); useWebSocketConnection({ diff --git a/apps/web/components/task/use-passthrough-terminal.ts b/apps/web/components/task/use-passthrough-terminal.ts index 1ae4880881..642359fc47 100644 --- a/apps/web/components/task/use-passthrough-terminal.ts +++ b/apps/web/components/task/use-passthrough-terminal.ts @@ -7,6 +7,8 @@ import { WebLinksAddon } from "@xterm/addon-web-links"; import { WebglAddon } from "@xterm/addon-webgl"; import { getTerminalTheme } from "@/lib/theme/terminal-theme"; import { startReconnectLoop } from "./ws-reconnect"; +import { SHORTCUTS } from "@/lib/keyboard/constants"; +import { matchesShortcut } from "@/lib/keyboard/utils"; // Debug flag - set to true to see detailed logs const DEBUG = false; @@ -57,6 +59,7 @@ function initTerminalInstance( >, fitAndResize: (force?: boolean) => void, linkHandler?: (event: MouseEvent, uri: string) => void, + onToggleBottomTerminal?: () => void, ) { if (refs.isInitializedRef.current || refs.xtermRef.current) return undefined; refs.isInitializedRef.current = true; @@ -79,14 +82,20 @@ function initTerminalInstance( terminal.unicode.activeVersion = "11"; const webLinksAddon = new WebLinksAddon(linkHandler); terminal.loadAddon(webLinksAddon); - log("Opening terminal in container"); terminal.open(termContainer); + // Intercept bottom terminal shortcut to toggle panel + terminal.attachCustomKeyEventHandler((event) => { + if (matchesShortcut(event, SHORTCUTS.BOTTOM_TERMINAL)) { + if (event.type === "keydown" && onToggleBottomTerminal) onToggleBottomTerminal(); + return false; + } + return true; + }); try { fitAddon.fit(); refs.lastDimensionsRef.current = { cols: terminal.cols, rows: terminal.rows }; - log("Initial fit:", terminal.cols, "x", terminal.rows); - } catch (e) { - log("Initial fit failed:", e); + } catch { + /* fit failed */ } refs.xtermRef.current = terminal; refs.fitAddonRef.current = fitAddon; @@ -141,6 +150,11 @@ function initTerminalInstance( }; } +type TerminalInitHookOptions = TerminalInitOptions & { + linkHandler?: (event: MouseEvent, uri: string) => void; + onToggleBottomTerminal?: () => void; +}; + export function useTerminalInit({ terminalRef, xtermRef, @@ -152,7 +166,8 @@ export function useTerminalInit({ fitAndResize, onReady, linkHandler, -}: TerminalInitOptions & { linkHandler?: (event: MouseEvent, uri: string) => void }) { + onToggleBottomTerminal, +}: TerminalInitHookOptions) { const refs = { xtermRef, fitAddonRef, @@ -178,7 +193,7 @@ export function useTerminalInit({ const rect = container.getBoundingClientRect(); log("Init check: dimensions", rect.width, "x", rect.height); if (rect.width >= MIN_WIDTH && rect.height >= MIN_HEIGHT) { - initTerminalInstance(container, refs, fitAndResize, linkHandler); + initTerminalInstance(container, refs, fitAndResize, linkHandler, onToggleBottomTerminal); onReady(); return true; } diff --git a/apps/web/hooks/use-editor-keybinds.ts b/apps/web/hooks/use-editor-keybinds.ts index f5ee202bc0..946be95535 100644 --- a/apps/web/hooks/use-editor-keybinds.ts +++ b/apps/web/hooks/use-editor-keybinds.ts @@ -4,6 +4,8 @@ import { useEffect, useRef } from "react"; import { useDockviewStore } from "@/lib/state/dockview-store"; import { useAppStoreApi } from "@/components/state-provider"; import { createUserShell } from "@/lib/api/domains/user-shell-api"; +import { SHORTCUTS } from "@/lib/keyboard/constants"; +import { matchesShortcut } from "@/lib/keyboard/utils"; import type { DockviewApi } from "dockview-react"; function handleTabNavigation(e: KeyboardEvent, api: DockviewApi) { @@ -89,10 +91,20 @@ function handleLayoutToggle(e: KeyboardEvent): boolean { return true; } - if (isCmdKey(e, "KeyJ")) { + return false; +} + +function handleBottomTerminal( + e: KeyboardEvent, + appStore: ReturnType, +): boolean { + if (isEditableTarget(e)) return false; + + // Cmd/Ctrl+J — toggle bottom terminal panel + if (matchesShortcut(e, SHORTCUTS.BOTTOM_TERMINAL)) { e.preventDefault(); e.stopPropagation(); - useDockviewStore.getState().toggleRightPanels(); + appStore.getState().toggleBottomTerminal(); return true; } @@ -104,7 +116,7 @@ function handleLayoutToggle(e: KeyboardEvent): boolean { * - Cmd/Ctrl+Shift+[ / ] — navigate prev/next tab in active group * - Ctrl+` — toggle terminal focus * - Cmd/Ctrl+B — toggle sidebar - * - Cmd/Ctrl+J — toggle right panels + * - Cmd/Ctrl+J — toggle bottom terminal panel */ export function useEditorKeybinds() { const previousPanelIdRef = useRef(null); @@ -137,10 +149,12 @@ export function useEditorKeybinds() { return; } - handleLayoutToggle(e); + if (handleLayoutToggle(e)) return; + handleBottomTerminal(e, appStore); }; - window.addEventListener("keydown", handler); - return () => window.removeEventListener("keydown", handler); + // Use capture phase so we receive events before xterm.js + window.addEventListener("keydown", handler, true); + return () => window.removeEventListener("keydown", handler, true); }, [appStore]); } diff --git a/apps/web/lib/keyboard/constants.ts b/apps/web/lib/keyboard/constants.ts index 55b1e55a3f..9fb4bde182 100644 --- a/apps/web/lib/keyboard/constants.ts +++ b/apps/web/lib/keyboard/constants.ts @@ -141,4 +141,8 @@ export const SHORTCUTS = { key: KEYS.Q, modifiers: { ctrlOrCmd: true, shift: true }, }, + BOTTOM_TERMINAL: { + key: KEYS.J, + modifiers: { ctrlOrCmd: true }, + }, } as const; diff --git a/apps/web/lib/state/dockview-store.ts b/apps/web/lib/state/dockview-store.ts index 3812da251f..3059957b07 100644 --- a/apps/web/lib/state/dockview-store.ts +++ b/apps/web/lib/state/dockview-store.ts @@ -287,6 +287,7 @@ function buildVisibilityActions(set: StoreSet, get: StoreGet) { }); } }, + setSidebarVisible: (visible: boolean) => { const { sidebarVisible } = get(); if (sidebarVisible === visible) return; diff --git a/apps/web/lib/state/slices/ui/types.ts b/apps/web/lib/state/slices/ui/types.ts index 211dd27a8a..d6d588a76d 100644 --- a/apps/web/lib/state/slices/ui/types.ts +++ b/apps/web/lib/state/slices/ui/types.ts @@ -69,6 +69,10 @@ export type SessionFailureNotification = { message: string; }; +export type BottomTerminalState = { + isOpen: boolean; +}; + export type UISliceState = { previewPanel: PreviewPanelState; rightPanel: RightPanelState; @@ -81,6 +85,7 @@ export type UISliceState = { systemHealth: SystemHealthState; quickChat: QuickChatState; sessionFailureNotification: SessionFailureNotification | null; + bottomTerminal: BottomTerminalState; }; export type UISliceActions = { @@ -108,6 +113,7 @@ export type UISliceActions = { setActiveQuickChatSession: (sessionId: string) => void; renameQuickChatSession: (sessionId: string, name: string) => void; setSessionFailureNotification: (n: SessionFailureNotification | null) => void; + toggleBottomTerminal: () => void; }; export type UISlice = UISliceState & UISliceActions; diff --git a/apps/web/lib/state/slices/ui/ui-slice.ts b/apps/web/lib/state/slices/ui/ui-slice.ts index e1b87f9320..5338be1446 100644 --- a/apps/web/lib/state/slices/ui/ui-slice.ts +++ b/apps/web/lib/state/slices/ui/ui-slice.ts @@ -21,6 +21,7 @@ export const defaultUIState: UISliceState = { systemHealth: { issues: [], healthy: true, loaded: false, loading: false }, quickChat: { isOpen: false, sessions: [], activeSessionId: null }, sessionFailureNotification: null, + bottomTerminal: { isOpen: false }, }; type ImmerSet = Parameters[0]; @@ -188,4 +189,13 @@ export const createUISlice: StateCreator { draft.sessionFailureNotification = n; }), + toggleBottomTerminal: () => + set((draft) => { + const newValue = !draft.bottomTerminal.isOpen; + draft.bottomTerminal.isOpen = newValue; + // Persist to localStorage + if (typeof window !== "undefined") { + localStorage.setItem("bottom-terminal-open", String(newValue)); + } + }), }); diff --git a/apps/web/lib/state/store.ts b/apps/web/lib/state/store.ts index f36b0bae57..4687848709 100644 --- a/apps/web/lib/state/store.ts +++ b/apps/web/lib/state/store.ts @@ -205,6 +205,7 @@ export type AppState = { systemHealth: (typeof defaultUIState)["systemHealth"]; quickChat: (typeof defaultUIState)["quickChat"]; sessionFailureNotification: (typeof defaultUIState)["sessionFailureNotification"]; + bottomTerminal: (typeof defaultUIState)["bottomTerminal"]; // GitHub actions setGitHubStatus: (status: GitHubStatus | null) => void; @@ -317,6 +318,7 @@ export type AppState = { setSessionFailureNotification: ( n: import("./slices/ui/types").SessionFailureNotification | null, ) => void; + toggleBottomTerminal: () => void; setMessages: ( sessionId: string, messages: Message[], @@ -457,6 +459,7 @@ const defaultState = { systemHealth: defaultUIState.systemHealth, quickChat: defaultUIState.quickChat, sessionFailureNotification: defaultUIState.sessionFailureNotification, + bottomTerminal: defaultUIState.bottomTerminal, }; function mergeInitialState(initialState?: Partial): typeof defaultState { @@ -532,6 +535,7 @@ function mergeInitialState(initialState?: Partial): typeof defaultStat quickChat: { ...defaultState.quickChat, ...initialState.quickChat }, sessionFailureNotification: initialState.sessionFailureNotification ?? defaultState.sessionFailureNotification, + bottomTerminal: { ...defaultState.bottomTerminal, ...initialState.bottomTerminal }, }; } @@ -614,6 +618,7 @@ export function createAppStore(initialState?: Partial) { systemHealth: merged.systemHealth, quickChat: merged.quickChat, sessionFailureNotification: merged.sessionFailureNotification, + bottomTerminal: merged.bottomTerminal, // Add hydrate method // eslint-disable-next-line @typescript-eslint/no-explicit-any hydrate: (state, options) => set((draft) => hydrateState(draft as any, state, options)),