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

pendingUserInput.ts

120 lines3.8 KB

1import type { UserInputQuestion } from "@t3tools/contracts";
2​
3export interface PendingUserInputDraftAnswer {
4 selectedOptionLabel?: string;
5 customAnswer?: string;
6}
7​
8export interface PendingUserInputProgress {
9 questionIndex: number;
10 activeQuestion: UserInputQuestion | null;
11 activeDraft: PendingUserInputDraftAnswer | undefined;
12 selectedOptionLabel: string | undefined;
13 customAnswer: string;
14 resolvedAnswer: 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​
31export function resolvePendingUserInputAnswer(
32 draft: PendingUserInputDraftAnswer | undefined,
33): string | null {
34 const customAnswer = normalizeDraftAnswer(draft?.customAnswer);
35 if (customAnswer) {
36 return customAnswer;
37 }
38​
39 return normalizeDraftAnswer(draft?.selectedOptionLabel);
41​
42export function setPendingUserInputCustomAnswer(
43 draft: PendingUserInputDraftAnswer | undefined,
44 customAnswer: string,
45): PendingUserInputDraftAnswer {
46 const selectedOptionLabel =
47 customAnswer.trim().length > 0 ? undefined : draft?.selectedOptionLabel;
48​
49 return {
50 customAnswer,
51 ...(selectedOptionLabel ? { selectedOptionLabel } : {}),
52 };
54​
55export function buildPendingUserInputAnswers(
56 questions: ReadonlyArray<UserInputQuestion>,
57 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
58): Record<string, string> | null {
59 const answers: Record<string, string> = {};
60​
61 for (const question of questions) {
62 const answer = resolvePendingUserInputAnswer(draftAnswers[question.id]);
63 if (!answer) {
64 return null;
65 }
66 answers[question.id] = answer;
67 }
68​
69 return answers;
71​
72export function countAnsweredPendingUserInputQuestions(
73 questions: ReadonlyArray<UserInputQuestion>,
74 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
75): number {
76 return questions.reduce((count, question) => {
77 return resolvePendingUserInputAnswer(draftAnswers[question.id]) ? count + 1 : count;
78 }, 0);
80​
81export function findFirstUnansweredPendingUserInputQuestionIndex(
82 questions: ReadonlyArray<UserInputQuestion>,
83 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
84): number {
85 const unansweredIndex = questions.findIndex(
86 (question) => !resolvePendingUserInputAnswer(draftAnswers[question.id]),
87 );
88​
89 return unansweredIndex === -1 ? Math.max(questions.length - 1, 0) : unansweredIndex;
91​
92export function derivePendingUserInputProgress(
93 questions: ReadonlyArray<UserInputQuestion>,
94 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
95 questionIndex: number,
96): PendingUserInputProgress {
97 const normalizedQuestionIndex =
98 questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
99 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
100 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
101 const resolvedAnswer = resolvePendingUserInputAnswer(activeDraft);
102 const customAnswer = activeDraft?.customAnswer ?? "";
103 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
104 const isLastQuestion =
105 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
106​
107 return {
108 questionIndex: normalizedQuestionIndex,
109 activeQuestion,
110 activeDraft,
111 selectedOptionLabel: activeDraft?.selectedOptionLabel,
112 customAnswer,
113 resolvedAnswer,
114 usingCustomAnswer: customAnswer.trim().length > 0,
115 answeredQuestionCount,
116 isLastQuestion,
117 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
118 canAdvance: Boolean(resolvedAnswer),
119 };