import { AppWindow, Boxes, ChevronDown, CircleGauge, Eraser, History, LayoutDashboard, MonitorSmartphone, RefreshCw, Settings, ShieldCheck, Sparkles, } from "lucide-react"; import { useCallback, useEffect, useState, type ReactNode } from "react"; import { useAppsWorkspace } from "./hooks/useAppsWorkspace"; import { usePermissionsWorkspace } from "./hooks/usePermissionsWorkspace"; import { useScanWorkspace } from "./hooks/useScanWorkspace"; import { useSystemSnapshot } from "./hooks/useSystemSnapshot"; import { useUpdateFlow } from "./hooks/useUpdateFlow"; import { detectLanguage, I18nProvider, useI18n } from "./i18n"; import { api } from "./lib/api"; import { messageFrom } from "./lib/format"; import type { Bootstrap, DeleteMode, Language, PageId, SettingsView, } from "./types"; import { useToast } from "./components/ui"; import { SetupPanel } from "./components/SetupPanel"; import appIconUrl from "../src-tauri/icons/128x128.png"; import { ApplicationsPage } from "./pages/ApplicationsPage"; import { HistoryPage } from "./pages/HistoryPage"; import { OverviewPage } from "./pages/OverviewPage"; import { PermissionsPage } from "./pages/PermissionsPage"; import { SettingsPage } from "./pages/SettingsPage"; import { SpacePage } from "./pages/SpacePage"; import { StoragePage } from "./pages/StoragePage"; import { SystemPage } from "./pages/SystemPage"; import { WidgetPage } from "./pages/WidgetPage"; const NAV_GROUPS: { labelEn: string; labelZh: string; items: { id: PageId; labelEn: string; labelZh: string; icon: typeof LayoutDashboard; }[]; }[] = [ { labelEn: "Maintenance", labelZh: "维护", items: [ { id: "overview", labelEn: "Computer overview", labelZh: "电脑概览", icon: LayoutDashboard, }, { id: "space", labelEn: "Space cleaning", labelZh: "空间清理", icon: Eraser, }, { id: "applications", labelEn: "Uninstall apps", labelZh: "应用卸载", icon: AppWindow, }, { id: "permissions", labelEn: "Permissions", labelZh: "权限管理", icon: ShieldCheck, }, ], }, { labelEn: "Insights", labelZh: "洞察", items: [ { id: "storage", labelEn: "Storage", labelZh: "存储空间", icon: Boxes, }, { id: "system", labelEn: "Real-time monitor", labelZh: "实时监控", icon: CircleGauge, }, ], }, { labelEn: "Desktop", labelZh: "桌面", items: [ { id: "widget", labelEn: "Desktop widget", labelZh: "桌面小工具", icon: MonitorSmartphone, }, ], }, ]; const PAGE_TITLES: Record = { overview: ["Computer overview", "电脑概览"], space: ["Space cleaning", "空间清理"], applications: ["Uninstall apps", "应用卸载"], permissions: ["Permissions", "权限管理"], widget: ["Desktop widget", "桌面小工具"], storage: ["Storage", "存储空间"], system: ["Real-time monitor", "实时监控"], history: ["History", "历史记录"], settings: ["Settings", "设置"], }; function BrandMark({ small = false }: { small?: boolean }) { return ( ); } function LoadingScreen() { return (
Squid Preparing your computer care workspace…
); } function BootstrapError({ message }: { message: string }) { return (
Squid could not start {message}
); } const SPACE_OPTIONAL_CATEGORIES = [ "update-installers", "project-artifacts", "developer", ]; /** 空间清理 defaults: daily groups pre-checked, opt-in groups untouched. */ function spaceDefaults(item: { category: string }): boolean { return !SPACE_OPTIONAL_CATEGORIES.includes(item.category); } export default function App() { const { push } = useToast(); const [bootstrap, setBootstrap] = useState(null); const [bootError, setBootError] = useState(null); const [page, setPage] = useState("overview"); const [lang, setLang] = useState(detectLanguage); const [settings, setSettings] = useState(null); const [autoCheck, setAutoCheck] = useState(true); const [setupOpen, setSetupOpen] = useState(false); // The 托盘驻留 case: a hidden main window must stop sampling the machine. const [windowVisible, setWindowVisible] = useState(true); // 空间清理 defaults: daily groups pre-checked, opt-in groups untouched. const cleanup = useScanWorkspace("cleanup", spaceDefaults); const purge = useScanWorkspace("purge", spaceDefaults); const apps = useAppsWorkspace(); const permissions = usePermissionsWorkspace(page === "permissions"); const systemActive = (page === "overview" || page === "system") && windowVisible; // Only the 实时监控 top-process table needs the expensive per-pid walk; // the overview tiles ride the light refresh. const system = useSystemSnapshot(systemActive, 1800, page === "system"); const update = useUpdateFlow(bootstrap?.updateFeedUrl ?? "", autoCheck); useEffect(() => { let cancelled = false; void api .bootstrap() .then((next) => { if (cancelled) return; setBootstrap(next); setSettings(next.settings); setLang(next.language); setAutoCheck(next.updateCheckEnabled); setSetupOpen(!next.settings.setupReviewed); document.documentElement.lang = next.language === "zh" ? "zh-CN" : "en"; }) .catch((reason) => { if (!cancelled) setBootError(messageFrom(reason)); }); return () => { cancelled = true; }; }, []); useEffect(() => { let cancelled = false; let unlisten: (() => void) | null = null; void api .onNavigate((next) => { if (!cancelled) setPage(next); }) .then((stop) => { if (cancelled) stop(); else unlisten = stop; }); return () => { cancelled = true; unlisten?.(); }; }, []); useEffect(() => { let cancelled = false; let unlisten: (() => void) | null = null; void api .onWindowVisible((next) => { if (!cancelled) setWindowVisible(next); }) .then((stop) => { if (cancelled) stop(); else unlisten = stop; }); return () => { cancelled = true; unlisten?.(); }; }, []); useEffect(() => { const order: PageId[] = [ "overview", "space", "applications", "permissions", "storage", "system", "widget", "history", "settings", ]; // Ctrl/Cmd+1..9 map in navigation order. const onKey = (event: KeyboardEvent) => { if (!(event.metaKey || event.ctrlKey) || event.altKey || event.shiftKey) return; const index = event.key === "0" ? 9 : Number(event.key) - 1; if (!Number.isInteger(index) || index < 0 || index >= order.length) return; event.preventDefault(); setPage(order[index]); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, []); const changeLanguage = useCallback( async (nextLanguage: Language) => { setLang(nextLanguage); document.documentElement.lang = nextLanguage === "zh" ? "zh-CN" : "en"; try { const next = await api.settingsSave({ language: nextLanguage }); setSettings(next); } catch (reason) { push(messageFrom(reason), "danger"); } }, [push], ); const changeAutoCheck = useCallback( (enabled: boolean) => { setAutoCheck(enabled); void api .setUpdateCheckEnabled(enabled) .catch((reason) => { setAutoCheck(!enabled); push(messageFrom(reason), "danger"); }); }, [push], ); const finishSetup = useCallback(async () => { setSetupOpen(false); try { const next = await api.settingsSave({ setupReviewed: true }); setSettings(next); } catch (reason) { push(messageFrom(reason), "danger"); } }, [push]); if (bootError) return ; if (!bootstrap || !settings) return ; const deleteMode: DeleteMode = settings.deleteMode; const activeUpdate = update.status?.state === "available" || update.status?.state === "ready" ? update.status : null; return ( {page === "overview" ? ( ) : null} {page === "space" ? ( setPage("history")} /> ) : null} {page === "applications" ? ( setPage("history")} /> ) : null} {page === "permissions" ? : null} {page === "widget" ? : null} {page === "storage" ? : null} {page === "system" ? ( ) : null} {page === "history" ? : null} {page === "settings" ? ( ) : null} {setupOpen ? void finishSetup()} /> : null} ); } function AppChrome({ bootstrap, page, setPage, updateVersion, children, }: { bootstrap: Bootstrap; page: PageId; setPage: (page: PageId) => void; updateVersion: string | null; children: ReactNode; }) { const { t, lang } = useI18n(); const title = PAGE_TITLES[page][lang === "zh" ? 1 : 0]; return (
{title} {bootstrap.hardware}
{t("Local & private", "本地与私密")} {updateVersion ? ( ) : null}
{children}
); }