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

TerminalContextSheet.tsx

128 lines4.8 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 { 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​
19export function TerminalContextSheet(props: {
20 text: string;
21 environmentId: EnvironmentId;
22 threadId: ThreadId;
23 terminalId: string;
24 terminalLabel: string;
25 onClose: () => void;
26 onAttach: () => void;
27}) {
28 const insets = useSafeAreaInsets();
29 const lines = props.text.replace(/\n+$/, "").split("\n");
30 const [range, setRange] = useState({ start: 0, end: lines.length - 1 });
31 const [anchor, setAnchor] = useState<number | null>(null);
32 const selectedText = lines.slice(range.start, range.end + 1).join("\n");
33 const tooLarge = selectedText.length > COMPOSER_CONTEXT_TERMINAL_TEXT_MAX_CHARS;
34 const attach = () => {
35 if (!selectedText.trim() || tooLarge) return;
36 const record = {
37 version: 1 as const,
38 kind: "terminal" as const,
39 contextId: ComposerContextId.make(uuidv4()),
40 label: `${props.terminalLabel} · visible lines ${range.start + 1}–${range.end + 1}`,
41 terminalId: props.terminalId,
42 terminalLabel: `${props.terminalLabel} (visible output)`,
43 lineStart: range.start + 1,
44 lineEnd: range.end + 1,
45 text: selectedText,
46 };
47 if (
48 !insertComposerDraftContext(`${props.environmentId}:${props.threadId}`, {
49 text: formatComposerContextReference(record),
50 context: { version: 1, records: [record] },
51 })
52 ) {
53 Dialog.alert("Too many context items", "Remove some context from the draft and try again.");
54 return;
55 }
56 props.onAttach();
57 };
58 return (
59 <Sheet title="Terminal output" onClose={props.onClose}>
60 <ScrollView
61 nestedScrollEnabled
62 className="flex-1"
63 contentInsetAdjustmentBehavior="automatic"
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(
71 (line, index) => (
72 /* oxlint-disable react/no-array-index-key -- Selection is addressed by line number in this captured terminal snapshot. */ <Pressable
73 key={index}
74 accessibilityRole="button"
75 accessibilityLabel={`Line ${index + 1}: ${line}`}
76 accessibilityState={{ selected: index >= range.start && index <= range.end }}
77 onPress={() => {
78 if (anchor === null) {
79 setAnchor(index);
80 setRange({ start: index, end: index });
81 } else {
82 setRange({ start: Math.min(anchor, index), end: Math.max(anchor, index) });
83 setAnchor(null);
84 }
85 }}
86 className={
87 index >= range.start && index <= range.end
88 ? "flex-row gap-3 bg-primary/10 px-3 py-2"
89 : "flex-row gap-3 px-3 py-2"
90 }
91 >
92 <Text
93 className="w-8 text-right text-sm text-foreground-muted"
94 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
95 >
96 {index + 1}
97 </Text>
98 <Text
99 className="flex-1 text-sm text-foreground"
100 style={{ fontFamily: REVIEW_MONO_FONT_FAMILY }}
101 >
102 {line || " "}
103 </Text>
104 </Pressable>
105 ) /* oxlint-enable react/no-array-index-key */,
106 )}
107 </View>
108 </ScrollView>
109 <View
110 className="gap-3 border-t border-border px-5 pt-4"
111 style={{ paddingBottom: Math.max(16, insets.bottom) }}
112 >
113 {tooLarge ? (
114 <Text className="text-sm text-foreground-muted">
115 Select fewer lines to fit the context limit.
116 </Text>
117 ) : null}
118 <Button
119 label="Attach selected output"
120 tone="primary"
121 fullWidth
122 disabled={!selectedText.trim() || tooLarge}
123 onPress={attach}
124 />
125 </View>
126 </Sheet>
127 );