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