import{j as e,h as u}from"./codemirror-core-vkcnXJgD.js";import{t as F,a as R}from"./chunk-ML27DD5T-5CdR_75m.js";import{u as E,c as _,J as w,I as $}from"./react-dom-CwaHFgt6.js";import{k as A,b as v,P as z,W as j}from"./index-GtUymKue.js";import{a as D,c as W}from"./chunk-5PILOUBS-DDuMUNdl.js";import{c as T}from"./chunk-D5XJWRAV-DfgCdzDj.js";import{s as y,l as C}from"./chunk-KVDW62ZT-DmjVioxk.js";import{X as S}from"./xterm-oPL-mNjt.js";import{c as B}from"./chunk-NAV3ZXLI-DQhzijvk.js";import{u as P}from"./index-B9v0kxJY.js";import"./chunk-UX7UMZL5-DCk8rE83.js";import"./index-CokAva9Y.js";import"./Item-CdZrGEbe.js";import"./chunk-SLABUSGS-CDFdm_lz.js";import"./chunk-WQVQ7P2I-C3ZGafSE.js";import"./iconBase-DqQMrcxU.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 m;const s=r.match(/\[[a-zA-Z]+\]/)||[];let a=r;const t=((m=s==null?void 0:s[0])==null?void 0:m.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(y,{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 m,x;return e.jsx(B,{size:"sm",color:M[(m=i.data)==null?void 0:m.value],variant:"flat",children:(x=i.data)==null?void 0:x.label},i.key)})}),children:t=>e.jsx(C,{value:t.value,children:t.label},t.value)})},q=r=>{const{list:s,onSelect:a,selectedLog:t,refreshList:i,refreshLog:m,listLoading:x,logContent:f,listError:p,logLoading:g}=r,c=u.useRef(null),[n,o]=u.useState(new Set(["info","warn","error"])),[b]=E(A.backgroundImage,""),L=!!b,I=d=>d.split(`
`).map(h=>X(h)).filter(h=>n==="all"?!0:n.has(h.level)).map(h=>h.content).join(`\r
`),U=()=>{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=I(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:_("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(T,{className:"flex-row justify-start gap-3",children:[e.jsx(y,{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(C,{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:U,size:"sm",variant:"flat",color:"primary",children:"下载日志"}),e.jsx(v,{onPress:i,size:"sm",variant:"flat",children:"刷新列表"}),e.jsx(v,{onPress:m,size:"sm",variant:"flat",children:"刷新日志"})]})]}),e.jsxs(W,{className:"relative",children:[e.jsx(z,{loading:g}),e.jsx(S,{className:"w-full h-full",ref:c})]})]})]})},H=()=>{const r=u.useRef(null),[s,a]=u.useState(new Set(["info","warn","error"])),[t,i]=u.useState([]),[m]=E(A.backgroundImage,""),x=!!m,f=()=>{const g=t.filter(b=>s==="all"?!0:s.has(b.level)).map(b=>N(b.message,b.level)).join(`\r
`),c=new Blob([g],{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 g,c;try{const n=t.filter(o=>s==="all"?!0:s.has(o.level)).map(o=>N(o.message,o.level)).join(`\r
`);(g=r.current)==null||g.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:_("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 ue(){const{data:r,loading:s,error:a,refresh:t}=$(j.getLogList),[i,m]=u.useState(null),[x,f]=u.useState(null),[p,g]=u.useState(!1),c=o=>{m(o)},n=async()=>{if(i){g(!0);try{const o=await j.getLogContent(i);f(o)}catch(o){const b=o.message;w.error(`加载日志失败: ${b}`)}finally{g(!1)}}};return u.useEffect(()=>{r&&r.length>0&&m(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(F,{"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(H,{})}),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{ue as default};
