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

PendingUserInputCard.tsx

108 lines4.5 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 // The surface is opaque on purpose: the card floats over the thread feed
28 // with no blur behind it, so a translucent background renders the questions
29 // on top of whatever message happens to sit underneath.
30 return (
31 <View className="gap-2.5 rounded-[20px] border border-neutral-200 bg-neutral-100 p-4 dark:border-white/6 dark:bg-neutral-900">
32 <Text className="font-t3-bold text-2xs uppercase tracking-[1.1px] text-sky-700 dark:text-sky-300">
33 User input needed
34 </Text>
35 <Text className="font-t3-bold text-lg text-neutral-950 dark:text-neutral-50">
36 Fill in the pending answers
37 </Text>
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 className={cn(
56 "rounded-full border px-3 py-2.5 ",
57 selected
58 ? "border-blue-300/50 bg-blue-50 dark:border-blue-400/28 dark:bg-blue-400/14"
59 : "border-neutral-200 bg-white dark:border-white/6 dark:bg-neutral-950/70",
60 )}
61 onPress={() =>
62 props.onSelectOption(
63 props.pendingUserInput.requestId,
64 question.id,
65 option.label,
66 )
67 }
68 >
69 <Text
70 className={cn(
71 "font-t3-bold text-sm",
72 selected
73 ? "text-sky-700 dark:text-sky-300"
74 : "text-neutral-600 dark:text-neutral-300",
75 )}
76 >
77 {option.label}
78 </Text>
79 </Pressable>
80 );
81 })}
82 </View>
83 <TextInput
84 value={draft?.customAnswer ?? ""}
85 onChangeText={(value) =>
86 props.onChangeCustomAnswer(props.pendingUserInput.requestId, question.id, value)
87 }
88 placeholder="Or type a custom answer"
89 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"
90 />
91 </View>
92 );
93 })}
94 <Pressable
95 className={cn(
96 "items-center justify-center rounded-2xl px-4 py-3.5",
97 props.answers ? "bg-blue-500" : "bg-neutral-200 dark:bg-neutral-700/60",
98 )}
99 disabled={
100 props.answers === null || props.respondingUserInputId === props.pendingUserInput.requestId
101 }
102 onPress={() => void props.onSubmit()}
103 >
104 <Text className="font-t3-extrabold text-sm text-white">Submit answers</Text>
105 </Pressable>
106 </View>
107 );