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

PendingUserInputCard.tsx

112 lines4.7 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 const canRespond = props.pendingUserInput.responseCapability === "live";
24 return (
25 <View className="gap-2.5 rounded-[20px] border border-neutral-200 bg-neutral-100/80 p-4 dark:border-white/6 dark:bg-neutral-900/80">
26 <Text className="font-t3-bold text-2xs uppercase tracking-[1.1px] text-sky-700 dark:text-sky-300">
27 User input needed
28 </Text>
29 <Text className="font-t3-bold text-lg text-neutral-950 dark:text-neutral-50">
30 Fill in the pending answers
31 </Text>
32 {!canRespond ? (
33 <Text className="font-sans text-sm leading-5 text-neutral-600 dark:text-neutral-400">
34 The provider process for this request is no longer available. Interrupt or restart the run
35 to continue.
36 </Text>
37 ) : null}
38 {props.pendingUserInput.questions.map((question) => {
39 const draft = props.drafts[question.id];
40 return (
41 <View key={question.id} className="gap-2 pt-1">
42 <Text className="font-t3-bold text-xs uppercase tracking-[1px] text-neutral-500 dark:text-neutral-500">
43 {question.header}
44 </Text>
45 <Text className="font-sans text-base leading-snug text-neutral-950 dark:text-neutral-50">
46 {question.question}
47 </Text>
48 <View className="flex-row flex-wrap gap-2.5">
49 {question.options.map((option) => {
50 const selected =
51 draft?.selectedOptionLabel === option.label && !draft.customAnswer?.trim().length;
52 return (
53 <Pressable
54 key={option.label}
55 disabled={!canRespond}
56 className={cn(
57 "rounded-full border px-3 py-2.5 ",
58 selected
59 ? "border-blue-300/50 bg-blue-50 dark:border-blue-400/28 dark:bg-blue-400/14"
60 : "border-neutral-200 bg-white dark:border-white/6 dark:bg-neutral-950/70",
61 )}
62 onPress={() =>
63 props.onSelectOption(
64 props.pendingUserInput.requestId,
65 question.id,
66 option.label,
67 )
68 }
69 >
70 <Text
71 className={cn(
72 "font-t3-bold text-sm",
73 selected
74 ? "text-sky-700 dark:text-sky-300"
75 : "text-neutral-600 dark:text-neutral-300",
76 )}
77 >
78 {option.label}
79 </Text>
80 </Pressable>
81 );
82 })}
83 </View>
84 <TextInput
85 editable={canRespond}
86 value={draft?.customAnswer ?? ""}
87 onChangeText={(value) =>
88 props.onChangeCustomAnswer(props.pendingUserInput.requestId, question.id, value)
89 }
90 placeholder="Or type a custom answer"
91 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"
92 />
93 </View>
94 );
95 })}
96 <Pressable
97 className={cn(
98 "items-center justify-center rounded-2xl px-4 py-3.5",
99 props.answers ? "bg-blue-500" : "bg-neutral-200 dark:bg-neutral-700/60",
100 )}
101 disabled={
102 !canRespond ||
103 props.answers === null ||
104 props.respondingUserInputId === props.pendingUserInput.requestId
105 }
106 onPress={() => void props.onSubmit()}
107 >
108 <Text className="font-t3-extrabold text-sm text-white">Submit answers</Text>
109 </Pressable>
110 </View>
111 );