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

main
tsx 136 lines 5.27 KB
Raw
1 import * as Dialog from "../../components/ui/Dialog";
2 import {
3 ComposerContextId,
4 COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
5 type EnvironmentId,
6 type ThreadId,
7 } from "@t3tools/contracts";
8 import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
9 import { msg, T, useGT } from "gt-react-native";
10 import { useState } from "react";
11 import { Pressable, ScrollView, View } from "react-native";
12 import { useSafeAreaInsets } from "react-native-safe-area-context";
13 import { REVIEW_MONO_FONT_FAMILY } from "../review/reviewDiffRendering";
14 import { AppText as Text } from "../../components/ui/AppText";
15 import { Sheet } from "../../components/ui/Sheet";
16 import { Button } from "../../components/ui/Button";
17 import { uuidv4 } from "../../lib/uuid";
18 import { m } from "../../i18n/messages";
19 import { insertComposerDraftContext } from "../../state/use-composer-drafts";
20
21 export function TerminalContextSheet(props: {
22 text: string;
23 environmentId: EnvironmentId;
24 threadId: ThreadId;
25 terminalId: string;
26 terminalLabel: string;
27 onClose: () => void;
28 onAttach: () => void;
29 }) {
30 const insets = useSafeAreaInsets();
31 const gt = useGT();
32 const lines = props.text.replace(/\n+$/, "").split("\n");
33 const [range, setRange] = useState({ start: 0, end: lines.length - 1 });
34 const [anchor, setAnchor] = useState<number | null>(null);
35 const selectedText = lines.slice(range.start, range.end + 1).join("\n");
36 const tooLarge = selectedText.length > COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS;
37 const attach = () => {
38 if (!selectedText.trim() || tooLarge) return;
39 const record = {
40 version: 1 as const,
41 kind: "terminal" as const,
42 contextId: ComposerContextId.make(uuidv4()),
43 // oxlint-disable-next-line t3code/no-untranslated-copy -- context label sent to the agent with the terminal text
44 label: `${props.terminalLabel} · visible lines ${range.start + 1}–${range.end + 1}`,
45 terminalId: props.terminalId,
46 // oxlint-disable-next-line t3code/no-untranslated-copy -- context label sent to the agent with the terminal text
47 terminalLabel: `${props.terminalLabel} (visible output)`,
48 lineStart: range.start + 1,
49 lineEnd: range.end + 1,
50 text: selectedText,
51 };
52 if (
53 !insertComposerDraftContext(`${props.environmentId}:${props.threadId}`, {
54 text: formatComposerContextReference(record),
55 context: { version: 1, records: [record] },
56 })
57 ) {
58 Dialog.alert(
59 m(msg("Too many context items")),
60 m(msg("Remove some context from the draft and try again.")),
61 );
62 return;
63 }
64 props.onAttach();
65 };
66 return (
67 <Sheet title={gt("Terminal output")} onClose={props.onClose}>
68 <ScrollView
69 nestedScrollEnabled
70 className="flex-1"
71 contentInsetAdjustmentBehavior="automatic"
72 contentContainerStyle={{ paddingHorizontal: 20, paddingTop: 12, paddingBottom: 20 }}
73 >
74 <Text className="pb-4 text-sm text-foreground-muted">
75 <T>Tap the first and last line to select a range.</T>
76 </Text>
77 <View className="overflow-hidden rounded-xl bg-card-alt">
78 {lines.map(
79 (line, index) => (
80 /* oxlint-disable react/no-array-index-key -- Selection is addressed by line number in this captured terminal snapshot. */ <Pressable
81 key={index}
82 accessibilityRole="button"
83 accessibilityLabel={gt("Line {number}: {line}", { number: index + 1, line })}
84 accessibilityState={{ selected: index >= range.start && index <= range.end }}
85 onPress={() => {
86 if (anchor === null) {
87 setAnchor(index);
88 setRange({ start: index, end: index });
89 } else {
90 setRange({ start: Math.min(anchor, index), end: Math.max(anchor, index) });
91 setAnchor(null);
92 }
93 }}
94 className={
95 index >= range.start && index <= range.end
96 ? "flex-row gap-3 bg-primary/10 px-3 py-2"
97 : "flex-row gap-3 px-3 py-2"
98 }
99 >
100 <Text
101 className="w-8 text-right text-sm text-foreground-muted"
102 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
103 >
104 {index + 1}
105 </Text>
106 <Text
107 className="flex-1 text-sm text-foreground"
108 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
109 >
110 {line || " "}
111 </Text>
112 </Pressable>
113 ) /* oxlint-enable react/no-array-index-key */,
114 )}
115 </View>
116 </ScrollView>
117 <View
118 className="gap-3 border-t border-border px-5 pt-4"
119 style={{ paddingBottom: Math.max(16, insets.bottom) }}
120 >
121 {tooLarge ? (
122 <Text className="text-sm text-foreground-muted">
123 <T>Select fewer lines to fit the context limit.</T>
124 </Text>
125 ) : null}
126 <Button
127 label={gt("Attach selected output")}
128 tone="primary"
129 fullWidth
130 disabled={!selectedText.trim() || tooLarge}
131 onPress={attach}
132 />
133 </View>
134 </Sheet>
135 );
136 }