forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShareSessionModal.razor
More file actions
709 lines (649 loc) · 36.9 KB
/
ShareSessionModal.razor
File metadata and controls
709 lines (649 loc) · 36.9 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
@namespace WebCodeCli.Components
@using System.Text.Json
@using WebCodeCli.Domain.Domain.Model
@using WebCodeCli.Domain.Repositories.Base.SessionShare
@using WebCodeCli.Domain.Domain.Service
@inject HttpClient Http
@inject IJSRuntime JSRuntime
@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="Close"></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-lg 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="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path>
</svg>
@T("share.title")
</h3>
<button @onclick="Close" 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">
<!-- Tab 切换 -->
<div class="flex border-b border-gray-200 mb-4">
<button @onclick='() => _activeTab = "create"'
class="px-4 py-2 text-sm font-medium @(_activeTab == "create" ? "text-gray-900 border-b-2 border-gray-900 -mb-px" : "text-gray-500 hover:text-gray-700")">
@T("share.tabs.create")
</button>
<button @onclick='() => _activeTab = "manage"'
class="px-4 py-2 text-sm font-medium @(_activeTab == "manage" ? "text-gray-900 border-b-2 border-gray-900 -mb-px" : "text-gray-500 hover:text-gray-700")">
@T("share.tabs.manage") (@_existingShares.Count)
</button>
</div>
@if (_activeTab == "create")
{
<!-- 创建分享表单 -->
@if (_createdShare != null)
{
<!-- 分享创建成功 -->
<div class="text-center py-4">
<div class="inline-flex items-center justify-center w-16 h-16 bg-gray-100 rounded-full mb-4">
<svg class="w-8 h-8 text-gray-700" 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>
</div>
<h4 class="text-lg font-semibold text-gray-800 mb-2">@T("share.createSuccessTitle")</h4>
<p class="text-sm text-gray-500 mb-4">@T("share.createSuccessDesc")</p>
<!-- 分享链接 -->
<div class="bg-gray-50 rounded-lg p-3 mb-4">
<div class="flex items-center gap-2">
<input type="text"
value="@_createdShare.ShareUrl"
readonly
class="flex-1 bg-transparent text-sm text-gray-700 font-mono outline-none" />
<button @onclick="CopyShareUrl"
class="p-2 hover:bg-gray-200 rounded-lg transition-colors"
title="@T("share.copyLink")">
@if (_copied)
{
<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="M5 13l4 4L19 7"></path>
</svg>
}
else
{
<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="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
</svg>
}
</button>
</div>
</div>
<!-- 分享信息 -->
<div class="text-left bg-gray-100 rounded-lg p-3 text-sm">
<p class="text-gray-700">
<strong>@T("share.accessPassword"):</strong>@_inputPassword
</p>
@if (_createdShare.ExpiresAt.HasValue)
{
<p class="text-gray-700 mt-1">
<strong>@T("share.expiresAt"):</strong>@_createdShare.ExpiresAt.Value.ToString("yyyy-MM-dd HH:mm")
</p>
}
</div>
<button @onclick="ResetForm" class="mt-4 px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 rounded-lg transition-colors">
@T("share.createNew")
</button>
</div>
}
else
{
<!-- 创建表单 -->
<div class="space-y-4">
<!-- 分享标题 -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">@T("share.form.title")</label>
<input type="text"
@bind="_inputTitle"
placeholder="@T("share.form.titlePlaceholder")"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-gray-900 focus:border-gray-900"
disabled="@_isCreating" />
</div>
<!-- 分享密码 -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">
@T("share.form.password") <span class="text-red-500">*</span>
</label>
<div class="relative">
<input type="@(_showPassword ? "text" : "password")"
@bind="_inputPassword"
placeholder="@T("share.form.passwordPlaceholder")"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-gray-900 focus:border-gray-900 pr-10"
disabled="@_isCreating" />
<button type="button"
@onclick="TogglePasswordVisibility"
class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600">
@if (_showPassword)
{
<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="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"></path>
</svg>
}
else
{
<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="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>
}
</button>
</div>
<p class="mt-1 text-xs text-gray-500">@T("share.form.passwordHint")</p>
</div>
<!-- 过期时间 -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">@T("share.form.expiration")</label>
<select @bind="_expirationOption"
class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-gray-900 focus:border-gray-900"
disabled="@_isCreating">
<option value="never">@T("share.form.expirationOptions.never")</option>
<option value="1h">@T("share.form.expirationOptions.1h")</option>
<option value="24h">@T("share.form.expirationOptions.24h")</option>
<option value="7d">@T("share.form.expirationOptions.7d")</option>
<option value="30d">@T("share.form.expirationOptions.30d")</option>
</select>
</div>
<!-- 错误提示 -->
@if (!string.IsNullOrEmpty(_error))
{
<div class="p-3 bg-red-50 border border-red-200 rounded-lg text-red-600 text-sm">
@_error
</div>
}
<!-- 提交按钮 -->
<button @onclick="CreateShare"
disabled="@(_isCreating || string.IsNullOrWhiteSpace(_inputPassword) || _inputPassword.Length < 4)"
class="w-full py-3 bg-gray-900 text-white rounded-lg font-medium hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed transition-colors flex items-center justify-center gap-2">
@if (_isCreating)
{
<div class="animate-spin rounded-full h-5 w-5 border-b-2 border-white"></div>
<span>@T("share.creating")</span>
}
else
{
<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.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path>
</svg>
<span>@T("share.createLink")</span>
}
</button>
</div>
}
}
else
{
<!-- 管理已有分享 -->
@if (_isLoadingShares)
{
<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 if (_existingShares.Count == 0)
{
<div class="py-8 text-center text-gray-500">
<svg class="w-12 h-12 mx-auto text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.368 2.684 3 3 0 00-5.368-2.684z"></path>
</svg>
<p>@T("share.noShares")</p>
<button @onclick='() => _activeTab = "create"' class="mt-2 text-gray-700 hover:underline text-sm">
@T("share.createFirst")
</button>
</div>
}
else
{
<div class="space-y-3 max-h-80 overflow-y-auto">
@foreach (var share in _existingShares)
{
<div class="border border-gray-200 rounded-lg p-3 @(share.IsActive && !share.IsExpired ? "" : "opacity-60")">
<div class="flex items-start justify-between">
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2 mb-1">
@if (!share.IsActive)
{
<span class="px-2 py-0.5 bg-gray-100 text-gray-600 text-xs rounded">@T("share.status.inactive")</span>
}
else if (share.IsExpired)
{
<span class="px-2 py-0.5 bg-red-100 text-red-600 text-xs rounded">@T("share.status.expired")</span>
}
else
{
<span class="px-2 py-0.5 bg-gray-100 text-gray-700 text-xs rounded">@T("share.status.active")</span>
}
<span class="text-sm font-medium text-gray-800 truncate">
@(string.IsNullOrEmpty(share.Title) ? share.ShareCode : share.Title)
</span>
</div>
<div class="text-xs text-gray-500 space-y-0.5">
<p>@T("share.createdAt"): @share.CreatedAt.ToString("MM-dd HH:mm")</p>
@if (share.ExpiresAt.HasValue)
{
<p>@T("share.expiresAt"): @share.ExpiresAt.Value.ToString("MM-dd HH:mm")</p>
}
<p>@T("share.viewCount"): @share.ViewCount</p>
</div>
</div>
<div class="flex items-center gap-1 ml-2">
@if (share.IsActive && !share.IsExpired)
{
<button @onclick="() => ToggleShareDetails(share.ShareCode)"
class="p-1.5 hover:bg-indigo-100 rounded transition-colors"
title="@T("share.viewLink")">
<svg class="w-4 h-4 text-indigo-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
@if (_expandedShareCode == share.ShareCode)
{
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"></path>
}
else
{
<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>
</button>
<button @onclick="() => RefreshShareSnapshot(share.ShareCode)"
disabled="@(_isRefreshingSnapshot == share.ShareCode)"
class="p-1.5 hover:bg-blue-100 rounded transition-colors disabled:opacity-50"
title="@T("share.refreshSnapshot")">
@if (_isRefreshingSnapshot == share.ShareCode)
{
<div class="w-4 h-4 animate-spin rounded-full border-2 border-blue-500 border-t-transparent"></div>
}
else
{
<svg class="w-4 h-4 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
</svg>
}
</button>
<button @onclick="() => CopyShareUrlByCode(share.ShareUrl)"
class="p-1.5 hover:bg-gray-100 rounded transition-colors"
title="@T("share.copyLink")">
<svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
</svg>
</button>
}
<button @onclick="() => DeleteShare(share.ShareCode)"
class="p-1.5 hover:bg-red-100 rounded transition-colors"
title="@T("common.delete")">
<svg class="w-4 h-4 text-red-500" 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>
<!-- 展开的分享详情 -->
@if (_expandedShareCode == share.ShareCode)
{
<div class="mt-3 pt-3 border-t border-gray-200">
<div class="bg-gray-50 rounded-lg p-3 space-y-2">
<div>
<label class="text-xs font-medium text-gray-500 block mb-1">@T("share.linkLabel")</label>
<div class="flex items-center gap-2 bg-white rounded border border-gray-200 p-2">
<input type="text"
value="@share.ShareUrl"
readonly
class="flex-1 bg-transparent text-xs text-gray-700 font-mono outline-none truncate" />
<button @onclick="() => CopyShareUrlByCode(share.ShareUrl)"
class="p-1.5 hover:bg-gray-100 rounded transition-colors flex-shrink-0"
title="@T("share.copyLink")">
<svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
</svg>
</button>
</div>
</div>
<div class="text-xs text-gray-500 flex items-center gap-1">
<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="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>@T("share.passwordHiddenHint")</span>
</div>
</div>
</div>
}
</div>
}
</div>
}
}
</div>
</div>
</div>
</div>
}
@code {
[Parameter]
public string SessionId { get; set; } = string.Empty;
[Parameter]
public EventCallback OnShareCreated { get; set; }
private bool _isVisible = false;
private string _activeTab = "create";
// 创建分享
private string _inputTitle = string.Empty;
private string _inputPassword = string.Empty;
private bool _showPassword = false;
private string _expirationOption = "never";
private bool _isCreating = false;
private string _error = string.Empty;
private ShareInfoResponse? _createdShare = null;
private bool _copied = false;
// 管理分享
private List<ShareInfoResponse> _existingShares = new();
private bool _isLoadingShares = false;
private string? _isRefreshingSnapshot = null; // 正在刷新的分享码
private string? _expandedShareCode = null; // 当前展开查看详情的分享码
// 会话数据 (从父组件传入)
private string? _sessionTitle;
private string? _toolId;
private string? _workspacePath;
private string? _messagesJson;
private DateTime? _sessionCreatedAt;
private DateTime? _sessionUpdatedAt;
private string? _outputEventsJson;
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
protected override async Task OnInitializedAsync()
{
await LoadTranslationsAsync();
}
public async Task ShowAsync(string sessionId, string? sessionTitle = null, string? toolId = null,
string? workspacePath = null, string? messagesJson = null,
DateTime? sessionCreatedAt = null, DateTime? sessionUpdatedAt = null,
string? outputEventsJson = null)
{
SessionId = sessionId;
_sessionTitle = sessionTitle;
_toolId = toolId;
_workspacePath = workspacePath;
_messagesJson = messagesJson;
_sessionCreatedAt = sessionCreatedAt;
_outputEventsJson = outputEventsJson;
_sessionUpdatedAt = sessionUpdatedAt;
_isVisible = true;
_activeTab = "create";
ResetForm();
// 每次显示时重新加载翻译,确保语言切换后显示正确
await LoadTranslationsAsync();
StateHasChanged();
await LoadExistingShares();
}
public void Close()
{
_isVisible = false;
StateHasChanged();
}
private async Task LoadExistingShares()
{
try
{
_isLoadingShares = true;
StateHasChanged();
var response = await Http.GetAsync($"/api/share/session/{SessionId}");
if (response.IsSuccessStatusCode)
{
_existingShares = await response.Content.ReadFromJsonAsync<List<ShareInfoResponse>>() ?? new();
}
}
catch (Exception ex)
{
Console.WriteLine($"加载分享列表失败: {ex.Message}");
}
finally
{
_isLoadingShares = false;
StateHasChanged();
}
}
private async Task CreateShare()
{
if (string.IsNullOrWhiteSpace(_inputPassword) || _inputPassword.Length < 4)
{
_error = T("share.error.passwordTooShort");
return;
}
try
{
_isCreating = true;
_error = string.Empty;
StateHasChanged();
var expiresAt = GetExpirationDate();
var request = new
{
SessionId = SessionId,
Password = _inputPassword,
Title = string.IsNullOrWhiteSpace(_inputTitle) ? null : _inputTitle,
ExpiresAt = expiresAt,
// 包含会话数据
SessionTitle = _sessionTitle,
ToolId = _toolId,
WorkspacePath = _workspacePath,
MessagesJson = _messagesJson,
SessionCreatedAt = _sessionCreatedAt,
SessionUpdatedAt = _sessionUpdatedAt,
OutputEventsJson = _outputEventsJson
};
var response = await Http.PostAsJsonAsync("/api/share", request);
if (response.IsSuccessStatusCode)
{
_createdShare = await response.Content.ReadFromJsonAsync<ShareInfoResponse>();
await LoadExistingShares();
await OnShareCreated.InvokeAsync();
}
else
{
var error = await response.Content.ReadFromJsonAsync<ErrorResponse>();
_error = error?.Error ?? T("share.error.createFailed");
}
}
catch (Exception ex)
{
_error = $"{T("share.error.createFailedWithMessage", ("message", ex.Message))}";
}
finally
{
_isCreating = false;
StateHasChanged();
}
}
#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;
}
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
private DateTime? GetExpirationDate()
{
return _expirationOption switch
{
"1h" => DateTime.Now.AddHours(1),
"24h" => DateTime.Now.AddHours(24),
"7d" => DateTime.Now.AddDays(7),
"30d" => DateTime.Now.AddDays(30),
_ => null
};
}
private async Task CopyShareUrl()
{
if (_createdShare != null)
{
await JSRuntime.InvokeAsync<bool>("clipboardCopy.copyText", _createdShare.ShareUrl);
_copied = true;
StateHasChanged();
await Task.Delay(2000);
_copied = false;
StateHasChanged();
}
}
private async Task CopyShareUrlByCode(string url)
{
await JSRuntime.InvokeAsync<bool>("clipboardCopy.copyText", url);
}
private async Task DeleteShare(string shareCode)
{
try
{
var response = await Http.DeleteAsync($"/api/share/{shareCode}");
if (response.IsSuccessStatusCode)
{
await LoadExistingShares();
}
}
catch (Exception ex)
{
Console.WriteLine($"删除分享失败: {ex.Message}");
}
}
private async Task RefreshShareSnapshot(string shareCode)
{
if (string.IsNullOrEmpty(_messagesJson) && string.IsNullOrEmpty(_sessionTitle))
{
Console.WriteLine("没有可更新的会话数据");
return;
}
try
{
_isRefreshingSnapshot = shareCode;
StateHasChanged();
var request = new
{
SessionTitle = _sessionTitle,
ToolId = _toolId,
WorkspacePath = _workspacePath,
MessagesJson = _messagesJson,
SessionUpdatedAt = DateTime.Now,
OutputEventsJson = _outputEventsJson
};
var response = await Http.PutAsJsonAsync($"/api/share/{shareCode}/snapshot", request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"分享快照已刷新: {shareCode}");
}
else
{
var error = await response.Content.ReadAsStringAsync();
Console.WriteLine($"刷新分享快照失败: {error}");
}
}
catch (Exception ex)
{
Console.WriteLine($"刷新分享快照失败: {ex.Message}");
}
finally
{
_isRefreshingSnapshot = null;
StateHasChanged();
}
}
private void TogglePasswordVisibility()
{
_showPassword = !_showPassword;
}
private void ToggleShareDetails(string shareCode)
{
if (_expandedShareCode == shareCode)
{
_expandedShareCode = null;
}
else
{
_expandedShareCode = shareCode;
}
}
private void ResetForm()
{
_inputTitle = string.Empty;
_inputPassword = string.Empty;
_expirationOption = "never";
_error = string.Empty;
_createdShare = null;
_copied = false;
_expandedShareCode = null;
}
private class ErrorResponse
{
public string? Error { get; set; }
}
}