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

PendingUserInputCard.tsx

325 lines12.4 KB

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