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

PendingUserInputCard.tsx

115 lines4.9 KB

1import type { RuntimeRequestId } from "@t3tools/contracts";
2import { Pressable, View } from "react-native";
3​
4import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
5import { cn } from "../../lib/cn";
6import type { PendingUserInput, PendingUserInputDraftAnswer } from "../../lib/threadActivity";
7​
8export interface PendingUserInputCardProps {
9 readonly pendingUserInput: PendingUserInput;
10 readonly drafts: Record<string, PendingUserInputDraftAnswer>;
11 readonly answers: Record<string, string> | null;
12 readonly respondingUserInputId: RuntimeRequestId | null;
13 readonly onSelectOption: (requestId: RuntimeRequestId, questionId: string, label: string) => void;
14 readonly onChangeCustomAnswer: (
15 requestId: RuntimeRequestId,
16 questionId: string,
17 customAnswer: string,
18 ) => void;
19 readonly onSubmit: () => Promise<unknown>;
21​
22export function PendingUserInputCard(props: PendingUserInputCardProps) {
23 // The surface is opaque on purpose: the card floats over the thread feed
24 // with no blur behind it, so a translucent background renders the questions
25 // on top of whatever message happens to sit underneath.
26 const canRespond = props.pendingUserInput.responseCapability === "live";
27 return (
28 <View className="gap-2.5 rounded-[20px] border border-neutral-200 bg-neutral-100 p-4 dark:border-white/6 dark:bg-neutral-900">
29 <Text className="font-t3-bold text-2xs uppercase tracking-[1.1px] text-sky-700 dark:text-sky-300">
30 User input needed
31 </Text>
32 <Text className="font-t3-bold text-lg text-neutral-950 dark:text-neutral-50">
33 Fill in the pending answers
34 </Text>
35 {!canRespond ? (
36 <Text className="font-sans text-sm leading-5 text-neutral-600 dark:text-neutral-400">
37 The provider process for this request is no longer available. Interrupt or restart the run
38 to continue.
39 </Text>
40 ) : null}
41 {props.pendingUserInput.questions.map((question) => {
42 const draft = props.drafts[question.id];
43 return (
44 <View key={question.id} className="gap-2 pt-1">
45 <Text className="font-t3-bold text-xs uppercase tracking-[1px] text-neutral-500 dark:text-neutral-500">
46 {question.header}
47 </Text>
48 <Text className="font-sans text-base leading-snug text-neutral-950 dark:text-neutral-50">
49 {question.question}
50 </Text>
51 <View className="flex-row flex-wrap gap-2.5">
52 {question.options.map((option) => {
53 const selected =
54 draft?.selectedOptionLabel === option.label && !draft.customAnswer?.trim().length;
55 return (
56 <Pressable
57 key={option.label}
58 disabled={!canRespond}
59 className={cn(
60 "rounded-full border px-3 py-2.5 ",
61 selected
62 ? "border-blue-300/50 bg-blue-50 dark:border-blue-400/28 dark:bg-blue-400/14"
63 : "border-neutral-200 bg-white dark:border-white/6 dark:bg-neutral-950/70",
64 )}
65 onPress={() =>
66 props.onSelectOption(
67 props.pendingUserInput.requestId,
68 question.id,
69 option.label,
70 )
71 }
72 >
73 <Text
74 className={cn(
75 "font-t3-bold text-sm",
76 selected
77 ? "text-sky-700 dark:text-sky-300"
78 : "text-neutral-600 dark:text-neutral-300",
79 )}
80 >
81 {option.label}
82 </Text>
83 </Pressable>
84 );
85 })}
86 </View>
87 <TextInput
88 editable={canRespond}
89 value={draft?.customAnswer ?? ""}
90 onChangeText={(value) =>
91 props.onChangeCustomAnswer(props.pendingUserInput.requestId, question.id, value)
92 }
93 placeholder="Or type a custom answer"
94 className="min-h-[54px] rounded-2xl border border-neutral-200 bg-white px-3.5 py-3 font-sans text-base text-neutral-950 dark:border-white/8 dark:bg-neutral-950/70 dark:text-neutral-50"
95 />
96 </View>
97 );
98 })}
99 <Pressable
100 className={cn(
101 "items-center justify-center rounded-2xl px-4 py-3.5",
102 props.answers ? "bg-blue-500" : "bg-neutral-200 dark:bg-neutral-700/60",
103 )}
104 disabled={
105 !canRespond ||
106 props.answers === null ||
107 props.respondingUserInputId === props.pendingUserInput.requestId
108 }
109 onPress={() => void props.onSubmit()}
110 >
111 <Text className="font-t3-extrabold text-sm text-white">Submit answers</Text>
112 </Pressable>
113 </View>
114 );