Files
kandev/apps/web/lib/terminal/send-shell-input.ts
T
José Almeida 9dc7f8a43b fix(web): address review feedback on mobile pickers + multi-terminal
Bots flagged a real first-mount bug and a handful of smaller items;
addressed each:

- mobile-terminal-pane: track xterm in state instead of a ref so the
  active-sender registration effect re-runs once xterm is ready. With
  the ref, the effect ran once on mount, found xtermRef.current === null
  while PassthroughTerminal was still initializing (notably under the
  ResizeObserver fallback path when the container starts at 0×0), and
  silently never registered — leaving the key-bar wired to the default
  shell instead of the visible terminal.
- send-shell-input: wrap the activeSender call in try/catch so a stale
  registry entry (xterm GC'd mid-frame) falls back to the WS path
  instead of dropping the keystroke and silently consuming modifiers.
  Added tests for the activeSender routing branch (success, modifier
  consumption, fallback on throw).
- mobile-sessions-section: remove the dead `showCloseConfirm ? "Delete" : "Delete"`
  ternary and the unused prop on SessionActionsMenu.
- use-mobile-terminals: clear the auto-create guard when addTerminal()
  rejects so the user gets a retry instead of an empty terminal list.
- mobile-repo-pill: sort taskRepos by position before falling back to
  the first repo, so the pill label matches the picker's ordering when
  no session is active yet.
- session-task-switcher-sheet-hooks: catch loadTaskSessionsForTask
  rejection in handleSelectTask (was an unhandled rejection that left
  the sheet open on backend errors), and stabilize useCallback deps in
  handleWorkspaceChange by spreading the SheetNavOptions fields so a
  fresh literal each render doesn't defeat memoization. Extracted
  selectTaskWithoutPrimarySession to keep useSheetActions under the
  function-size lint cap.
2026-05-06 01:56:17 +01:00

56 lines
2.2 KiB
TypeScript

import { getWebSocketClient } from "@/lib/ws/connection";
import { applyShellModifiers } from "./apply-shell-modifiers";
import { getActiveTerminalSender } from "./mobile-active-terminal";
import { useShellModifiersStore, isActive } from "./shell-modifiers";
/**
* Single entry point for shell input. Applies active ctrl/shift modifiers,
* sends over the WS, then consumes latched (non-sticky) modifiers.
*
* Used by both the virtual key-bar and xterm's own `onData` callback so that
* a Ctrl latch set from the key-bar modifies the next character the user
* types on the OS keyboard — which is the whole point of the modifier.
*
* Mobile multi-terminal: when a PassthroughTerminal is registered as the
* active key-bar target, route input through it (xterm.paste → onData → its
* dedicated WS) instead of the per-session default shell. Falls back to the
* default `shell.input` action when no active terminal is registered.
*/
export function sendShellInput(sessionId: string, data: string): void {
if (!data) return;
const store = useShellModifiersStore.getState();
const ctrlActive = isActive(store.ctrl);
const shiftActive = isActive(store.shift);
const transformed = applyShellModifiers(data, { ctrl: ctrlActive, shift: shiftActive });
const activeSender = getActiveTerminalSender();
if (activeSender) {
try {
activeSender(transformed);
} catch (err) {
// A stale registry entry (e.g. xterm disposed mid-frame) shouldn't
// silently drop the keystroke nor consume modifiers. Fall through to
// the WS path so the input still has a chance of landing.
console.error("Active terminal sender threw, falling back to shell.input:", err);
const client = getWebSocketClient();
if (!client) return;
client.send({
type: "request",
action: "shell.input",
payload: { session_id: sessionId, data: transformed },
});
}
} else {
const client = getWebSocketClient();
if (!client) return; // keep modifiers armed; user can retry once reconnected
client.send({
type: "request",
action: "shell.input",
payload: { session_id: sessionId, data: transformed },
});
}
if (ctrlActive) store.consumeCtrl();
if (shiftActive) store.consumeShift();
}