import { forwardRef, useImperativeHandle, useRef, useState, type FormEvent, type KeyboardEvent, } from "react"; import { ApiError } from "../lib/api-client"; import { useInterruptRun, useSteerRun } from "../lib/mutations"; import { ErrorMessage } from "./ui"; export interface SteerBarProps { runId: string; } export interface SteerBarHandle { focus(): void; } export const SteerBar = forwardRef(function SteerBar( { runId }, ref, ) { const [text, setText] = useState(""); const [errorMessage, setErrorMessage] = useState(null); const textareaRef = useRef(null); const steer = useSteerRun(runId); const interrupt = useInterruptRun(runId); const pending = steer.isMutating || interrupt.isMutating; useImperativeHandle(ref, () => ({ focus() { textareaRef.current?.focus(); }, })); const trimmed = text.trim(); const canSend = trimmed.length > 0 && !pending; async function sendSteering() { if (!canSend) return; setErrorMessage(null); try { await steer.trigger({ text: trimmed, interrupt: false }); setText(""); } catch (err) { setErrorMessage(formatSteerError(err)); } } async function fireInterrupt() { if (pending) return; setErrorMessage(null); try { await interrupt.trigger(); } catch (err) { setErrorMessage(formatInterruptError(err)); } } function handleSubmit(e: FormEvent) { e.preventDefault(); void sendSteering(); } function handleKeyDown(e: KeyboardEvent) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); void sendSteering(); } } return (