import { Archive, Disc3, Cloud, FileQuestion, FileVideo, HardDrive, Lock, MapPin, Package, ScanSearch, ShieldAlert, Trash2, } from "lucide-react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import * as apiClient from "../lib/api"; import { subscribeJobProgress } from "../lib/api"; import { useI18n } from "../i18n"; import { agoLabel, formatBytes, formatCount, messageFrom } from "../lib/format"; import { Button, Checkbox, EmptyState, InlineError, PageHeader, ProgressCard, useToast, } from "../components/ui"; import { CleanBar } from "../components/CleanBar"; import type { DeleteResult, JobProgress, LargeFile } from "../types"; const THRESHOLDS = [ { value: 100 * 1024 * 1024, label: "≥100MB" }, { value: 500 * 1024 * 1024, label: "≥500MB" }, { value: 1024 * 1024 * 1024, label: "≥1GB" }, ]; const CLASSES = [ { id: "all", en: "All", zh: "全部", icon: HardDrive }, { id: "video", en: "Video", zh: "视频", icon: FileVideo }, { id: "archive", en: "Archives", zh: "归档", icon: Archive }, { id: "installer", en: "Installers", zh: "安装包", icon: Package }, { id: "image", en: "Disk images", zh: "镜像/VHD", icon: Disc3 }, ]; const CLASS_ICON: Record = { video: FileVideo, archive: Archive, installer: Package, image: Disc3, other: HardDrive, }; /** 休眠判定沿用清理页口径:180 天无修改即值得标记。 */ const DORMANT_SECS = 180 * 86_400; export function LargeFilesPage({ embedded = false, onBadge, onHistory, }: { /** Rendered inside 空间清理 hub: page title hides, stats flow to onBadge. */ embedded?: boolean; onBadge?: (value: string | null) => void; /** Jump to 历史记录 for an undo. */ onHistory?: () => void; } = {}) { const { t, lang } = useI18n(); const { push } = useToast(); const [files, setFiles] = useState(null); const [scope, setScope] = useState([]); const [threshold, setThreshold] = useState(500 * 1024 * 1024); const [klass, setKlass] = useState("all"); const [hideCritical, setHideCritical] = useState(true); const [scanning, setScanning] = useState(false); const [deleting, setDeleting] = useState(false); const [error, setError] = useState(null); const [progress, setProgress] = useState(null); const [picked, setPicked] = useState>(new Set()); const [confirming, setConfirming] = useState(false); const [cleanDone, setCleanDone] = useState< { outcome: DeleteResult; elapsedMs: number } | null >(null); const job = useRef(null); const run = useCallback(async () => { if (scanning) return; const id = crypto.randomUUID?.() ?? String(Date.now()); job.current = id; setScanning(true); setError(null); setFiles(null); setPicked(new Set()); setProgress(null); const stop = await subscribeJobProgress(id, (event) => { if (event.event === "metered") { setProgress(event); } else if (event.event === "tick" || event.event === "phase") { setProgress(event); } }); try { const found = await apiClient.api.largeFilesScan( id, scope, threshold, klass === "all" ? null : klass, ); setFiles(found); } catch (reason) { if (!messageFrom(reason).toLowerCase().includes("cancel")) { setError(messageFrom(reason)); } } finally { stop(); setScanning(false); job.current = null; } }, [klass, scanning, scope, threshold]); const visible = useMemo(() => { if (!files) return null; return files.filter((file) => !hideCritical || !file.maybeMounted); }, [files, hideCritical]); const totalBytes = visible?.reduce((sum, file) => sum + file.size, 0) ?? 0; const pickedBytes = visible ?.filter((file) => picked.has(file.path)) .reduce((sum, file) => sum + file.size, 0) ?? 0; const pickedCount = picked.size; // Hub badge: found files, or null before a scan. useEffect(() => { if (!embedded || !onBadge) return; onBadge( visible && visible.length > 0 ? `${formatCount(visible.length)} 个 · ${formatBytes(totalBytes)}` : null, ); }, [embedded, onBadge, totalBytes, visible]); const toggle = (path: string) => { setPicked((current) => { const next = new Set(current); if (next.has(path)) next.delete(path); else next.add(path); return next; }); }; const confirmDelete = async () => { const id = crypto.randomUUID?.() ?? String(Date.now()); job.current = id; setDeleting(true); setProgress(null); const stop = await subscribeJobProgress(id, (event) => { if (event.event === "tick" || event.event === "deleted") setProgress(event); }); const startedAt = Date.now(); try { const outcome = await apiClient.api.largeFilesDelete(id, [...picked], "trash"); setFiles((current) => current?.filter((f) => !picked.has(f.path)) ?? null); setPicked(new Set()); setConfirming(false); setCleanDone({ outcome, elapsedMs: Date.now() - startedAt }); push( lang === "zh" ? `已将 ${outcome.removed} 个大文件移入回收站(${formatBytes(outcome.freed)}),可在「历史记录」撤销` : `Moved ${outcome.removed} files to the Recycle Bin (${formatBytes(outcome.freed)})`, "success", ); } catch (reason) { setError(messageFrom(reason)); } finally { stop(); setDeleting(false); job.current = null; } }; const now = Date.now(); return (
{embedded ? null : ( )} {error ? setError(null)} /> : null}
{t("Scope", "范围")}
{["C:", "D:", "E:"].map((drive) => ( ))}
{t("At least", "阈值")}
{THRESHOLDS.map((option) => ( ))}
{t("Type", "类型")}
{CLASSES.map((option) => { const Icon = option.icon; return ( ); })}
{scanning ? ( { if (job.current) void apiClient.api.cancelJob(job.current); }} /> ) : null} {deleting ? ( onHistory?.()} onDismiss={() => undefined} /> ) : null} {!deleting && cleanDone ? ( onHistory?.()} onDismiss={() => setCleanDone(null)} onRescan={() => void run()} /> ) : null} {!files && !scanning ? ( } title={t("No large-file scan yet", "还没有扫描过大文件")} description={t( "Start scan walks every drive above the size floor; results sort heaviest first.", "点击「开始扫描」按体积门槛遍历所选磁盘,结果从最大往下排。", )} /> ) : null} {visible && visible.length === 0 && !scanning ? ( } title={t("No files above the threshold", "没有超过阈值的大文件")} description={t( "Try a lower size floor or another scope.", "可以调低体积门槛或更换扫描范围再试。", )} /> ) : null} {visible && visible.length > 0 ? ( <>
{formatCount(visible.length)} {t("large files", "个大文件")} ·{" "} {formatBytes(totalBytes)} {pickedCount > 0 ? t( `${pickedCount} selected · ${formatBytes(pickedBytes)} → Recycle Bin`, `已选 ${pickedCount} 项 · ${formatBytes(pickedBytes)} → 移入回收站`, ) : t( "Dormant files (180 days untouched) are tagged; disk images held by Hyper-V/WSL stay locked. Page files are hidden; hiberfil.sys frees via turning hibernation off.", "长期未修改(180 天+)的文件会带休眠标记;被 Hyper-V/WSL 使用的镜像会锁定。页面文件(pagefile/swapfile)已自动隐藏;hiberfil.sys 可通过关闭休眠释放。", )}
{confirming ? (
{lang === "zh" ? `将 ${pickedCount} 个大文件(${formatBytes(pickedBytes)})移入回收站?` : `Move ${pickedCount} files (${formatBytes(pickedBytes)}) to the Recycle Bin?`} {t( "Undo from History while the Recycle Bin holds them.", "回收站清空前,可随时在「历史记录」撤销。", )}
) : null}
    {visible.map((file, index) => { const Icon = CLASS_ICON[file.class] ?? HardDrive; const dormant = file.modifiedMs > 0 && (now - file.modifiedMs) / 1000 > DORMANT_SECS; return (
  • toggle(file.path)} label={file.displayPath} /> {String(index + 1).padStart(2, "0")}
    {file.path.split(/[\\/]/).pop()} {file.displayPath}
    {dormant ? ( {agoLabel(file.modifiedMs, lang)} {t("untouched", "未修改")} ) : null} {file.cloudHint ? ( {t("cloud copy?", "网盘可能有副本?")} ) : null} {file.maybeMounted ? ( {t("check in use", "占用检查")} ) : null} {formatBytes(file.size)}
  • ); })}
) : null}
); }