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

ComposerPendingUserInputPanel.tsx

182 lines6.9 KB

1import { type ApprovalRequestId } from "@t3tools/contracts";
2import { memo, useCallback, useEffect, useRef } from "react";
3import { type PendingUserInput } from "../../session-logic";
4import {
5 derivePendingUserInputProgress,
6 type PendingUserInputDraftAnswer,
7} from "../../pendingUserInput";
8import { CheckIcon } from "lucide-react";
9import { cn } from "~/lib/utils";
10​
11interface PendingUserInputPanelProps {
12 pendingUserInputs: PendingUserInput[];
13 respondingRequestIds: ApprovalRequestId[];
14 answers: Record<string, PendingUserInputDraftAnswer>;
15 questionIndex: number;
16 onSelectOption: (questionId: string, optionLabel: string) => void;
17 onAdvance: () => void;
19​
20export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserInputPanel({
21 pendingUserInputs,
22 respondingRequestIds,
23 answers,
24 questionIndex,
25 onSelectOption,
26 onAdvance,
27}: PendingUserInputPanelProps) {
28 if (pendingUserInputs.length === 0) return null;
29 const activePrompt = pendingUserInputs[0];
30 if (!activePrompt) return null;
31​
32 return (
33 <ComposerPendingUserInputCard
34 key={activePrompt.requestId}
35 prompt={activePrompt}
36 isResponding={respondingRequestIds.includes(activePrompt.requestId)}
37 answers={answers}
38 questionIndex={questionIndex}
39 onSelectOption={onSelectOption}
40 onAdvance={onAdvance}
41 />
42 );
43});
44​
45const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard({
46 prompt,
47 isResponding,
48 answers,
49 questionIndex,
50 onSelectOption,
51 onAdvance,
52}: {
53 prompt: PendingUserInput;
54 isResponding: boolean;
55 answers: Record<string, PendingUserInputDraftAnswer>;
56 questionIndex: number;
57 onSelectOption: (questionId: string, optionLabel: string) => void;
58 onAdvance: () => void;
59}) {
60 const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex);
61 const activeQuestion = progress.activeQuestion;
62 const autoAdvanceTimerRef = useRef<number | null>(null);
63​
64 // Clear auto-advance timer on unmount
65 useEffect(() => {
66 return () => {
67 if (autoAdvanceTimerRef.current !== null) {
68 window.clearTimeout(autoAdvanceTimerRef.current);
69 }
70 };
71 }, []);
72​
73 const selectOptionAndAutoAdvance = useCallback(
74 (questionId: string, optionLabel: string) => {
75 onSelectOption(questionId, optionLabel);
76 if (autoAdvanceTimerRef.current !== null) {
77 window.clearTimeout(autoAdvanceTimerRef.current);
78 }
79 autoAdvanceTimerRef.current = window.setTimeout(() => {
80 autoAdvanceTimerRef.current = null;
81 onAdvance();
82 }, 200);
83 },
84 [onSelectOption, onAdvance],
85 );
86​
87 // Keyboard shortcut: number keys 1-9 select corresponding option and auto-advance.
88 // Works even when the Lexical composer (contenteditable) has focus — the composer
89 // doubles as a custom-answer field during user input, and when it's empty the digit
90 // keys should pick options instead of typing into the editor.
91 useEffect(() => {
92 if (!activeQuestion || isResponding) return;
93 const handler = (event: globalThis.KeyboardEvent) => {
94 if (event.metaKey || event.ctrlKey || event.altKey) return;
95 const target = event.target;
96 if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
97 return;
98 }
99 // If the user has started typing a custom answer in the contenteditable
100 // composer, let digit keys pass through so they can type numbers.
101 if (target instanceof HTMLElement && target.isContentEditable) {
102 const hasCustomText = progress.customAnswer.length > 0;
103 if (hasCustomText) return;
104 }
105 const digit = Number.parseInt(event.key, 10);
106 if (Number.isNaN(digit) || digit < 1 || digit > 9) return;
107 const optionIndex = digit - 1;
108 if (optionIndex >= activeQuestion.options.length) return;
109 const option = activeQuestion.options[optionIndex];
110 if (!option) return;
111 event.preventDefault();
112 selectOptionAndAutoAdvance(activeQuestion.id, option.label);
113 };
114 document.addEventListener("keydown", handler);
115 return () => document.removeEventListener("keydown", handler);
116 }, [activeQuestion, isResponding, selectOptionAndAutoAdvance, progress.customAnswer.length]);
117​
118 if (!activeQuestion) {
119 return null;
120 }
121​
122 return (
123 <div className="px-4 py-3 sm:px-5">
124 <div className="flex items-center gap-3">
125 <div className="flex items-center gap-2">
126 {prompt.questions.length > 1 ? (
127 <span className="flex h-5 items-center rounded-md bg-muted/60 px-1.5 text-[10px] font-medium tabular-nums text-muted-foreground/60">
128 {questionIndex + 1}/{prompt.questions.length}
129 </span>
130 ) : null}
131 <span className="text-[11px] font-semibold tracking-widest text-muted-foreground/50 uppercase">
132 {activeQuestion.header}
133 </span>
134 </div>
135 </div>
136 <p className="mt-1.5 text-sm text-foreground/90">{activeQuestion.question}</p>
137 <div className="mt-3 space-y-1">
138 {activeQuestion.options.map((option, index) => {
139 const isSelected = progress.selectedOptionLabel === option.label;
140 const shortcutKey = index < 9 ? index + 1 : null;
141 return (
142 <button
143 key={`${activeQuestion.id}:${option.label}`}
144 type="button"
145 disabled={isResponding}
146 onClick={() => selectOptionAndAutoAdvance(activeQuestion.id, option.label)}
147 className={cn(
148 "group flex w-full items-center gap-3 rounded-lg border px-3 py-2 text-left transition-all duration-150",
149 isSelected
150 ? "border-blue-500/40 bg-blue-500/8 text-foreground"
151 : "border-transparent bg-muted/20 text-foreground/80 hover:bg-muted/40 hover:border-border/40",
152 isResponding && "opacity-50 cursor-not-allowed",
153 )}
154 >
155 {shortcutKey !== null ? (
156 <kbd
157 className={cn(
158 "flex size-5 shrink-0 items-center justify-center rounded text-[11px] font-medium tabular-nums transition-colors duration-150",
159 isSelected
160 ? "bg-blue-500/20 text-blue-400"
161 : "bg-muted/40 text-muted-foreground/50 group-hover:bg-muted/60 group-hover:text-muted-foreground/70",
162 )}
163 >
164 {shortcutKey}
165 </kbd>
166 ) : null}
167 <div className="min-w-0 flex-1">
168 <span className="text-sm font-medium">{option.label}</span>
169 {option.description && option.description !== option.label ? (
170 <span className="ml-2 text-xs text-muted-foreground/50">
171 {option.description}
172 </span>
173 ) : null}
174 </div>
175 {isSelected ? <CheckIcon className="size-3.5 shrink-0 text-blue-400" /> : null}
176 </button>
177 );
178 })}
179 </div>
180 </div>
181 );
182});