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

FeedbackRequestCard.tsx

214 lines7.5 KB

1import { T, msg, useMessages, useGT } from "gt-react-native";
2import { pipe } from "effect/Function";
3import {
4 feedbackCardPreview,
5 feedbackConversationHint,
6 feedbackPreviewFields,
7 feedbackRequestDisplay,
8 feedbackRequestFailureMessage,
9 type FeedbackRequestItem,
10 feedbackTraceLines,
11 feedbackTraceSummary,
12} from "@t3tools/client-runtime/feedback-request";
13import {
14 isAtomCommandInterrupted,
15 squashAtomCommandFailure,
16} from "@t3tools/client-runtime/state/runtime";
17import type {
18 AgentFeedbackPreview,
19 EnvironmentId,
20 OrchestrationV2ProjectedTurnItem,
21} from "@t3tools/contracts";
22import { useAtomValue } from "@effect/atom-react";
23import { useRef, useState } from "react";
24import { Pressable, View, type ColorValue } from "react-native";
25​
26import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
27import { AppText as Text } from "../../components/ui/AppText";
28import { Button } from "../../components/ui/Button";
29import { ThemedSwitch } from "../../components/ui/ThemedSwitch";
30import { serverEnvironment } from "../../state/server";
31import { useAtomCommand } from "../../state/use-atom-command";
32​
33const TITLE = msg("Feedback for the T3 team");
34const LOCK_SYMBOL: AppSymbolName = { ios: "lock", android: "lock" };
35​
36export function FeedbackRequestCard(props: {
37 readonly environmentId: EnvironmentId;
38 readonly projectedItem: OrchestrationV2ProjectedTurnItem;
39 readonly iconColor: ColorValue;
40}) {
41 const m = useMessages();
42 const { item, visibility } = props.projectedItem;
43 const [open, setOpen] = useState(false);
44 if (item.type !== "feedback_request") return null;
45 const display = feedbackRequestDisplay(item, visibility, m);
46 if (display.kind === "pending") {
47 return <PendingFeedbackRequest environmentId={props.environmentId} item={item} />;
48 }
49 const icon: AppSymbolName =
50 display.kind === "pending-elsewhere"
51 ? LOCK_SYMBOL
52 : display.outcome === "sent"
53 ? "checkmark"
54 : "minus";
55 return (
56 <View className="mb-3 gap-2 px-1">
57 <Pressable
58 accessibilityRole="button"
59 accessibilityState={{ expanded: open }}
60 onPress={() => setOpen((value) => !value)}
61 className="min-h-9 flex-row items-center gap-2"
62 >
63 <SymbolView name={icon} size={13} tintColor={props.iconColor} type="monochrome" />
64 <Text className="flex-1 font-sans text-sm text-foreground-muted" numberOfLines={2}>
65 {m(TITLE)} · {display.label}
66 </Text>
67 </Pressable>
68 {open ? <FeedbackPreview preview={item.preview} /> : null}
69 </View>
70 );
72​
73function PendingFeedbackRequest(props: {
74 readonly environmentId: EnvironmentId;
75 readonly item: FeedbackRequestItem;
76}) {
77 const m = useMessages();
78 const gt = useGT();
79 const answer = useAtomCommand(serverEnvironment.answerFeedbackRequest, {
80 label: "answer feedback request",
81 reportFailure: false,
82 });
83 const canAnswer = useAtomValue(
84 serverEnvironment.answerFeedbackRequest.permissionAtom(props.environmentId),
85 );
86 const [submitting, setSubmitting] = useState(false);
87 const [error, setError] = useState<string | null>(null);
88 const [includeConversation, setIncludeConversation] = useState(false);
89 // Two taps can land before a re-render; this guard is synchronous.
90 const inFlight = useRef(false);
91​
92 const send = async (reply: "send" | "decline") => {
93 if (inFlight.current) return;
94 inFlight.current = true;
95 setSubmitting(true);
96 setError(null);
97 const result = await answer({
98 environmentId: props.environmentId,
99 input: {
100 threadId: props.item.threadId,
101 turnItemId: props.item.id,
102 answer: reply,
103 includeConversation,
104 },
105 }).finally(() => {
106 inFlight.current = false;
107 setSubmitting(false);
108 });
109 if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;
110 pipe(
111 squashAtomCommandFailure(result),
112 (failure) => feedbackRequestFailureMessage(failure, m),
113 setError,
114 );
115 };
116​
117 return (
118 <View className="mb-3 gap-3 rounded-[20px] border border-border bg-card-alt p-4">
119 <View className="gap-1">
120 <Text className="font-t3-bold text-base text-foreground">
121 <T>Send this report to the T3 team?</T>
122 </Text>
123 <Text className="font-sans text-xs text-foreground-muted">
124 <T>This is everything that leaves your machine. Nothing is sent unless you tap Send.</T>
125 </Text>
126 </View>
127 {props.item.conversationTrace !== undefined ? (
128 <View className="flex-row items-center gap-3">
129 <View className="flex-1 gap-0.5">
130 <Text className="font-sans text-sm text-foreground">
131 <T>Include the conversation</T>
132 </Text>
133 <Text className="font-sans text-xs text-foreground-muted">
134 {feedbackConversationHint(props.item.conversationTrace, m)}
135 </Text>
136 </View>
137 <ThemedSwitch
138 accessibilityLabel={gt("Include the conversation")}
139 disabled={submitting || !canAnswer}
140 onValueChange={setIncludeConversation}
141 value={includeConversation}
142 />
143 </View>
144 ) : null}
145 <FeedbackPreview preview={feedbackCardPreview(props.item, includeConversation)} />
146 {error !== null ? (
147 <Text accessibilityRole="alert" className="font-sans text-sm text-danger-foreground">
148 {error}
149 </Text>
150 ) : !canAnswer ? (
151 <Text className="font-sans text-xs text-foreground-muted">
152 <T>Only someone who can run agents on this machine can send or decline it.</T>
153 </Text>
154 ) : null}
155 <Button
156 label={gt("Send")}
157 tone="primary"
158 fullWidth
159 disabled={submitting || !canAnswer}
160 onPress={() => void send("send")}
161 />
162 <Button
163 label={gt("Don't send")}
164 tone="text"
165 disabled={submitting || !canAnswer}
166 onPress={() => void send("decline")}
167 />
168 </View>
169 );
171​
172function FeedbackPreview({ preview }: { readonly preview: AgentFeedbackPreview }) {
173 const m = useMessages();
174 const gt = useGT();
175​
176 const [outlineOpen, setOutlineOpen] = useState(false);
177 return (
178 <View className="gap-2">
179 {feedbackPreviewFields(preview, m).map((field) => (
180 <View key={field.label} className="gap-0.5">
181 <Text className="font-sans text-xs text-foreground-muted">{field.label}</Text>
182 <Text selectable className="font-sans text-sm text-foreground">
183 {field.value}
184 </Text>
185 </View>
186 ))}
187 {preview.trace !== null ? (
188 <View className="gap-1">
189 <Pressable
190 accessibilityRole="button"
191 accessibilityState={{ expanded: outlineOpen }}
192 onPress={() => setOutlineOpen((value) => !value)}
193 >
194 <Text className="font-sans text-xs text-foreground-muted">
195 {feedbackTraceSummary(preview.trace, m)} · {outlineOpen ? gt("Hide") : gt("Show")}
196 </Text>
197 </Pressable>
198 {outlineOpen
199 ? feedbackTraceLines(preview.trace, m).map((entry) => (
200 <View key={entry.key} className="gap-0.5">
201 <Text className="font-mono text-xs text-foreground-muted">{entry.line}</Text>
202 {entry.text !== undefined ? (
203 <Text selectable className="font-mono text-xs text-foreground">
204 {entry.text}
205 </Text>
206 ) : null}
207 </View>
208 ))
209 : null}
210 </View>
211 ) : null}
212 </View>
213 );