-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFileMonitorPanel.razor
More file actions
500 lines (451 loc) · 21.4 KB
/
FileMonitorPanel.razor
File metadata and controls
500 lines (451 loc) · 21.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
@using WebCodeCli.Domain.Domain.Model
@using WebCodeCli.Domain.Domain.Service
@using System.Text.Json
@implements IDisposable
@inject IFileMonitorService FileMonitorService
@inject ICliExecutorService CliExecutorService
@inject ILocalizationService L
<div class="h-full flex flex-col bg-white">
<!-- 头部:控制栏 -->
<div class="p-3 sm:p-4 border-b border-gray-200 flex-shrink-0">
<div class="flex items-center justify-between gap-2 mb-3">
<div class="flex items-center gap-2">
<button @onclick="ToggleMonitoring"
class="@(_isMonitoring ? "btn-default" : "btn-primary") px-4 py-2 text-sm flex items-center gap-2">
@if (_isMonitoring)
{
<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="M10 9v6m4-6v6m7-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>@T("fileMonitor.stopMonitoring")</span>
}
else
{
<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="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>@T("fileMonitor.startMonitoring")</span>
}
</button>
@if (_isMonitoring)
{
<span class="flex items-center gap-1.5 text-sm text-green-600">
<span class="w-2 h-2 bg-green-500 rounded-full animate-pulse"></span>
<span class="font-semibold">@T("fileMonitor.monitoring")</span>
</span>
}
</div>
<div class="flex items-center gap-2">
<!-- 过滤器 -->
<select @bind="_filterType"
@bind:event="oninput"
class="px-3 py-2 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-gray-500">
<option value="all">@T("fileMonitor.filterAll")</option>
<option value="Created">@T("fileMonitor.filterCreated")</option>
<option value="Modified">@T("fileMonitor.filterModified")</option>
<option value="Deleted">@T("fileMonitor.filterDeleted")</option>
<option value="Renamed">@T("fileMonitor.filterRenamed")</option>
</select>
<button @onclick="ClearHistory"
class="btn-default px-4 py-2 text-sm flex items-center gap-2">
<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>
@T("common.clear")
</button>
</div>
</div>
<!-- 统计信息 -->
@if (_changes.Any())
{
<div class="grid grid-cols-2 sm:grid-cols-5 gap-2 mt-3">
<div class="bg-gray-50 rounded-lg p-2 border border-gray-200">
<div class="text-xs text-gray-500">@T("fileMonitor.statsTotal")</div>
<div class="text-lg font-bold text-gray-800">@_changes.Count</div>
</div>
<div class="bg-green-50 rounded-lg p-2 border border-green-200">
<div class="text-xs text-green-600">@T("fileMonitor.statsCreated")</div>
<div class="text-lg font-bold text-green-700">@_changes.Count(c => c.ChangeType == FileChangeType.Created)</div>
</div>
<div class="bg-blue-50 rounded-lg p-2 border border-blue-200">
<div class="text-xs text-blue-600">@T("fileMonitor.statsModified")</div>
<div class="text-lg font-bold text-blue-700">@_changes.Count(c => c.ChangeType == FileChangeType.Modified)</div>
</div>
<div class="bg-red-50 rounded-lg p-2 border border-red-200">
<div class="text-xs text-red-600">@T("fileMonitor.statsDeleted")</div>
<div class="text-lg font-bold text-red-700">@_changes.Count(c => c.ChangeType == FileChangeType.Deleted)</div>
</div>
<div class="bg-yellow-50 rounded-lg p-2 border border-yellow-200">
<div class="text-xs text-yellow-600">@T("fileMonitor.statsRenamed")</div>
<div class="text-lg font-bold text-yellow-700">@_changes.Count(c => c.ChangeType == FileChangeType.Renamed)</div>
</div>
</div>
}
</div>
<!-- 内容区域:事件流 -->
<div class="flex-1 overflow-y-auto p-4">
@if (!_changes.Any())
{
<!-- 无事件提示 -->
<div class="flex flex-col items-center justify-center h-full text-gray-400">
<div class="w-20 h-20 bg-gray-100 rounded-full flex items-center justify-center mb-4">
<svg class="w-10 h-10 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
</svg>
</div>
<p class="text-base font-semibold text-gray-600">@T("fileMonitor.noChangesTitle")</p>
<p class="text-sm mt-2 text-gray-500">@(_isMonitoring ? T("fileMonitor.noChangesMonitoring") : T("fileMonitor.noChangesNotMonitoring"))</p>
</div>
}
else
{
<!-- 事件列表 -->
<div class="space-y-2">
@foreach (var change in FilteredChanges)
{
<div class="border border-gray-200 rounded-lg p-3 hover:shadow-md transition-shadow bg-white">
<div class="flex items-start gap-3">
<!-- 类型图标 -->
<div class="flex-shrink-0 w-10 h-10 @GetChangeTypeColor(change.ChangeType) rounded-full flex items-center justify-center">
@GetChangeTypeIcon(change.ChangeType)
</div>
<!-- 变更信息 -->
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2 mb-1 flex-wrap">
<span class="@GetChangeTypeBadgeClass(change.ChangeType) text-xs font-semibold px-2 py-0.5 rounded-full">
@GetChangeTypeLabel(change.ChangeType)
</span>
<span class="text-xs text-gray-500">@FormatTime(change.Timestamp)</span>
@if (change.IsDirectory)
{
<span class="bg-purple-100 text-purple-700 text-xs font-semibold px-2 py-0.5 rounded-full">@T("fileMonitor.folderTag")</span>
}
else if (!string.IsNullOrEmpty(change.FileExtension))
{
<span class="bg-gray-100 text-gray-700 text-xs font-mono px-2 py-0.5 rounded-full">@change.FileExtension</span>
}
</div>
<div class="text-sm font-mono text-gray-800 break-all">
@if (change.ChangeType == FileChangeType.Renamed && !string.IsNullOrEmpty(change.OldFilePath))
{
<div class="space-y-1">
<div class="text-red-600">- @GetRelativePath(change.OldFilePath)</div>
<div class="text-green-600">+ @GetRelativePath(change.FilePath)</div>
</div>
}
else
{
<div>@GetRelativePath(change.FilePath)</div>
}
</div>
<!-- 详细信息 -->
<div class="mt-2 space-y-1">
@if (change.FileSize.HasValue)
{
<div class="text-xs text-gray-500">
<span class="font-semibold">@T("fileMonitor.size"):</span> @FormatFileSize(change.FileSize.Value)
</div>
}
@if (change.LastModified.HasValue)
{
<div class="text-xs text-gray-500">
<span class="font-semibold">@T("fileMonitor.lastModified"):</span> @change.LastModified.Value.ToString("yyyy-MM-dd HH:mm:ss")
</div>
}
@if (change.CreatedTime.HasValue && change.ChangeType == FileChangeType.Created)
{
<div class="text-xs text-gray-500">
<span class="font-semibold">@T("fileMonitor.createdTime"):</span> @change.CreatedTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
</div>
}
@if (!string.IsNullOrEmpty(change.ContentPreview))
{
<details class="mt-2">
<summary class="text-xs text-blue-600 cursor-pointer hover:text-blue-700 font-semibold">@T("fileMonitor.previewContent")</summary>
<pre class="mt-1 text-[10px] bg-gray-50 p-2 rounded border border-gray-200 overflow-x-auto">@change.ContentPreview</pre>
</details>
}
</div>
</div>
</div>
</div>
}
</div>
}
</div>
</div>
@code {
[Parameter]
public string SessionId { get; set; } = string.Empty;
private List<FileChangeEvent> _changes = new();
private bool _isMonitoring = false;
private string _filterType = "all";
private System.Threading.Timer? _refreshTimer;
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
protected override async Task OnInitializedAsync()
{
await LoadTranslationsAsync();
// 订阅文件变更事件
FileMonitorService.OnFileChanged += OnFileChanged;
// 启动定时刷新(每秒刷新一次)
_refreshTimer = new System.Threading.Timer(async _ =>
{
await LoadChanges();
await SyncMonitoringState();
}, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
protected override void OnParametersSet()
{
base.OnParametersSet();
// 从服务获取真实的监控状态(在参数设置后)
if (!string.IsNullOrEmpty(SessionId))
{
_isMonitoring = FileMonitorService.IsMonitoring(SessionId);
}
}
private async Task LoadChanges()
{
try
{
if (string.IsNullOrEmpty(SessionId))
{
return;
}
_changes = await Task.Run(() => FileMonitorService.GetRecentChanges(SessionId, 100));
await InvokeAsync(StateHasChanged);
}
catch (Exception ex)
{
Console.WriteLine($"加载文件变更记录失败: {ex.Message}");
}
}
private async Task SyncMonitoringState()
{
try
{
if (string.IsNullOrEmpty(SessionId))
{
return;
}
var actualState = FileMonitorService.IsMonitoring(SessionId);
if (_isMonitoring != actualState)
{
_isMonitoring = actualState;
await InvokeAsync(StateHasChanged);
}
}
catch (Exception ex)
{
Console.WriteLine($"同步监控状态失败: {ex.Message}");
}
}
private void OnFileChanged(object? sender, FileChangeEvent e)
{
if (e.SessionId == SessionId)
{
InvokeAsync(async () =>
{
await LoadChanges();
});
}
}
private void ToggleMonitoring()
{
try
{
if (_isMonitoring)
{
FileMonitorService.StopMonitoring(SessionId);
_isMonitoring = false;
}
else
{
var workspacePath = CliExecutorService.GetSessionWorkspacePath(SessionId);
FileMonitorService.StartMonitoring(SessionId, workspacePath);
_isMonitoring = true;
}
StateHasChanged();
}
catch (Exception ex)
{
Console.WriteLine($"切换监控状态失败: {ex.Message}");
}
}
private void ClearHistory()
{
FileMonitorService.ClearHistory(SessionId);
_changes.Clear();
StateHasChanged();
}
private IEnumerable<FileChangeEvent> FilteredChanges
{
get
{
if (_filterType == "all")
{
return _changes;
}
if (Enum.TryParse<FileChangeType>(_filterType, out var type))
{
return _changes.Where(c => c.ChangeType == type);
}
return _changes;
}
}
private string GetRelativePath(string fullPath)
{
try
{
var workspacePath = CliExecutorService.GetSessionWorkspacePath(SessionId);
return Path.GetRelativePath(workspacePath, fullPath);
}
catch
{
return fullPath;
}
}
private string GetChangeTypeColor(FileChangeType type)
{
return type switch
{
FileChangeType.Created => "bg-green-100 text-green-600",
FileChangeType.Modified => "bg-blue-100 text-blue-600",
FileChangeType.Deleted => "bg-red-100 text-red-600",
FileChangeType.Renamed => "bg-yellow-100 text-yellow-600",
_ => "bg-gray-100 text-gray-600"
};
}
private string GetChangeTypeBadgeClass(FileChangeType type)
{
return type switch
{
FileChangeType.Created => "bg-green-100 text-green-700",
FileChangeType.Modified => "bg-blue-100 text-blue-700",
FileChangeType.Deleted => "bg-red-100 text-red-700",
FileChangeType.Renamed => "bg-yellow-100 text-yellow-700",
_ => "bg-gray-100 text-gray-700"
};
}
private string GetChangeTypeLabel(FileChangeType type)
{
return type switch
{
FileChangeType.Created => T("fileMonitor.changeCreated"),
FileChangeType.Modified => T("fileMonitor.changeModified"),
FileChangeType.Deleted => T("fileMonitor.changeDeleted"),
FileChangeType.Renamed => T("fileMonitor.changeRenamed"),
_ => type.ToString()
};
}
private MarkupString GetChangeTypeIcon(FileChangeType type)
{
var icon = type switch
{
FileChangeType.Created => "<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 4v16m8-8H4\"></path></svg>",
FileChangeType.Modified => "<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=\"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>",
FileChangeType.Deleted => "<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=\"M20 12H4\"></path></svg>",
FileChangeType.Renamed => "<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=\"M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4\"></path></svg>",
_ => "<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=\"M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z\"></path></svg>"
};
return new MarkupString(icon);
}
#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}");
}
}
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;
}
#endregion
private string FormatTime(DateTime time)
{
var diff = DateTime.Now - time;
if (diff.TotalSeconds < 10)
return "刚刚";
if (diff.TotalSeconds < 60)
return $"{(int)diff.TotalSeconds} 秒前";
if (diff.TotalMinutes < 60)
return $"{(int)diff.TotalMinutes} 分钟前";
if (diff.TotalHours < 24)
return $"{(int)diff.TotalHours} 小时前";
return time.ToString("MM-dd HH:mm:ss");
}
private string FormatFileSize(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len = len / 1024;
}
return $"{len:0.##} {sizes[order]}";
}
public void Dispose()
{
FileMonitorService.OnFileChanged -= OnFileChanged;
_refreshTimer?.Dispose();
// 注意:不在这里停止监控服务
// 监控服务是全局的,应该由用户手动控制启动/停止
// 组件销毁不应该影响监控服务的运行状态
}
}