import { useCallback, useEffect, useMemo, useState } from "react"; import type { ResourceManagerAppProps, ResourceManagerTab } from "@/resource-manager/contract.js"; import { type ResourceUsageCategory, type ResourceUsageSnapshot } from "@zcode/shared"; import { TID_RESOURCE_MANAGER_TAB, testId } from "@zcode/shared/test-ids"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs.js"; import { useZCodeIntl } from "@/i18n/index.js"; import { StorageSection } from "./storage/StorageSection.js"; import { NetworkSection } from "./NetworkSection.js"; import { UNSAMPLED_PLACEHOLDER, UsageGroup, UsageMeter } from "./resourceUsageParts.js"; import { clampPercent, formatBytes, formatPercent, groupResourceUsage, type ResourceUsageGroupView, } from "./resourceUsageView.js"; export type { ResourceManagerAppProps, ResourceManagerTab } from "@/resource-manager/contract.js"; const RESOURCE_MANAGER_TABS: ResourceManagerTab[] = ["cpu", "memory", "storage", "network"]; const TAB_LABEL_IDS: Record = { cpu: "resourceManager.cpu", memory: "resourceManager.memory", storage: "resourceManager.storage", network: "resourceManager.network", }; const DEFAULT_REFRESH_INTERVAL_MS = 1_000; const CATEGORY_LABEL_IDS: Record = { base: "resourceManager.category.base", task: "resourceManager.category.task", "builtin-plugin": "resourceManager.category.builtinPlugin", "community-plugin": "resourceManager.category.communityPlugin", }; /** * 资源管理器窗口 UI。 * 组件放在 packages/ui 内是为了纳入 Tailwind 扫描;desktop 的独立 renderer 入口只负责挂载。 */ export function ResourceManagerApp({ getSnapshot, setSamplingActive, storage, network, refreshIntervalMs = DEFAULT_REFRESH_INTERVAL_MS, initialTab = "cpu", }: ResourceManagerAppProps) { const { intl } = useZCodeIntl(); const [tab, setTab] = useState(initialTab); // CPU / 内存共用一份进程快照;存储 tab 激活时停止轮询,避免和扫盘争抢 IO。 const pollingActive = tab === "cpu" || tab === "memory"; const [snapshot, setSnapshot] = useState(null); const [error, setError] = useState(null); const [expanded, setExpanded] = useState>({ base: true, task: true, "builtin-plugin": true, "community-plugin": true, }); useEffect(() => { if (!getSnapshot) { setError(intl.formatMessage({ id: "resourceManager.unavailable" })); return; } if (!pollingActive) return; setSamplingActive?.(true); let disposed = false; let inFlight = false; async function refresh() { // 请求串行:上一轮未返回(Host 采样慢)时不叠加新请求。 if (inFlight) return; inFlight = true; try { const next = await getSnapshot!(); if (disposed) return; setSnapshot(next); setError(null); } catch (err) { if (disposed) return; setError(err instanceof Error ? err.message : String(err)); } finally { inFlight = false; } } void refresh(); const interval = window.setInterval(() => void refresh(), refreshIntervalMs); return () => { disposed = true; window.clearInterval(interval); // 停止 renderer 定时器还不够,Host 在途采样也必须随页面生命周期取消。 setSamplingActive?.(false); }; }, [getSnapshot, intl, pollingActive, refreshIntervalMs, setSamplingActive]); const groups = useMemo(() => groupResourceUsage(snapshot?.processes ?? []), [snapshot]); const toggleGroup = useCallback((category: ResourceUsageCategory) => { setExpanded((current) => ({ ...current, [category]: !current[category] })); }, []); return (

{intl.formatMessage({ id: "resourceManager.title" })}

{pollingActive ? (
{snapshot ? intl.formatMessage( { id: "resourceManager.processCount" }, { count: snapshot.processes.length }, ) : intl.formatMessage({ id: "resourceManager.loading" })}
) : null}
setTab(value as ResourceManagerTab)}> {RESOURCE_MANAGER_TABS.map((item) => ( {intl.formatMessage({ id: TAB_LABEL_IDS[item] })} ))}
{tab === "storage" ? ( ) : tab === "network" ? ( ) : ( )}
); } /** CPU / 内存 tab 的正文:左侧对应指标卡,右侧分组列表只显示当前指标列。 */ function UsageTabContent({ metric, snapshot, error, groups, expanded, onToggleGroup, }: { metric: "cpu" | "memory"; snapshot: ResourceUsageSnapshot | null; error: string | null; groups: ResourceUsageGroupView[]; expanded: Record; onToggleGroup: (category: ResourceUsageCategory) => void; }) { const { intl } = useZCodeIntl(); return ( <> {error ? (
{error}
) : null}
{groups.map((group) => ( onToggleGroup(group.category)} emptyText={intl.formatMessage({ id: "resourceManager.empty" })} samplingText={intl.formatMessage({ id: "resourceManager.sampling" })} columns={{ process: intl.formatMessage({ id: "resourceManager.column.process" }), pid: intl.formatMessage({ id: "resourceManager.column.pid" }), cpu: intl.formatMessage({ id: "resourceManager.cpu" }), memory: intl.formatMessage({ id: "resourceManager.memory" }), }} /> ))}
); }