import { Activity, BatteryCharging, Cpu, HardDrive, Network, Power, RefreshCw, ShieldAlert, Timer, } from "lucide-react"; import { useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; import { formatBytes, formatCount, formatMemory, formatRate, formatUptime, messageFrom, percent, } from "../lib/format"; import type { ProcessSample, SystemSnapshot } from "../types"; import { ConfirmDialog, Gauge, InlineError, MetricBar, PageHeader, SectionTitle, UsageBar, useToast, } from "../components/ui"; export function SystemPage({ snapshot, error, loading, refresh, }: { snapshot: SystemSnapshot | null; error: string | null; loading: boolean; refresh: () => Promise; }) { const { t, lang } = useI18n(); const { push } = useToast(); const [victim, setVictim] = useState(null); const [stopping, setStopping] = useState(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 (
void refresh()} disabled={loading} > {t("Refresh", "刷新")} } /> {error ? : null}
{t("Processor", "处理器")} {snapshot?.cpu.brand || t("Reading processor…", "正在读取处理器…")}
{Math.round(snapshot?.cpu.total ?? 0)}% {t("in use", "占用")}
{snapshot?.cpu.physical_cores ?? 0} {t("physical", "物理")} /{" "} {snapshot?.cpu.logical_cores ?? 0} {t("logical", "逻辑")} {t("Load", "负载")} {(snapshot?.cpu.load[0] ?? 0).toFixed(2)}
{(snapshot?.cpu.per_core ?? []).slice(0, 16).map((value, index) => ( 75 ? "is-hot" : value > 40 ? "is-busy" : "" }`} title={`${t("Core", "核心")} ${index + 1}: ${Math.round(value)}%`} > ))}
{t("Memory", "内存")} {snapshot ? `${formatMemory(snapshot.memory.used)} / ${formatMemory( Math.max(snapshot.memory.installed, snapshot.memory.total), )}` : t("Reading memory…", "正在读取内存…")}
{Math.round(memoryPercent)}% {snapshot ? formatMemory(snapshot.memory.available) : "—"} {t("free", "可用")}
{t("Used", "已用")} {formatMemory(snapshot?.memory.used ?? 0)}
{(snapshot?.memory.installed ?? 0) > (snapshot?.memory.total ?? 0) ? (
{t("Reserved (GPU)", "硬件保留(含核显)")} {formatMemory( Math.max( 0, (snapshot?.memory.installed ?? 0) - (snapshot?.memory.total ?? 0), ), )}
) : null}
{t("Swap", "交换")} {formatMemory(snapshot?.memory.swap_used ?? 0)} / {formatMemory(snapshot?.memory.swap_total ?? 0)}
{t("Network", "网络")} {snapshot?.network.primary || t("All interfaces", "全部接口")}
↓ {formatRate(snapshot?.network.rx_per_sec ?? 0)} {t("Download", "下载")}
↑ {formatRate(snapshot?.network.tx_per_sec ?? 0)} {t("Upload", "上传")}
R {formatRate(snapshot?.disk.read_per_sec ?? 0)} {t("Disk read", "磁盘读取")}
W {formatRate(snapshot?.disk.write_per_sec ?? 0)} {t("Disk write", "磁盘写入")}
{snapshot ? t( `Up for ${formatUptime(snapshot.uptime_secs, lang)}`, `已运行 ${formatUptime(snapshot.uptime_secs, lang)}`, ) : t("Sampling…", "正在采样…")}
{snapshot?.battery?.present ? (
{t("Battery", "电池")} {snapshot.battery.percent}% {snapshot.battery.charging ? t("Charging", "正在充电") : snapshot.battery.on_ac ? t("Connected to power", "已接通电源") : t("On battery", "使用电池")}
{snapshot.battery.health_percent !== null ? (
{t("Health", "健康度")} {snapshot.battery.health_percent}%
) : null} {snapshot.battery.cycle_count !== null ? (
{t("Cycles", "循环次数")} {snapshot.battery.cycle_count}
) : null} {snapshot.battery.temperature_c !== null ? (
{t("Temperature", "温度")} {snapshot.battery.temperature_c.toFixed(1)}°C
) : null}
) : null}
{t("Process", "进程")} CPU {t("Memory", "内存")}
{(snapshot?.top_processes ?? []).map((process) => (
{process.name} PID {process.pid} {process.cpu.toFixed(1)}% {formatBytes(process.memory)}
))}
{(snapshot?.volumes ?? []).map((volume) => { const use = percent(volume.total - volume.free, volume.total); return (
{volume.name || volume.mount_point} {formatBytes(volume.free)} {t("free of", "可用,共")}{" "} {formatBytes(volume.total)} 88 ? "red" : "blue"} />
{Math.round(use)}%
); })}
{snapshot?.host.hostname || t("This computer", "这台电脑")} {[ snapshot?.host.os_version || snapshot?.host.os_name, snapshot?.host.arch, ] .filter(Boolean) .join(" · ")}
{t( `${formatCount(snapshot?.processes ?? 0)} processes · ${formatCount( snapshot?.network.interfaces ?? 0, )} network interfaces`, `${formatCount(snapshot?.processes ?? 0)} 个进程 · ${formatCount( snapshot?.network.interfaces ?? 0, )} 个网络接口`, )}
void stop()} onClose={() => setVictim(null)} busy={stopping} icon={} />
); }