The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 96 lines 3.4 KB
Raw
1 import { T, useGT } from "gt-react-native";
2 import type { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
3 import { TERMINAL_PALETTES, terminalOutputSpans } from "@t3tools/shared/terminalOutput";
4 import type React from "react";
5 import { useMemo, useRef } from "react";
6 import { ScrollView, View } from "react-native";
7
8 import { AppText as Text } from "../../components/ui/AppText";
9 import { useServerConfigs } from "../../state/entities";
10 import { orchestrationEnvironment } from "../../state/orchestration";
11 import { useEnvironmentQuery } from "../../state/query";
12
13 const FOLLOW_SLACK = 8;
14
15 export function useCommandOutputStreaming(environmentId: EnvironmentId): boolean {
16 return (
17 useServerConfigs().get(environmentId)?.environment.capabilities.commandOutputStreaming === true
18 );
19 }
20
21 /** Mount only while useCommandOutputStreaming holds; unmount to stop subscriptions for collapsed rows or other threads. */
22 export function ThreadCommandOutput(props: {
23 readonly environmentId: EnvironmentId;
24 readonly threadId: ThreadId;
25 readonly itemId: TurnItemId;
26 readonly themeAppearance: "light" | "dark";
27 }) {
28 const gt = useGT();
29 const { data, error } = useEnvironmentQuery(
30 orchestrationEnvironment.v2.commandOutput({
31 environmentId: props.environmentId,
32 input: { threadId: props.threadId, itemId: props.itemId },
33 }),
34 );
35 const scrollRef = useRef<React.ComponentRef<typeof ScrollView>>(null);
36 const followRef = useRef(true);
37 const text = data?.output.text ?? "";
38 const spans = useMemo(
39 () => terminalOutputSpans(text, TERMINAL_PALETTES[props.themeAppearance]),
40 [text, props.themeAppearance],
41 );
42 if (text.length === 0) {
43 const message = error
44 ? gt("Couldn't load output: {error}", { error })
45 : data === null
46 ? gt("Loading output…")
47 : data.running
48 ? null
49 : gt("No output.");
50 return message === null ? null : (
51 <Text className="pt-1.5 font-mono text-2xs leading-normal text-foreground-muted">
52 {message}
53 </Text>
54 );
55 }
56 return (
57 <View className="pt-1.5">
58 <ScrollView
59 ref={scrollRef}
60 nestedScrollEnabled
61 directionalLockEnabled
62 showsVerticalScrollIndicator
63 className="max-h-60"
64 contentContainerStyle={{ paddingRight: 8 }}
65 scrollEventThrottle={100}
66 onScroll={(event) => {
67 const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
68 followRef.current =
69 contentSize.height - contentOffset.y - layoutMeasurement.height <= FOLLOW_SLACK;
70 }}
71 onContentSizeChange={() => {
72 if (followRef.current) scrollRef.current?.scrollToEnd({ animated: false });
73 }}
74 >
75 {data?.output.truncated ? (
76 <Text className="pb-0.5 text-3xs text-foreground-subtle">
77 <T>Earlier output not shown</T>
78 </Text>
79 ) : null}
80 <Text selectable className="font-mono text-2xs leading-normal text-foreground-muted">
81 {spans.map((span, index) =>
82 span.style === null ? (
83 span.text
84 ) : (
85 // Spans are rebuilt from scratch on every update and never reorder.
86 // oxlint-disable-next-line react/no-array-index-key
87 <Text key={index} style={span.style}>
88 {span.text}
89 </Text>
90 ),
91 )}
92 </Text>
93 </ScrollView>
94 </View>
95 );
96 }