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

ComposerPendingUserInputPanel.tsx

186 lines6.8 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 (target instanceof HTMLElement && target.isContentEditable) {
104 return;
105 }
106 const digit = Number.parseInt(event.key, 10);
107 if (Number.isNaN(digit) || digit < 1 || digit > 9) return;
108 const optionIndex = digit - 1;
109 if (optionIndex >= activeQuestion.options.length) return;
110 const option = activeQuestion.options[optionIndex];
111 if (!option) return;
112 event.preventDefault();
113 handleOptionSelection(activeQuestion.id, option.label);
114 };
115 document.addEventListener("keydown", handler);
116 return () => document.removeEventListener("keydown", handler);
117 }, [activeQuestion, isResponding]);
118​
119 if (!activeQuestion) {
120 return null;
121 }
122​
123 return (
124 <div className="px-4 py-3 sm:px-5">
125 <div className="flex items-center gap-3">
126 <div className="flex items-center gap-2">
127 {prompt.questions.length > 1 ? (
128 <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">
129 {questionIndex + 1}/{prompt.questions.length}
130 </span>
131 ) : null}
132 <span className="text-[11px] font-semibold tracking-widest text-muted-foreground/50 uppercase">
133 {activeQuestion.header}
134 </span>
135 </div>
136 </div>
137 <p className="mt-1.5 text-sm text-foreground/90">{activeQuestion.question}</p>
138 {activeQuestion.multiSelect ? (
139 <p className="mt-1 text-xs text-muted-foreground/65">Select one or more options.</p>
140 ) : null}
141 <div className="mt-3 space-y-1">
142 {activeQuestion.options.map((option, index) => {
143 const isSelected = progress.selectedOptionLabels.includes(option.label);
144 const shortcutKey = index < 9 ? index + 1 : null;
145 return (
146 <button
147 key={`${activeQuestion.id}:${option.label}`}
148 type="button"
149 disabled={isResponding}
150 onClick={() => handleOptionSelection(activeQuestion.id, option.label)}
151 className={cn(
152 "group flex w-full items-center gap-3 rounded-lg border px-3 py-2 text-left transition-all duration-150",
153 isSelected
154 ? "border-blue-500/40 bg-blue-500/8 text-foreground"
155 : "border-transparent bg-muted/20 text-foreground/80 hover:bg-muted/40 hover:border-border/40",
156 isResponding && "opacity-50 cursor-not-allowed",
157 )}
158 >
159 {shortcutKey !== null ? (
160 <kbd
161 className={cn(
162 "flex size-5 shrink-0 items-center justify-center rounded text-[11px] font-medium tabular-nums transition-colors duration-150",
163 isSelected
164 ? "bg-blue-500/20 text-blue-400"
165 : "bg-muted/40 text-muted-foreground/50 group-hover:bg-muted/60 group-hover:text-muted-foreground/70",
166 )}
167 >
168 {shortcutKey}
169 </kbd>
170 ) : null}
171 <div className="min-w-0 flex-1">
172 <span className="text-sm font-medium">{option.label}</span>
173 {option.description && option.description !== option.label ? (
174 <span className="ml-2 text-xs text-muted-foreground/50">
175 {option.description}
176 </span>
177 ) : null}
178 </div>
179 {isSelected ? <CheckIcon className="size-3.5 shrink-0 text-blue-400" /> : null}
180 </button>
181 );
182 })}
183 </div>
184 </div>
185 );
186});