Open History to see the last commit
1import { Button } from "../../components/ui/Button";
2import { QuestionAttachments } from "./QuestionAttachments";
3import type { EnvironmentId, RuntimeRequestId } from "@t3tools/contracts";
4import type { ThreadUserInputQuestion } from "@t3tools/client-runtime/state/thread-requests";
5import { useRef } from "react";
6import { Platform, Pressable, ScrollView, View, type LayoutChangeEvent } from "react-native";
7import Animated, {
8 Easing,
9 FadeInUp,
10 FadeOutDown,
11 useAnimatedStyle,
12 useSharedValue,
13 type DerivedValue,
14 type SharedValue,
15} from "react-native-reanimated";
16
17import { USER_INPUT_TOGGLE_DURATION_MS } from "./pendingUserInputLayout";
18
19import { SymbolView } from "../../components/ui/AppSymbol";
20import { AppText as Text } from "../../components/ui/AppText";
21import { ControlPill } from "../../components/ui/ControlPill";
22import { cn } from "../../lib/cn";
23import { SelectableMarkdownText } from "../../native/SelectableMarkdownText";
24import { useOpenPullRequest } from "../../native/useOpenPullRequest";
25import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview";
26import { useLiveThreadLinkLabels } from "../../state/entities";
27import {
28 isPendingUserInputOptionSelected,
29 type PendingUserInputDraftAnswer,
30} from "@t3tools/client-runtime/state/pending-user-input";
31import type { PendingUserInput } from "../../lib/threadActivity";
32
33export interface PendingUserInputCardProps {
34 readonly environmentId: EnvironmentId;
35 readonly canOperateThread: boolean;
36 readonly pendingUserInput: PendingUserInput;
37 readonly maxHeight: DerivedValue<number>;
38 readonly collapsed: boolean;
39 readonly onToggleCollapsed: () => void;
40 readonly onStopThread?: () => void;
41 readonly cardProgress?: SharedValue<number>;
42 readonly cardCoverage?: SharedValue<number>;
43 readonly onInputFocusChange?: (focused: boolean) => void;
44 readonly drafts: Record<string, PendingUserInputDraftAnswer>;
45 readonly answers: Record<string, string | ReadonlyArray<string>> | null;
46 readonly respondingUserInputId: RuntimeRequestId | null;
47 readonly onSelectOption: (
48 requestId: RuntimeRequestId,
49 question: ThreadUserInputQuestion,
50 value: string,
51 ) => void;
52 readonly onChangeCustomAnswer: (
53 requestId: RuntimeRequestId,
54 questionId: string,
55 customAnswer: string,
56 ) => void;
57 readonly onSubmit: () => Promise<unknown>;
58 readonly onDismiss: () => Promise<unknown>;
59}
60
61/** Android cannot hit-test overlays beyond their parent's bounds; render its expanded card in-flow. */
62const EXPANDED_CARD_IS_OVERLAY = Platform.OS === "ios";
63
64function QuestionText(props: { readonly markdown: string; readonly environmentId: EnvironmentId }) {
65 const styles = useMarkdownPreviewStyles();
66 const markdown = useLiveThreadLinkLabels(props.markdown, props.environmentId);
67 const openLink = useOpenPullRequest();
68 return (
69 <SelectableMarkdownText
70 markdown={markdown}
71 textStyle={styles.nativeTextStyle}
72 onLinkPress={(href) => void openLink(href)}
73 />
74 );
75}
76
77export function PendingUserInputCard(props: PendingUserInputCardProps) {
78 const questionCount = props.pendingUserInput.questions.length;
79 const canRespond = props.pendingUserInput.responseCapability !== "not_resumable";
80 const isResponding = props.respondingUserInputId === props.pendingUserInput.requestId;
81 const responseDisabled = !canRespond || isResponding;
82
83 const cardCoverage = props.cardCoverage;
84 const barHeightRef = useRef(0);
85 const cardHeightRef = useRef(0);
86 const cardHeight = useSharedValue(0);
87 const notifyCoverage = () => {
88 if (!cardCoverage) {
89 return;
90 }
91 // The card resizes with the keyboard every frame, so easing each sample would leave the
92 // feed inset and floating controls trailing it.
93 cardCoverage.set(Math.max(0, cardHeightRef.current - barHeightRef.current));
94 };
95 const handleBarLayout = (event: LayoutChangeEvent) => {
96 barHeightRef.current = event.nativeEvent.layout.height;
97 notifyCoverage();
98 };
99 const handleCardLayout = (event: LayoutChangeEvent) => {
100 cardHeightRef.current = event.nativeEvent.layout.height;
101 cardHeight.set(event.nativeEvent.layout.height);
102 notifyCoverage();
103 };
104 const cardProgress = props.cardProgress;
105 const maxHeight = props.maxHeight;
106 const cardAnimatedStyle = useAnimatedStyle(() => {
107 const progress = cardProgress === undefined ? 1 : cardProgress.value;
108 return {
109 maxHeight: maxHeight.value,
110 transform: [{ translateY: (1 - progress) * cardHeight.value }],
111 };
112 });
113 const cardMaxHeightStyle = useAnimatedStyle(() => ({ maxHeight: maxHeight.value }));
114 const overlayHeightStyle = useAnimatedStyle(() => ({ height: maxHeight.value }));
115
116 // Keep the iOS card mounted so expansion starts on the UI thread without waiting for React layout.
117 const renderCard = EXPANDED_CARD_IS_OVERLAY || !props.collapsed;
118 const showBar = props.collapsed || EXPANDED_CARD_IS_OVERLAY;
119 const bar = showBar ? (
120 <View
121 onLayout={handleBarLayout}
122 pointerEvents={props.collapsed ? "auto" : "none"}
123 accessibilityElementsHidden={!props.collapsed}
124 importantForAccessibility={props.collapsed ? "auto" : "no-hide-descendants"}
125 className="flex-row items-center gap-2 rounded-full border border-border bg-card-alt py-1.5 pl-4 pr-1.5"
126 >
127 <Pressable
128 accessibilityRole="button"
129 accessibilityLabel={`Expand user input, ${questionCount} question${
130 questionCount === 1 ? "" : "s"
131 }`}
132 onPress={props.onToggleCollapsed}
133 className="min-h-10 flex-1 flex-row items-center gap-2 active:opacity-70"
134 >
135 <Text className="font-t3-bold text-2xs uppercase tracking-[1.1px] text-foreground-secondary">
136 User input needed
137 </Text>
138 <Text className="font-sans text-xs text-foreground-muted">
139 {questionCount} question{questionCount === 1 ? "" : "s"}
140 </Text>
141 <View className="flex-1" />
142 <SymbolView
143 name="chevron.up"
144 size={12}
145 tintColorClassName={"accent-icon-subtle"}
146 type="monochrome"
147 />
148 </Pressable>
149 {props.onStopThread ? (
150 <ControlPill
151 accessibilityLabel="Stop"
152 icon="stop.fill"
153 variant="danger"
154 className="h-9 w-9"
155 disabled={!props.canOperateThread}
156 onPress={props.onStopThread}
157 />
158 ) : null}
159 </View>
160 ) : null;
161 const card = renderCard ? (
162 <Animated.View
163 onLayout={handleCardLayout}
164 pointerEvents={props.collapsed ? "none" : "auto"}
165 accessibilityElementsHidden={props.collapsed}
166 importantForAccessibility={props.collapsed ? "no-hide-descendants" : "auto"}
167 entering={
168 EXPANDED_CARD_IS_OVERLAY
169 ? undefined
170 : FadeInUp.duration(USER_INPUT_TOGGLE_DURATION_MS).easing(Easing.out(Easing.cubic))
171 }
172 exiting={
173 EXPANDED_CARD_IS_OVERLAY
174 ? undefined
175 : FadeOutDown.duration(USER_INPUT_TOGGLE_DURATION_MS).easing(Easing.out(Easing.cubic))
176 }
177 className="overflow-hidden gap-2.5 rounded-[20px] border border-border bg-card-alt p-4"
178 style={EXPANDED_CARD_IS_OVERLAY ? cardAnimatedStyle : cardMaxHeightStyle}
179 >
180 <Pressable
181 accessibilityRole="button"
182 accessibilityLabel="Collapse user input"
183 onPress={props.onToggleCollapsed}
184 className="flex-row items-start gap-2"
185 >
186 <View className="flex-1 gap-2.5">
187 <Text className="font-t3-bold text-2xs uppercase tracking-[1.1px] text-foreground-secondary">
188 User input needed
189 </Text>
190 <Text className="font-t3-bold text-lg text-foreground">Fill in the pending answers</Text>
191 </View>
192 <View className="h-8 w-8 items-center justify-center rounded-full bg-subtle-strong">
193 <SymbolView
194 name="chevron.down"
195 size={13}
196 tintColorClassName={"accent-icon-subtle"}
197 type="monochrome"
198 />
199 </View>
200 </Pressable>
201 <ScrollView
202 bounces={false}
203 className="min-h-0"
204 contentContainerClassName="gap-2.5 pb-1"
205 keyboardShouldPersistTaps="handled"
206 nestedScrollEnabled
207 showsVerticalScrollIndicator
208 style={{ flexShrink: 1 }}
209 >
210 {!canRespond ? (
211 <Text className="font-sans text-sm leading-5 text-foreground-muted">
212 The provider process for this request is no longer available. Interrupt or restart the
213 run to continue.
214 </Text>
215 ) : null}
216 {props.pendingUserInput.questions.map((question) => {
217 const draft = props.drafts[question.id];
218 return (
219 <View key={question.id} className="gap-2 pt-1">
220 <Text className="font-t3-bold text-xs uppercase tracking-[1px] text-foreground-muted">
221 {question.header}
222 </Text>
223 <QuestionText markdown={question.question} environmentId={props.environmentId} />
224 <View className="gap-2">
225 {question.options.map((option) => {
226 const optionValue = option.value ?? option.label.trim();
227 const selected = isPendingUserInputOptionSelected(question, draft, optionValue);
228 const description =
229 option.description !== option.label ? option.description : undefined;
230 return (
231 <Pressable
232 key={optionValue}
233 accessibilityRole={question.multiSelect ? "checkbox" : "radio"}
234 accessibilityState={{ checked: selected, disabled: responseDisabled }}
235 disabled={responseDisabled}
236 className={cn(
237 "min-h-12 w-full rounded-2xl border px-3.5 py-3",
238 selected ? "border-primary bg-primary/10" : "border-border bg-input",
239 )}
240 onPress={() =>
241 props.onSelectOption(
242 props.pendingUserInput.requestId,
243 question,
244 optionValue,
245 )
246 }
247 >
248 <View className="min-w-0 flex-1 gap-0.5">
249 <Text
250 className={cn(
251 "font-t3-bold text-sm",
252 selected ? "text-foreground" : "text-foreground-secondary",
253 )}
254 >
255 {option.label}
256 </Text>
257 {description ? (
258 <Text className="font-sans text-sm leading-5 text-foreground-muted">
259 {description}
260 </Text>
261 ) : null}
262 </View>
263 </Pressable>
264 );
265 })}
266 </View>
267 {question.allowCustomAnswer !== false ? (
268 <QuestionAttachments
269 key={`${props.pendingUserInput.requestId}:${question.id}`}
270 requestId={props.pendingUserInput.requestId}
271 question={question}
272 questions={props.pendingUserInput.questions}
273 disabled={responseDisabled}
274 value={draft?.customAnswer ?? ""}
275 onChangeText={(value) =>
276 props.onChangeCustomAnswer(props.pendingUserInput.requestId, question.id, value)
277 }
278 onInputFocusChange={props.onInputFocusChange}
279 />
280 ) : null}
281 </View>
282 );
283 })}
284 </ScrollView>
285 <Button
286 label="Submit answers"
287 fullWidth
288 tone={props.answers ? "primary" : "secondary"}
289 disabled={!props.canOperateThread || responseDisabled || props.answers === null}
290 onPress={() => void props.onSubmit()}
291 />
292 {props.pendingUserInput.dismissible ? (
293 <Button
294 label="Dismiss without answering"
295 tone="text"
296 disabled={isResponding}
297 onPress={() => void props.onDismiss()}
298 />
299 ) : null}
300 {!props.canOperateThread ? (
301 <Text className="font-sans text-xs text-foreground-tertiary">
302 This connection cannot submit answers.
303 </Text>
304 ) : null}
305 </Animated.View>
306 ) : null;
307 return (
308 <View className="relative">
309 {bar}
310 {EXPANDED_CARD_IS_OVERLAY ? (
311 <Animated.View
312 pointerEvents={props.collapsed ? "none" : "box-none"}
313 className="absolute inset-x-0 bottom-0 justify-end overflow-hidden"
314 style={overlayHeightStyle}
315 >
316 {card}
317 </Animated.View>
318 ) : (
319 card
320 )}
321 </View>
322 );
323}