Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:23blocks OS Ai maestro Use Terminal Hook

From Leeroopedia
Knowledge Sources
Domains UI, Terminal
Last Updated 2026-10-26 18:00 GMT

Overview

Concrete tool for creating and managing an xterm.js terminal instance with WebGL rendering, addon loading, and lifecycle control, provided by AI Maestro's `hooks/useTerminal.ts`.

Description

The useTerminal hook encapsulates the full lifecycle of an xterm.js `Terminal` instance. On initialization it dynamically imports xterm core and addons (FitAddon, WebLinksAddon, ClipboardAddon, Unicode11Addon, WebglAddon), creates the terminal with PTY-safe settings (`convertEol: false`, 10000 scrollback, alternate screen buffer support), opens it in a container, and fits. WebGL is loaded inline during init (not via a separate effect) to avoid a race condition on agent switch; on WebGL context loss (mobile backgrounding) it falls back to canvas by re-opening the terminal element. The hook exposes the terminal ref, fit addon ref, a `sendData` ref for the WebSocket layer to inject PTY data, and register/unregister callbacks. Per AI Maestro terminal rules, initialization happens once on mount (empty dependency array) and `session.id` is never added to that effect.

Usage

Call this hook from a terminal view component that owns a container `

`. Pass `disableWebGL` on touch devices. Use `onRegister` to receive the FitAddon for resize handling, and set `sendDataRef.current` to forward user input to the PTY via WebSocket.

Code Reference

Source Location

  • Repository: ai-maestro
  • File: hooks/useTerminal.ts
  • Lines: L1-L391

Signature

interface UseTerminalOptions {
  fontSize?: number
  fontFamily?: string
  theme?: Record<string, string>
  sessionId?: string
  disableWebGL?: boolean
  onRegister?: (fitAddon: FitAddon) => void
  onUnregister?: () => void
}

export function useTerminal(options?: UseTerminalOptions): {
  terminalRef: React.MutableRefObject<Terminal | null>
  fitAddonRef: React.MutableRefObject<FitAddon | null>
  initializeTerminal: (container: HTMLElement) => Promise<void>
  sendDataRef: React.MutableRefObject<((data: string) => void) | null>
}

Import

import { useTerminal } from '@/hooks/useTerminal'

I/O Contract

Inputs

Name Type Required Description
options.fontSize number No Terminal font size (default 16)
options.fontFamily string No Monospace font stack
options.theme Record<string,string> No xterm color theme
options.sessionId string No For labeling helper textarea
options.disableWebGL boolean No Skip WebGL on touch devices
options.onRegister (fitAddon) => void No Callback when FitAddon is ready
options.onUnregister () => void No Cleanup callback

Outputs

Name Type Description
terminalRef null> The xterm Terminal instance
fitAddonRef null> FitAddon for resize calls
initializeTerminal (container) => Promise<void> Async init: creates terminal + loads addons
sendDataRef null> Set by parent to forward input to PTY

Usage Examples

Basic terminal mount

import { useRef, useEffect } from 'react'
import { useTerminal } from '@/hooks/useTerminal'

function MyTerminal({ sessionId }: { sessionId: string }) {
  const containerRef = useRef<HTMLDivElement>(null)
  const { terminalRef, fitAddonRef, initializeTerminal, sendDataRef } =
    useTerminal({ sessionId })

  useEffect(() => {
    if (containerRef.current) {
      initializeTerminal(containerRef.current)
    }
    // Empty dependency array — init once on mount (per terminal rules)
  }, [])

  return <div ref={containerRef} style={{ width: '100%', height: '100%' }} />
}

Resize handling via onRegister

const { initializeTerminal } = useTerminal({
  sessionId,
  onRegister: (fitAddon) => {
    window.addEventListener('resize', () => fitAddon.fit())
  },
})

Related Pages

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment