Skip to content

Commit afef22f

Browse files
refactor(mobile): terminal lifecycle runs in a session store, not screen effects
The mobile terminal screen kept its lifecycle in 14 effects plus two helper hooks: reopening an ended shell, closing and navigating away on exit, replaying the grid size on each output generation, writing the launch's initial input, resetting per-terminal state when the route param changed, and a 180 ms buffer-replay timer. The lifecycle now lives in a client-runtime session store (`terminalEnvironment.createSession`), mounted with `useAtomMount`. Its pure `transitionTerminalLifecycle` replaces `useTerminalLifecycle`, and grid replay, initial input and paste-session resets react to the attach stream where it lands. The screen is keyed by terminal target, so per-terminal state resets by remount instead of keyed-entry effects. The keyboard accessory follows will-show/will-hide through useSyncExternalStore. The fallback surface focuses its input through an imperative handle called from the handler. Effects: ThreadTerminalRouteScreen 14 -> 0, NativeTerminalSurface 4 -> 2 (the streaming output writer effects from #24261 stay: they own a native writer's lifetime), terminalLaunchContext 1 -> 0, useTerminalLifecycle and useTerminalGridSync deleted (1 -> 0 each). Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1c10563 commit afef22f

10 files changed

Lines changed: 972 additions & 1026 deletions

‎apps/mobile/src/features/terminal/NativeTerminalSurface.tsx‎

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { TerminalOutputState } from "@t3tools/client-runtime/state/terminal";
2-
import { memo, useCallback, useEffect, useRef, useState } from "react";
2+
import {
3+
memo,
4+
useCallback,
5+
useEffect,
6+
useImperativeHandle,
7+
useRef,
8+
useState,
9+
type Ref,
10+
} from "react";
311
import {
412
Pressable,
513
ScrollView,
@@ -38,7 +46,13 @@ interface TerminalResizeEvent {
3846
readonly rows: number;
3947
}
4048

49+
export interface TerminalSurfaceHandle {
50+
/** The text fallback focuses its input; the native view follows `keyboardFocusRequest`. */
51+
readonly focusKeyboard: () => void;
52+
}
53+
4154
interface TerminalSurfaceProps extends ViewProps {
55+
readonly ref?: Ref<TerminalSurfaceHandle>;
4256
readonly terminalKey: string;
4357
readonly buffer: string | null;
4458
readonly output: TerminalOutputState | null;
@@ -71,9 +85,10 @@ function estimateGridSize(input: {
7185
};
7286
}
7387

74-
const FallbackTerminalSurface = memo(function FallbackTerminalSurface(
75-
props: ReadyTerminalSurfaceProps,
76-
) {
88+
const FallbackTerminalSurface = memo(function FallbackTerminalSurface({
89+
ref,
90+
...props
91+
}: ReadyTerminalSurfaceProps) {
7792
const fontSize = props.fontSize ?? MOBILE_TYPOGRAPHY.label.fontSize;
7893
const inputRef = useRef<TextInputInstance>(null);
7994
const { themeAppearance, themeId } = useAppearancePreferences();
@@ -89,15 +104,14 @@ const FallbackTerminalSurface = memo(function FallbackTerminalSurface(
89104
props.onResize(estimateGridSize({ width, height, fontSize }));
90105
};
91106

92-
useEffect(() => {
93-
if (!props.readOnly && (props.keyboardFocusRequest ?? 0) > 0) {
107+
useImperativeHandle(ref, () => ({
108+
focusKeyboard: () => {
109+
if (props.readOnly) return;
110+
// Blur first so a focused input still raises the keyboard; focus lands after the blur commits.
94111
inputRef.current?.blur();
95-
const focusFrame = requestAnimationFrame(() => inputRef.current?.focus());
96-
return () => cancelAnimationFrame(focusFrame);
97-
}
98-
99-
return undefined;
100-
}, [props.keyboardFocusRequest, props.readOnly]);
112+
requestAnimationFrame(() => inputRef.current?.focus());
113+
},
114+
}));
101115

102116
return (
103117
<View
@@ -200,7 +214,6 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
200214
const theme = props.theme ?? getMobileTerminalTheme(themeId, themeAppearance);
201215
const { onInput, onResize } = props;
202216
const NativeTerminalSurfaceView = resolveNativeTerminalSurfaceView();
203-
const hasNativeSurface = Boolean(NativeTerminalSurfaceView);
204217
const streaming = supportsNativeTerminalOutput();
205218
const nativeRef = useRef<NativeTerminalSurfaceHandle>(null);
206219
const writerRef = useRef<ReturnType<typeof createTerminalOutputWriter> | null>(null);
@@ -224,15 +237,6 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
224237
});
225238
}, [nativeReady, props.output, streaming]);
226239

227-
useEffect(() => {
228-
terminalDebugLog("native:surface", {
229-
terminalKey: props.terminalKey,
230-
native: hasNativeSurface,
231-
hardwareKeyRevision: getNativeTerminalHardwareKeyRevision(),
232-
bufferLen: props.buffer.length,
233-
isRunning: props.isRunning,
234-
});
235-
}, [hasNativeSurface, props.buffer.length, props.isRunning, props.terminalKey]);
236240
const handleNativeInput = useCallback(
237241
(event: NativeSyntheticEvent<TerminalInputEvent>) => {
238242
if (!props.isRunning || props.readOnly) {
@@ -257,9 +261,21 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
257261
[onResize],
258262
);
259263

264+
const terminalKey = props.terminalKey;
265+
const logNativeMount = useCallback(
266+
(node: unknown) => {
267+
if (node === null) return;
268+
terminalDebugLog("native:surface", {
269+
terminalKey,
270+
hardwareKeyRevision: getNativeTerminalHardwareKeyRevision(),
271+
});
272+
},
273+
[terminalKey],
274+
);
275+
260276
if (NativeTerminalSurfaceView) {
261277
return (
262-
<View style={props.style}>
278+
<View ref={logNativeMount} style={props.style}>
263279
<NativeTerminalSurfaceView
264280
ref={nativeRef}
265281
appearanceScheme={themeAppearance}

0 commit comments

Comments
 (0)