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

TerminalContextSheet.tsx

126 lines4.7 KB

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