Files
yakpanel-core/YakPanel-server/frontend/dist/assets/DatabasePage-CuN032AR.js
2026-04-07 05:05:28 +05:30

2 lines
8.2 KiB
JavaScript

import{r as n,j as e,a as D,B as M,C as K,D as G,E as V,F as W}from"./index-cE9w-Kq7.js";import{M as a}from"./Modal-CL3xZqxR.js";import{A as b}from"./AdminAlert-yrdXFH0e.js";import{A as _}from"./AdminButton-ByutG8m-.js";import{A as X}from"./AdminTable-eCi7S__-.js";import{E as Y}from"./EmptyState-CmnFWkSO.js";import{P as v}from"./PageHeader-HdM4gpcn.js";function re(){const[j,T]=n.useState([]),[A,C]=n.useState(!0),[o,c]=n.useState(""),[I,d]=n.useState(!1),[B,S]=n.useState(!1),[P,f]=n.useState(""),[l,m]=n.useState(null),[L,g]=n.useState([]),[N,u]=n.useState(!1),[y,h]=n.useState(null),[E,p]=n.useState(""),w=()=>{C(!0),D("/database/list").then(T).catch(s=>c(s.message)).finally(()=>C(!1))};n.useEffect(()=>{w()},[]);const Q=s=>{s.preventDefault();const t=s.currentTarget,r=t.elements.namedItem("name").value.trim(),i=t.elements.namedItem("username").value.trim(),x=t.elements.namedItem("password").value,k=t.elements.namedItem("db_type").value,O=t.elements.namedItem("ps").value.trim();if(!r||!i||!x){f("Name, username and password are required");return}S(!0),f(""),D("/database/create",{method:"POST",body:JSON.stringify({name:r,username:i,password:x,db_type:k,ps:O})}).then(()=>{d(!1),t.reset(),w()}).catch(J=>f(J.message)).finally(()=>S(!1))},F=(s,t)=>{confirm(`Delete database "${t}"?`)&&D(`/database/${s}`,{method:"DELETE"}).then(w).catch(r=>c(r.message))},q=s=>{m(s),g([]),M(s).then(t=>g(t.backups||[])).catch(t=>c(t.message))},H=()=>{l&&(u(!0),K(l).then(()=>M(l).then(s=>g(s.backups||[]))).catch(s=>c(s.message)).finally(()=>u(!1)))},R=s=>{!l||!confirm(`Restore from ${s}? This will overwrite the database.`)||(u(!0),V(l,s).then(()=>m(null)).catch(t=>c(t.message)).finally(()=>u(!1)))},z=s=>{l&&G(l,s).catch(t=>c(t.message))},U=(s,t)=>{s.preventDefault();const r=s.currentTarget,i=r.elements.namedItem("new_password").value,x=r.elements.namedItem("confirm_password").value;if(!i||i.length<6){p("Password must be at least 6 characters");return}if(i!==x){p("Passwords do not match");return}p(""),W(t,i).then(()=>h(null)).catch(k=>p(k.message))},$=s=>s<1024?s+" B":s<1024*1024?(s/1024).toFixed(1)+" KB":(s/1024/1024).toFixed(1)+" MB";return A?e.jsxs(e.Fragment,{children:[e.jsx(v,{title:"Databases"}),e.jsx("div",{className:"text-center py-5 text-muted",children:"Loading…"})]}):o&&!j.length?e.jsxs(e.Fragment,{children:[e.jsx(v,{title:"Databases"}),e.jsx(b,{variant:"danger",children:o})]}):e.jsxs(e.Fragment,{children:[e.jsx(v,{title:"Databases",actions:e.jsxs(_,{onClick:()=>d(!0),children:[e.jsx("i",{className:"ti ti-plus me-1"}),"Add Database"]})}),o?e.jsx(b,{variant:"warning",children:o}):null,e.jsxs(a,{show:I,onHide:()=>d(!1),centered:!0,children:[e.jsx(a.Header,{closeButton:!0,children:e.jsx(a.Title,{children:"Create Database"})}),e.jsxs("form",{onSubmit:Q,children:[e.jsxs(a.Body,{children:[P?e.jsx(b,{variant:"danger",children:P}):null,e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"Database Name"}),e.jsx("input",{name:"name",type:"text",placeholder:"mydb",className:"form-control",required:!0})]}),e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"Username"}),e.jsx("input",{name:"username",type:"text",placeholder:"dbuser",className:"form-control",required:!0})]}),e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"Password"}),e.jsx("input",{name:"password",type:"password",className:"form-control",required:!0})]}),e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"Type"}),e.jsxs("select",{name:"db_type",className:"form-select",children:[e.jsx("option",{value:"MySQL",children:"MySQL (full support)"}),e.jsx("option",{value:"PostgreSQL",children:"PostgreSQL (full support)"}),e.jsx("option",{value:"MongoDB",children:"MongoDB (full support)"}),e.jsx("option",{value:"Redis",children:"Redis (panel record only)"})]}),e.jsx("div",{className:"form-text",children:"MySQL, PostgreSQL, MongoDB: create, delete, backup/restore. Password change supported for those types."})]}),e.jsxs("div",{className:"mb-0",children:[e.jsx("label",{className:"form-label",children:"Note (optional)"}),e.jsx("input",{name:"ps",type:"text",placeholder:"My database",className:"form-control"})]})]}),e.jsxs(a.Footer,{children:[e.jsx("button",{type:"button",className:"btn btn-light",onClick:()=>d(!1),children:"Cancel"}),e.jsx("button",{type:"submit",disabled:B,className:"btn btn-primary",children:B?"Creating…":"Create"})]})]})]}),e.jsx("div",{className:"card shadow-sm border-0",children:e.jsx("div",{className:"card-body p-0",children:e.jsxs(X,{children:[e.jsx("thead",{className:"table-light",children:e.jsxs("tr",{children:[e.jsx("th",{children:"Name"}),e.jsx("th",{children:"Username"}),e.jsx("th",{children:"Type"}),e.jsx("th",{children:"Note"}),e.jsx("th",{className:"text-end",children:"Actions"})]})}),e.jsx("tbody",{children:j.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:5,className:"p-0",children:e.jsx(Y,{title:"No databases",description:'Click "Add Database" to create one.'})})}):j.map(s=>e.jsxs("tr",{children:[e.jsx("td",{className:"align-middle",children:s.name}),e.jsx("td",{className:"align-middle",children:s.username}),e.jsx("td",{className:"align-middle",children:s.db_type}),e.jsx("td",{className:"align-middle text-muted",children:s.ps||"—"}),e.jsxs("td",{className:"align-middle text-end",children:[(s.db_type==="MySQL"||s.db_type==="PostgreSQL"||s.db_type==="MongoDB")&&e.jsx("button",{type:"button",onClick:()=>q(s.id),className:"btn btn-sm btn-outline-primary me-1",title:"Backup",children:e.jsx("i",{className:"ti ti-archive"})}),(s.db_type==="MySQL"||s.db_type==="PostgreSQL"||s.db_type==="MongoDB")&&e.jsx("button",{type:"button",onClick:()=>h(s.id),className:"btn btn-sm btn-outline-warning me-1",title:"Change password",children:e.jsx("i",{className:"ti ti-key"})}),e.jsx("button",{type:"button",onClick:()=>F(s.id,s.name),className:"btn btn-sm btn-outline-danger",title:"Delete",children:e.jsx("i",{className:"ti ti-trash"})})]})]},s.id))})]})})}),e.jsxs(a,{show:l!=null,onHide:()=>m(null),centered:!0,size:"lg",children:[e.jsx(a.Header,{closeButton:!0,children:e.jsx(a.Title,{children:"Database Backup"})}),e.jsxs(a.Body,{children:[e.jsx("div",{className:"mb-3",children:e.jsxs(_,{onClick:H,disabled:N,children:[e.jsx("i",{className:"ti ti-archive me-1"}),N?"Creating…":"Create Backup"]})}),e.jsx("h6",{className:"text-muted small text-uppercase",children:"Existing backups"}),L.length===0?e.jsx("p",{className:"text-muted small mb-0",children:"No backups yet"}):e.jsx("ul",{className:"list-group list-group-flush",children:L.map(s=>e.jsxs("li",{className:"list-group-item d-flex align-items-center justify-content-between gap-2",children:[e.jsx("code",{className:"small flex-grow-1 text-truncate",children:s.filename}),e.jsx("span",{className:"text-muted small flex-shrink-0",children:$(s.size)}),e.jsxs("span",{className:"flex-shrink-0",children:[e.jsx("button",{type:"button",className:"btn btn-sm btn-link p-1",onClick:()=>z(s.filename),title:"Download",children:e.jsx("i",{className:"ti ti-download"})}),e.jsx("button",{type:"button",className:"btn btn-sm btn-link p-1 text-warning",onClick:()=>R(s.filename),disabled:N,title:"Restore",children:e.jsx("i",{className:"ti ti-restore"})})]})]},s.filename))})]}),e.jsx(a.Footer,{children:e.jsx("button",{type:"button",className:"btn btn-light",onClick:()=>m(null),children:"Close"})})]}),e.jsxs(a,{show:y!=null,onHide:()=>h(null),centered:!0,children:[e.jsx(a.Header,{closeButton:!0,children:e.jsx(a.Title,{children:"Change Database Password"})}),y!=null?e.jsxs("form",{onSubmit:s=>U(s,y),children:[e.jsxs(a.Body,{children:[E?e.jsx(b,{variant:"danger",children:E}):null,e.jsxs("div",{className:"mb-3",children:[e.jsx("label",{className:"form-label",children:"New Password"}),e.jsx("input",{name:"new_password",type:"password",minLength:6,className:"form-control",required:!0})]}),e.jsxs("div",{className:"mb-0",children:[e.jsx("label",{className:"form-label",children:"Confirm Password"}),e.jsx("input",{name:"confirm_password",type:"password",minLength:6,className:"form-control",required:!0})]})]}),e.jsxs(a.Footer,{children:[e.jsx("button",{type:"button",className:"btn btn-light",onClick:()=>h(null),children:"Cancel"}),e.jsx("button",{type:"submit",className:"btn btn-primary",children:"Update"})]})]}):null]})]})}export{re as DatabasePage};