refactor frontend

This commit is contained in:
Julian Freeman
2026-04-17 10:39:25 -04:00
parent 9e06791019
commit 11a8955aca
17 changed files with 1328 additions and 996 deletions

View File

@@ -0,0 +1,133 @@
import { computed, ref } from "vue";
import {
startBrowserClean as runBrowserCleanCommand,
startBrowserScan as runBrowserScanCommand,
} from "../services/tauri/cleaner";
import type { AlertOptions, BrowserScanResult, CleanResult } from "../types/cleaner";
import { formatItemSize } from "../utils/format";
interface BrowserState {
isScanning: boolean;
isCleaning: boolean;
isDone: boolean;
scanResult: BrowserScanResult | null;
cleanResult: CleanResult | null;
}
export function useBrowserClean(
browser: "chrome" | "edge",
showAlert: (options: AlertOptions) => void,
) {
const state = ref<BrowserState>({
isScanning: false,
isCleaning: false,
isDone: false,
scanResult: null,
cleanResult: null,
});
const selectedStats = computed(() => {
const scanResult = state.value.scanResult;
if (!scanResult) return { sizeStr: "0 B", count: 0, hasSelection: false };
const enabledProfiles = scanResult.profiles.filter((profile) => profile.enabled);
const totalBytes = enabledProfiles.reduce((acc, profile) => acc + profile.cache_size, 0);
return {
sizeStr: formatItemSize(totalBytes),
count: enabledProfiles.length,
hasSelection: enabledProfiles.length > 0,
};
});
async function startScan() {
const current = state.value;
current.isScanning = true;
current.isDone = false;
current.scanResult = null;
current.cleanResult = null;
try {
const result = await runBrowserScanCommand(browser);
current.scanResult = {
...result,
profiles: result.profiles
.map((profile) => ({ ...profile, enabled: true }))
.sort((a, b) => b.cache_size - a.cache_size),
};
} catch (err) {
showAlert({
title: "扫描失败",
message: String(err),
type: "error",
});
} finally {
current.isScanning = false;
}
}
async function startClean() {
const current = state.value;
if (!current.scanResult || current.isCleaning) return;
const selectedProfiles = current.scanResult.profiles
.filter((profile) => profile.enabled)
.map((profile) => profile.path_name);
if (selectedProfiles.length === 0) {
showAlert({
title: "未选择",
message: "请选择至少一个用户资料进行清理。",
type: "info",
});
return;
}
current.isCleaning = true;
try {
current.cleanResult = await runBrowserCleanCommand(browser, selectedProfiles);
current.isDone = true;
current.scanResult = null;
} catch (err) {
showAlert({
title: "清理失败",
message: String(err),
type: "error",
});
} finally {
current.isCleaning = false;
}
}
function toggleAllProfiles(enabled: boolean) {
state.value.scanResult?.profiles.forEach((profile) => {
profile.enabled = enabled;
});
}
function invertProfiles() {
state.value.scanResult?.profiles.forEach((profile) => {
profile.enabled = !profile.enabled;
});
}
function reset() {
state.value = {
isScanning: false,
isCleaning: false,
isDone: false,
scanResult: null,
cleanResult: null,
};
}
return {
state,
selectedStats,
startScan,
startClean,
toggleAllProfiles,
invertProfiles,
reset,
};
}