forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserQuestionPanel.razor
More file actions
213 lines (184 loc) · 9.04 KB
/
UserQuestionPanel.razor
File metadata and controls
213 lines (184 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@namespace WebCodeCli.Components
@using WebCodeCli.Domain.Domain.Model
@if (UserQuestion != null)
{
var toolUseId = UserQuestion.ToolUseId ?? "";
var isAnswered = UserQuestion.IsAnswered;
<div class="space-y-3">
@if (isAnswered)
{
<div class="bg-emerald-50/50 border border-emerald-100 rounded-lg px-3 py-2 text-emerald-700 flex items-center gap-2 text-sm">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
</svg>
<span>已回答</span>
</div>
}
else
{
@for (var qIdx = 0; qIdx < UserQuestion.Questions.Count; qIdx++)
{
var question = UserQuestion.Questions[qIdx];
var questionIndex = qIdx;
<div class="bg-slate-50/50 border border-slate-200 rounded-lg p-3 space-y-2">
@if (!string.IsNullOrWhiteSpace(question.Header))
{
<div class="text-xs font-semibold text-slate-600 uppercase tracking-wide">@question.Header</div>
}
@if (!string.IsNullOrWhiteSpace(question.Question))
{
<div class="text-sm text-gray-700">@question.Question</div>
}
@if (question.Options.Count > 0)
{
<div class="space-y-1.5 mt-2">
@for (var oIdx = 0; oIdx < question.Options.Count; oIdx++)
{
var option = question.Options[oIdx];
var optionIndex = oIdx;
var isSelected = IsOptionSelected(questionIndex, optionIndex);
<button type="button"
class="@(isSelected
? "w-full text-left px-3 py-2 rounded-lg border border-slate-400 bg-slate-100 transition-all flex items-start gap-2.5"
: "w-full text-left px-3 py-2 rounded-lg border border-gray-200 bg-white hover:border-slate-300 hover:bg-slate-50/50 transition-all flex items-start gap-2.5")"
@onclick="() => ToggleOption(questionIndex, optionIndex, question.MultiSelect)">
<div class="flex-shrink-0 mt-0.5">
@if (question.MultiSelect)
{
<div class="@(isSelected
? "w-4 h-4 rounded border-2 border-slate-500 bg-slate-500 flex items-center justify-center"
: "w-4 h-4 rounded border-2 border-gray-300 bg-white")">
@if (isSelected)
{
<svg class="w-2.5 h-2.5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"></path>
</svg>
}
</div>
}
else
{
<div class="@(isSelected
? "w-4 h-4 rounded-full border-2 border-slate-500 bg-white flex items-center justify-center"
: "w-4 h-4 rounded-full border-2 border-gray-300 bg-white")">
@if (isSelected)
{
<div class="w-2 h-2 rounded-full bg-slate-500"></div>
}
</div>
}
</div>
<div class="flex-1 min-w-0">
<div class="text-sm font-medium text-gray-800">@option.Label</div>
@if (!string.IsNullOrWhiteSpace(option.Description))
{
<div class="text-xs text-gray-500 mt-0.5">@option.Description</div>
}
</div>
</button>
}
</div>
}
</div>
}
<div class="mt-3">
<label class="block text-xs font-medium text-gray-500 mb-1">或者输入自定义回复:</label>
<input type="text"
class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:ring-1 focus:ring-slate-400 focus:border-slate-400 bg-white"
placeholder="输入您的回复..."
value="@_questionInput"
@oninput="(ChangeEventArgs e) => SetQuestionInput(e.Value?.ToString() ?? string.Empty)" />
</div>
<div class="mt-3 flex justify-end">
<button type="button"
class="px-3 py-1.5 bg-slate-600 text-white text-sm rounded-lg hover:bg-slate-700 transition-colors font-medium flex items-center gap-1.5 shadow-sm"
@onclick="() => SubmitAnswer(toolUseId)">
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
</svg>
提交回答
</button>
</div>
}
</div>
}
@code {
[Parameter] public UserQuestion? UserQuestion { get; set; }
/// <summary>
/// 用户回答问题的回调
/// 参数: (toolUseId, 回答内容)
/// </summary>
[Parameter] public EventCallback<(string toolUseId, string answer)> OnAnswerQuestion { get; set; }
private readonly Dictionary<int, HashSet<int>> _questionSelections = new();
private string _questionInput = string.Empty;
private bool IsOptionSelected(int questionIndex, int optionIndex)
{
if (!_questionSelections.TryGetValue(questionIndex, out var options))
return false;
return options.Contains(optionIndex);
}
private void ToggleOption(int questionIndex, int optionIndex, bool multiSelect)
{
if (!_questionSelections.ContainsKey(questionIndex))
_questionSelections[questionIndex] = new HashSet<int>();
var options = _questionSelections[questionIndex];
if (multiSelect)
{
if (options.Contains(optionIndex))
options.Remove(optionIndex);
else
options.Add(optionIndex);
}
else
{
options.Clear();
options.Add(optionIndex);
}
StateHasChanged();
}
private void SetQuestionInput(string value)
{
_questionInput = value;
}
private async Task SubmitAnswer(string toolUseId)
{
if (string.IsNullOrEmpty(toolUseId) || UserQuestion == null) return;
var answer = BuildAnswerText(UserQuestion);
if (string.IsNullOrWhiteSpace(answer))
{
return;
}
UserQuestion.IsAnswered = true;
_questionSelections.Clear();
_questionInput = string.Empty;
if (OnAnswerQuestion.HasDelegate)
{
await OnAnswerQuestion.InvokeAsync((toolUseId, answer));
}
StateHasChanged();
}
private string BuildAnswerText(UserQuestion userQuestion)
{
if (!string.IsNullOrWhiteSpace(_questionInput))
{
return _questionInput.Trim();
}
var answers = new List<string>();
for (var qIdx = 0; qIdx < userQuestion.Questions.Count; qIdx++)
{
if (!_questionSelections.TryGetValue(qIdx, out var selectedOptions) || selectedOptions.Count == 0)
continue;
var question = userQuestion.Questions[qIdx];
var selectedLabels = selectedOptions
.Where(idx => idx >= 0 && idx < question.Options.Count)
.Select(idx => question.Options[idx].Label)
.Where(label => !string.IsNullOrEmpty(label))
.ToList();
if (selectedLabels.Count > 0)
{
answers.Add(string.Join(", ", selectedLabels));
}
}
return string.Join("; ", answers);
}
}