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

pendingUserInput.ts

179 lines5.9 KB

1/** The question fields answer handling reads. Web and mobile question types both fit it. */
2export interface PendingUserInputQuestion {
3 readonly id: string;
4 readonly options: ReadonlyArray<{ readonly label: string; readonly value?: string | undefined }>;
5 readonly multiSelect?: boolean | undefined;
6 readonly allowCustomAnswer?: boolean | undefined;
7 readonly initialAnswer?: string | undefined;
8 readonly required?: boolean | undefined;
9}
10​
11export interface PendingUserInputDraftAnswer {
12 readonly selectedOptionValues?: ReadonlyArray<string>;
13 readonly customAnswer?: string;
14 readonly attachmentCount?: number;
15 readonly attachmentsBlocked?: boolean;
17​
18export type PendingUserInputAnswer = string | ReadonlyArray<string>;
19​
20function normalizeDraftAnswer(value: string | undefined): string | null {
21 if (typeof value !== "string") return null;
22 const trimmed = value.trim();
23 return trimmed.length > 0 ? trimmed : null;
25​
26// Provider option values stay exact, including whitespace. Only options without a value match by label.
27function resolvePendingUserInputOptionValue(
28 question: PendingUserInputQuestion,
29 value: string,
30): string | null {
31 if (question.options.some((option) => option.value === value)) {
32 return value;
33 }
34​
35 const label = value.trim();
36 return label.length > 0 &&
37 question.options.some((option) => option.value === undefined && option.label.trim() === label)
38 ? label
39 : null;
41​
42/** The draft's selections that name an offered option, deduplicated, in selection order. */
43export function normalizeSelectedOptionValues(
44 question: PendingUserInputQuestion,
45 value: ReadonlyArray<string> | undefined,
46): ReadonlyArray<string> {
47 if (!Array.isArray(value)) {
48 return [];
49 }
50​
51 return Array.from(
52 new Set(
53 value
54 .map((entry) => resolvePendingUserInputOptionValue(question, entry))
55 .filter((entry): entry is string => entry !== null),
56 ),
57 );
59​
60/** The answer to submit for one question, or null while it is unanswered. */
61export function resolvePendingUserInputAnswer(
62 question: PendingUserInputQuestion,
63 draft: PendingUserInputDraftAnswer | undefined,
64): PendingUserInputAnswer | null {
65 if (draft?.attachmentsBlocked) return null;
66 const customAnswer =
67 question.allowCustomAnswer === false
68 ? null
69 : question.initialAnswer !== undefined
70 ? (draft?.customAnswer ?? null)
71 : normalizeDraftAnswer(draft?.customAnswer);
72 if (customAnswer !== null && customAnswer.length > 0) {
73 return customAnswer;
74 }
75 if (question.required === false && question.options.length === 0) {
76 return "";
77 }
78​
79 const selectedOptionValues = normalizeSelectedOptionValues(question, draft?.selectedOptionValues);
80 const fallback =
81 customAnswer ??
82 (question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null);
83 if (question.multiSelect) {
84 // Checking nothing answers "none of these" unless the provider requires a choice.
85 return selectedOptionValues.length > 0
86 ? selectedOptionValues
87 : (fallback ?? (question.required ? null : []));
88 }
89 return selectedOptionValues[0] ?? fallback;
91​
92export function setPendingUserInputCustomAnswer(
93 question: PendingUserInputQuestion,
94 draft: PendingUserInputDraftAnswer | undefined,
95 customAnswer: string,
96): PendingUserInputDraftAnswer {
97 if (question.allowCustomAnswer === false) {
98 return draft ?? {};
99 }
100​
101 const selectedOptionValues =
102 question.initialAnswer !== undefined || customAnswer.trim().length > 0
103 ? undefined
104 : normalizeSelectedOptionValues(question, draft?.selectedOptionValues);
105 return {
106 customAnswer,
107 ...(selectedOptionValues && selectedOptionValues.length > 0 ? { selectedOptionValues } : {}),
108 };
110​
111export function isPendingUserInputOptionSelected(
112 question: PendingUserInputQuestion,
113 draft: PendingUserInputDraftAnswer | undefined,
114 optionValue: string,
115): boolean {
116 if (question.allowCustomAnswer !== false && normalizeDraftAnswer(draft?.customAnswer)) {
117 return false;
118 }
119​
120 const resolvedOptionValue = resolvePendingUserInputOptionValue(question, optionValue);
121 return (
122 resolvedOptionValue !== null &&
123 normalizeSelectedOptionValues(question, draft?.selectedOptionValues).includes(
124 resolvedOptionValue,
125 )
126 );
128​
129/** Toggles an offered option. An unknown option returns the same draft. */
130export function togglePendingUserInputOptionSelection(
131 question: PendingUserInputQuestion,
132 draft: PendingUserInputDraftAnswer | undefined,
133 optionValue: string,
134): PendingUserInputDraftAnswer {
135 const resolvedOptionValue = resolvePendingUserInputOptionValue(question, optionValue);
136 if (resolvedOptionValue === null) {
137 return draft ?? {};
138 }
139​
140 if (question.multiSelect) {
141 const selectedOptionValues = normalizeSelectedOptionValues(
142 question,
143 draft?.selectedOptionValues,
144 );
145 const nextSelectedOptionValues = selectedOptionValues.includes(resolvedOptionValue)
146 ? selectedOptionValues.filter((value) => value !== resolvedOptionValue)
147 : [...selectedOptionValues, resolvedOptionValue];
148​
149 return {
150 customAnswer: "",
151 ...(nextSelectedOptionValues.length > 0
152 ? { selectedOptionValues: nextSelectedOptionValues }
153 : {}),
154 };
155 }
156​
157 return {
158 customAnswer: "",
159 selectedOptionValues: [resolvedOptionValue],
160 };
162​
163/** Every question's answer keyed by question id, or null while any is unanswered. */
164export function buildPendingUserInputAnswers(
165 questions: ReadonlyArray<PendingUserInputQuestion>,
166 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
167): Record<string, PendingUserInputAnswer> | null {
168 const entries: Array<[string, PendingUserInputAnswer]> = [];
169​
170 for (const question of questions) {
171 const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
172 if (answer === null) {
173 return null;
174 }
175 entries.push([question.id, answer]);
176 }
177​
178 return Object.fromEntries(entries);