Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadTerminalPanel.tsx

161 lines5.0 KB

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