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

PendingUserInputCard.tsx

105 lines4.3 KB

1import type { ApprovalRequestId } 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: ApprovalRequestId | null;
13 readonly onSelectOption: (
14 requestId: ApprovalRequestId,
15 questionId: string,
16 label: string,
17 ) => void;
18 readonly onChangeCustomAnswer: (
19 requestId: ApprovalRequestId,
20 questionId: string,
21 customAnswer: string,
22 ) => void;
23 readonly onSubmit: () => Promise<unknown>;
25​
26export function PendingUserInputCard(props: PendingUserInputCardProps) {
27 return (
28 <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">
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 {props.pendingUserInput.questions.map((question) => {
36 const draft = props.drafts[question.id];
37 return (
38 <View key={question.id} className="gap-2 pt-1">
39 <Text className="font-t3-bold text-xs uppercase tracking-[1px] text-neutral-500 dark:text-neutral-500">
40 {question.header}
41 </Text>
42 <Text className="font-sans text-base leading-snug text-neutral-950 dark:text-neutral-50">
43 {question.question}
44 </Text>
45 <View className="flex-row flex-wrap gap-2.5">
46 {question.options.map((option) => {
47 const selected =
48 draft?.selectedOptionLabel === option.label && !draft.customAnswer?.trim().length;
49 return (
50 <Pressable
51 key={option.label}
52 className={cn(
53 "rounded-full border px-3 py-2.5 ",
54 selected
55 ? "border-blue-300/50 bg-blue-50 dark:border-blue-400/28 dark:bg-blue-400/14"
56 : "border-neutral-200 bg-white dark:border-white/6 dark:bg-neutral-950/70",
57 )}
58 onPress={() =>
59 props.onSelectOption(
60 props.pendingUserInput.requestId,
61 question.id,
62 option.label,
63 )
64 }
65 >
66 <Text
67 className={cn(
68 "font-t3-bold text-sm",
69 selected
70 ? "text-sky-700 dark:text-sky-300"
71 : "text-neutral-600 dark:text-neutral-300",
72 )}
73 >
74 {option.label}
75 </Text>
76 </Pressable>
77 );
78 })}
79 </View>
80 <TextInput
81 value={draft?.customAnswer ?? ""}
82 onChangeText={(value) =>
83 props.onChangeCustomAnswer(props.pendingUserInput.requestId, question.id, value)
84 }
85 placeholder="Or type a custom answer"
86 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"
87 />
88 </View>
89 );
90 })}
91 <Pressable
92 className={cn(
93 "items-center justify-center rounded-2xl px-4 py-3.5",
94 props.answers ? "bg-blue-500" : "bg-neutral-200 dark:bg-neutral-700/60",
95 )}
96 disabled={
97 props.answers === null || props.respondingUserInputId === props.pendingUserInput.requestId
98 }
99 onPress={() => void props.onSubmit()}
100 >
101 <Text className="font-t3-extrabold text-sm text-white">Submit answers</Text>
102 </Pressable>
103 </View>
104 );