import{u as E,h as F,i as C,j as e}from"./index-Dyazi077.js";import{b as d}from"./react-vendor-CjpoAPrF.js";import{B as u}from"./Badge-DcwOuuF4.js";function T(t){for(const s of Object.values(t||{})){const r=v(s);if(r)return r}return null}function v(t){if(!Array.isArray(t))return null;for(const s of t){if((s==null?void 0:s.type)==="file"&&(s!=null&&s.path))return s.path;if((s==null?void 0:s.type)==="dir"&&Array.isArray(s.children)){const r=v(s.children);if(r)return r}}return null}function p(t){return(t||[]).reduce((s,r)=>s+(r.type==="file"?1:p(r.children)),0)}function y({items:t,expanded:s,selectedPath:r,onToggle:f,onSelect:x,depth:l=0}){return!Array.isArray(t)||!t.length?null:t.map(a=>{const c=a.path||`${l}-${a.name}`;if(a.type==="dir"){const o=s[c]??l<1;return e.jsxs("li",{children:[e.jsxs("button",{type:"button",className:`tree-item tree-dir ${o?"open":""}`.trim(),onClick:()=>f(c),children:[e.jsx("span",{className:"tree-glyph"}),e.jsx("span",{className:"tree-name",children:a.name})]}),o?e.jsx("ul",{className:"tree-children",children:e.jsx(y,{items:a.children,expanded:s,selectedPath:r,onToggle:f,onSelect:x,depth:l+1})}):null]},c)}return e.jsx("li",{children:e.jsxs("button",{type:"button",className:`tree-item tree-file ${r===a.path?"active":""}`.trim(),onClick:()=>x(a.path),children:[e.jsx("span",{className:"tree-glyph file"}),e.jsx("span",{className:"tree-name",children:a.name})]})},c)})}function P(){const{refreshToken:t}=E(),[s,r]=d.useState({}),[f,x]=d.useState({}),[l,a]=d.useState(null),[c,o]=d.useState(""),[m,N]=d.useState(!1);d.useEffect(()=>{let n=!1;return F().then(i=>{if(!n){r(i);const h=T(i);h&&a(j=>j||h)}}).catch(()=>{n||r({})}),()=>{n=!0}},[t]),d.useEffect(()=>{if(!l)return;let n=!1;return N(!0),C(l).then(i=>{n||o(i.content||"")}).catch(()=>{n||o("[读取失败]")}).finally(()=>{n||N(!1)}),()=>{n=!0}},[l]);const g=d.useMemo(()=>Object.values(s).reduce((n,i)=>n+p(i),0),[s]),b=c?c.split(/\r?\n/).length:0;return e.jsxs("section",{className:"dashboard-page",children:[e.jsxs("header",{className:"page-header",children:[e.jsx("h2",{children:"文档浏览"}),e.jsxs(u,{tone:"blue",children:[g," 个文件"]})]}),e.jsxs("div",{className:"content-grid files-layout",children:[e.jsxs("article",{className:"card files-tree-card",children:[e.jsxs("div",{className:"card-header",children:[e.jsxs("div",{children:[e.jsx("div",{className:"section-label",children:"FILE TREE"}),e.jsx("div",{className:"card-title",children:"目录树"})]}),e.jsx(u,{tone:"cyan",children:"正文 / 大纲 / 设定集"})]}),e.jsx("div",{className:"folder-group-list",children:Object.entries(s).map(([n,i])=>e.jsxs("section",{className:"folder-block",children:[e.jsxs("div",{className:"folder-title",children:[e.jsx("span",{children:n}),e.jsx(u,{tone:"purple",children:p(i)})]}),e.jsx("ul",{className:"file-tree",children:e.jsx(y,{items:i,expanded:f,selectedPath:l,onToggle:h=>{d.startTransition(()=>{x(j=>({...j,[h]:!j[h]}))})},onSelect:a})})]},n))})]}),e.jsxs("article",{className:"card files-preview-card",children:[e.jsxs("div",{className:"card-header",children:[e.jsxs("div",{children:[e.jsx("div",{className:"section-label",children:"FILE PREVIEW"}),e.jsx("div",{className:"card-title",children:"内容预览"})]}),l?e.jsxs("div",{className:"header-badges",children:[e.jsxs(u,{tone:"amber",children:[b," 行"]}),e.jsxs(u,{tone:"green",children:[c.length," 字符"]})]}):null]}),l?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"selected-path",children:l}),e.jsx("pre",{className:`file-preview ${m?"loading":""}`.trim(),children:m?"读取中…":c})]}):e.jsx("div",{className:"empty-state",children:e.jsx("p",{children:"选择左侧文件以预览内容"})})]})]})]})}export{P as default};
