import{j as e,h as u}from"./codemirror-core-DIzFijqu.js";import{s as E,l as _,t as I,a as R}from"./chunk-KVDW62ZT-D3Q7mIBo.js";import{u as A,c as y,_ as w,Z as $}from"./react-dom-C0IJc3nR.js";import{k as C,b as v,P as z,W as j}from"./index-DiFbo-KR.js";import{b as D,c as W,a as T}from"./chunk-5PILOUBS-CCbxiGgU.js";import{X as S}from"./xterm-uFUIfIg0.js";import{c as B}from"./chunk-NAV3ZXLI-DYK7odP-.js";import{q as P}from"./index-Cis8d5Y_.js";import"./chunk-WQVQ7P2I-De41EkH8.js";import"./index-CokAva9Y.js";import"./chunk-SLABUSGS-DTr9GL0S.js";import"./chunk-M3MASYO7-BtOvSeRL.js";import"./iconBase-ClXMXqAg.js";var l=(r=>(r.DEBUG="debug",r.INFO="info",r.WARN="warn",r.ERROR="error",r.FATAL="fatal",r))(l||{});const G={[l.DEBUG]:"green",[l.INFO]:"black",[l.WARN]:"yellow",[l.ERROR]:"red",[l.FATAL]:"red"},X=r=>{var g;const s=r.match(/\[[a-zA-Z]+\]/)||[];let a=r;const t=((g=s==null?void 0:s[0])==null?void 0:g.replace("[","").replace("]",""))??l.INFO;switch(G[t]){case"green":a=`\x1B[32m${a}\x1B[0m`;break;case"black":a=`\x1B[30m${a}\x1B[0m`;break;case"yellow":a=`\x1B[33m${a}\x1B[0m`;break;case"red":a=`\x1B[31m${a}\x1B[0m`;break;default:a=`\x1B[30m${a}\x1B[0m`}return{content:a,level:t}},N=(r,s)=>{let a=r;switch(s){case l.DEBUG:a=`\x1B[32m[DEBUG] ${r}\x1B[0m`;break;case l.INFO:a=`\x1B[30m[INFO] ${r}\x1B[0m`;break;case l.WARN:a=`\x1B[33m[WARN] ${r}\x1B[0m`;break;case l.ERROR:a=`\x1B[31m[ERROR] ${r}\x1B[0m`;break;case l.FATAL:a=`\x1B[31m[FATAL] ${r}\x1B[0m`;break;default:a=`\x1B[30m${r}\x1B[0m`}return a},M={[l.DEBUG]:"default",[l.INFO]:"primary",[l.WARN]:"warning",[l.ERROR]:"primary",[l.FATAL]:"primary"},O=r=>{const{selectedKeys:s,onSelectionChange:a}=r;return e.jsx(E,{selectedKeys:s,onSelectionChange:t=>{t!=="all"&&(t==null?void 0:t.size)===0&&(t="all"),a(t)},label:"日志级别",selectionMode:"multiple","aria-label":"Log Level",classNames:{label:"mb-2",trigger:"bg-default-100/50 backdrop-blur-sm hover:!bg-default-200/50",popoverContent:"bg-opacity-50 backdrop-blur-sm"},size:"sm",items:[{label:"Debug",value:l.DEBUG},{label:"Info",value:l.INFO},{label:"Warn",value:l.WARN},{label:"Error",value:l.ERROR},{label:"Fatal",value:l.FATAL}],renderValue:t=>t.length===5?e.jsx(B,{size:"sm",color:"primary",variant:"flat",children:"全部"}):e.jsx("div",{className:"flex gap-2",children:t.map(i=>{var g,x;return e.jsx(B,{size:"sm",color:M[(g=i.data)==null?void 0:g.value],variant:"flat",children:(x=i.data)==null?void 0:x.label},i.key)})}),children:t=>e.jsx(_,{value:t.value,children:t.label},t.value)})},q=r=>{const{list:s,onSelect:a,selectedLog:t,refreshList:i,refreshLog:g,listLoading:x,logContent:f,listError:p,logLoading:m}=r,c=u.useRef(null),[n,o]=u.useState(new Set(["info","warn","error"])),[b]=A(C.backgroundImage,""),L=!!b,U=d=>d.split(`
`).map(h=>X(h)).filter(h=>n==="all"?!0:n.has(h.level)).map(h=>h.content).join(`\r
`),F=()=>{if(!f)return;const d=new Blob([f],{type:"text/plain"}),k=URL.createObjectURL(d),h=document.createElement("a");h.href=k,h.download=`${t}.log`,h.click(),URL.revokeObjectURL(k)};return u.useEffect(()=>{if(!c.current||!f)return;c.current.clear();const d=U(f);c.current.write(d+`\r
napcat@webui:~$ `)},[f,n]),e.jsxs(e.Fragment,{children:[e.jsx("title",{children:"历史日志 - NapCat WebUI"}),e.jsxs(D,{className:y("max-w-full h-full backdrop-blur-sm border border-white/40 dark:border-white/10 shadow-sm",L?"bg-white/20 dark:bg-black/10":"bg-white/60 dark:bg-black/40"),children:[e.jsxs(W,{className:"flex-row justify-start gap-3",children:[e.jsx(E,{label:"选择日志",size:"sm",isLoading:x,errorMessage:p==null?void 0:p.message,classNames:{trigger:"bg-default-100/50 backdrop-blur-sm hover:!bg-default-200/50"},placeholder:"选择日志",onChange:d=>{const k=d.target.value;k&&a(k)},selectedKeys:[t||""],items:s.map(d=>({value:d,label:d})),children:d=>e.jsx(_,{value:d.value,children:d.label},d.value)}),e.jsx(O,{selectedKeys:n,onSelectionChange:o}),e.jsxs("div",{className:"flex gap-2 ml-auto",children:[e.jsx(v,{className:"flex-shrink-0",onPress:F,size:"sm",variant:"flat",color:"primary",children:"下载日志"}),e.jsx(v,{onPress:i,size:"sm",variant:"flat",children:"刷新列表"}),e.jsx(v,{onPress:g,size:"sm",variant:"flat",children:"刷新日志"})]})]}),e.jsxs(T,{className:"relative",children:[e.jsx(z,{loading:m}),e.jsx(S,{className:"w-full h-full",ref:c})]})]})]})},Z=()=>{const r=u.useRef(null),[s,a]=u.useState(new Set(["info","warn","error"])),[t,i]=u.useState([]),[g]=A(C.backgroundImage,""),x=!!g,f=()=>{const m=t.filter(b=>s==="all"?!0:s.has(b.level)).map(b=>N(b.message,b.level)).join(`\r
`),c=new Blob([m],{type:"text/plain"}),n=URL.createObjectURL(c),o=document.createElement("a");o.href=n,o.download="napcat.log",o.click(),URL.revokeObjectURL(n)},p=()=>{var m,c;try{const n=t.filter(o=>s==="all"?!0:s.has(o.level)).map(o=>N(o.message,o.level)).join(`\r
`);(m=r.current)==null||m.clear(),(c=r.current)==null||c.write(n)}catch(n){console.error(n),w.error("获取实时日志失败")}};return u.useEffect(()=>{p()},[s,t]),u.useEffect(()=>{const c=(()=>{try{const n=j.getRealTimeLogs(o=>{i(b=>{const L=[...b,...o];return L.length>1e3&&L.splice(0,L.length-1e3),L})});return()=>{n.close()}}catch{w.error("获取实时日志失败")}})();return()=>{console.log("close"),c==null||c()}},[]),e.jsxs(e.Fragment,{children:[e.jsx("title",{children:"实时日志 - NapCat WebUI"}),e.jsxs("div",{className:y("flex items-center gap-2 p-2 rounded-2xl border backdrop-blur-sm transition-all shadow-sm mb-4",x?"bg-white/20 dark:bg-black/10 border-white/40 dark:border-white/10":"bg-white/60 dark:bg-black/40 border-white/40 dark:border-white/10"),children:[e.jsx(O,{selectedKeys:s,onSelectionChange:a}),e.jsx(v,{className:"flex-shrink-0",onPress:f,startContent:e.jsx(P,{className:"text-lg"}),color:"primary",variant:"flat",children:"下载日志"})]}),e.jsx("div",{className:"flex-1 h-full overflow-hidden",children:e.jsx(S,{ref:r})})]})};function ce(){const{data:r,loading:s,error:a,refresh:t}=$(j.getLogList),[i,g]=u.useState(null),[x,f]=u.useState(null),[p,m]=u.useState(!1),c=o=>{g(o)},n=async()=>{if(i){m(!0);try{const o=await j.getLogContent(i);f(o)}catch(o){const b=o.message;w.error(`加载日志失败: ${b}`)}finally{m(!1)}}};return u.useEffect(()=>{r&&r.length>0&&g(r[0])},[r]),u.useEffect(()=>{i&&n()},[i]),e.jsx("div",{className:"h-[calc(100vh_-_8rem)] flex flex-col gap-4 items-center pt-4 px-2",children:e.jsxs(I,{"aria-label":"Logs",classNames:{panel:"w-full flex-1 h-full py-0 flex flex-col gap-4",base:"flex-shrink-0 !h-fit",tabList:"bg-white/40 dark:bg-black/20 backdrop-blur-md",cursor:"bg-white/80 dark:bg-white/10 backdrop-blur-md shadow-sm"},children:[e.jsx(R,{title:"实时日志",children:e.jsx(Z,{})}),e.jsx(R,{title:"历史日志",children:e.jsx(q,{list:r||[],onSelect:c,selectedLog:i||void 0,refreshList:t,refreshLog:n,listLoading:s,logLoading:p,listError:a,logContent:x||void 0})})]})})}export{ce as default};
