import{r as o,j as e,c as p,bN as R,bO as y,a3 as N,aH as $,bH as T,bP as P,L as A,bQ as I,o as B}from"./index-iQt3bUih.js";function U({client:n,instanceId:m,running:d,initialStats:c,initialLive:l}){const[s,f]=o.useState(c??null),[j,b]=o.useState(l??null),[S,v]=o.useState(null),[i,u]=o.useState((l==null?void 0:l.available)===!1?l.reason??"REST API 無法連線":null);if(o.useEffect(()=>{c&&f(c)},[c]),o.useEffect(()=>{l&&(b(l),u(l.available?null:l.reason??"REST API 無法連線"))},[l]),o.useEffect(()=>{if(!d){f(null),b(null),v(null),u(null);return}let x=!0;const E=async()=>{const C=n.stats(m).then(r=>{x&&(f(r),v(null))}).catch(r=>{x&&v(r instanceof Error?r.message:String(r))}),F=n.live(m).then(r=>{x&&(b(r),u(r.available?null:r.reason??"REST API 無法連線"))}).catch(r=>{x&&u(r instanceof Error?r.message:String(r))});await Promise.allSettled([C,F])};E();const w=window.setInterval(()=>void E(),5e3);return()=>{x=!1,window.clearInterval(w)}},[n,m,d]),!d)return e.jsx("div",{className:`${p} text-sm text-ink-muted`,children:"伺服器尚未啟動，啟動後才會開始收集即時效能資料。"});const t=(j==null?void 0:j.metrics)??null,g=s&&R(s.cpuPercent)?`${s.cpuPercent.toFixed(0)}%`:s?"正在取樣":"等待資料",k=s?`${y(s.memoryBytes)} / ${y(s.memoryLimitBytes)}`:"等待資料",h=i?"REST 連線失敗":"等待 REST 資料";return e.jsxs("div",{className:"performance-cockpit flex flex-col gap-4",children:[e.jsxs("section",{className:"performance-intro",children:[e.jsxs("div",{children:[e.jsx("span",{className:"command-kicker",children:"LIVE TELEMETRY"}),e.jsx("h2",{children:"即時效能監控"}),e.jsx("p",{children:"每 5 秒同步主機與遊戲伺服器資料。"})]}),e.jsxs("div",{className:"performance-status",children:[e.jsx("span",{}),"監測中"]})]}),S&&e.jsxs("div",{className:`${p} border-amber-400 text-sm`,role:"alert",children:[e.jsx("b",{children:"主機效能讀取失敗："}),S]}),e.jsxs("section",{className:`${p} performance-snapshot`,children:[e.jsxs("h3",{className:"mb-4 inline-flex items-center gap-2 text-sm font-extrabold",children:[e.jsx(N,{}),"即時數值"]}),e.jsxs("div",{className:"grid gap-3 sm:grid-cols-2 lg:grid-cols-3",children:[e.jsx(a,{icon:e.jsx($,{}),label:(s==null?void 0:s.perCoreScope)==="system"?"主機 CPU":"CPU",value:g,note:(s==null?void 0:s.perCoreScope)==="system"?"Windows 主機總使用率":"PalServer 行程使用率"}),e.jsx(a,{icon:e.jsx(T,{}),label:(s==null?void 0:s.perCoreScope)==="system"?"主機記憶體":"記憶體",value:k,note:(s==null?void 0:s.perCoreScope)==="system"?"Windows 主機總用量":"PalServer 行程用量"}),e.jsx(a,{icon:e.jsx(A,{}),label:"伺服器運行時間",value:s!=null&&s.uptimeSeconds?P(s.uptimeSeconds):t?P(t.uptime):"暫無資料"}),e.jsx(a,{icon:e.jsx(I,{}),label:"遊戲時間",value:t?`第 ${t.days} 天`:h,note:t?void 0:i??void 0}),e.jsx(a,{icon:e.jsx(B,{}),label:"伺服器 FPS",value:t?String(t.serverfps):h,note:t?void 0:i??void 0}),e.jsx(a,{icon:e.jsx(N,{}),label:"影格時間",value:t?`${t.serverframetime.toFixed(1)} ms`:h,note:t?void 0:i??void 0})]})]})]})}function a({icon:n,label:m,value:d,note:c}){return e.jsxs("div",{className:`${p} min-w-0`,children:[e.jsxs("span",{className:"inline-flex items-center gap-2 text-xs font-bold text-ink-muted",children:[n,m]}),e.jsx("strong",{className:"mt-3 block break-words text-xl",children:d}),c&&e.jsx("small",{className:"mt-2 block text-ink-muted",children:c})]})}export{U as PerformanceTab};
