-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathcomponents.tsx
More file actions
772 lines (741 loc) · 27.2 KB
/
Copy pathcomponents.tsx
File metadata and controls
772 lines (741 loc) · 27.2 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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
import { useState, type ReactElement, type ReactNode } from "react";
import {
ArrowRight,
BellOff,
Check,
ChevronDown,
CircleCheckBig,
Download,
Globe,
Loader2,
PackageCheck,
RefreshCw,
ShieldAlert,
Timer,
TriangleAlert,
X,
} from "lucide-react";
import { useTranslation } from "react-i18next";
import { cn } from "@App/pkg/utils/cn";
import { formatUnixTime } from "@App/pkg/utils/day_format";
import { Button } from "@App/pages/components/ui/button";
import { Checkbox } from "@App/pages/components/ui/checkbox";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@App/pages/components/ui/collapsible";
import { Progress } from "@App/pages/components/ui/progress";
import { Skeleton } from "@App/pages/components/ui/skeleton";
import { StateScreen } from "@App/pages/components/ui/state-screen";
import { DataPanel } from "@App/pages/components/ui/data-panel";
import { Tooltip, TooltipContent, TooltipTrigger } from "@App/pages/components/ui/tooltip";
import { Popconfirm } from "@App/pages/components/ui/popconfirm";
import type { BatchProgress, RowState, UpdateItem, UpdateRisk } from "./logic";
/** 批量更新视图(桌面/移动共用)所需的数据与回调 */
export interface BatchUpdateViewProps {
updates: UpdateItem[];
ignored: UpdateItem[];
/** 本次检查覆盖的脚本总数(用于空状态文案) */
totalChecked: number;
checktime: number;
checking: boolean;
loading: boolean;
selected: Set<string>;
/** 自动关闭剩余秒数;为 null 表示不再倒计时 */
autoClose: number | null;
/** 倒计时曾存在但已被取消(显式点击或隐式操作都算);用于把药丸切成「已取消」而不是直接消失 */
autoCloseCancelled: boolean;
/** 按 uuid 索引的行内更新状态;不在表内即为初始态 */
rowStates: Record<string, RowState>;
/** 批量操作进度;为 null 表示当前没有批量操作 */
batchProgress: BatchProgress | null;
/** 服务端检查结果已失效,需重新检查更新 */
recordExpired: boolean;
onToggle: (uuid: string) => void;
onToggleAll: () => void;
onUpdate: (item: UpdateItem) => void;
onIgnore: (item: UpdateItem) => void;
onRestore: (item: UpdateItem) => void;
onUpdateSelected: () => void;
onIgnoreSelected: () => void;
onRestoreAll: () => void;
onCheckNow: () => void;
onCancelAutoClose: () => void;
/** 打开单个脚本的更新详情页 */
onOpen: (uuid: string) => void;
/** 打开 options 脚本列表,便于事后核对刚更新了哪些脚本 */
onOpenScriptList: () => void;
}
/** 悬停 tooltip:用于展示过长被截断的内容(脚本名、来源)或附加信息(相似度、新增连接) */
function HoverTip({ content, children }: { content: ReactNode; children: ReactElement }) {
return (
<Tooltip>
<TooltipTrigger asChild>{children}</TooltipTrigger>
<TooltipContent className="max-w-[320px] break-all">{content}</TooltipContent>
</Tooltip>
);
}
/** 脚本图标:有 @icon 时显示图片,失败或缺省时回退为首字母方块 */
export function ScriptAvatar({ name, iconUrl, size = 28 }: { name: string; iconUrl: string; size?: number }) {
const [error, setError] = useState(false);
if (iconUrl && !error) {
return (
<img
src={iconUrl}
alt={name}
onError={() => setError(true)}
className="shrink-0 rounded-md object-cover"
style={{ width: size, height: size }}
/>
);
}
return (
<span
className="flex shrink-0 items-center justify-center rounded-md bg-muted text-xs font-medium text-fg-secondary"
style={{ width: size, height: size }}
>
{name.charAt(0).toUpperCase()}
</span>
);
}
const PILL = "inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-medium whitespace-nowrap";
const RISK_CLASS: Record<UpdateRisk, string> = {
major: "bg-destructive/10 text-destructive",
noticeable: "bg-primary/10 text-primary",
tiny: "bg-success-bg text-success-fg",
};
const RISK_KEY: Record<UpdateRisk, string> = {
major: "codechange_major",
noticeable: "codechange_noticeable",
tiny: "codechange_tiny",
};
export function RiskBadge({ risk, similarity }: { risk: UpdateRisk; similarity: number }) {
const { t } = useTranslation();
return (
<HoverTip content={`${t("install:updatepage.similarity")} ${Math.round(similarity * 100)}%`}>
<span className={cn(PILL, RISK_CLASS[risk], "cursor-default")}>{t(`install:updatepage.${RISK_KEY[risk]}`)}</span>
</HoverTip>
);
}
export function ConnectBadge({ newConnects }: { newConnects: string[] }) {
const { t } = useTranslation();
const content =
newConnects.length > 0
? `${t("install:updatepage.new_connects")}: ${newConnects.join(", ")}`
: t("install:updatepage.tag_new_connect");
return (
<HoverTip content={content}>
<span className={cn(PILL, "bg-warning-bg text-warning-fg cursor-default")}>
<ShieldAlert className="size-3" />
{t("install:updatepage.tag_new_connect")}
</span>
</HoverTip>
);
}
export function StatusBadge({ enabled }: { enabled: boolean }) {
const { t } = useTranslation();
return (
<span className={cn(PILL, enabled ? "bg-success-bg text-success-fg" : "bg-muted text-muted-foreground")}>
{enabled ? t("install:updatepage.enabled") : t("install:updatepage.disabled")}
</span>
);
}
export function VersionDiff({ oldVersion, newVersion }: { oldVersion: string; newVersion: string }) {
return (
<div className="flex items-center gap-1.5 font-mono text-[13px]">
<span className="text-muted-foreground">{`v${oldVersion}`}</span>
<ArrowRight className="size-3.5 shrink-0 text-muted-foreground" />
<span className="font-semibold text-primary">{`v${newVersion}`}</span>
</div>
);
}
export function SourceCell({ source }: { source: string }) {
if (!source) return <span className="text-muted-foreground">{"—"}</span>;
return (
<HoverTip content={source}>
<span className="flex min-w-0 cursor-default items-center gap-1.5 text-[13px] text-fg-secondary">
<Globe className="size-3.5 shrink-0 text-muted-foreground" />
<span className="truncate">{source}</span>
</span>
</HoverTip>
);
}
/** 可点击跳转更新详情页的脚本名(过长时 tooltip 显示全名) */
export function ScriptName({ name, onClick }: { name: string; onClick: () => void }) {
return (
<HoverTip content={name}>
<button
type="button"
onClick={onClick}
className="min-w-0 flex-1 truncate text-left text-sm font-medium text-foreground hover:text-primary hover:underline"
>
{name}
</button>
</HoverTip>
);
}
/** 行内文字按钮(更新 / 忽略 / 恢复) */
function LinkAction({ label, onClick, muted }: { label: string; onClick: () => void; muted?: boolean }) {
return (
<button
type="button"
onClick={onClick}
className={cn(
"text-[13px] font-medium hover:underline",
muted ? "text-muted-foreground font-normal" : "text-primary"
)}
>
{label}
</button>
);
}
/**
* 行内更新状态区:进行中/成功/失败时接管操作区,初始态才让位给常规操作按钮。
* 桌面与移动共用,保证两套视图的状态语义完全一致。
*/
export function RowStatus({
item,
state,
onRetry,
children,
}: {
item: UpdateItem;
state: RowState | undefined;
onRetry: () => void;
children: ReactNode;
}) {
const { t } = useTranslation();
const testId = `row-status-${item.uuid}`;
const wrap = (phase: string, content: ReactNode) => (
<span data-testid={testId} data-phase={phase} className="flex items-center justify-end gap-1.5 whitespace-nowrap">
{content}
</span>
);
switch (state?.phase) {
case "queued":
return wrap(
"queued",
<span className="text-[13px] text-muted-foreground">{t("install:updatepage.row_queued")}</span>
);
case "working":
return wrap(
"working",
<>
<Loader2 className="size-3.5 animate-spin text-primary" />
<span className="text-[13px] font-medium text-primary">{t("install:updatepage.row_updating")}</span>
</>
);
case "success":
case "exiting":
return wrap(
state.phase,
<>
<Check className="size-3.5 text-success-fg animate-in zoom-in-50 duration-300 ease-out" />
<span className="text-[13px] font-medium text-success-fg">
{t("install:updatepage.row_updated", { version: item.newVersion })}
</span>
</>
);
case "fail":
return wrap(
"fail",
<>
<HoverTip content={state.error || t("install:updatepage.unknown_error")}>
<span className="cursor-default text-[13px] font-medium text-destructive">
{t("install:updatepage.row_failed")}
</span>
</HoverTip>
<span className="text-muted-foreground">{"·"}</span>
<button
type="button"
data-testid={`row-retry-${item.uuid}`}
onClick={onRetry}
className="text-[13px] font-medium text-primary hover:underline"
>
{t("install:updatepage.retry")}
</button>
</>
);
default:
return wrap("idle", children);
}
}
/** 行底 2px 扫光条:更新中的持续信号,复用全局不确定进度条动画 */
export function RowWorkingBar() {
const { t } = useTranslation();
return (
<Progress
variant="top"
indeterminate
className="absolute inset-x-0 bottom-0"
aria-label={t("install:updatepage.row_updating")}
/>
);
}
/** 行容器随状态变化的底色与退场动画 */
export function rowPhaseClass(state: RowState | undefined): string {
switch (state?.phase) {
case "queued":
case "working":
// overflow-hidden:把行底扫光条裁进圆角(移动端卡片是圆角承载面)
return "bg-primary/5 overflow-hidden";
case "success":
return "bg-success-bg/70";
case "exiting":
return "bg-success-bg/70 overflow-hidden translate-x-6 transition-transform duration-200 ease-out animate-collapse-bar";
case "fail":
return "bg-destructive/10";
default:
return "";
}
}
/** 已忽略分组的「全部恢复并更新」:批量安装且不可撤销,必须先确认后果 */
export function RestoreAllAction({ view, className }: { view: BatchUpdateViewProps; className?: string }) {
const { t } = useTranslation();
const connectCount = view.ignored.filter((item) => item.withNewConnect).length;
const description = [
t("install:updatepage.restore_all_confirm", { count: view.ignored.length }),
connectCount > 0 ? t("install:updatepage.restore_all_confirm_connect", { count: connectCount }) : "",
]
.filter(Boolean)
.join(" ");
return (
<Popconfirm description={description} onConfirm={view.onRestoreAll} side="bottom" align="end">
<Button
variant="link"
size="sm"
data-testid="ignored-restore-all"
className={cn("h-auto p-0 text-[13px]", className)}
>
{t("install:updatepage.restore_all")}
</Button>
</Popconfirm>
);
}
/** 批量进度与收尾汇总:确定性进度条 + 一条汇总文案 + 事后核对入口 */
export function BatchSummary({
progress,
onOpenScriptList,
className,
}: {
progress: BatchProgress;
onOpenScriptList: () => void;
className?: string;
}) {
const { t } = useTranslation();
const { done, total, failed, finished } = progress;
const updated = done - failed;
const succeeded = finished && failed === 0;
const text = !finished
? t("install:updatepage.batch_progress", { done, total })
: failed > 0
? t("install:updatepage.batch_done_partial", { updated, failed })
: t("install:updatepage.batch_done", { count: updated });
return (
<div data-testid="batch-summary" className="shrink-0 border-b border-border bg-card">
<Progress
variant="top"
value={done}
max={total}
aria-label={text}
indicatorClassName={succeeded ? "bg-success" : failed > 0 ? "bg-warning" : undefined}
/>
<div className={cn("flex items-center justify-between gap-3 py-1.5", className)}>
<span
className={cn(
"truncate text-[13px]",
succeeded ? "text-success-fg" : failed > 0 && finished ? "text-warning-fg" : "text-fg-secondary"
)}
>
{text}
</span>
{finished && updated > 0 && (
<button
type="button"
data-testid="batch-open-scripts"
onClick={onOpenScriptList}
className="shrink-0 text-[13px] font-medium text-primary hover:underline"
>
{t("install:updatepage.view_updated_scripts")}
</button>
)}
</div>
</div>
);
}
/** 检查结果已随 Service Worker 回收失效:点更新不会有任何效果,必须显式告知 */
export function RecordExpiredNotice({ className }: { className?: string }) {
const { t } = useTranslation();
return (
<div
data-testid="record-expired"
className={cn(
"flex shrink-0 items-center gap-2 border-b border-border bg-warning-bg py-2 text-[13px] text-warning-fg",
className
)}
>
<TriangleAlert className="size-4 shrink-0" />
<span>{t("install:updatepage.record_expired")}</span>
</div>
);
}
const COL = {
version: "w-[170px] shrink-0",
change: "w-[230px] shrink-0",
source: "w-[160px] shrink-0",
// 需容纳最宽的行内状态(「更新失败 · 重试」/ 各语言的「已更新 vX.Y.Z」),不能按初始态的两个按钮取宽
action: "w-[180px] shrink-0",
};
/** 桌面端单行(待更新或已忽略) */
function DesktopRow({
item,
state,
selected,
onToggle,
onOpen,
onUpdate,
onIgnore,
onRestore,
ignoredRow,
}: {
item: UpdateItem;
state?: RowState;
selected?: boolean;
onToggle?: (uuid: string) => void;
onOpen: (uuid: string) => void;
onUpdate?: (item: UpdateItem) => void;
onIgnore?: (item: UpdateItem) => void;
onRestore?: (item: UpdateItem) => void;
ignoredRow?: boolean;
}) {
const { t } = useTranslation();
const dim = item.enabled ? "" : "opacity-55";
const primaryAction = () => (ignoredRow ? onRestore?.(item) : onUpdate?.(item));
return (
<div
className={cn(
"relative flex h-14 items-center px-4 border-b border-border last:border-b-0 hover:bg-accent/40 transition-colors",
rowPhaseClass(state)
)}
>
<div className="flex w-9 shrink-0 items-center">
{ignoredRow ? (
<BellOff className="size-3.5 text-muted-foreground" />
) : (
<Checkbox checked={!!selected} onCheckedChange={() => onToggle?.(item.uuid)} />
)}
</div>
<div className={cn("flex flex-1 items-center gap-2.5 min-w-0", dim)}>
<ScriptAvatar name={item.name} iconUrl={item.iconUrl} />
<ScriptName name={item.name} onClick={() => onOpen(item.uuid)} />
<StatusBadge enabled={item.enabled} />
</div>
<div className={cn(COL.version, dim)}>
<VersionDiff oldVersion={item.oldVersion} newVersion={item.newVersion} />
</div>
<div className={cn(COL.change, "flex items-center gap-1.5 flex-wrap", dim)}>
<RiskBadge risk={item.risk} similarity={item.similarity} />
{item.withNewConnect && <ConnectBadge newConnects={item.newConnects} />}
</div>
<div className={cn(COL.source, dim)}>
<SourceCell source={item.source} />
</div>
<div className={cn(COL.action, "flex items-center justify-end gap-2")}>
<RowStatus item={item} state={state} onRetry={primaryAction}>
{ignoredRow ? (
<LinkAction label={t("install:updatepage.restore")} onClick={() => onRestore?.(item)} />
) : (
<>
<LinkAction label={t("install:updatepage.update")} onClick={() => onUpdate?.(item)} />
<span className="h-3 w-px bg-border" />
<LinkAction label={t("install:updatepage.ignore")} onClick={() => onIgnore?.(item)} muted />
</>
)}
</RowStatus>
</div>
{state?.phase === "working" && <RowWorkingBar />}
</div>
);
}
function DesktopTable({ view }: { view: BatchUpdateViewProps }) {
const { t } = useTranslation();
return (
<DataPanel>
<div className="flex h-10 items-center px-4 border-b border-border bg-muted/40 text-xs font-medium text-muted-foreground">
<div className="w-9 shrink-0" />
<div className="flex-1">{t("install:updatepage.col_script")}</div>
<div className={COL.version}>{t("install:updatepage.col_version")}</div>
<div className={COL.change}>{t("install:updatepage.col_change")}</div>
<div className={COL.source}>{t("install:updatepage.col_source")}</div>
<div className={cn(COL.action, "text-right")}>{t("install:updatepage.col_action")}</div>
</div>
{view.updates.map((item) => (
<DesktopRow
key={item.uuid}
item={item}
state={view.rowStates[item.uuid]}
selected={view.selected.has(item.uuid)}
onToggle={view.onToggle}
onOpen={view.onOpen}
onUpdate={view.onUpdate}
onIgnore={view.onIgnore}
/>
))}
</DataPanel>
);
}
function DesktopIgnored({ view }: { view: BatchUpdateViewProps }) {
const { t } = useTranslation();
return (
<Collapsible defaultOpen>
<DataPanel>
<div className="flex h-12 items-center justify-between px-4 border-b border-border">
<CollapsibleTrigger className="group flex items-center gap-2">
<ChevronDown className="size-[18px] text-fg-secondary transition-transform group-data-[state=closed]:-rotate-90" />
<span className="text-sm font-medium text-foreground">{t("install:updatepage.ignored_section")}</span>
<span className="rounded-full bg-muted px-2 py-0.5 text-xs font-medium text-muted-foreground">
{view.ignored.length}
</span>
</CollapsibleTrigger>
<RestoreAllAction view={view} />
</div>
<CollapsibleContent>
{view.ignored.map((item) => (
<DesktopRow
key={item.uuid}
item={item}
state={view.rowStates[item.uuid]}
ignoredRow
onOpen={view.onOpen}
onRestore={view.onRestore}
/>
))}
</CollapsibleContent>
</DataPanel>
</Collapsible>
);
}
function DesktopToolbar({ view }: { view: BatchUpdateViewProps }) {
const { t } = useTranslation();
const selectedCount = view.updates.filter((u) => view.selected.has(u.uuid)).length;
const allSelected = view.updates.length > 0 && selectedCount === view.updates.length;
return (
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<Checkbox checked={allSelected} onCheckedChange={view.onToggleAll} />
<span className="text-sm font-medium text-foreground">
{t("install:updatepage.selected_count", { selected: selectedCount, total: view.updates.length })}
</span>
{view.ignored.length > 0 && (
<>
<span className="text-muted-foreground">{"·"}</span>
<span className="text-[13px] text-muted-foreground">
{t("install:updatepage.ignored_count", { count: view.ignored.length })}
</span>
</>
)}
</div>
<div className="flex items-center gap-2.5">
<Button variant="outline" size="sm" disabled={selectedCount === 0} onClick={view.onIgnoreSelected}>
<BellOff />
{t("install:updatepage.ignore_selected")}
</Button>
<Button size="sm" disabled={selectedCount === 0} onClick={view.onUpdateSelected}>
<Download />
{t("install:updatepage.update_selected", { count: selectedCount })}
</Button>
</div>
</div>
);
}
/** 顶部不确定进度条:检查更新进行中时的即时反馈信号(贴在 header 下方,不随内容滚动) */
export function TopProgressBar() {
const { t } = useTranslation();
return <Progress variant="top" indeterminate aria-label={t("install:updatepage.status_checking_updates")} />;
}
/** 骨架占位灰条 */
export function SkeletonBar({ className }: { className?: string }) {
return <Skeleton className={className} />;
}
/** 桌面端检查中的骨架表格:保留表头 + 占位行,取代冻结的空状态/大转圈 */
function SkeletonTable() {
const { t } = useTranslation();
return (
<DataPanel data-testid="update-skeleton">
<div className="flex h-10 items-center px-4 border-b border-border bg-muted/40 text-xs font-medium text-muted-foreground">
<div className="w-9 shrink-0" />
<div className="flex-1">{t("install:updatepage.col_script")}</div>
<div className={COL.version}>{t("install:updatepage.col_version")}</div>
<div className={COL.change}>{t("install:updatepage.col_change")}</div>
<div className={COL.source}>{t("install:updatepage.col_source")}</div>
<div className={cn(COL.action, "text-right")}>{t("install:updatepage.col_action")}</div>
</div>
{Array.from({ length: 4 }).map((_, i) => (
<div key={i} className="flex h-14 items-center px-4 border-b border-border last:border-b-0">
<div className="w-9 shrink-0">
<SkeletonBar className="size-4 rounded-md" />
</div>
<div className="flex flex-1 items-center gap-2.5 min-w-0">
<SkeletonBar className="size-7 shrink-0 rounded-md" />
<SkeletonBar className="h-4 w-40 max-w-[55%]" />
</div>
<div className={COL.version}>
<SkeletonBar className="h-4 w-24" />
</div>
<div className={COL.change}>
<SkeletonBar className="h-5 w-20 rounded-full" />
</div>
<div className={COL.source}>
<SkeletonBar className="h-4 w-16" />
</div>
<div className={cn(COL.action, "flex justify-end")}>
<SkeletonBar className="h-4 w-12" />
</div>
</div>
))}
</DataPanel>
);
}
/** 空状态:所有脚本均为最新 */
export function EmptyState({ totalChecked, onCheckNow }: { totalChecked: number; onCheckNow: () => void }) {
const { t } = useTranslation();
return (
<StateScreen
data-testid="update-empty"
icon={CircleCheckBig}
tone="primary"
compact
className="py-24"
title={t("install:updatepage.empty_title")}
description={t("install:updatepage.empty_desc", { count: totalChecked })}
action={
<Button data-testid="empty-recheck" onClick={onCheckNow}>
<RefreshCw />
{t("install:updatepage.recheck")}
</Button>
}
/>
);
}
/** 顶部状态/自动关闭信息条 */
function HeaderStatus({ view }: { view: BatchUpdateViewProps }) {
const { t } = useTranslation();
const text = view.checking
? t("install:updatepage.status_checking_updates")
: view.checktime
? t("install:updatepage.last_check", { time: formatUnixTime(Math.floor(view.checktime / 1000)) })
: "";
if (!text) return null;
return (
<>
<span className="text-muted-foreground">{"·"}</span>
<span className="truncate text-[13px] text-muted-foreground">{text}</span>
</>
);
}
const CHIP = "inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-xs";
/**
* 自动关闭倒计时小药丸:可点击刹车。
* 倒计时结束前的最后几秒转警示色并轻微脉冲,避免"一晃神页面没了";
* 隐式取消(勾选/更新/忽略等)也会走到已取消态,不让取消这件事无声发生。
*/
export function AutoCloseChip({
seconds,
cancelled,
onCancel,
}: {
seconds: number | null;
cancelled: boolean;
onCancel: () => void;
}) {
const { t } = useTranslation();
if (seconds === null) {
if (!cancelled) return null;
return (
<span data-testid="auto-close-chip" data-state="cancelled" className={cn(CHIP, "bg-muted text-muted-foreground")}>
<Timer className="size-3.5" />
{t("install:updatepage.auto_close_cancelled")}
</span>
);
}
const urgent = seconds <= 3;
return (
<button
type="button"
data-testid="auto-close-chip"
data-state="counting"
onClick={onCancel}
className={cn(
CHIP,
"group hover:bg-accent hover:text-accent-foreground",
urgent ? "bg-warning-bg text-warning-fg animate-pulse" : "bg-muted text-muted-foreground"
)}
>
<Timer className="size-3.5" />
<span className="group-hover:hidden">{t("install:updatepage.auto_close", { count: seconds })}</span>
<span className="hidden group-hover:inline">{t("install:updatepage.auto_close_cancel_hint")}</span>
</button>
);
}
/** 桌面端整页视图 */
export function DesktopView({ view }: { view: BatchUpdateViewProps }) {
const { t } = useTranslation();
const empty = view.updates.length === 0 && view.ignored.length === 0;
return (
<div className="flex h-dvh flex-col bg-background text-foreground">
<header className="flex h-[60px] shrink-0 items-center justify-between border-b border-border bg-card px-6">
<div className="flex min-w-0 items-center gap-3">
<PackageCheck className="size-[22px] shrink-0 text-primary" />
<h1 className="text-lg font-semibold text-foreground">{t("install:updatepage.title")}</h1>
<HeaderStatus view={view} />
</div>
<div className="flex shrink-0 items-center gap-3">
<Button
variant={view.recordExpired ? "default" : "outline"}
size="sm"
disabled={view.checking}
onClick={view.onCheckNow}
>
<RefreshCw className={cn(view.checking && "animate-spin")} />
{t("install:updatepage.main_header")}
</Button>
<AutoCloseChip
seconds={view.autoClose}
cancelled={view.autoCloseCancelled}
onCancel={view.onCancelAutoClose}
/>
<Button
variant="ghost"
size="icon-sm"
className="text-fg-secondary"
aria-label={t("common:close")}
onClick={() => window.close()}
>
<X />
</Button>
</div>
</header>
{view.checking && <TopProgressBar />}
{view.recordExpired && <RecordExpiredNotice className="px-6" />}
{view.batchProgress && (
<BatchSummary progress={view.batchProgress} onOpenScriptList={view.onOpenScriptList} className="px-6" />
)}
<div className="flex-1 overflow-auto scrollbar-custom">
<div className="mx-auto flex w-full max-w-[1100px] flex-col gap-4 px-6 py-6">
{view.loading || (view.checking && empty) ? (
<SkeletonTable />
) : empty ? (
<EmptyState totalChecked={view.totalChecked} onCheckNow={view.onCheckNow} />
) : (
<>
{view.updates.length > 0 && (
<>
<DesktopToolbar view={view} />
<DesktopTable view={view} />
</>
)}
{view.ignored.length > 0 && <DesktopIgnored view={view} />}
</>
)}
</div>
</div>
</div>
);
}