Add close-terminal actions and state handling in thread drawer
- Add terminal close controls in tab and split terminal views - Send `exit` before closing and update active terminal/focus in chat state - Add reducer support and tests for closing tabs, split fallback, and final-terminal drawer hide
This commit is contained in:
1 parent
800eb3b547
commit
ee463f6b76
4 files changed
+182
-18
No files matched your search
@@ -299,6 +299,21 @@ export default function ChatView() {
|
||||
},
|
||||
[activeThreadId, dispatch],
|
||||
);
|
||||
const closeTerminal = useCallback(
|
||||
(terminalId: string) => {
|
||||
if (!activeThreadId || !api) return;
|
||||
void api.terminal.write({ threadId: activeThreadId, terminalId, data: "exit\n" }).catch(() => {
|
||||
// Ignore write errors so the UI can still close an uninitialized or already-exited terminal.
|
||||
});
|
||||
dispatch({
|
||||
type: "CLOSE_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId,
|
||||
});
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
},
|
||||
[activeThreadId, api, dispatch],
|
||||
);
|
||||
|
||||
const handleRuntimeModeChange = async (mode: "approval-required" | "full-access") => {
|
||||
if (mode === state.runtimeMode) return;
|
||||
@@ -1489,6 +1504,7 @@ export default function ChatView() {
|
||||
onSplitTerminal={splitTerminal}
|
||||
onNewTerminal={createNewTerminal}
|
||||
onActiveTerminalChange={activateTerminal}
|
||||
onCloseTerminal={closeTerminal}
|
||||
onHeightChange={(height) =>
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_HEIGHT",
|
||||
|
||||
@@ -421,6 +421,7 @@ interface ThreadTerminalDrawerProps {
|
||||
onSplitTerminal: () => void;
|
||||
onNewTerminal: () => void;
|
||||
onActiveTerminalChange: (terminalId: string) => void;
|
||||
onCloseTerminal: (terminalId: string) => void;
|
||||
onHeightChange: (height: number) => void;
|
||||
}
|
||||
|
||||
@@ -437,6 +438,7 @@ export default function ThreadTerminalDrawer({
|
||||
onSplitTerminal,
|
||||
onNewTerminal,
|
||||
onActiveTerminalChange,
|
||||
onCloseTerminal,
|
||||
onHeightChange,
|
||||
}: ThreadTerminalDrawerProps) {
|
||||
const [drawerHeight, setDrawerHeight] = useState(() => clampDrawerHeight(height));
|
||||
@@ -482,6 +484,13 @@ export default function ThreadTerminalDrawer({
|
||||
: [resolvedActiveTerminalId];
|
||||
|
||||
const showTabBar = terminalLayout === "tabs" && normalizedTerminalIds.length > 1;
|
||||
const terminalLabelById = useMemo(
|
||||
() =>
|
||||
new Map(
|
||||
normalizedTerminalIds.map((terminalId, index) => [terminalId, `Terminal ${index + 1}`]),
|
||||
),
|
||||
[normalizedTerminalIds],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
onHeightChangeRef.current = onHeightChange;
|
||||
@@ -593,21 +602,37 @@ export default function ThreadTerminalDrawer({
|
||||
{showTabBar ? (
|
||||
<div className="min-w-0 flex-1 overflow-x-auto">
|
||||
<div className="inline-flex rounded-md border border-border/80 bg-muted/40 p-0.5">
|
||||
{normalizedTerminalIds.map((terminalId, index) => {
|
||||
{normalizedTerminalIds.map((terminalId) => {
|
||||
const isActive = terminalId === resolvedActiveTerminalId;
|
||||
return (
|
||||
<button
|
||||
<div
|
||||
key={terminalId}
|
||||
type="button"
|
||||
className={`rounded px-2.5 py-1 text-xs transition-colors ${
|
||||
className={`inline-flex items-center gap-1 rounded px-2.5 py-1 text-xs transition-colors ${
|
||||
isActive
|
||||
? "bg-background text-foreground shadow-sm"
|
||||
: "text-muted-foreground hover:text-foreground"
|
||||
: "text-muted-foreground"
|
||||
}`}
|
||||
onClick={() => onActiveTerminalChange(terminalId)}
|
||||
>
|
||||
{`Terminal ${index + 1}`}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="pr-1 hover:text-foreground"
|
||||
onClick={() => onActiveTerminalChange(terminalId)}
|
||||
>
|
||||
{terminalLabelById.get(terminalId) ?? "Terminal"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded px-1 text-[11px] text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
onCloseTerminal(terminalId);
|
||||
}}
|
||||
aria-label={`Close ${terminalLabelById.get(terminalId) ?? "terminal"}`}
|
||||
title={`Close ${terminalLabelById.get(terminalId) ?? "terminal"}`}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
@@ -632,6 +657,14 @@ export default function ThreadTerminalDrawer({
|
||||
>
|
||||
New Terminal
|
||||
</button>
|
||||
<div className="h-5 w-px bg-border/80" />
|
||||
<button
|
||||
type="button"
|
||||
className="px-2.5 py-1 text-xs text-foreground/90 transition-colors hover:bg-accent"
|
||||
onClick={() => onCloseTerminal(resolvedActiveTerminalId)}
|
||||
>
|
||||
Close Terminal
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -639,17 +672,45 @@ export default function ThreadTerminalDrawer({
|
||||
{terminalLayout === "split" && visibleTerminalIds.length === 2 ? (
|
||||
<div className="grid h-full w-full grid-cols-2 gap-1">
|
||||
{visibleTerminalIds.map((terminalId) => (
|
||||
<TerminalViewport
|
||||
<div
|
||||
key={terminalId}
|
||||
api={api}
|
||||
threadId={threadId}
|
||||
terminalId={terminalId}
|
||||
cwd={cwd}
|
||||
focusRequestId={focusRequestId}
|
||||
autoFocus={terminalId === resolvedActiveTerminalId}
|
||||
resizeEpoch={resizeEpoch}
|
||||
drawerHeight={drawerHeight}
|
||||
/>
|
||||
className="flex min-h-0 flex-col rounded-[4px] border border-border/70"
|
||||
>
|
||||
<div className="flex items-center justify-between border-b border-border/70 px-2 py-1">
|
||||
<button
|
||||
type="button"
|
||||
className={`text-xs ${
|
||||
terminalId === resolvedActiveTerminalId
|
||||
? "text-foreground"
|
||||
: "text-muted-foreground hover:text-foreground"
|
||||
}`}
|
||||
onClick={() => onActiveTerminalChange(terminalId)}
|
||||
>
|
||||
{terminalLabelById.get(terminalId) ?? "Terminal"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rounded px-1 text-xs text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
|
||||
onClick={() => onCloseTerminal(terminalId)}
|
||||
aria-label={`Close ${terminalLabelById.get(terminalId) ?? "terminal"}`}
|
||||
title={`Close ${terminalLabelById.get(terminalId) ?? "terminal"}`}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
<div className="min-h-0 flex-1">
|
||||
<TerminalViewport
|
||||
api={api}
|
||||
threadId={threadId}
|
||||
terminalId={terminalId}
|
||||
cwd={cwd}
|
||||
focusRequestId={focusRequestId}
|
||||
autoFocus={terminalId === resolvedActiveTerminalId}
|
||||
resizeEpoch={resizeEpoch}
|
||||
drawerHeight={drawerHeight}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
|
||||
@@ -160,6 +160,42 @@ describe("store reducer thread continuity", () => {
|
||||
expect(next.threads[0]?.activeTerminalId).toBe("term-2");
|
||||
});
|
||||
|
||||
it("closes a terminal tab and falls back to single layout when one remains", () => {
|
||||
const state = makeState(
|
||||
makeThread({
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID, "term-2"],
|
||||
activeTerminalId: "term-2",
|
||||
terminalLayout: "tabs",
|
||||
}),
|
||||
);
|
||||
const next = reducer(state, {
|
||||
type: "CLOSE_THREAD_TERMINAL",
|
||||
threadId: "thread-local-1",
|
||||
terminalId: "term-2",
|
||||
});
|
||||
|
||||
expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID]);
|
||||
expect(next.threads[0]?.activeTerminalId).toBe(DEFAULT_THREAD_TERMINAL_ID);
|
||||
expect(next.threads[0]?.terminalLayout).toBe("single");
|
||||
});
|
||||
|
||||
it("closes the final terminal and hides the drawer", () => {
|
||||
const state = makeState(
|
||||
makeThread({
|
||||
terminalOpen: true,
|
||||
}),
|
||||
);
|
||||
const next = reducer(state, {
|
||||
type: "CLOSE_THREAD_TERMINAL",
|
||||
threadId: "thread-local-1",
|
||||
terminalId: DEFAULT_THREAD_TERMINAL_ID,
|
||||
});
|
||||
|
||||
expect(next.threads[0]?.terminalOpen).toBe(false);
|
||||
expect(next.threads[0]?.terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID]);
|
||||
expect(next.threads[0]?.terminalLayout).toBe("single");
|
||||
});
|
||||
|
||||
it("backfills codexThreadId from routed provider events", () => {
|
||||
const state = makeState(makeThread({ codexThreadId: null }));
|
||||
const next = reducer(state, {
|
||||
|
||||
@@ -35,6 +35,7 @@ type Action =
|
||||
| { type: "SPLIT_THREAD_TERMINAL"; threadId: string; terminalId: string }
|
||||
| { type: "NEW_THREAD_TERMINAL"; threadId: string; terminalId: string }
|
||||
| { type: "SET_THREAD_ACTIVE_TERMINAL"; threadId: string; terminalId: string }
|
||||
| { type: "CLOSE_THREAD_TERMINAL"; threadId: string; terminalId: string }
|
||||
| { type: "TOGGLE_DIFF" }
|
||||
| {
|
||||
type: "APPLY_EVENT";
|
||||
@@ -189,6 +190,48 @@ function normalizeThreadTerminals(thread: Thread): Thread {
|
||||
};
|
||||
}
|
||||
|
||||
function closeThreadTerminal(thread: Thread, terminalId: string): Thread {
|
||||
if (!thread.terminalIds.includes(terminalId)) {
|
||||
return thread;
|
||||
}
|
||||
|
||||
const remainingTerminalIds = thread.terminalIds.filter((id) => id !== terminalId);
|
||||
if (remainingTerminalIds.length === 0) {
|
||||
return normalizeThreadTerminals({
|
||||
...thread,
|
||||
terminalOpen: false,
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
|
||||
terminalLayout: "single",
|
||||
splitTerminalIds: [],
|
||||
});
|
||||
}
|
||||
|
||||
const nextActiveTerminalId =
|
||||
thread.activeTerminalId === terminalId
|
||||
? (remainingTerminalIds[0] ?? DEFAULT_THREAD_TERMINAL_ID)
|
||||
: thread.activeTerminalId;
|
||||
let nextLayout = thread.terminalLayout;
|
||||
let nextSplitTerminalIds = thread.splitTerminalIds.filter((id) => id !== terminalId);
|
||||
|
||||
if (nextLayout === "split" && nextSplitTerminalIds.length < 2) {
|
||||
nextLayout = remainingTerminalIds.length > 1 ? "tabs" : "single";
|
||||
nextSplitTerminalIds = [];
|
||||
}
|
||||
|
||||
if (nextLayout === "tabs" && remainingTerminalIds.length < 2) {
|
||||
nextLayout = "single";
|
||||
}
|
||||
|
||||
return normalizeThreadTerminals({
|
||||
...thread,
|
||||
terminalIds: remainingTerminalIds,
|
||||
activeTerminalId: nextActiveTerminalId,
|
||||
terminalLayout: nextLayout,
|
||||
splitTerminalIds: nextSplitTerminalIds,
|
||||
});
|
||||
}
|
||||
|
||||
function findThreadBySessionId(threads: Thread[], sessionId: string): Thread | undefined {
|
||||
return threads.find((t) => t.session?.sessionId === sessionId);
|
||||
}
|
||||
@@ -454,6 +497,14 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
}),
|
||||
};
|
||||
|
||||
case "CLOSE_THREAD_TERMINAL":
|
||||
return {
|
||||
...state,
|
||||
threads: updateThread(state.threads, action.threadId, (thread) =>
|
||||
closeThreadTerminal(thread, action.terminalId),
|
||||
),
|
||||
};
|
||||
|
||||
case "TOGGLE_DIFF":
|
||||
return { ...state, diffOpen: !state.diffOpen };
|
||||
|
||||
|
||||
Reference in new issue
Block a user