import { Activity, BatteryCharging, Cpu, HardDrive, MonitorSmartphone, Network, Power, RefreshCw, ShieldAlert, Timer, } from "lucide-react"; import { useEffect, useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; import { formatBytes, formatCount, formatMemoryBytes, formatRate, formatUptime, messageFrom, percent, } from "../lib/format"; import type { ProcessSample, SystemSnapshot, WidgetPrefs } from "../types"; import type { SecurityWorkspace } from "../hooks/useSecurityWorkspace"; import { ProcessTable } from "../components/ProcessTable"; import { Badge, Button, ConfirmDialog, Gauge, IconButton, InlineError, MetricBar, Notice, PageHeader, Row, Section, Tabs, Toggle, UsageBar, useToast, } from "../components/ui"; export function SystemPage({ snapshot, error, loading, refresh, initialProcessFilter, security, onOpenStartup, }: { snapshot: SystemSnapshot | null; error: string | null; loading: boolean; refresh: () => Promise; initialProcessFilter: string | null; security: SecurityWorkspace; onOpenStartup: (query: string) => void; }) { const { t, lang } = useI18n(); const { push } = useToast(); const [victim, setVictim] = useState(null); const [stopping, setStopping] = useState(false); // Two tabs rather than a third page: performance and processes are the same // question asked at different resolutions, and the roadmap did not want a // separate "Processes" entry in a sidebar that is already at ten. const [tab, setTab] = useState<"performance" | "processes">( initialProcessFilter ? "processes" : "performance", ); const [widget, setWidget] = useState(null); const [widgetBusy, setWidgetBusy] = useState(false); useEffect(() => { let cancelled = false; let stop: (() => void) | null = null; void api .widgetGet() .then((view) => !cancelled && setWidget(view.prefs)) .catch(() => undefined); void api .onWidgetSettings((next) => !cancelled && setWidget(next)) .then((unlisten) => { if (cancelled) unlisten(); else stop = unlisten; }); return () => { cancelled = true; stop?.(); }; }, []); const toggleWidget = async (enabled: boolean) => { setWidgetBusy(true); try { const next = await api.widgetSet({ enabled }); setWidget(next); push( enabled ? t("Desktop widget is now on.", "桌面实时监控已显示。") : t("Desktop widget is now hidden.", "桌面实时监控已隐藏。"), "success", ); } catch (reason) { push(messageFrom(reason), "danger"); } finally { setWidgetBusy(false); } }; const stop = async () => { if (!victim) return; setStopping(true); try { await api.stopProcess(victim); push( t(`${victim.name} was closed`, `已结束 ${victim.name}`), "success", ); setVictim(null); await refresh(); } catch (reason) { push(messageFrom(reason), "danger"); } finally { setStopping(false); } }; const memoryPercent = snapshot ? percent(snapshot.memory.used, snapshot.memory.total) : 0; return (
} onClick={() => void refresh()} disabled={loading} > {t("Refresh", "刷新")} } /> {error ? : null} {tab === "processes" ? ( ) : null} void stop()} onClose={() => setVictim(null)} busy={stopping} icon={} />
); }