forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectSelectModal.razor
More file actions
304 lines (272 loc) · 12.3 KB
/
ProjectSelectModal.razor
File metadata and controls
304 lines (272 loc) · 12.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
@namespace WebCodeCli.Components
@using WebCodeCli.Domain.Domain.Model
@using WebCodeCli.Domain.Domain.Service
@using System.Text.Json
@inject HttpClient Http
@inject ILocalizationService L
<!-- 项目选择模态框 -->
@if (_isVisible)
{
<div class="fixed inset-0 z-50 overflow-y-auto">
<!-- 背景遮罩 -->
<div class="fixed inset-0 bg-black bg-opacity-50 transition-opacity" @onclick="Cancel"></div>
<!-- 模态框内容 -->
<div class="flex min-h-full items-center justify-center p-4">
<div class="relative bg-white rounded-2xl shadow-xl w-full max-w-md transform transition-all" @onclick:stopPropagation="true">
<!-- 头部 -->
<div class="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
<h3 class="text-lg font-semibold text-gray-800 flex items-center gap-2">
<svg class="w-5 h-5 text-gray-700" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path>
</svg>
@T("project.selectProject")
</h3>
<button @onclick="Cancel" class="p-1 hover:bg-gray-100 rounded-lg transition-colors">
<svg class="w-5 h-5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<!-- 内容 -->
<div class="p-6">
<p class="text-sm text-gray-600 mb-4">
@T("project.selectProjectDesc")
</p>
@if (_isLoading)
{
<div class="py-8 text-center">
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900 mx-auto"></div>
<p class="mt-2 text-gray-500">@T("common.loading")</p>
</div>
}
else
{
<div class="space-y-2 max-h-80 overflow-y-auto">
<!-- 空白会话选项 -->
<button @onclick="() => SelectProject(null)"
class="w-full p-4 border-2 rounded-lg text-left transition-all hover:border-gray-400 @(_selectedProjectId == null ? "border-gray-900 bg-gray-50" : "border-gray-200")">
<div class="flex items-center gap-3">
<div class="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center">
<svg class="w-5 h-5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
</svg>
</div>
<div>
<h4 class="font-medium text-gray-800">@T("project.blankSession")</h4>
<p class="text-xs text-gray-500">@T("project.blankSessionDesc")</p>
</div>
</div>
</button>
<!-- 项目列表 -->
@foreach (var project in _projects.Where(p => p.Status == "ready"))
{
<button @onclick="() => SelectProject(project.ProjectId)"
class="w-full p-4 border-2 rounded-lg text-left transition-all hover:border-gray-400 @(_selectedProjectId == project.ProjectId ? "border-gray-900 bg-gray-50" : "border-gray-200")">
<div class="flex items-center gap-3">
<div class="w-10 h-10 bg-green-100 rounded-lg flex items-center justify-center">
<svg class="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path>
</svg>
</div>
<div class="flex-1 min-w-0">
<h4 class="font-medium text-gray-800 truncate">@project.Name</h4>
<p class="text-xs text-gray-500 truncate">@project.GitUrl</p>
<p class="text-xs text-gray-400">@T("project.branch"): @project.Branch</p>
</div>
</div>
</button>
}
@if (!_projects.Any(p => p.Status == "ready"))
{
<div class="py-4 text-center text-gray-400 text-sm">
<p>@T("project.noReadyProjects")</p>
<p class="text-xs mt-1">@T("project.noReadyProjectsHint")</p>
</div>
}
</div>
<!-- Git 选项 -->
<div class="mt-4 p-3 bg-gray-50 rounded-lg border border-gray-200">
<label class="flex items-start gap-2 cursor-pointer">
<input type="checkbox"
class="mt-0.5 rounded border-gray-300"
@bind="_includeGit"
disabled="@(_selectedProjectId == null)" />
<span class="text-sm text-gray-700">@T("project.includeGit")</span>
</label>
<p class="text-xs text-gray-500 mt-1 ml-6">
@T("project.includeGitHint")
</p>
</div>
<!-- 操作按钮 -->
<div class="flex gap-3 mt-6">
<button @onclick="Cancel"
class="flex-1 py-3 border border-gray-300 rounded-lg font-medium hover:bg-gray-50 transition-colors">
@T("common.cancel")
</button>
<button @onclick="Confirm"
class="flex-1 py-3 bg-gray-900 text-white rounded-lg font-medium hover:bg-gray-800 transition-colors">
@T("common.confirm")
</button>
</div>
}
</div>
</div>
</div>
</div>
}
@code {
[Parameter]
public EventCallback<ProjectSelectionResult> OnProjectSelected { get; set; }
private bool _isVisible = false;
private List<ProjectInfo> _projects = new();
private bool _isLoading = false;
private string? _selectedProjectId = null;
private bool _includeGit = false;
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
private bool _isReloadingLanguage = false;
protected override async Task OnInitializedAsync()
{
await LoadTranslationsAsync();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var language = await L.GetCurrentLanguageAsync();
if (language != _currentLanguage && !_isReloadingLanguage)
{
_isReloadingLanguage = true;
_currentLanguage = language;
await LoadTranslationsAsync();
_isReloadingLanguage = false;
StateHasChanged();
}
}
public async Task ShowAsync()
{
_isVisible = true;
_selectedProjectId = null;
_includeGit = false;
StateHasChanged();
await LoadTranslationsAsync();
await LoadProjects();
}
public void Close()
{
_isVisible = false;
StateHasChanged();
}
private async Task LoadProjects()
{
try
{
_isLoading = true;
StateHasChanged();
var response = await Http.GetAsync("/api/project");
if (response.IsSuccessStatusCode)
{
_projects = await response.Content.ReadFromJsonAsync<List<ProjectInfo>>() ?? new();
}
}
catch (Exception ex)
{
Console.WriteLine($"加载项目列表失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private void SelectProject(string? projectId)
{
_selectedProjectId = projectId;
if (_selectedProjectId == null)
{
_includeGit = false;
}
StateHasChanged();
}
private async Task Confirm()
{
Close();
var result = new ProjectSelectionResult(_selectedProjectId, _selectedProjectId != null && _includeGit);
await OnProjectSelected.InvokeAsync(result);
}
private void Cancel()
{
Close();
}
#region 本地化辅助方法
private async Task LoadTranslationsAsync()
{
try
{
_currentLanguage = await L.GetCurrentLanguageAsync();
var allTranslations = await L.GetAllTranslationsAsync(_currentLanguage);
_translations = FlattenTranslations(allTranslations);
}
catch (Exception ex)
{
Console.WriteLine($"[项目选择] 加载翻译资源失败: {ex.Message}");
_translations = new Dictionary<string, string>();
}
}
private Dictionary<string, string> FlattenTranslations(Dictionary<string, object> source, string prefix = "")
{
var result = new Dictionary<string, string>();
foreach (var kvp in source)
{
var key = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}.{kvp.Key}";
if (kvp.Value is JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.Object)
{
var nested = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonElement.GetRawText());
if (nested != null)
{
foreach (var item in FlattenTranslations(nested, key))
{
result[item.Key] = item.Value;
}
}
}
else if (jsonElement.ValueKind == JsonValueKind.String)
{
result[key] = jsonElement.GetString() ?? key;
}
}
else if (kvp.Value is Dictionary<string, object> dict)
{
foreach (var item in FlattenTranslations(dict, key))
{
result[item.Key] = item.Value;
}
}
else if (kvp.Value is string str)
{
result[key] = str;
}
}
return result;
}
private string T(string key)
{
if (_translations.TryGetValue(key, out var translation))
{
return translation;
}
var parts = key.Split('.');
return parts.Length > 0 ? parts[^1] : key;
}
private string T(string key, params (string name, string value)[] parameters)
{
var text = T(key);
foreach (var (name, value) in parameters)
{
text = text.Replace($"{{{name}}}", value);
}
return text;
}
#endregion
}