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

pendingUserInput.ts

169 lines5.4 KB

1import type { UserInputQuestion } from "@t3tools/contracts";
2​
3export interface PendingUserInputDraftAnswer {
4 selectedOptionValues?: string[];
5 customAnswer?: string;
6 attachmentCount?: number;
7 attachmentsBlocked?: boolean;
8}
9​
10export interface PendingUserInputProgress {
11 questionIndex: number;
12 activeQuestion: UserInputQuestion | null;
13 activeDraft: PendingUserInputDraftAnswer | undefined;
14 selectedOptionValues: string[];
15 customAnswer: string;
16 resolvedAnswer: string | string[] | null;
17 usingCustomAnswer: boolean;
18 answeredQuestionCount: number;
19 isLastQuestion: boolean;
20 isComplete: boolean;
21 canAdvance: boolean;
23​
24function normalizeDraftAnswer(value: string | undefined): string | null {
25 if (typeof value !== "string") {
26 return null;
27 }
28​
29 const trimmed = value.trim();
30 return trimmed.length > 0 ? trimmed : null;
32​
33function normalizeSelectedOptionValues(value: string[] | undefined): string[] {
34 if (!Array.isArray(value)) {
35 return [];
36 }
37​
38 // Provider option IDs must stay unchanged, including whitespace.
39 return Array.from(new Set(value.filter((entry) => typeof entry === "string")));
41​
42export function resolvePendingUserInputAnswer(
43 question: UserInputQuestion,
44 draft: PendingUserInputDraftAnswer | undefined,
45): string | string[] | null {
46 if (draft?.attachmentsBlocked) return null;
47 const customAnswer =
48 question.allowCustomAnswer === false ? null : normalizeDraftAnswer(draft?.customAnswer);
49 if (customAnswer) {
50 return customAnswer;
51 }
52​
53 const selectedOptionValues = normalizeSelectedOptionValues(draft?.selectedOptionValues).filter(
54 (value) => question.options.some((option) => (option.value ?? option.label) === value),
55 );
56 if (question.multiSelect) {
57 return selectedOptionValues.length > 0
58 ? selectedOptionValues
59 : question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0
60 ? ""
61 : null;
62 }
63​
64 return (
65 selectedOptionValues[0] ??
66 (question.allowCustomAnswer !== false && (draft?.attachmentCount ?? 0) > 0 ? "" : null)
67 );
69​
70export function setPendingUserInputCustomAnswer(
71 draft: PendingUserInputDraftAnswer | undefined,
72 customAnswer: string,
73): PendingUserInputDraftAnswer {
74 const selectedOptionValues =
75 customAnswer.trim().length > 0
76 ? undefined
77 : normalizeSelectedOptionValues(draft?.selectedOptionValues);
78​
79 return {
80 customAnswer,
81 ...(selectedOptionValues && selectedOptionValues.length > 0 ? { selectedOptionValues } : {}),
82 };
84​
85export function togglePendingUserInputOptionSelection(
86 question: UserInputQuestion,
87 draft: PendingUserInputDraftAnswer | undefined,
88 optionValue: string,
89): PendingUserInputDraftAnswer {
90 if (question.multiSelect) {
91 const selectedOptionValues = normalizeSelectedOptionValues(draft?.selectedOptionValues);
92 const nextSelectedOptionValues = selectedOptionValues.includes(optionValue)
93 ? selectedOptionValues.filter((value) => value !== optionValue)
94 : [...selectedOptionValues, optionValue];
95​
96 return {
97 customAnswer: "",
98 ...(nextSelectedOptionValues.length > 0
99 ? { selectedOptionValues: nextSelectedOptionValues }
100 : {}),
101 };
102 }
103​
104 return {
105 customAnswer: "",
106 selectedOptionValues: [optionValue],
107 };
109​
110export function buildPendingUserInputAnswers(
111 questions: ReadonlyArray<UserInputQuestion>,
112 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
113): Record<string, string | string[]> | null {
114 const answers: Record<string, string | string[]> = {};
115​
116 for (const question of questions) {
117 const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
118 if (answer === null) {
119 return null;
120 }
121 answers[question.id] = answer;
122 }
123​
124 return answers;
126​
127export function countAnsweredPendingUserInputQuestions(
128 questions: ReadonlyArray<UserInputQuestion>,
129 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
130): number {
131 return questions.reduce((count, question) => {
132 return resolvePendingUserInputAnswer(question, draftAnswers[question.id]) !== null
133 ? count + 1
134 : count;
135 }, 0);
137​
138export function derivePendingUserInputProgress(
139 questions: ReadonlyArray<UserInputQuestion>,
140 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
141 questionIndex: number,
142): PendingUserInputProgress {
143 const normalizedQuestionIndex =
144 questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
145 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
146 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
147 const resolvedAnswer = activeQuestion
148 ? resolvePendingUserInputAnswer(activeQuestion, activeDraft)
149 : null;
150 const customAnswer =
151 activeQuestion?.allowCustomAnswer === false ? "" : (activeDraft?.customAnswer ?? "");
152 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
153 const isLastQuestion =
154 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
155​
156 return {
157 questionIndex: normalizedQuestionIndex,
158 activeQuestion,
159 activeDraft,
160 selectedOptionValues: normalizeSelectedOptionValues(activeDraft?.selectedOptionValues),
161 customAnswer,
162 resolvedAnswer,
163 usingCustomAnswer: customAnswer.trim().length > 0,
164 answeredQuestionCount,
165 isLastQuestion,
166 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
167 canAdvance: resolvedAnswer !== null,
168 };