Open History to see the last commit
1import { DEFAULT_TERMINAL_ID, type EnvironmentId, type ThreadId } from "@t3tools/contracts";
2import { SymbolView } from "expo-symbols";
3import { memo, useCallback, useEffect, useRef, useState } from "react";
4import { Pressable, View } from "react-native";
5
6import { AppText as Text } from "../../components/AppText";
7import { getEnvironmentClient } from "../../state/environment-session-registry";
8import {
9 attachTerminalSession,
10 useTerminalSession,
11 useTerminalSessionTarget,
12} from "../../state/use-terminal-session";
13import { TerminalSurface } from "./NativeTerminalSurface";
14import { hasNativeTerminalSurface } from "./nativeTerminalModule";
15import { terminalDebugLog } from "./terminalDebugLog";
16
17interface ThreadTerminalPanelProps {
18 readonly environmentId: EnvironmentId;
19 readonly threadId: ThreadId;
20 readonly cwd: string;
21 readonly worktreePath: string | null;
22 readonly visible: boolean;
23 readonly onClose: () => void;
24}
25
26const DEFAULT_TERMINAL_COLS = 80;
27const DEFAULT_TERMINAL_ROWS = 24;
28
29export const ThreadTerminalPanel = memo(function ThreadTerminalPanel(
30 props: ThreadTerminalPanelProps,
31) {
32 const nativeTerminalAvailable = hasNativeTerminalSurface();
33 const terminalId = DEFAULT_TERMINAL_ID;
34 const target = useTerminalSessionTarget({
35 environmentId: props.environmentId,
36 threadId: props.threadId,
37 terminalId,
38 });
39 const terminal = useTerminalSession(target);
40 const [lastGridSize, setLastGridSize] = useState({
41 cols: DEFAULT_TERMINAL_COLS,
42 rows: DEFAULT_TERMINAL_ROWS,
43 });
44 const lastGridSizeRef = useRef(lastGridSize);
45 lastGridSizeRef.current = lastGridSize;
46
47 const terminalKey = `${props.environmentId}:${props.threadId}:${terminalId}`;
48 const isRunning = terminal.status === "running" || terminal.status === "starting";
49
50 useEffect(() => {
51 if (!props.visible) {
52 return;
53 }
54
55 const client = getEnvironmentClient(props.environmentId);
56 if (!client) {
57 terminalDebugLog("panel:attach-skip", {
58 reason: "no-environment-client",
59 environmentId: props.environmentId,
60 });
61 return;
62 }
63
64 terminalDebugLog("panel:attach", {
65 environmentId: props.environmentId,
66 threadId: props.threadId,
67 terminalId,
68 });
69
70 return attachTerminalSession({
71 environmentId: props.environmentId,
72 client,
73 terminal: {
74 threadId: props.threadId,
75 terminalId,
76 cwd: props.cwd,
77 worktreePath: props.worktreePath,
78 cols: lastGridSizeRef.current.cols,
79 rows: lastGridSizeRef.current.rows,
80 },
81 });
82 }, [
83 props.cwd,
84 props.environmentId,
85 props.threadId,
86 props.worktreePath,
87 props.visible,
88 terminalId,
89 ]);
90
91 const handleInput = useCallback(
92 (data: string) => {
93 const client = getEnvironmentClient(props.environmentId);
94 if (!client || !isRunning) {
95 return;
96 }
97
98 void client.terminal.write({
99 threadId: props.threadId,
100 terminalId,
101 data,
102 });
103 },
104 [isRunning, props.environmentId, props.threadId, terminalId],
105 );
106
107 const handleResize = useCallback(
108 (size: { readonly cols: number; readonly rows: number }) => {
109 if (size.cols === lastGridSize.cols && size.rows === lastGridSize.rows) {
110 return;
111 }
112
113 setLastGridSize(size);
114 const client = getEnvironmentClient(props.environmentId);
115 if (!client || !isRunning) {
116 return;
117 }
118
119 void client.terminal.resize({
120 threadId: props.threadId,
121 terminalId,
122 cols: size.cols,
123 rows: size.rows,
124 });
125 },
126 [
127 isRunning,
128 lastGridSize.cols,
129 lastGridSize.rows,
130 props.environmentId,
131 props.threadId,
132 terminalId,
133 ],
134 );
135
136 if (!props.visible) {
137 return null;
138 }
139
140 return (
141 <View className="absolute inset-x-3 bottom-28 top-28 overflow-hidden rounded-[8px] border border-white/10 bg-neutral-950 shadow-2xl">
142 <View className="flex-row items-center justify-between border-b border-white/10 px-3 py-2">
143 <View className="min-w-0 flex-1">
144 <Text className="font-t3-bold text-[13px] text-neutral-100" numberOfLines={1}>
145 Terminal
146 </Text>
147 <Text className="text-[11px] text-neutral-500" numberOfLines={1}>
148 {nativeTerminalAvailable ? "Native Ghostty surface" : "Text fallback active"}
149 </Text>
150 </View>
151 <View className="flex-row items-center gap-2">
152 {terminal.error ? (
153 <Text className="max-w-44 text-right text-[11px] text-red-300" numberOfLines={1}>
154 {terminal.error}
155 </Text>
156 ) : null}
157 <Pressable
158 className="h-8 w-8 items-center justify-center rounded-[8px] bg-white/10"
159 onPress={props.onClose}
160 >
161 <SymbolView name="xmark" size={13} tintColor="#e5e5e5" type="monochrome" />
162 </Pressable>
163 </View>
164 </View>
165 <TerminalSurface
166 terminalKey={terminalKey}
167 buffer={terminal.buffer}
168 isRunning={isRunning}
169 onInput={handleInput}
170 onResize={handleResize}
171 style={{ flex: 1 }}
172 />
173 </View>
174 );
175});