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

pendingUserInput.ts

161 lines4.9 KB

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