@vetta-org/theme-ui 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/package.json +8 -16
  2. package/src/action-approval/StorageCleanListView.test.tsx +36 -0
  3. package/src/action-approval/StorageCleanListView.tsx +52 -0
  4. package/src/action-approval/index.ts +2 -0
  5. package/src/activity/ActivityPanel.tsx +7 -1
  6. package/src/activity/ActivityPanelFrame.tsx +1 -1
  7. package/src/activity/BackgroundTasksTabPanelView.tsx +18 -2
  8. package/src/activity/BrowserPanelView.tsx +15 -1
  9. package/src/activity/BrowserSelectionView.tsx +199 -0
  10. package/src/activity/CodePreview.tsx +15 -1
  11. package/src/activity/MarkdownPreviewView.test.tsx +53 -0
  12. package/src/activity/MarkdownPreviewView.tsx +86 -10
  13. package/src/activity/index.ts +1 -0
  14. package/src/chat/ContextRingView.test.tsx +81 -0
  15. package/src/chat/ContextRingView.tsx +78 -62
  16. package/src/chat/EditDiffView.tsx +4 -4
  17. package/src/chat/HeaderActionBarView.tsx +135 -48
  18. package/src/chat/HeaderActionOverflowMenu.tsx +11 -20
  19. package/src/chat/MessageFeedLayoutView.tsx +6 -5
  20. package/src/chat/MessageInputView.tsx +5 -1
  21. package/src/chat/index.ts +2 -1
  22. package/src/chat/message-feed-fade.css +80 -0
  23. package/src/chat/message-feed-fade.test.tsx +75 -0
  24. package/src/chat/streaming-reveal.test.ts +1 -74
  25. package/src/chat/streaming-reveal.ts +0 -3
  26. package/src/file-preview/FilePreviewDialogView.tsx +2 -1
  27. package/src/file-preview/ReadonlyCodeView.tsx +46 -0
  28. package/src/file-preview/syntax-tokens.css +1 -1
  29. package/src/image-assets.d.ts +4 -0
  30. package/src/layout/ResizeHandle.tsx +4 -1
  31. package/src/layout/resize-handle-reset.test.tsx +50 -0
  32. package/src/markdown/FileLinkBadge.tsx +77 -0
  33. package/src/markdown/MarkdownContent.tsx +61 -31
  34. package/src/markdown/MarkdownImage.tsx +6 -16
  35. package/src/markdown/MermaidBlock.tsx +50 -11
  36. package/src/markdown/UrlLinkBadge.tsx +84 -0
  37. package/src/markdown/image-actions.ts +29 -0
  38. package/src/markdown/index.ts +2 -0
  39. package/src/markdown/nodes.ts +6 -1
  40. package/src/markdown/preview-policy.test.ts +31 -0
  41. package/src/markdown/preview-policy.ts +33 -0
  42. package/src/markdown/progressive-chunks.test.ts +43 -0
  43. package/src/markdown/progressive-chunks.ts +58 -0
  44. package/src/markdown/rich-labels.ts +9 -0
  45. package/src/markdown/streaming-fade.test.ts +83 -0
  46. package/src/markdown/streaming-fade.tsx +141 -0
  47. package/src/markdown/streaming-reveal.ts +0 -67
  48. package/src/markdown/streaming.ts +64 -131
  49. package/src/overlays/CommandMenuRow.tsx +47 -26
  50. package/src/overlays/CommandMenuView.tsx +107 -15
  51. package/src/overlays/DiagnosticsExportDialogView.tsx +236 -0
  52. package/src/overlays/MagpieHandoverDialogView.tsx +217 -0
  53. package/src/overlays/ReleaseNoteImage.test.tsx +105 -0
  54. package/src/overlays/ReleaseNoteMarkdown.test.tsx +49 -0
  55. package/src/overlays/ReleaseNoteMarkdown.tsx +120 -7
  56. package/src/overlays/SplashPanel.tsx +4 -4
  57. package/src/overlays/SplashParts.tsx +6 -2
  58. package/src/overlays/UpdateCheckerView.tsx +17 -15
  59. package/src/overlays/UpdateRestartDialogView.tsx +76 -2
  60. package/src/overlays/assets/diagnostics-banner.webp +0 -0
  61. package/src/overlays/command-menu-types.ts +15 -1
  62. package/src/overlays/index.ts +13 -0
  63. package/src/plugin-ui/index.ts +4 -0
  64. package/src/project/DefaultConversationSectionView.tsx +17 -22
  65. package/src/project/DefaultSessionRowView.tsx +8 -0
  66. package/src/project/ManageSessionsSplashView.tsx +0 -2
  67. package/src/settings/ArchivedProjectsSettingsView.tsx +122 -1
  68. package/src/settings/GeneralSettingsView.tsx +21 -5
  69. package/src/settings/LauncherSettingsView.test.tsx +95 -0
  70. package/src/settings/LauncherSettingsView.tsx +272 -0
  71. package/src/settings/PetActionGalleryView.test.tsx +163 -0
  72. package/src/settings/PetActionGalleryView.tsx +216 -0
  73. package/src/settings/PetSettingsView.test.tsx +117 -0
  74. package/src/settings/PetSettingsView.tsx +86 -22
  75. package/src/settings/PresetProviderRowView.tsx +37 -98
  76. package/src/settings/ShortcutsSettingsView.tsx +0 -4
  77. package/src/settings/SpaceEditorDialogView.tsx +94 -56
  78. package/src/settings/SpacesSettingsView.tsx +53 -8
  79. package/src/settings/StorageBar.tsx +70 -0
  80. package/src/settings/StorageExplorer.tsx +488 -0
  81. package/src/settings/StorageReadout.tsx +64 -0
  82. package/src/settings/StorageSettingsView.test.tsx +181 -0
  83. package/src/settings/StorageSettingsView.tsx +458 -0
  84. package/src/settings/assets/space-editor-illustration.webp +0 -0
  85. package/src/settings/index.ts +28 -39
  86. package/src/settings/storage-treemap-layout.test.ts +32 -0
  87. package/src/settings/storage-treemap-layout.ts +55 -0
  88. package/src/shared/SegmentedControl.test.tsx +68 -0
  89. package/src/shared/SegmentedControl.tsx +18 -11
  90. package/src/shared/StageDialogContent.test.tsx +61 -0
  91. package/src/shared/StageDialogContent.tsx +80 -0
  92. package/src/shared/TabBar.tsx +2 -2
  93. package/src/shared/VettaKbd.tsx +20 -1
  94. package/src/shared/index.ts +4 -1
  95. package/src/shared/platform.ts +9 -0
  96. package/src/sidebar/AddProjectMenuTriggerView.tsx +13 -12
  97. package/src/sidebar/SettingsMenuTriggerView.tsx +2 -1
  98. package/src/sidebar/SidebarSectionActionButton.test.tsx +91 -0
  99. package/src/sidebar/SidebarSectionActionButton.tsx +52 -0
  100. package/src/styles.css +7 -0
  101. package/src/markdown/streaming.test.ts +0 -57
  102. package/src/settings/AchievementNavigationButtonView.tsx +0 -90
  103. package/src/settings/AchievementPromotionDialogView.tsx +0 -91
  104. package/src/settings/AchievementSettingsView.tsx +0 -43
  105. package/src/settings/PetBubbleStylePreviewView.tsx +0 -44
  106. package/src/settings/QuickPanelSettingsSectionView.tsx +0 -64
  107. package/src/settings/achievements/AchievementCurtains.tsx +0 -50
  108. package/src/settings/achievements/AchievementPromotionBadge3D.tsx +0 -350
  109. package/src/settings/achievements/AchievementPromotionConfetti.tsx +0 -71
  110. package/src/settings/achievements/AchievementTitle.tsx +0 -52
  111. package/src/settings/achievements/index.ts +0 -13
@@ -0,0 +1,181 @@
1
+ // @vitest-environment jsdom
2
+
3
+ import { act } from "react";
4
+ import { createRoot, type Root } from "react-dom/client";
5
+ import { afterEach, describe, expect, it } from "vitest";
6
+ import { StorageSettingsView, type StorageSettingsViewProps } from "./StorageSettingsView";
7
+
8
+ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
9
+
10
+ const mounted: Array<{ root: Root; container: HTMLDivElement }> = [];
11
+
12
+ afterEach(() => {
13
+ for (const { root, container } of mounted.splice(0)) {
14
+ act(() => root.unmount());
15
+ container.remove();
16
+ }
17
+ });
18
+
19
+ function render(overrides: Partial<StorageSettingsViewProps> = {}): HTMLDivElement {
20
+ const props: StorageSettingsViewProps = {
21
+ labels: {
22
+ title: "Storage",
23
+ description: "What Vetta keeps",
24
+ total: "Total",
25
+ scanning: "Calculating",
26
+ rescan: "Recalculate",
27
+ cleaning: "Clearing",
28
+ clean: (size) => `Clear ${size}`,
29
+ cleanEmpty: "Clear",
30
+ inUse: "In use",
31
+ nothingToClean: "Nothing to clear",
32
+ afterClean: (size) => `${size} after clearing`,
33
+ selectHint: "Select items",
34
+ assistTitle: "Clean up with Vetta",
35
+ assistDescription: "Vetta lists items",
36
+ assistAction: "Start chat",
37
+ open: "Open",
38
+ reveal: "Show location",
39
+ delete: "Delete",
40
+ sections: { overview: "Overview", clean: "Clean up", largest: "Largest" },
41
+ largestDescription: "Your content",
42
+ tabs: { overview: "Overview", content: "Chats" },
43
+ view: "View",
44
+ all: "All",
45
+ rest: "Smaller items",
46
+ empty: "Nothing yet",
47
+ },
48
+ overviewSection: { id: "storage-overview" },
49
+ cleanSection: { id: "storage-clean" },
50
+ largestSection: { id: "storage-largest" },
51
+ overview: {
52
+ totalBytes: 3072,
53
+ capacityBytes: 3072,
54
+ shares: [
55
+ { kind: "yours", label: "Yours", bytes: 2048, items: [{ id: "projects", label: "Projects", bytes: 2048 }] },
56
+ { kind: "cleanable", label: "Clearable", bytes: 1024, items: [{ id: "downloads", label: "Download cache", bytes: 1024 }] },
57
+ ],
58
+ },
59
+ assistPotential: null,
60
+ cleanActions: [
61
+ { id: "downloads", title: "Download cache", description: "npm", bytes: 1024, blocked: false, selected: true },
62
+ { id: "interface", title: "Interface cache", description: "ui", bytes: 0, blocked: true, selected: true },
63
+ ],
64
+ tab: "overview",
65
+ onTabChange: () => {},
66
+ explorer: { totalBytes: 0, owners: [] },
67
+ onReveal: () => {},
68
+ scanning: false,
69
+ cleaning: false,
70
+ cleanResult: null,
71
+ error: null,
72
+ onRescan: () => {},
73
+ onToggleClean: () => {},
74
+ onClean: () => {},
75
+ onAssist: () => {},
76
+ ...overrides,
77
+ };
78
+ const container = document.createElement("div");
79
+ document.body.append(container);
80
+ const root = createRoot(container);
81
+ mounted.push({ root, container });
82
+ act(() => root.render(<StorageSettingsView {...props} />));
83
+ return container;
84
+ }
85
+
86
+ const button = (container: HTMLElement, label: string) =>
87
+ [...container.querySelectorAll("button")].find((item) => item.textContent === label);
88
+
89
+ describe("StorageSettingsView", () => {
90
+ it("预告清理后的占用;使用中的条目不能勾选", () => {
91
+ const container = render();
92
+ expect(container.textContent).toContain("2.00 KB after clearing");
93
+ expect(container.textContent).toContain("In use");
94
+ expect([...container.querySelectorAll("input")].map((box) => [box.checked, box.disabled])).toEqual([
95
+ [true, false],
96
+ [false, true],
97
+ ]);
98
+ expect(button(container, "Clear 1.00 KB")?.disabled).toBe(false);
99
+ });
100
+
101
+ it("聚焦清理色块时在标题下读出说明,移开后恢复清理后的占用", () => {
102
+ const container = render();
103
+ const box = container.querySelector("input");
104
+ const status = container.querySelector('[role="status"]');
105
+ act(() => box?.focus());
106
+ expect(status?.nextElementSibling?.textContent).toBe("npm");
107
+ act(() => box?.blur());
108
+ expect(status?.nextElementSibling).toBeNull();
109
+ expect(status?.textContent).toBe("2.00 KB after clearing");
110
+ });
111
+
112
+ it("聚焦图例时读出该类的占比,移开后恢复总占用", () => {
113
+ const container = render();
114
+ const legend = button(container, "Clearable1.00 KB");
115
+ act(() => legend?.focus());
116
+ expect(container.textContent).toContain("33%");
117
+ act(() => legend?.blur());
118
+ expect(container.textContent).not.toContain("33%");
119
+ expect(container.textContent).toContain("Total3.00 KB");
120
+ });
121
+
122
+ it("清理中或重新计算中不能再发起清理", () => {
123
+ expect(button(render({ cleaning: true }), "Clearing")?.disabled).toBe(true);
124
+ expect(button(render({ scanning: true }), "Clear 1.00 KB")?.disabled).toBe(true);
125
+ });
126
+
127
+ it("没有可清理内容时只留一句说明,不显示清理按钮", () => {
128
+ const container = render({ cleanActions: [] });
129
+ expect(container.textContent).toContain("Nothing to clear");
130
+ expect(button(container, "Clear")).toBeUndefined();
131
+ });
132
+
133
+ it("点开项目进入下一层,点面包屑返回;不能再展开的内容显示位置", () => {
134
+ const revealed: string[] = [];
135
+ const container = render({
136
+ tab: "content",
137
+ explorer: {
138
+ totalBytes: 4096,
139
+ owners: [owner("Site", 4096, [{ name: "video.mp4", bytes: 3072, directory: false }])],
140
+ },
141
+ onReveal: (path) => revealed.push(path),
142
+ });
143
+ expect(rows(container)).toEqual(["SiteProject4.00 KB"]);
144
+ click(container, "View Site");
145
+ expect(rows(container)).toEqual(["video.mp43.00 KB", "Smaller items1.00 KB"]);
146
+ click(container, "Show location video.mp4");
147
+ expect(revealed).toEqual(["workspace/Site/video.mp4"]);
148
+ act(() => button(container, "All")?.click());
149
+ expect(rows(container)).toEqual(["SiteProject4.00 KB"]);
150
+ });
151
+
152
+ it("较小的项目合成一项,点开后可以继续查看和删除", () => {
153
+ const deleted: string[] = [];
154
+ const owners = Array.from({ length: 15 }, (_, index) => ({
155
+ ...owner(`P${index}`, 2048, [{ name: "a.md", bytes: 2048, directory: false }]),
156
+ onDelete: () => deleted.push(`P${index}`),
157
+ }));
158
+ const container = render({ tab: "content", explorer: { totalBytes: 15 * 2048, owners } });
159
+ expect(rows(container)).toHaveLength(13);
160
+ click(container, "View Smaller items");
161
+ expect(rows(container)).toEqual(["P12Project2.00 KB", "P13Project2.00 KB", "P14Project2.00 KB"]);
162
+ click(container, "Delete P13");
163
+ expect(deleted).toEqual(["P13"]);
164
+ click(container, "View P14");
165
+ expect(rows(container)).toEqual(["a.md2.00 KB"]);
166
+ });
167
+ });
168
+
169
+ type Owner = NonNullable<StorageSettingsViewProps["explorer"]>["owners"][number];
170
+
171
+ const owner = (title: string, bytes: number, children: Owner["children"]): Owner => ({
172
+ id: `workspace/${title}`,
173
+ kind: "projects",
174
+ title,
175
+ meta: "Project",
176
+ bytes,
177
+ children,
178
+ });
179
+ const rows = (container: HTMLElement) => [...container.querySelectorAll("li")].map((row) => row.textContent);
180
+ const click = (container: HTMLElement, label: string) =>
181
+ act(() => container.querySelector<HTMLElement>(`[aria-label="${label}"]`)?.click());
@@ -0,0 +1,458 @@
1
+ import { Button } from "@vetta-org/ui/button";
2
+ import { cn } from "@vetta-org/ui/utils";
3
+ import { type JSX, type ReactNode, useId, useState } from "react";
4
+ import { formatFileSize } from "../knowledge/types";
5
+ import { SegmentedControl } from "../shared/SegmentedControl";
6
+ import { SettingHeading, type SettingSectionMeta } from "./SettingChrome";
7
+ import { StorageBar, type StorageBarSegment } from "./StorageBar";
8
+ import { StorageExplorer, type StorageExplorerLabels, type StorageExplorerProps } from "./StorageExplorer";
9
+
10
+ export type StorageTabView = "overview" | "content";
11
+
12
+ export type StorageShareKindView = "yours" | "cleanable";
13
+
14
+ export interface StorageShareItemView {
15
+ readonly id: string;
16
+ readonly label: string;
17
+ readonly bytes: number;
18
+ readonly onOpen?: () => void;
19
+ }
20
+
21
+ export interface StorageShareView {
22
+ readonly kind: StorageShareKindView;
23
+ readonly label: string;
24
+ readonly bytes: number;
25
+ readonly items: readonly StorageShareItemView[];
26
+ }
27
+
28
+ export interface StorageCleanActionView {
29
+ /** 与「可以清理」分段里的条目同 id,占用条与清单靠它互相高亮。 */
30
+ readonly id: string;
31
+ readonly title: string;
32
+ readonly description: string;
33
+ /** 现在就能清理的体积;0 时不可勾选。 */
34
+ readonly bytes: number;
35
+ /** 有内容但正在使用,现在清不了。 */
36
+ readonly blocked: boolean;
37
+ readonly selected: boolean;
38
+ }
39
+
40
+ export interface StorageSettingsViewLabels extends StorageExplorerLabels {
41
+ readonly title: string;
42
+ readonly description: string;
43
+ readonly total: string;
44
+ readonly cleaning: string;
45
+ readonly clean: (size: string) => string;
46
+ readonly cleanEmpty: string;
47
+ readonly inUse: string;
48
+ readonly nothingToClean: string;
49
+ /** 勾选后的预告:清理后还占多少。 */
50
+ readonly afterClean: (size: string) => string;
51
+ readonly selectHint: string;
52
+ readonly assistTitle: string;
53
+ readonly assistDescription: string;
54
+ readonly assistAction: string;
55
+ readonly tabs: Readonly<Record<StorageTabView, string>>;
56
+ readonly sections: { readonly overview: string; readonly clean: string; readonly largest: string };
57
+ }
58
+
59
+ export interface StorageSettingsViewProps {
60
+ readonly labels: StorageSettingsViewLabels;
61
+ readonly tab: StorageTabView;
62
+ readonly onTabChange: (tab: StorageTabView) => void;
63
+ readonly overviewSection: SettingSectionMeta;
64
+ readonly cleanSection: SettingSectionMeta;
65
+ readonly largestSection: SettingSectionMeta;
66
+ /** null 表示首次扫描还没有结果。capacityBytes 是占用条整条代表的体积,清理后大于 totalBytes。 */
67
+ readonly overview: {
68
+ readonly totalBytes: number;
69
+ readonly capacityBytes: number;
70
+ readonly shares: readonly StorageShareView[];
71
+ } | null;
72
+ readonly cleanActions: readonly StorageCleanActionView[];
73
+ /** 需要 Vetta 判断才能清理的体积说明;没有时不显示。 */
74
+ readonly assistPotential: string | null;
75
+ readonly explorer: StorageExplorerProps["explorer"];
76
+ readonly scanning: boolean;
77
+ readonly cleaning: boolean;
78
+ /** 上一次清理的结果,显示在清理按钮旁。 */
79
+ readonly cleanResult: string | null;
80
+ readonly error: string | null;
81
+ readonly onRescan: () => void;
82
+ readonly onToggleClean: (id: string, selected: boolean) => void;
83
+ readonly onClean: () => void;
84
+ readonly onAssist: () => void;
85
+ /** path 相对数据目录。 */
86
+ readonly onReveal: (path: string) => void;
87
+ }
88
+
89
+ /** 你的内容每类一个色相,可以清理单独一个暖色。 */
90
+ const CONTENT_COLORS: Record<string, string> = {
91
+ conversations: "var(--context-segment-1)",
92
+ projects: "var(--context-segment-2)",
93
+ knowledge: "var(--context-segment-3)",
94
+ };
95
+ const CLEANABLE_COLOR = "var(--context-segment-5)";
96
+
97
+ interface OverviewPart extends StorageBarSegment {
98
+ readonly label: string;
99
+ readonly onSelect?: () => void;
100
+ }
101
+
102
+ /** 你的内容按类别分段,可以清理的合成一段。 */
103
+ function toParts(shares: readonly StorageShareView[], goTo: (target: "clean" | "content") => void): OverviewPart[] {
104
+ return shares.flatMap((share): OverviewPart[] => {
105
+ if (share.kind === "yours") {
106
+ return share.items.map((item) => ({
107
+ id: item.id,
108
+ label: item.label,
109
+ bytes: item.bytes,
110
+ color: CONTENT_COLORS[item.id] ?? "var(--muted-foreground)",
111
+ onSelect: item.onOpen ?? (() => goTo("content")),
112
+ }));
113
+ }
114
+ return [
115
+ {
116
+ id: share.kind,
117
+ label: share.label,
118
+ bytes: share.bytes,
119
+ color: CLEANABLE_COLOR,
120
+ hatched: true,
121
+ onSelect: () => goTo("clean"),
122
+ },
123
+ ];
124
+ });
125
+ }
126
+
127
+ export function StorageSettingsView(props: StorageSettingsViewProps): JSX.Element {
128
+ const { labels, tab, largestSection } = props;
129
+ return (
130
+ <div className="mx-auto w-full max-w-[680px] px-8 pt-2 pb-16">
131
+ <h1 className="mb-1.5 text-[20px] font-bold text-foreground">{labels.title}</h1>
132
+ <p className="text-[13px] text-muted-foreground">{labels.description}</p>
133
+ <SegmentedControl
134
+ className="mt-5"
135
+ items={[
136
+ { key: "overview", label: labels.tabs.overview },
137
+ { key: "content", label: labels.tabs.content },
138
+ ]}
139
+ value={tab}
140
+ onChange={props.onTabChange}
141
+ />
142
+
143
+ {props.error && (
144
+ <p role="alert" className="mt-4 text-[12px] text-destructive">
145
+ {props.error}
146
+ </p>
147
+ )}
148
+
149
+ <div key={tab} className="animate-in fade-in-0 duration-200 motion-reduce:animate-none">
150
+ {tab === "overview" ? (
151
+ <OverviewTab {...props} />
152
+ ) : (
153
+ <section
154
+ id={largestSection.id}
155
+ aria-label={labels.sections.largest}
156
+ data-setting-section-id={largestSection.id}
157
+ data-setting-section-highlight-target={largestSection.id}
158
+ className="mt-8"
159
+ >
160
+ <StorageExplorer
161
+ labels={labels}
162
+ explorer={props.explorer}
163
+ scanning={props.scanning}
164
+ cleaning={props.cleaning}
165
+ onRescan={props.onRescan}
166
+ onReveal={props.onReveal}
167
+ />
168
+ </section>
169
+ )}
170
+ </div>
171
+ </div>
172
+ );
173
+ }
174
+
175
+ function OverviewTab(props: StorageSettingsViewProps): JSX.Element {
176
+ const { labels } = props;
177
+ return (
178
+ <>
179
+ <StorageOverview {...props} />
180
+ <CleanSection {...props} />
181
+
182
+ <section className="mt-10 flex items-center gap-8">
183
+ <div className="min-w-0 flex-1">
184
+ <h2 className="text-[15px] font-semibold text-foreground">{labels.assistTitle}</h2>
185
+ <p className="mt-1.5 text-[12px] leading-relaxed text-muted-foreground">{labels.assistDescription}</p>
186
+ {props.assistPotential && <p className="mt-1.5 text-[12px] text-primary">{props.assistPotential}</p>}
187
+ </div>
188
+ <Button variant="field" onClick={props.onAssist}>
189
+ <span aria-hidden="true" className="icon-[solar--magic-stick-3-linear] h-3.5 w-3.5" />
190
+ {labels.assistAction}
191
+ </Button>
192
+ </section>
193
+ </>
194
+ );
195
+ }
196
+
197
+ function StorageOverview({
198
+ labels,
199
+ overview,
200
+ overviewSection,
201
+ cleanSection,
202
+ scanning,
203
+ cleaning,
204
+ onRescan,
205
+ onTabChange,
206
+ }: StorageSettingsViewProps): JSX.Element {
207
+ const [activeId, setActiveId] = useState<string | null>(null);
208
+ const parts = toParts(overview?.shares ?? [], (target) => {
209
+ if (target === "content") onTabChange("content");
210
+ else document.getElementById(cleanSection.id)?.scrollIntoView({ behavior: "smooth", block: "start" });
211
+ });
212
+ const active = parts.find((part) => part.id === activeId);
213
+ const total = overview?.totalBytes ?? 0;
214
+ const refreshLabel = scanning ? labels.scanning : labels.rescan;
215
+
216
+ return (
217
+ <section
218
+ id={overviewSection.id}
219
+ aria-label={labels.sections.overview}
220
+ data-setting-section-id={overviewSection.id}
221
+ data-setting-section-highlight-target={overviewSection.id}
222
+ className="mt-4 rounded-xl bg-muted-foreground/7 px-4 pt-3.5 pb-2.5"
223
+ >
224
+ <div className="flex items-start justify-between gap-4">
225
+ <div className="min-w-0">
226
+ <div className="truncate text-[12px] leading-4 text-muted-foreground">
227
+ {active?.label ?? labels.total}
228
+ {active && total > 0 && ` · ${formatShare(active.bytes / total)}`}
229
+ </div>
230
+ <div className="mt-0.5 text-[22px] leading-7 font-semibold tracking-tight text-foreground">
231
+ {overview ? formatFileSize(active?.bytes ?? total) : <span className="text-muted-foreground">—</span>}
232
+ </div>
233
+ </div>
234
+ <Button
235
+ variant="ghost"
236
+ size="icon-sm"
237
+ className="-mt-1 -mr-1.5"
238
+ title={refreshLabel}
239
+ aria-label={refreshLabel}
240
+ disabled={scanning || cleaning}
241
+ onClick={onRescan}
242
+ >
243
+ <span aria-hidden="true" className={cn("icon-[solar--refresh-linear] h-4 w-4", scanning && "animate-spin")} />
244
+ </Button>
245
+ </div>
246
+
247
+ <div className="mt-2.5">
248
+ <StorageBar
249
+ segments={parts}
250
+ capacityBytes={overview?.capacityBytes ?? 0}
251
+ activeId={activeId}
252
+ onActiveChange={setActiveId}
253
+ />
254
+ </div>
255
+
256
+ {/* 首次扫描时先留出一行,结果出来后卡片不会变高。 */}
257
+ <ul className="-mx-2 mt-1.5 flex min-h-7 flex-wrap justify-between" onMouseLeave={() => setActiveId(null)}>
258
+ {parts
259
+ .filter((part) => part.bytes > 0)
260
+ .map((part) => (
261
+ <li key={part.id}>
262
+ <Button
263
+ variant="ghost"
264
+ className={cn("h-7 gap-1.5 px-2 text-[12px] font-normal", activeId === part.id && "bg-accent/50")}
265
+ onMouseEnter={() => setActiveId(part.id)}
266
+ onFocus={() => setActiveId(part.id)}
267
+ onBlur={() => setActiveId(null)}
268
+ onClick={part.onSelect}
269
+ >
270
+ <span aria-hidden="true" className="h-2 w-2 shrink-0 rounded-full" style={{ background: part.color }} />
271
+ <span className="text-muted-foreground">{part.label}</span>
272
+ <span className="text-foreground tabular-nums">{formatFileSize(part.bytes)}</span>
273
+ </Button>
274
+ </li>
275
+ ))}
276
+ </ul>
277
+ </section>
278
+ );
279
+ }
280
+
281
+ const formatShare = (share: number): string => (share < 0.01 ? "<1%" : `${Math.round(share * 100)}%`);
282
+
283
+ /** 一排可勾选的色块;说明不常驻,指到或聚焦哪块就在标题下读哪块。 */
284
+ function CleanSection({
285
+ labels,
286
+ overview,
287
+ cleanSection,
288
+ cleanActions,
289
+ cleanResult,
290
+ scanning,
291
+ cleaning,
292
+ onToggleClean,
293
+ onClean,
294
+ }: StorageSettingsViewProps): JSX.Element {
295
+ const [activeId, setActiveId] = useState<string | null>(null);
296
+ // 首次扫描前先按完整清单占位,有结果后只留真的有内容的条目。
297
+ const actions = overview ? cleanActions.filter((item) => item.bytes > 0 || item.blocked) : cleanActions;
298
+ const active = actions.find((action) => action.id === activeId);
299
+ const cleanableBytes = actions.reduce((sum, action) => sum + action.bytes, 0);
300
+ const selectedBytes = actions.reduce((sum, action) => sum + (action.selected ? action.bytes : 0), 0);
301
+ const empty = actions.length === 0;
302
+
303
+ let status: ReactNode = empty ? labels.nothingToClean : labels.selectHint;
304
+ if (selectedBytes > 0) status = labels.afterClean(formatFileSize((overview?.totalBytes ?? 0) - selectedBytes));
305
+ if (cleanResult) {
306
+ status = (
307
+ <span className="flex items-center gap-1.5 text-foreground">
308
+ <span aria-hidden="true" className="icon-[solar--check-circle-linear] h-4 w-4 shrink-0 text-emerald-400" />
309
+ {cleanResult}
310
+ </span>
311
+ );
312
+ }
313
+
314
+ return (
315
+ <section
316
+ className="mt-10"
317
+ data-setting-section-id={cleanSection.id}
318
+ data-setting-section-highlight-target={cleanSection.id}
319
+ >
320
+ <div className="flex items-start justify-between gap-6">
321
+ <div className="min-w-0 flex-1">
322
+ <SettingHeading section={cleanSection} title={labels.sections.clean} />
323
+ {/* 留足两行,说明变长时下面的色块不会跳动。 */}
324
+ <div className={cn("mt-1.5 text-[12px] leading-[18px] text-muted-foreground tabular-nums", !empty && "min-h-9")}>
325
+ <div role="status" className={cn(active && "sr-only")}>
326
+ {status}
327
+ </div>
328
+ {active && <div aria-hidden="true">{active.description}</div>}
329
+ </div>
330
+ </div>
331
+ {!empty && (
332
+ <Button
333
+ variant="primary"
334
+ disabled={!overview || scanning || cleaning || selectedBytes === 0}
335
+ onClick={onClean}
336
+ >
337
+ {cleaning ? labels.cleaning : selectedBytes > 0 ? labels.clean(formatFileSize(selectedBytes)) : labels.cleanEmpty}
338
+ </Button>
339
+ )}
340
+ </div>
341
+ {!empty && (
342
+ <div
343
+ className="mt-2 grid gap-1"
344
+ style={{ gridTemplateColumns: `repeat(${actions.length}, minmax(0, 1fr))` }}
345
+ onMouseLeave={() => setActiveId(null)}
346
+ >
347
+ {actions.map((action) => (
348
+ <CleanTile
349
+ key={action.id}
350
+ action={action}
351
+ share={cleanableBytes > 0 ? action.bytes / cleanableBytes : 0}
352
+ disabled={cleaning || action.bytes === 0}
353
+ emptyLabel={overview ? labels.inUse : "—"}
354
+ onActiveChange={(on) => setActiveId(on ? action.id : null)}
355
+ onToggle={(selected) => onToggleClean(action.id, selected)}
356
+ />
357
+ ))}
358
+ </div>
359
+ )}
360
+ </section>
361
+ );
362
+ }
363
+
364
+ function CleanTile({
365
+ action,
366
+ share,
367
+ disabled,
368
+ emptyLabel,
369
+ onActiveChange,
370
+ onToggle,
371
+ }: {
372
+ readonly action: StorageCleanActionView;
373
+ /** 占全部可清理体积的比例,决定底部色条多宽。 */
374
+ readonly share: number;
375
+ readonly disabled: boolean;
376
+ readonly emptyLabel: string;
377
+ readonly onActiveChange: (active: boolean) => void;
378
+ readonly onToggle: (selected: boolean) => void;
379
+ }): JSX.Element {
380
+ const descriptionId = useId();
381
+ const available = action.bytes > 0;
382
+ const checked = action.selected && available;
383
+ const [amount, unit] = available ? formatFileSize(action.bytes).split(" ") : [];
384
+ return (
385
+ <label
386
+ className={cn(
387
+ "relative overflow-hidden rounded-lg px-3 pt-2.5 pb-4 transition-colors duration-200 has-focus-visible:ring-2 has-focus-visible:ring-ring",
388
+ disabled ? "cursor-default" : "cursor-pointer",
389
+ )}
390
+ style={{
391
+ backgroundColor: checked
392
+ ? `color-mix(in srgb, ${CLEANABLE_COLOR} 12%, transparent)`
393
+ : "color-mix(in srgb, var(--muted-foreground) 8%, transparent)",
394
+ }}
395
+ onMouseEnter={() => onActiveChange(true)}
396
+ >
397
+ <input
398
+ type="checkbox"
399
+ className="sr-only"
400
+ aria-label={action.title}
401
+ aria-describedby={descriptionId}
402
+ checked={checked}
403
+ disabled={disabled}
404
+ onFocus={() => onActiveChange(true)}
405
+ onBlur={() => onActiveChange(false)}
406
+ onChange={(event) => onToggle(event.target.checked)}
407
+ />
408
+ <span id={descriptionId} className="sr-only">
409
+ {action.description}
410
+ </span>
411
+ <span
412
+ aria-hidden="true"
413
+ className={cn(
414
+ "absolute bottom-0 left-0 h-1 origin-left transition-transform duration-300 ease-out motion-reduce:transition-none",
415
+ !checked && "scale-x-0",
416
+ )}
417
+ style={{
418
+ width: `${share * 100}%`,
419
+ backgroundColor: CLEANABLE_COLOR,
420
+ }}
421
+ />
422
+ <span className="flex items-center justify-between gap-2">
423
+ <span className={cn("truncate text-[12px]", checked ? "text-foreground" : "text-muted-foreground")}>
424
+ {action.title}
425
+ </span>
426
+ {available && (
427
+ <span
428
+ aria-hidden="true"
429
+ className={cn(
430
+ "flex h-4 w-4 shrink-0 items-center justify-center rounded-[5px] transition-colors duration-200",
431
+ checked ? "text-background" : "bg-muted-foreground/20 text-transparent",
432
+ )}
433
+ style={checked ? { backgroundColor: CLEANABLE_COLOR } : undefined}
434
+ >
435
+ <span className="icon-[mdi--check] h-3 w-3" />
436
+ </span>
437
+ )}
438
+ </span>
439
+ <span className="mt-1.5 flex h-6 items-baseline gap-1">
440
+ {available ? (
441
+ <>
442
+ <span
443
+ className={cn(
444
+ "text-[20px] leading-6 font-semibold tracking-tight tabular-nums",
445
+ checked ? "text-foreground" : "text-muted-foreground",
446
+ )}
447
+ >
448
+ {amount}
449
+ </span>
450
+ <span className="text-[12px] text-muted-foreground">{unit}</span>
451
+ </>
452
+ ) : (
453
+ <span className="text-[13px] leading-6 text-muted-foreground">{emptyLabel}</span>
454
+ )}
455
+ </span>
456
+ </label>
457
+ );
458
+ }