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

TerminalContextSheet.tsx

118 lines4.5 KB

1import {
2 ComposerContextId,
3 COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS,
4 type EnvironmentId,
5 type ThreadId,
6} from "@t3tools/contracts";
7import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
8import { useState } from "react";
9import { Alert, Modal, Platform, Pressable, ScrollView, View } from "react-native";
10import { useSafeAreaInsets } from "react-native-safe-area-context";
11import { REVIEW_MONO_FONT_FAMILY } from "../review/reviewDiffRendering";
12import { AppText as Text } from "../../components/AppText";
13import { uuidv4 } from "../../lib/uuid";
14import { insertComposerDraftContext } from "../../state/use-composer-drafts";
15​
16/** Line numbers are relative to this frozen viewport, not the terminal's scrollback. */
17export function TerminalContextSheet(props: {
18 text: string;
19 environmentId: EnvironmentId;
20 threadId: ThreadId;
21 terminalId: string;
22 terminalLabel: string;
23 onClose: () => void;
24 onAttach: () => void;
25}) {
26 const insets = useSafeAreaInsets();
27 const lines = props.text.replace(/\n+$/, "").split("\n");
28 const [range, setRange] = useState({ start: 0, end: lines.length - 1 });
29 const [anchor, setAnchor] = useState<number | null>(null);
30 const selectedText = lines.slice(range.start, range.end + 1).join("\n");
31 const tooLarge = selectedText.length > COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS;
32 const attach = () => {
33 if (!selectedText.trim() || tooLarge) return;
34 const record = {
35 version: 1 as const,
36 kind: "terminal" as const,
37 contextId: ComposerContextId.make(uuidv4()),
38 label: `${props.terminalLabel} · visible lines ${range.start + 1}–${range.end + 1}`,
39 terminalId: props.terminalId,
40 terminalLabel: `${props.terminalLabel} (visible output)`,
41 lineStart: range.start + 1,
42 lineEnd: range.end + 1,
43 text: selectedText,
44 };
45 if (
46 !insertComposerDraftContext(`${props.environmentId}:${props.threadId}`, {
47 text: formatComposerContextReference(record),
48 context: { version: 1, records: [record] },
49 })
50 ) {
51 Alert.alert("Too many context items", "Remove some context from the draft and try again.");
52 return;
53 }
54 props.onAttach();
55 };
56 return (
57 <Modal presentationStyle="pageSheet" animationType="slide" onRequestClose={props.onClose}>
58 <View
59 className="flex-1 bg-sheet-solid"
60 style={
61 Platform.OS === "android"
62 ? { paddingTop: insets.top, paddingBottom: insets.bottom }
63 : undefined
64 }
65 >
66 <View className="flex-row items-center justify-between p-4">
67 <Text className="text-lg text-foreground">Visible terminal output</Text>
68 <Pressable accessibilityRole="button" onPress={props.onClose} className="p-3">
69 <Text className="text-foreground">Cancel</Text>
70 </Pressable>
71 </View>
72 <Text className="px-4 pb-3 text-foreground-muted">
73 Tap the first and last line to select a range.
74 </Text>
75 <ScrollView className="flex-1" contentContainerStyle={{ padding: 16 }}>
76 {lines.map((line, index) => (
77 <Pressable
78 key={index}
79 accessibilityRole="button"
80 accessibilityLabel={`Line ${index + 1}: ${line}`}
81 accessibilityState={{ selected: index >= range.start && index <= range.end }}
82 onPress={() => {
83 if (anchor === null) {
84 setAnchor(index);
85 setRange({ start: index, end: index });
86 } else {
87 setRange({ start: Math.min(anchor, index), end: Math.max(anchor, index) });
88 setAnchor(null);
89 }
90 }}
91 className={index >= range.start && index <= range.end ? "bg-subtle py-1" : "py-1"}
92 >
93 <Text
94 className="text-sm text-foreground"
95 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
96 >
97 {index + 1} {line || " "}
98 </Text>
99 </Pressable>
100 ))}
101 </ScrollView>
102 {tooLarge ? (
103 <Text className="px-4 text-foreground-muted">
104 Select fewer lines to fit the context limit.
105 </Text>
106 ) : null}
107 <Pressable
108 accessibilityRole="button"
109 disabled={!selectedText.trim() || tooLarge}
110 onPress={attach}
111 className="m-4 mb-10 rounded-xl bg-subtle p-4"
112 >
113 <Text className="text-center text-foreground">Attach selected output</Text>
114 </Pressable>
115 </View>
116 </Modal>
117 );