import { useCallback, useMemo, useRef, useState } from "react"; import { api, subscribeJobProgress } from "../lib/api"; import { isCancelled, messageFrom } from "../lib/format"; import type { Category, CleanupItem, DeleteMode, DeleteResult, JobProgress, ScanResult, } from "../types"; function jobId(): string { return crypto.randomUUID?.() ?? `${Date.now()}-${Math.random().toString(36).slice(2)}`; } export type ScanKind = "cleanup" | "purge"; /** Seed the checkbox state of a fresh result: unlocked items the policy * keeps win; without a policy the rule metadata (`item.selected`) speaks. */ export function seedSelection( items: CleanupItem[], defaultFor?: (item: CleanupItem) => boolean, ): Set { const picked = new Set(); items.forEach((item, index) => { if (item.lock) return; if (defaultFor ? defaultFor(item) : item.selected) picked.add(index); }); return picked; } export interface ScanWorkspace { kind: ScanKind; result: ScanResult | null; selected: Set; progress: JobProgress | null; scanning: boolean; deleting: boolean; error: string | null; summary: DeleteResult | null; currentJob: string | null; selectedTotal: number; selectedCount: number; runScan: (options?: { force?: boolean; minAgeDays?: number | null; roots?: string[]; }) => Promise; cancel: () => Promise; toggleItem: (index: number) => void; toggleCategory: (category: Category) => void; setCategorySelection: (category: Category, on: boolean) => void; deselect: (indexes: number[]) => void; selectAll: () => void; clearSelection: () => void; clean: (mode: DeleteMode, indexes?: number[]) => Promise; clearError: () => void; reset: () => void; } export function useScanWorkspace( kind: ScanKind, /** Overrides the rule metadata's per-item default when a scan lands * (空间清理 uses this to pre-check every daily group and leave opt-in * groups untouched). Read through a ref, so a fresh closure per render is * fine without restarting the hook. */ selectionPolicy?: (item: CleanupItem) => boolean, ): ScanWorkspace { const policyRef = useRef(selectionPolicy); policyRef.current = selectionPolicy; const [result, setResult] = useState(null); const [selected, setSelected] = useState>(new Set()); const [progress, setProgress] = useState(null); const [scanning, setScanning] = useState(false); const [deleting, setDeleting] = useState(false); const [error, setError] = useState(null); const [summary, setSummary] = useState(null); const [currentJob, setCurrentJob] = useState(null); const activeJob = useRef(null); const runScan = useCallback( async (options?: { force?: boolean; minAgeDays?: number | null; roots?: string[]; }) => { if (scanning || deleting) return; const id = jobId(); activeJob.current = id; setCurrentJob(id); setScanning(true); setError(null); setSummary(null); setProgress(null); const stop = await subscribeJobProgress(id, setProgress); try { const next = kind === "cleanup" ? await api.cleanupScan(id, [], options?.minAgeDays ?? null, false) : await api.purgeScan( id, options?.roots ?? [], options?.minAgeDays ?? null, options?.force ?? false, ); if (activeJob.current !== id) return; setResult(next); setSelected(seedSelection(next.items, policyRef.current)); } catch (reason) { if (!isCancelled(reason)) setError(messageFrom(reason)); } finally { stop(); if (activeJob.current === id) { setScanning(false); setProgress(null); setCurrentJob(null); } } }, [deleting, kind, scanning], ); const cancel = useCallback(async () => { if (activeJob.current) await api.cancelJob(activeJob.current).catch(() => false); }, []); const toggleItem = useCallback((index: number) => { setSelected((current) => { const next = new Set(current); if (next.has(index)) next.delete(index); else next.add(index); return next; }); }, []); const toggleCategory = useCallback( (category: Category) => { if (!result) return; const indexes = result.items .map((item, index) => ({ item, index })) .filter(({ item }) => item.category === category && !item.lock) .map(({ index }) => index); setSelected((current) => { const next = new Set(current); const allSelected = indexes.length > 0 && indexes.every((index) => next.has(index)); for (const index of indexes) { if (allSelected) next.delete(index); else next.add(index); } return next; }); }, [result], ); const selectAll = useCallback(() => { if (!result) return; setSelected((current) => { const all = result.items .map((item, index) => ({ item, index })) .filter(({ item }) => !item.lock) .map(({ index }) => index); if (all.length > 0 && all.every((index) => current.has(index))) return new Set(); return new Set(all); }); }, [result]); const clearSelection = useCallback(() => setSelected(new Set()), []); // Deterministic category state: the 空间清理 page uses this to leave the // optional groups (升级安装包 / 项目产物 / 开发文件) unchecked after a // scan, wherever the rule defaults happened to point. const setCategorySelection = useCallback( (category: Category, on: boolean) => { if (!result) return; const indexes = result.items .map((item, index) => ({ item, index })) .filter(({ item }) => item.category === category && !item.lock) .map(({ index }) => index); if (indexes.length === 0) return; setSelected((current) => { const next = new Set(current); for (const index of indexes) { if (on) next.add(index); else next.delete(index); } return next; }); }, [result], ); const deselect = useCallback((indexes: number[]) => { if (indexes.length === 0) return; const drop = new Set(indexes); setSelected((current) => { const next = new Set(current); for (const index of drop) next.delete(index); return next; }); }, []); const selectedTotal = useMemo(() => { if (!result) return 0; let total = 0; for (const index of selected) total += result.items[index]?.onDisk ?? 0; return total; }, [result, selected]); const selectedCount = selected.size; const clean = useCallback( async (mode: DeleteMode, indexes?: number[]) => { if (!result || deleting) return null; const targets = indexes ?? [...selected]; if (targets.length === 0) return null; const id = jobId(); activeJob.current = id; setCurrentJob(id); setDeleting(true); setError(null); setProgress(null); const stop = await subscribeJobProgress(id, setProgress); try { const next = kind === "cleanup" ? await api.cleanupDelete(id, result.scanId, targets, mode) : await api.purgeDelete(id, result.scanId, targets, mode); if (activeJob.current !== id) return null; setResult(null); setSelected(new Set()); setSummary(next); return next; } catch (reason) { if (!isCancelled(reason)) setError(messageFrom(reason)); return null; } finally { stop(); if (activeJob.current === id) { setDeleting(false); setProgress(null); setCurrentJob(null); } } }, [deleting, kind, result, selected], ); const reset = useCallback(() => { setResult(null); setSelected(new Set()); setSummary(null); setError(null); setProgress(null); }, []); return { kind, result, selected, progress, scanning, deleting, error, summary, currentJob, selectedTotal, selectedCount, runScan, cancel, toggleItem, toggleCategory, setCategorySelection, deselect, selectAll, clearSelection, clean, clearError: () => setError(null), reset, }; }