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

ComposerPendingUserInputPanel.tsx

189 lines6.9 KB

1import { type ApprovalRequestId } from "@t3tools/contracts";
2import { memo, useEffect, useEffectEvent, 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 onToggleOption: (questionId: string, optionLabel: string) => void;
17 onAdvance: () => void;
19​
20export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserInputPanel({
21 pendingUserInputs,
22 respondingRequestIds,
23 answers,
24 questionIndex,
25 onToggleOption,
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 onToggleOption={onToggleOption}
40 onAdvance={onAdvance}
41 />
42 );
43});
44​
45const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard({
46 prompt,
47 isResponding,
48 answers,
49 questionIndex,
50 onToggleOption,
51 onAdvance,
52}: {
53 prompt: PendingUserInput;
54 isResponding: boolean;
55 answers: Record<string, PendingUserInputDraftAnswer>;
56 questionIndex: number;
57 onToggleOption: (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 const onAdvanceRef = useRef(onAdvance);
64​
65 useEffect(() => {
66 onAdvanceRef.current = onAdvance;
67 }, [onAdvance]);
68​
69 // Clear auto-advance timer on unmount
70 useEffect(() => {
71 return () => {
72 if (autoAdvanceTimerRef.current !== null) {
73 window.clearTimeout(autoAdvanceTimerRef.current);
74 }
75 };
76 }, []);
77​
78 const handleOptionSelection = useEffectEvent((questionId: string, optionLabel: string) => {
79 onToggleOption(questionId, optionLabel);
80 if (activeQuestion?.multiSelect) {
81 return;
82 }
83 if (autoAdvanceTimerRef.current !== null) {
84 window.clearTimeout(autoAdvanceTimerRef.current);
85 }
86 autoAdvanceTimerRef.current = window.setTimeout(() => {
87 autoAdvanceTimerRef.current = null;
88 onAdvanceRef.current();
89 }, 200);
90 });
91​
92 // Keyboard shortcut: number keys 1-9 select corresponding options when focus is
93 // outside editable fields. Multi-select prompts toggle options in place; single-
94 // select prompts keep the existing auto-advance behavior.
95 useEffect(() => {
96 if (!activeQuestion || isResponding) return;
97 const handler = (event: globalThis.KeyboardEvent) => {
98 if (event.metaKey || event.ctrlKey || event.altKey) return;
99 const target = event.target;
100 if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) {
101 return;
102 }
103 if (
104 target instanceof HTMLElement &&
105 target.closest('[contenteditable]:not([contenteditable="false"])')
106 ) {
107 return;
108 }
109 const digit = Number.parseInt(event.key, 10);
110 if (Number.isNaN(digit) || digit < 1 || digit > 9) return;
111 const optionIndex = digit - 1;
112 if (optionIndex >= activeQuestion.options.length) return;
113 const option = activeQuestion.options[optionIndex];
114 if (!option) return;
115 event.preventDefault();
116 handleOptionSelection(activeQuestion.id, option.label);
117 };
118 document.addEventListener("keydown", handler);
119 return () => document.removeEventListener("keydown", handler);
120 }, [activeQuestion, isResponding]);
121​
122 if (!activeQuestion) {
123 return null;
124 }
125​
126 return (
127 <div className="px-4 py-3 sm:px-5">
128 <div className="flex items-center gap-3">
129 <div className="flex items-center gap-2">
130 {prompt.questions.length > 1 ? (
131 <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">
132 {questionIndex + 1}/{prompt.questions.length}
133 </span>
134 ) : null}
135 <span className="text-[11px] font-semibold tracking-widest text-muted-foreground/50 uppercase">
136 {activeQuestion.header}
137 </span>
138 </div>
139 </div>
140 <p className="mt-1.5 text-sm text-foreground/90">{activeQuestion.question}</p>
141 {activeQuestion.multiSelect ? (
142 <p className="mt-1 text-xs text-muted-foreground/65">Select one or more options.</p>
143 ) : null}
144 <div className="mt-3 space-y-1">
145 {activeQuestion.options.map((option, index) => {
146 const isSelected = progress.selectedOptionLabels.includes(option.label);
147 const shortcutKey = index < 9 ? index + 1 : null;
148 return (
149 <button
150 key={`${activeQuestion.id}:${option.label}`}
151 type="button"
152 disabled={isResponding}
153 onClick={() => handleOptionSelection(activeQuestion.id, option.label)}
154 className={cn(
155 "group flex w-full items-center gap-3 rounded-lg border px-3 py-2 text-left transition-all duration-150",
156 isSelected
157 ? "border-blue-500/40 bg-blue-500/8 text-foreground"
158 : "border-transparent bg-muted/20 text-foreground/80 hover:bg-muted/40 hover:border-border/40",
159 isResponding && "opacity-50 cursor-not-allowed",
160 )}
161 >
162 {shortcutKey !== null ? (
163 <kbd
164 className={cn(
165 "flex size-5 shrink-0 items-center justify-center rounded text-[11px] font-medium tabular-nums transition-colors duration-150",
166 isSelected
167 ? "bg-blue-500/20 text-blue-400"
168 : "bg-muted/40 text-muted-foreground/50 group-hover:bg-muted/60 group-hover:text-muted-foreground/70",
169 )}
170 >
171 {shortcutKey}
172 </kbd>
173 ) : null}
174 <div className="min-w-0 flex-1">
175 <span className="text-sm font-medium">{option.label}</span>
176 {option.description && option.description !== option.label ? (
177 <span className="ml-2 text-xs text-muted-foreground/50">
178 {option.description}
179 </span>
180 ) : null}
181 </div>
182 {isSelected ? <CheckIcon className="size-3.5 shrink-0 text-blue-400" /> : null}
183 </button>
184 );
185 })}
186 </div>
187 </div>
188 );
189});