Skip to content

Commit c624544

Browse files
committed
feat(gui): implement Zustand state management and full UI reactivity
- Add Zustand with persist and devtools middleware for centralized state - Migrate all state management from local hooks to global store - Remove deprecated useScan and useClean hooks - Implement instant UI updates for delete and whitelist operations - Add optimistic updates for trash and schedule operations - Enable state persistence for settings and last scan results - Eliminate prop drilling across all components UI improvements: - Delete items: instant removal from list (no rescan needed) - Whitelist: instant removal and deselection - Trash restore/delete: immediate UI feedback - Schedule toggle: instant status updates - Settings persist across app restarts
1 parent 4a11a8d commit c624544

9 files changed

Lines changed: 586 additions & 316 deletions

File tree

‎crates/cleanser-gui/ui/package-lock.json‎

Lines changed: 34 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/cleanser-gui/ui/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"@tauri-apps/plugin-opener": "^2",
1515
"@tauri-apps/plugin-shell": "^2",
1616
"react": "^18.3.1",
17-
"react-dom": "^18.3.1"
17+
"react-dom": "^18.3.1",
18+
"zustand": "^5.0.11"
1819
},
1920
"devDependencies": {
2021
"@tauri-apps/cli": "^2",

‎crates/cleanser-gui/ui/src/App.tsx‎

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
1-
import { useState, useCallback } from "react";
2-
import { useScan } from "./hooks/useScan";
3-
import { useClean } from "./hooks/useClean";
4-
import { useVersion } from "./hooks/useVersion";
1+
import { useState, useEffect } from "react";
2+
import { useAppStore } from "./store/appStore";
53
import { ProgressBar } from "./components/ProgressBar";
64
import { ScanView } from "./components/ScanView";
75
import { Settings } from "./components/Settings";
86
import { MapView } from "./components/MapView";
97
import { TrashView } from "./components/TrashView";
108
import { ScheduleView } from "./components/ScheduleView";
9+
import { useVersion } from "./hooks/useVersion";
1110
import { useI18n } from "./i18n";
1211
import type { ScanConfig } from "./types";
13-
import { formatSize } from "./utils";
1412

1513
type ScanSpeed = "quick" | "normal" | "thorough";
1614
type Tab = "scan" | "map" | "trash" | "schedule";
1715

18-
// Default scan settings
19-
const DEFAULT_MIN_FILE_SIZE_MB = 100;
20-
2116
function App() {
2217
const { t } = useI18n();
2318
const [activeTab, setActiveTab] = useState<Tab>("scan");
24-
const [selectedPaths, setSelectedPaths] = useState<Set<string>>(new Set());
2519
const [speed, setSpeed] = useState<ScanSpeed>("normal");
26-
const [minFileSizeMb, setMinFileSizeMb] = useState(DEFAULT_MIN_FILE_SIZE_MB);
27-
const [findDuplicates, setFindDuplicates] = useState(false);
2820
const [showSettings, setShowSettings] = useState(false);
29-
30-
const { results, isScanning, progress: scanProgress, error: scanError, scan, clearResults } = useScan();
31-
const { isCleaning, progress: cleanProgress, result: cleanResult, error: cleanError, cleanItems, clearResult } = useClean();
3221
const { versionInfo } = useVersion();
3322

23+
// Zustand store
24+
const {
25+
scanResults,
26+
isScanning,
27+
scanProgress,
28+
scanError,
29+
isCleaning,
30+
cleanProgress,
31+
cleanResult,
32+
cleanError,
33+
selectedPaths,
34+
minFileSizeMb,
35+
findDuplicates,
36+
scan,
37+
clearScanResults,
38+
cleanItems,
39+
clearCleanResult,
40+
setMinFileSizeMb,
41+
setFindDuplicates,
42+
initialize,
43+
} = useAppStore();
44+
45+
// Initialize store on mount
46+
useEffect(() => {
47+
initialize();
48+
}, [initialize]);
49+
3450
const handleScan = async () => {
35-
setSelectedPaths(new Set());
36-
clearResult();
3751
const config: ScanConfig = {
3852
speed,
3953
min_file_size_mb: minFileSizeMb,
@@ -46,50 +60,12 @@ function App() {
4660
}
4761
};
4862

49-
const handleToggleItem = useCallback((path: string) => {
50-
setSelectedPaths((prev) => {
51-
const next = new Set(prev);
52-
if (next.has(path)) {
53-
next.delete(path);
54-
} else {
55-
next.add(path);
56-
}
57-
return next;
58-
});
59-
}, []);
60-
61-
const handleToggleCategory = useCallback(
62-
(category: string, selected: boolean) => {
63-
if (!results) return;
64-
65-
setSelectedPaths((prev) => {
66-
const next = new Set(prev);
67-
const categoryItems = results.items.filter(
68-
(item) => item.category === category
69-
);
70-
71-
for (const item of categoryItems) {
72-
if (selected) {
73-
next.add(item.path);
74-
} else {
75-
next.delete(item.path);
76-
}
77-
}
78-
79-
return next;
80-
});
81-
},
82-
[results]
83-
);
84-
8563
const handleClean = async () => {
8664
if (selectedPaths.size === 0) return;
8765

8866
const paths = Array.from(selectedPaths);
8967
try {
9068
await cleanItems(paths, false);
91-
setSelectedPaths(new Set());
92-
await scan({ speed, min_file_size_mb: minFileSizeMb, find_duplicates: findDuplicates });
9369
} catch (e) {
9470
console.error("Clean failed:", e);
9571
}
@@ -276,9 +252,9 @@ function App() {
276252
</div>
277253
</div>
278254

279-
{results && !isWorking && (
255+
{scanResults && !isWorking && (
280256
<button
281-
onClick={clearResults}
257+
onClick={clearScanResults}
282258
className="ml-auto text-sm text-muted hover:text-secondary transition-colors"
283259
>
284260
{t("clearResults")}
@@ -308,7 +284,7 @@ function App() {
308284
<div>
309285
<p className="font-semibold risk-safe">{t("cleanComplete")}</p>
310286
<p className="text-sm text-secondary">
311-
{t("itemsCleaned", { count: cleanResult.cleaned_count, size: formatSize(cleanResult.cleaned_size) })}
287+
{t("itemsCleaned", { count: cleanResult.cleaned_count, size: cleanResult.cleaned_size })}
312288
</p>
313289
{cleanResult.failed_count > 0 && (
314290
<p className="text-sm risk-risky">
@@ -318,7 +294,7 @@ function App() {
318294
</div>
319295
</div>
320296
<button
321-
onClick={clearResult}
297+
onClick={clearCleanResult}
322298
className="text-muted hover:text-secondary p-1"
323299
>
324300
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
@@ -345,14 +321,9 @@ function App() {
345321

346322
{/* Scan results or empty state */}
347323
<div className="flex-1 overflow-hidden">
348-
{results ? (
324+
{scanResults ? (
349325
<ScanView
350-
results={results}
351-
selectedPaths={selectedPaths}
352-
onToggleItem={handleToggleItem}
353-
onToggleCategory={handleToggleCategory}
354326
onClean={handleClean}
355-
isCleaning={isCleaning}
356327
/>
357328
) : (
358329
<div className="flex flex-col items-center justify-center h-full">

‎crates/cleanser-gui/ui/src/components/ScanView.tsx‎

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import { useState, useMemo } from "react";
2-
import { invoke } from "@tauri-apps/api/core";
3-
import type { ScanResults, CleanableItem, CategoryGroup } from "../types";
2+
import { useAppStore } from "../store/appStore";
3+
import type { CleanableItem, CategoryGroup } from "../types";
44
import { CategoryCard } from "./CategoryCard";
55
import { formatSize } from "../utils";
66
import { useI18n } from "../i18n";
77

88
interface Props {
9-
results: ScanResults;
10-
selectedPaths: Set<string>;
11-
onToggleItem: (path: string) => void;
12-
onToggleCategory: (category: string, selected: boolean) => void;
139
onClean: () => void;
14-
isCleaning: boolean;
1510
}
1611

17-
export function ScanView({
18-
results,
19-
selectedPaths,
20-
onToggleItem,
21-
onToggleCategory,
22-
onClean,
23-
isCleaning,
24-
}: Props) {
12+
export function ScanView({ onClean }: Props) {
2513
const { t } = useI18n();
2614
const [sortBy, setSortBy] = useState<"size" | "count">("size");
2715

16+
// Get state from Zustand store
17+
const {
18+
scanResults,
19+
selectedPaths,
20+
isCleaning,
21+
toggleItem,
22+
toggleCategory,
23+
selectAll,
24+
deselectAll,
25+
addToWhitelist,
26+
} = useAppStore();
27+
2828
// Group items by category
2929
const categoryGroups = useMemo(() => {
30+
if (!scanResults) return [];
31+
3032
const groups = new Map<string, CleanableItem[]>();
31-
for (const item of results.items) {
33+
for (const item of scanResults.items) {
3234
const existing = groups.get(item.category) || [];
3335
existing.push(item);
3436
groups.set(item.category, existing);
@@ -51,50 +53,49 @@ export function ScanView({
5153
}
5254

5355
return categoryList;
54-
}, [results.items, sortBy]);
56+
}, [scanResults, sortBy]);
5557

5658
// Calculate selected size
5759
const selectedSize = useMemo(() => {
58-
return results.items
60+
if (!scanResults) return 0;
61+
return scanResults.items
5962
.filter((item) => selectedPaths.has(item.path))
6063
.reduce((sum, item) => sum + item.size, 0);
61-
}, [results.items, selectedPaths]);
64+
}, [scanResults, selectedPaths]);
6265

6366
const handleAddToWhitelist = async (path: string) => {
6467
try {
65-
await invoke("add_to_whitelist", { path });
68+
await addToWhitelist(path);
6669
} catch (e) {
6770
console.error("Failed to add to whitelist:", e);
6871
}
6972
};
7073

7174
const handleSelectAll = () => {
72-
const allPaths = results.items.map((item) => item.path);
75+
if (!scanResults) return;
76+
77+
const allPaths = scanResults.items.map((item) => item.path);
7378
const allSelected = allPaths.every((path) => selectedPaths.has(path));
7479

7580
if (allSelected) {
76-
for (const path of allPaths) {
77-
onToggleItem(path);
78-
}
81+
deselectAll();
7982
} else {
80-
for (const path of allPaths) {
81-
if (!selectedPaths.has(path)) {
82-
onToggleItem(path);
83-
}
84-
}
83+
selectAll();
8584
}
8685
};
8786

87+
if (!scanResults) return null;
88+
8889
return (
8990
<div className="flex flex-col h-full">
9091
{/* Summary header */}
9192
<div className="flex items-center justify-between mb-4">
9293
<div>
9394
<h2 className="text-xl font-bold text-primary">
94-
{t("found")} <span className="accent-text">{formatSize(results.total_size)}</span>
95+
{t("found")} <span className="accent-text">{formatSize(scanResults.total_size)}</span>
9596
</h2>
9697
<p className="text-sm text-secondary">
97-
{results.items_count} {results.items_count === 1 ? t("item") : t("items")} {t("inCategories", { count: categoryGroups.length })}
98+
{scanResults.items_count} {scanResults.items_count === 1 ? t("item") : t("items")} {t("inCategories", { count: categoryGroups.length })}
9899
</p>
99100
</div>
100101

@@ -112,7 +113,7 @@ export function ScanView({
112113
onClick={handleSelectAll}
113114
className="btn btn-secondary text-sm"
114115
>
115-
{selectedPaths.size === results.items_count
116+
{selectedPaths.size === scanResults.items_count
116117
? t("deselectAll")
117118
: t("selectAll")}
118119
</button>
@@ -127,8 +128,8 @@ export function ScanView({
127128
category={group.category}
128129
items={group.items}
129130
selectedPaths={selectedPaths}
130-
onToggleItem={onToggleItem}
131-
onToggleCategory={onToggleCategory}
131+
onToggleItem={toggleItem}
132+
onToggleCategory={toggleCategory}
132133
onAddToWhitelist={handleAddToWhitelist}
133134
/>
134135
))}

0 commit comments

Comments
 (0)