-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathEnvironmentVariableConfigModal.razor
More file actions
719 lines (666 loc) · 34.4 KB
/
EnvironmentVariableConfigModal.razor
File metadata and controls
719 lines (666 loc) · 34.4 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
@using System.Text.Json
@using WebCodeCli.Domain.Domain.Model
@using WebCodeCli.Domain.Domain.Service
@inject ICliExecutorService CliExecutorService
@inject ICliToolEnvironmentService CliToolEnvironmentService
@inject ILocalizationService L
<div class="env-config-modal">
@if (_isVisible)
{
<div class="fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center p-4" @onclick="OnBackgroundClick">
<div class="bg-white rounded-2xl shadow-2xl max-w-3xl w-full max-h-[90vh] flex flex-col border border-gray-300" @onclick:stopPropagation="true">
<!-- 头部 -->
<div class="flex items-center justify-between p-6 border-b border-gray-300 bg-gray-50 rounded-t-2xl">
<div class="flex items-center gap-3">
<svg class="w-6 h-6 text-gray-700" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
</svg>
<h3 class="text-lg font-bold text-gray-800">
@(string.IsNullOrWhiteSpace(_selectedTool?.Name)
? T("envConfig.title")
: T("envConfig.titleWithTool", ("tool", _selectedTool!.Name)))
</h3>
</div>
<button @onclick="Close" class="text-gray-500 hover:text-gray-700 hover:bg-gray-200 rounded-lg p-2 transition-all">
<svg class="w-6 h-6" 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="flex-1 overflow-y-auto p-6">
@if (_isLoading)
{
<div class="flex items-center justify-center py-12">
<div class="animate-spin h-8 w-8 border-4 border-gray-700 rounded-full border-t-transparent"></div>
</div>
}
else
{
<div class="space-y-4">
<!-- 说明 -->
<div class="bg-gray-50 border border-gray-300 rounded-xl p-4 shadow-sm">
<div class="flex">
<svg class="w-5 h-5 text-gray-700 mt-0.5 mr-3 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path>
</svg>
<div class="text-sm text-gray-800">
<p class="font-bold mb-2">@T("envConfig.descTitle")</p>
<p class="mb-1">• @T("envConfig.descSaveDb")</p>
<p class="mb-1">• @T("envConfig.descSecretPrefix")<span class="font-semibold text-red-600">@T("envConfig.descSecretHighlight")</span></p>
<p>• @T("envConfig.descReset")</p>
</div>
</div>
</div>
<!-- 配置方案管理区 -->
<div class="bg-blue-50 border border-blue-200 rounded-xl p-4">
<div class="flex items-center justify-between mb-3">
<div>
<p class="text-sm font-bold text-blue-800">@T("envConfig.profilesTitle")</p>
<p class="text-xs text-blue-600 mt-0.5">@T("envConfig.profilesDesc")</p>
</div>
<button @onclick="StartAddProfile"
class="flex items-center gap-1.5 px-3 py-1.5 text-sm font-semibold text-blue-700 bg-white border border-blue-300 rounded-lg hover:bg-blue-50 hover:border-blue-500 transition-all shadow-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="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
</svg>
@T("envConfig.addProfile")
</button>
</div>
@if (_profiles.Any())
{
<div class="space-y-2">
@foreach (var profile in _profiles)
{
@if (_profilePendingDelete?.Id == profile.Id)
{
<!-- 删除确认行 -->
<div class="flex items-center gap-2 bg-red-50 border border-red-300 rounded-lg px-3 py-2">
<svg class="w-4 h-4 text-red-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span class="flex-1 text-sm text-red-700">@T("envConfig.deleteProfileConfirm")</span>
<button @onclick="() => DeleteProfile(profile)"
class="px-2 py-1 text-xs font-semibold text-white bg-red-600 rounded-md hover:bg-red-700 transition-all">
@T("common.confirm")
</button>
<button @onclick="CancelDeleteProfile"
class="px-2 py-1 text-xs font-semibold text-gray-600 bg-white border border-gray-300 rounded-md hover:bg-gray-50 transition-all">
@T("common.cancel")
</button>
</div>
}
else
{
<div class="flex items-center gap-2 bg-white border @(profile.IsActive ? "border-green-400" : "border-gray-200") rounded-lg px-3 py-2 shadow-sm">
@if (profile.IsActive)
{
<span class="flex-shrink-0 inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold text-green-700 bg-green-100 rounded-full">
<svg class="w-3 h-3" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
</svg>
@T("envConfig.activeProfile")
</span>
}
<span class="flex-1 text-sm font-medium text-gray-800 truncate">@profile.ProfileName</span>
<div class="flex items-center gap-1 flex-shrink-0">
@if (!profile.IsActive)
{
<button @onclick="() => ActivateProfile(profile)"
class="px-2 py-1 text-xs font-semibold text-green-700 bg-green-50 border border-green-200 rounded-md hover:bg-green-100 transition-all"
title="@T("envConfig.activateProfile")">
@T("envConfig.activateProfile")
</button>
}
else
{
<button @onclick="DeactivateProfiles"
class="px-2 py-1 text-xs font-semibold text-gray-600 bg-gray-100 border border-gray-200 rounded-md hover:bg-gray-200 transition-all"
title="@T("envConfig.deactivateProfile")">
@T("envConfig.deactivateProfile")
</button>
}
<button @onclick="() => EditProfile(profile)"
class="p-1.5 text-blue-600 hover:bg-blue-50 rounded-md transition-all"
title="@T("envConfig.editProfile")">
<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="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
</svg>
</button>
<button @onclick="() => RequestDeleteProfile(profile)"
class="p-1.5 text-red-500 hover:bg-red-50 rounded-md transition-all"
title="@T("envConfig.deleteProfile")">
<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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
</svg>
</button>
</div>
</div>
}
}
</div>
}
</div>
<!-- 编辑区标题 -->
<div class="flex items-center justify-between">
<p class="text-sm font-bold text-gray-700">
@if (_editingProfile != null)
{
@T("envConfig.editingProfile", ("name", _editingProfile.ProfileName))
}
else if (_isAddingProfile)
{
@T("envConfig.addProfile")
}
else
{
@T("envConfig.editingDefault")
}
</p>
@if (_editingProfile != null || _isAddingProfile)
{
<button @onclick="CancelProfileEdit"
class="text-xs text-gray-500 hover:text-gray-700 underline">
@T("envConfig.cancelEdit")
</button>
}
</div>
<!-- 新建/编辑方案时的方案名称输入 -->
@if (_isAddingProfile || _editingProfile != null)
{
<div class="card border-blue-200 bg-blue-50 p-4">
<label class="block text-sm font-bold text-blue-800 mb-2">@T("envConfig.profilesTitle")</label>
<input type="text"
@bind="_newProfileName"
class="w-full px-4 py-2.5 border border-blue-300 rounded-xl focus:outline-none focus:ring-1 focus:ring-blue-500 text-sm"
placeholder="@T("envConfig.profileNamePlaceholder")" />
</div>
}
<!-- 环境变量列表 -->
@foreach (var (index, kvp) in _envVars.Select((kvp, i) => (i, kvp)))
{
<div class="card border-gray-300 shadow-sm hover:shadow-md transition-shadow p-4">
<div class="flex gap-4 items-start">
<div class="flex-1">
<label class="block text-sm font-bold text-gray-700 mb-2">@T("envConfig.keyLabel")</label>
<input type="text"
@bind="kvp.Key"
class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500 text-sm transition-all hover:border-gray-400"
placeholder="@T("envConfig.keyPlaceholder")" />
</div>
<div class="flex-1">
<label class="block text-sm font-bold text-gray-700 mb-2">@T("envConfig.valueLabel")</label>
<div class="relative">
<input type="password"
@bind="kvp.Value"
autocomplete="off"
oncopy="return false"
oncut="return false"
onpaste="return true"
class="w-full pl-4 pr-10 py-2.5 border border-gray-300 rounded-xl focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500 text-sm transition-all hover:border-gray-400"
placeholder="@(string.IsNullOrWhiteSpace(kvp.Value) ? T("envConfig.valuePlaceholder") : "••••••••")" />
@if (!string.IsNullOrWhiteSpace(kvp.Value))
{
<button type="button"
@onclick="() => ClearValue(index)"
class="absolute right-1 top-1/2 -translate-y-1/2 p-1 text-gray-400 hover:text-gray-600 hover:bg-gray-100 rounded-md transition-all z-10"
title="@T("envConfig.clearValueTitle")">
<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="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
}
</div>
</div>
<div class="flex items-end">
<button @onclick="() => RemoveEnvVar(index)"
class="p-2.5 text-red-600 hover:bg-red-50 hover:text-red-700 rounded-xl transition-all border border-transparent hover:border-red-200"
title="@T("envConfig.removeTitle")">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
</svg>
</button>
</div>
</div>
</div>
}
<!-- 添加按钮 -->
<button @onclick="AddEnvVar"
class="w-full py-3 border-2 border-dashed border-gray-400 rounded-xl hover:border-gray-600 hover:bg-gray-50 transition-all flex items-center justify-center gap-2 text-gray-700 hover:text-gray-900 font-semibold group">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
</svg>
<span>@T("envConfig.addButton")</span>
</button>
</div>
}
</div>
<!-- 底部按钮 -->
<div class="flex items-center justify-between p-6 border-t border-gray-300 bg-gray-50 rounded-b-2xl">
@if (_editingProfile == null && !_isAddingProfile)
{
<button @onclick="ResetToDefault"
disabled="@_isLoading"
class="px-5 py-2.5 text-sm font-semibold text-amber-700 bg-white border border-amber-300 rounded-xl hover:bg-amber-50 hover:border-amber-500 disabled:opacity-50 disabled:cursor-not-allowed transition-all shadow-sm hover:shadow">
@T("envConfig.resetButton")
</button>
}
else
{
<div></div>
}
<div class="flex gap-3">
<button @onclick="Close"
disabled="@_isLoading"
class="btn-default disabled:opacity-50 disabled:cursor-not-allowed">
@T("common.cancel")
</button>
<button @onclick="Save"
disabled="@_isLoading"
class="btn-primary disabled:opacity-50 disabled:cursor-not-allowed min-w-[100px]">
@if (_isLoading)
{
<div class="flex items-center gap-2">
<div class="animate-spin h-4 w-4 border-2 border-white rounded-full border-t-transparent"></div>
<span>@T("envConfig.saving")</span>
</div>
}
else if (_editingProfile != null || _isAddingProfile)
{
<span>@T("envConfig.saveProfile")</span>
}
else
{
<span>@T("envConfig.save")</span>
}
</button>
</div>
</div>
</div>
</div>
}
</div>
@code {
private bool _isVisible = false;
private bool _isLoading = false;
private CliToolConfig? _selectedTool;
private List<EnvVarItem> _envVars = new();
// 配置方案相关
private List<CliToolEnvProfile> _profiles = new();
private CliToolEnvProfile? _editingProfile = null;
private bool _isAddingProfile = false;
private string _newProfileName = string.Empty;
private CliToolEnvProfile? _profilePendingDelete = null;
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
private class EnvVarItem
{
public string Key { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
}
protected override async Task OnInitializedAsync()
{
await LoadTranslationsAsync();
}
private async Task ReloadEditorForCurrentStateAsync()
{
if (_selectedTool == null)
{
_envVars = new List<EnvVarItem>();
return;
}
if (_profiles.Any(p => p.IsActive))
{
_envVars = new List<EnvVarItem>();
return;
}
var envVars = await CliToolEnvironmentService.GetEnvironmentVariablesAsync(_selectedTool.Id);
_envVars = envVars.Select(kvp => new EnvVarItem { Key = kvp.Key, Value = kvp.Value }).ToList();
}
public async Task ShowAsync(CliToolConfig tool)
{
_selectedTool = tool;
_isVisible = true;
_isLoading = true;
_editingProfile = null;
_isAddingProfile = false;
_newProfileName = string.Empty;
StateHasChanged();
try
{
_currentLanguage = await L.GetCurrentLanguageAsync();
await LoadTranslationsAsync();
// 加载配置方案列表
_profiles = await CliToolEnvironmentService.GetProfilesAsync(tool.Id);
await ReloadEditorForCurrentStateAsync();
}
catch (Exception ex)
{
Console.WriteLine($"加载环境变量失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private void Close()
{
_isVisible = false;
_selectedTool = null;
_envVars.Clear();
_profiles.Clear();
_editingProfile = null;
_isAddingProfile = false;
_newProfileName = string.Empty;
_profilePendingDelete = null;
StateHasChanged();
}
private void OnBackgroundClick()
{
Close();
}
private void AddEnvVar()
{
_envVars.Add(new EnvVarItem());
StateHasChanged();
}
private void RemoveEnvVar(int index)
{
if (index >= 0 && index < _envVars.Count)
{
_envVars.RemoveAt(index);
StateHasChanged();
}
}
private void ClearValue(int index)
{
if (index >= 0 && index < _envVars.Count)
{
_envVars[index].Value = string.Empty;
StateHasChanged();
}
}
// ── 配置方案操作 ──
private void StartAddProfile()
{
_isAddingProfile = true;
_editingProfile = null;
_newProfileName = string.Empty;
_profilePendingDelete = null;
_envVars = new List<EnvVarItem>();
StateHasChanged();
}
private void EditProfile(CliToolEnvProfile profile)
{
_editingProfile = profile;
_isAddingProfile = false;
_newProfileName = profile.ProfileName;
_profilePendingDelete = null;
try
{
if (!string.IsNullOrWhiteSpace(profile.EnvVarsJson))
{
var dict = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(profile.EnvVarsJson) ?? new();
_envVars = dict.Select(kvp => new EnvVarItem { Key = kvp.Key, Value = kvp.Value }).ToList();
}
else
{
_envVars = new List<EnvVarItem>();
}
}
catch
{
_envVars = new List<EnvVarItem>();
}
StateHasChanged();
}
private async Task CancelProfileEdit()
{
_editingProfile = null;
_isAddingProfile = false;
_newProfileName = string.Empty;
_profilePendingDelete = null;
await ReloadEditorForCurrentStateAsync();
StateHasChanged();
}
private async Task ActivateProfile(CliToolEnvProfile profile)
{
if (_selectedTool == null) return;
_isLoading = true;
StateHasChanged();
try
{
await CliToolEnvironmentService.ActivateProfileAsync(_selectedTool.Id, profile.Id);
_profiles = await CliToolEnvironmentService.GetProfilesAsync(_selectedTool.Id);
_editingProfile = null;
_isAddingProfile = false;
_newProfileName = string.Empty;
_profilePendingDelete = null;
await ReloadEditorForCurrentStateAsync();
Console.WriteLine($"方案已激活: {profile.ProfileName}");
}
catch (Exception ex)
{
Console.WriteLine($"激活方案失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private async Task DeactivateProfiles()
{
if (_selectedTool == null) return;
_isLoading = true;
StateHasChanged();
try
{
await CliToolEnvironmentService.DeactivateProfilesAsync(_selectedTool.Id);
_profiles = await CliToolEnvironmentService.GetProfilesAsync(_selectedTool.Id);
_editingProfile = null;
_isAddingProfile = false;
_newProfileName = string.Empty;
_profilePendingDelete = null;
await ReloadEditorForCurrentStateAsync();
Console.WriteLine("已取消所有方案激活");
}
catch (Exception ex)
{
Console.WriteLine($"取消激活失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private void RequestDeleteProfile(CliToolEnvProfile profile)
{
_profilePendingDelete = profile;
StateHasChanged();
}
private void CancelDeleteProfile()
{
_profilePendingDelete = null;
StateHasChanged();
}
private async Task DeleteProfile(CliToolEnvProfile profile)
{
if (_selectedTool == null) return;
_profilePendingDelete = null;
_isLoading = true;
StateHasChanged();
try
{
await CliToolEnvironmentService.DeleteProfileAsync(_selectedTool.Id, profile.Id);
_profiles = await CliToolEnvironmentService.GetProfilesAsync(_selectedTool.Id);
if (_editingProfile?.Id == profile.Id)
{
await CancelProfileEdit();
}
else
{
await ReloadEditorForCurrentStateAsync();
}
Console.WriteLine($"方案已删除: {profile.ProfileName}");
}
catch (Exception ex)
{
Console.WriteLine($"删除方案失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private async Task Save()
{
if (_selectedTool == null) return;
_isLoading = true;
StateHasChanged();
try
{
// 过滤掉空键或空值的条目
var validEnvVars = _envVars
.Where(ev => !string.IsNullOrWhiteSpace(ev.Key) && !string.IsNullOrWhiteSpace(ev.Value))
.ToDictionary(ev => ev.Key.Trim(), ev => ev.Value!.Trim());
if (_isAddingProfile || _editingProfile != null)
{
// 保存配置方案(新建和编辑均使用 _newProfileName,支持重命名)
var profileName = _newProfileName.Trim();
if (string.IsNullOrWhiteSpace(profileName)) profileName = T("envConfig.unnamedProfile");
var profileId = _editingProfile?.Id ?? 0;
var savedProfile = await CliToolEnvironmentService.SaveProfileAsync(_selectedTool.Id, profileId, profileName, validEnvVars);
if (savedProfile != null)
{
_profiles = await CliToolEnvironmentService.GetProfilesAsync(_selectedTool.Id);
await CancelProfileEdit();
Console.WriteLine($"方案已保存: {profileName}");
}
else
{
Console.WriteLine("方案保存失败");
}
}
else
{
// 保存默认配置
var success = await CliToolEnvironmentService.SaveEnvironmentVariablesAsync(_selectedTool.Id, validEnvVars);
if (success)
{
Console.WriteLine("默认环境变量保存成功");
Close();
}
else
{
Console.WriteLine("环境变量保存失败");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"保存失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
private async Task ResetToDefault()
{
if (_selectedTool == null) return;
_isLoading = true;
StateHasChanged();
try
{
var defaultEnvVars = await CliToolEnvironmentService.ResetToDefaultAsync(_selectedTool.Id);
_envVars = defaultEnvVars.Select(kvp => new EnvVarItem { Key = kvp.Key, Value = kvp.Value }).ToList();
Console.WriteLine("已重置为默认配置");
}
catch (Exception ex)
{
Console.WriteLine($"重置失败: {ex.Message}");
}
finally
{
_isLoading = false;
StateHasChanged();
}
}
#region 本地化辅助方法
private async Task LoadTranslationsAsync()
{
try
{
var allTranslations = await L.GetAllTranslationsAsync(_currentLanguage);
_translations = FlattenTranslations(allTranslations);
}
catch (Exception ex)
{
Console.WriteLine($"[环境变量配置] 加载翻译资源失败: {ex.Message}");
}
}
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
}