MCPcopy
hub / github.com/kagent-dev/kagent / AskUserDisplay

Function AskUserDisplay

ui/src/components/chat/AskUserDisplay.tsx:36–196  ·  view source on GitHub ↗
({
  questions,
  onSubmit,
  isResolved = false,
  resolvedAnswers,
  subagentName,
}: AskUserDisplayProps)

Source from the content-addressed store, hash-verified

34 * selected answers are highlighted, and a green checkmark badge is shown.
35 */
36export default function AskUserDisplay({
37 questions,
38 onSubmit,
39 isResolved = false,
40 resolvedAnswers,
41 subagentName,
42}: AskUserDisplayProps) {
43 // One entry per question: set of selected choices.
44 const [selectedChoices, setSelectedChoices] = useState<string[][]>(
45 questions.map(() => [])
46 );
47 // One free-text answer per question.
48 const [freeTextAnswers, setFreeTextAnswers] = useState<string[]>(
49 questions.map(() => "")
50 );
51 const [isSubmitting, setIsSubmitting] = useState(false);
52
53 const toggleChoice = (qIdx: number, choice: string) => {
54 if (isResolved || isSubmitting || !onSubmit) return;
55 setSelectedChoices(prev => {
56 const next = prev.map(s => [...s]);
57 const q = questions[qIdx];
58 const isMultiple = q.multiple ?? false;
59 if (next[qIdx].includes(choice)) {
60 next[qIdx] = next[qIdx].filter(c => c !== choice);
61 } else if (isMultiple) {
62 next[qIdx] = [...next[qIdx], choice];
63 } else {
64 // Single-select: deselect others
65 next[qIdx] = [choice];
66 }
67 return next;
68 });
69 };
70
71 const setFreeText = (qIdx: number, value: string) => {
72 setFreeTextAnswers(prev => {
73 const next = [...prev];
74 next[qIdx] = value;
75 return next;
76 });
77 };
78
79 /** Whether every question has at least one selected choice or a non-empty free-text answer. */
80 const isReadyToSubmit = questions.every((_, i) => {
81 return selectedChoices[i].length > 0 || freeTextAnswers[i].trim().length > 0;
82 });
83
84 const handleSubmit = () => {
85 if (!onSubmit || !isReadyToSubmit || isSubmitting) return;
86 setIsSubmitting(true);
87 const answers = questions.map((_, i) => {
88 const chips = selectedChoices[i];
89 const text = freeTextAnswers[i].trim();
90 // Combine chip selections and free-text into a single answer list.
91 const combined = [...chips];
92 if (text && !combined.includes(text)) {
93 combined.push(text);

Callers

nothing calls this directly

Calls 4

cnFunction · 0.90
toggleChoiceFunction · 0.85
setFreeTextFunction · 0.85

Tested by

no test coverage detected