"use strict";exports.id=8704,exports.ids=[8704],exports.modules={39506:(e,t,a)=>{a.d(t,{A:()=>o});var s=a(8732),l=a(82015),n=a.n(l),r=a(16757),i=a(83921),d=a(46169);function c({paradigm:e,payload:t,setLoading:a,table:s,database:l}){a(!0),(0,d.A)("/api/exportTableData",{method:"POST",body:{paradigm:e,dbName:l.db_slug,tableName:s.tableName,payload:t}},!0).then(t=>{if(t.success){if("export"==e){let e=new Blob([JSON.stringify(t.data||[],null,4)],{type:"application/json"}),a=URL.createObjectURL(e),n=document.createElement("a");n.href=a,n.style.display="none",n.download=`${l.db_slug}-${s.tableName}-${Date.now()}.json`,document.body.appendChild(n),n.click(),document.body.removeChild(n)}else"import"==e&&(window.alert("Data Imported Successfully"),window.location.reload())}else t.msg&&window.alert(t.msg)}).catch(e=>{console.log(e)}).finally(()=>{a(!1)})}function o({database:e,table:t}){let[a,l]=n().useState(!1),[d,o]=n().useState(!0),u=n().useRef(null);return n().useRef(null),(0,s.jsxs)("div",{className:"relative",children:[a&&(0,s.jsx)(r.A,{width:"15px"}),(0,s.jsx)("div",{className:"button outlined gray",onClick:a=>{c({paradigm:"export",database:e,setLoading:l,table:t})},children:"Export"}),(0,s.jsx)("div",{className:"button outlined gray",onClick:a=>{u.current?.addEventListener("change",a=>{l(!0);let s=a.target;if(!s.files?.[0]){window.alert("No FIles selected"),l(!1);return}i.A({inputFile:s.files[0]}).then(a=>{window.confirm(`Use this file '${a.fileName}' as your import file?`)?c({paradigm:"import",payload:a.fileBase64,database:e,setLoading:l,table:t}):l(!1),o(!1),setTimeout(()=>{o(!0)},200)})}),u.current?.click()},children:"Import"}),d&&(0,s.jsx)("input",{type:"file",name:"json_file_input",id:"json_file_input",accept:".json",className:"hidden",ref:u})]})}},42427:(e,t,a)=>{a.d(t,{A:()=>x});var s=a(8732),l=a(82015),n=a.n(l),r=a(41836),i=a(95939),d=a(1346),c=a(80830),o=a(16757),u=a(84483),m=a(46169);function h({database:e,table:t,setOpenPopover:a,dbSchemaData:l,query:r}){let[i,d]=n().useState(!1);return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)("button",{className:"ghost p-3 justify-start",onClick:()=>{d(!0),setTimeout(()=>{a&&a(!1)},200)},children:"Sync Data"}),(0,s.jsx)(u.A,{open:i,setOpen:d,children:(0,s.jsx)(b,{database:e,table:t,dbSchemaData:l,query:r})})]})}function b({database:e,table:t,query:a,dbSchemaData:l}){let[r,i]=n().useState(l),[u,h]=n().useState("Push"),[b,x]=n().useState(r?.[0]),[p,f]=n().useState(b?.tables),[g,j]=n().useState(p?.[0]),[N,y]=n().useState(!1),[w,v]=n().useState(!1);return(0,s.jsxs)("div",{className:"stack gap-2 relative w-full",children:[N||!r&&(0,s.jsx)(o.A,{width:"20px"}),(0,s.jsx)("h3",{className:"m-0 text-lg",children:"Sync Data with another table"}),w?(0,s.jsxs)("span",{className:"info green inline",children:["The selected table matches ",(0,s.jsx)("b",{children:t.tableFullName})," ","schema"]}):(0,s.jsxs)("span",{className:"info warning inline",children:["The selected table is not compatible with"," ",(0,s.jsx)("b",{children:t.tableFullName})]}),r&&(0,s.jsxs)("form",{onSubmit:s=>{!function({e,setLoading:t,database:a,table:s,query:l}){e.preventDefault(),t(!0);let n=e.target,r=n.paradigm?.value,i=n.database?.value,d=n.table?.value,c={paradigm:r,src:{db:a.db_slug,table:s.tableName},dst:{db:i,table:d},delegated:l.delegated};if(!window.confirm("Pull"==r?"NOTE: This operation will override the current database entries. Continue?":"NOTE: This operation will override the selected database entries. Continue?")){setTimeout(()=>{t(!1)},1e3);return}(0,m.A)("/api/sync-table-data",{method:"POST",body:{...c}},!0).then(e=>{e.success?(window.alert("Data Sync Successful"),window.location.reload()):e.msg&&window.alert("ERROR: "+e.msg)}).finally(()=>{setTimeout(()=>{t(!1)},1e3)})}({e:s,database:e,setLoading:y,table:t,query:a})},className:"w-full stack gap-2 items-stretch",children:[(0,s.jsxs)("div",{className:"stack gap-2 my-2",children:[(0,s.jsx)(d.A,{radioValues:[{title:"Push",payload:"Push",default:!0},{title:"Pull",payload:"Pull"}],name:"paradigm",smallText:!0,flexRow:!0,onChangeHandler:e=>{h(e.target.value)}}),(0,s.jsx)("span",{className:"text-xs text-slate-500 dark:text-slate-400",children:"Push"==u?(0,s.jsxs)(s.Fragment,{children:["Sync data from the current table(",(0,s.jsx)("b",{children:t.tableFullName}),") to the selected table below"]}):(0,s.jsxs)(s.Fragment,{children:["Sync data from the selected table below to the current table(",(0,s.jsx)("b",{children:t.tableFullName}),")"]})})]}),(0,s.jsx)(c.A,{selectOptions:r.filter(t=>t.dbFullName!==e.db_full_name).map(e=>({title:e.dbName,payload:e.dbSlug})),name:"database",onChangeHandler:e=>{let t=r.find(t=>t.dbSlug==e.target.value);t&&(x(t),f(t.tables),j(t.tables?.[0]))}}),(0,s.jsx)(c.A,{selectOptions:p.map(e=>({title:e.tableFullName,payload:e.tableName})),name:"table",onChangeHandler:e=>{let t=p.find(t=>t.tableName===e.target.value);t&&j(t)}}),(0,s.jsx)("button",{className:w?"":"disabled",children:"Sync"})]})]})}function x({database:e,table:t,dbSchemaData:a,query:l}){let[d,c]=n().useState(!1);return(0,s.jsx)(r.Popover,{isOpen:d,onClickOutside:()=>c(!1),positions:["bottom"],align:"end",content:(0,s.jsxs)("div",{className:"stack items-stretch bg-white rounded shadow-md gap-0 min-w-[200px]",children:[(0,s.jsx)(h,{database:e,table:t,setOpenPopover:c,dbSchemaData:a,query:l}),(0,s.jsx)("hr",{className:"m-0"})]}),containerClassName:"z-[1000]",children:(0,s.jsx)("button",{onClick:()=>{c(!d)},className:"outlined gray px-2 py-1.5",children:(0,s.jsx)(i.A,{})})})}a(27825)},7141:(e,t,a)=>{a.a(e,async(e,s)=>{try{let b;a.d(t,{A:()=>h});var l=a(8732),n=a(82015),r=a.n(n),i=a(78704),d=a(80830),c=a(83155),o=a(46169),u=a(16757),m=e([i]);function h({data:e}){let{table:t,entries:s,activeEntries:n,setActiveEntries:m,confirmedDelegetedUser:h,database:x,targetField:p,searchTerm:f}=r().useContext(i.TableEntriesContext);a(94327),h?.delegated,h?.priviledges?.match(/Edit Tables/i),h?.priviledges?.match(/Delete Tables/i),h?.priviledges?.match(/Create Tables/i);let[g,j]=r().useState(!1),N=r().useRef(),[y,w]=r().useState(null);function v(e){if(j(!0),n&&m(null),!e?.toString().match(/./)){setTimeout(()=>{m&&m(s||[]),j(!1)},500);return}window.clearTimeout(b);let a=t?.fields.filter(e=>e.fieldName===p?.current),l=a&&a[0]&&a[0].dataType?.match(/int/i)||p?.current?.match(/^id$/)?"equal":"like";b=setTimeout(()=>{j(!1),(0,o.A)(`/api/queryEntriesSearchTerm?db=${x?.db_full_name}&table=${t?.tableName}&field=${p?.current}&term=${e}&operator=${l}`).then(e=>{"number"==typeof e.count&&w(e.count),e.success&&m&&m(e.result),j(!1)})},500)}return(0,l.jsxs)("div",{className:"w-full flex-col items-stretch",children:[(0,l.jsxs)("form",{onSubmit:e=>{e.preventDefault(),f?.current&&v(f.current)},className:"flex flex-row items-stretch gap-4 w-full",children:[(0,l.jsx)("div",{className:"max-w-xs",children:(0,l.jsx)(d.A,{required:!0,selectOptions:t?t.fields.map(e=>({title:e.fieldName||"",payload:e.fieldName||""})):[],name:"database_tables",onChangeHandler:e=>{p&&(p.current=e.target.value)},title:"Select Field",defaultValue:p?.current,id:"entries-search-field-select"})}),(0,l.jsx)("div",{className:"grow",children:(0,l.jsx)(c.A,{title:"Search Term",placeholder:"Enter search term that corresponds with the selected field type ...",onInputHandler:e=>{let t=e.target;t.value?.match(/./)&&f?f.current=t.value:f&&(f.current=null),v(t.value)},elementRef:N,id:"entries-search-input"})}),g&&(0,l.jsx)("div",{className:"relative items-center justify-center",children:(0,l.jsx)("div",{className:"relative mt-5 w-6",children:(0,l.jsx)(u.A,{width:"20px"})})})]}),f?.current?.match(/./)&&(0,l.jsx)("div",{className:"py-4",children:(0,l.jsxs)("span",{className:"text-lg",children:["number"==typeof y?y>0?(0,l.jsxs)("b",{children:[y," "]}):(0,l.jsx)(l.Fragment,{children:"No "}):"... ","Search result",y&&1!==y?"s":""," for"," ",(0,l.jsxs)("b",{children:['"',f.current,'"']})]})})]})}i=(m.then?(await m)():m)[0],s()}catch(e){s(e)}})},61925:(e,t,a)=>{a.a(e,async(e,s)=>{try{a.d(t,{A:()=>j});var l=a(8732),n=a(82015),r=a.n(n),i=a(78704),d=a(59023),c=a(53926),o=a(73872),u=a(70468),m=a(537),h=a(51275),b=a(23791),x=a(16757);a(46169);var p=a(39506),f=a(42427),g=e([i,o,u,b]);function j(){let{user:e,database:t,table:s,dbSchemaData:n,setTargetEntry:x,richTextEditors:g,jsonTextEditors:j,query:y,confirmedDelegetedUser:w,activeEntries:v,entriesCount:S,setActiveEntries:A}=r().useContext(i.TableEntriesContext),T=a(94327);return w?.delegated,w?.priviledges?.match(/Edit Tables/i),w?.priviledges?.match(/Delete Tables/i),w?.priviledges?.match(/Create Tables/i),(0,l.jsxs)(r().Fragment,{children:[(0,l.jsxs)("div",{className:"flex-col items-start gap-0 w-full relative z-50",children:[(0,l.jsxs)("div",{className:"w-full justify-between flex-wrap",children:[(0,l.jsxs)("div",{className:"flex-wrap",children:[(0,l.jsx)(d.A,{}),(0,l.jsx)("h1",{className:"text-3xl m-0",children:(0,l.jsxs)("span",{className:"flex items-center flex-wrap gap-2",children:["All Entries In"," ",(0,l.jsxs)("span",{className:"bg-slate-100 dark:bg-slate-800 px-2 py-1 border border-solid border-slate-400 dark:border-slate-600 rounded relative",children:[s.tableFullName,(0,l.jsx)("div",{className:"absolute -top-5 -right-4",children:(0,l.jsx)(o.A,{slugText:s.tableName,justIcon:!0})})]})," ","Table"]})})]}),(0,l.jsxs)("div",{className:"w-full sm:w-auto",children:[(0,l.jsxs)("a",{href:`/admin/${e?.id}/databases/${t.db_slug}/tables/${s.tableName}/entries/add-entry`,className:"button flex items-center gap-2 w-full sm:w-auto justify-center",children:[(0,l.jsx)("span",{className:"symbol",children:"+"}),(0,l.jsx)("span",{children:"Add New Entry"})]}),(0,l.jsx)(p.A,{database:t,table:s}),(0,l.jsx)(f.A,{database:t,table:s,dbSchemaData:n,query:y})]})]}),(0,l.jsx)(c.A,{confirmedDelegetedUser:y?.delegated,user:e})]}),t.active_clone?(0,l.jsx)(m.A,{database:t,user:e}):(0,l.jsx)(l.Fragment,{}),t.remote_connected?(0,l.jsx)(b.p,{database:t}):(0,l.jsx)(l.Fragment,{}),(0,l.jsx)(h.A,{table:s,database:t,user:e}),(0,l.jsx)(u.A,{user:e,entries:v,database:t,table:s,setTargetEntry:x,dbSchemaData:n,richTextEditors:g,dataTypes:T,query:y,confirmedDelegetedUser:w,jsonTextEditors:j,search:!0}),S>20&&(0,l.jsx)(N,{activeEntries:v,setActiveEntries:A,database:t,table:s,entriesCount:S})]})}function N({activeEntries:e,setActiveEntries:t,database:a,table:s,entriesCount:n}){r().useContext(i.TableEntriesContext);let d=r().useRef(),c=r().useRef(1);return 20*Number(c.current)>=n?(0,l.jsx)(r().Fragment,{}):(0,l.jsx)("div",{className:"w-full flex items-center justify-center",ref:d,children:(0,l.jsx)(x.A,{position:"relative",style:{backgroundColor:"transparent"},width:"20px"})})}[i,o,u,b]=g.then?(await g)():g,s()}catch(e){s(e)}})},70468:(e,t,a)=>{a.a(e,async(e,s)=>{try{a.d(t,{A:()=>b});var l=a(8732),n=a(82015),r=a.n(n),i=a(23180),d=a(7141),c=a(16757),o=a(39506),u=a(42427),m=a(2833),h=e([d]);function b({user:e,entries:t,database:a,table:s,setTargetEntry:n,dbSchemaData:h,richTextEditors:b,jsonTextEditors:x,dataTypes:p,title:f,confirmedDelegetedUser:g,query:j,search:N,entriesCount:y}){g?.delegated,g?.priviledges?.match(/Edit Tables/i),g?.priviledges?.match(/Delete Tables/i),g?.priviledges?.match(/Create Tables/i),g?.priviledges?.match(/Edit Entries/i),g?.priviledges?.match(/Delete Entries/i);let{active_clone:w,active_clone_parent_db:v,remote_connected:S}=a;return(0,l.jsx)(r().Fragment,{children:(0,l.jsxs)("section",{className:"paper gray gray-bg",children:[f&&(0,l.jsxs)("div",{className:"justify-between w-full flex-wrap px-4 sm:px-0 pt-4 sm:pt-0",children:[(0,l.jsx)("h2",{className:"text-xl m-0",children:f}),(0,l.jsxs)("div",{children:[(0,l.jsxs)("a",{className:"button flex gap-2 items-center",href:`/admin/${e?.id}/databases/${a.db_slug}/tables/${s.tableName}/entries/add-entry`,children:[(0,l.jsx)("span",{className:"symbol",children:"+"}),(0,l.jsx)("span",{children:"Add New Entry"})]}),(0,l.jsx)(o.A,{database:a,table:s}),(0,l.jsx)(u.A,{database:a,table:s,dbSchemaData:h,query:j})]})]}),N&&(0,l.jsx)(d.A,{}),(0,l.jsxs)("div",{className:"w-full flex-col gap-6",children:[t?.[0]&&t.map(t=>(0,l.jsx)(r().Fragment,{children:(0,l.jsx)(i.A,{user:e,database:a,table:s,dbSchemaData:h,richTextEditors:b,dataTypes:p,entry:t,query:j,confirmedDelegetedUser:g,jsonTextEditors:x})},t.id)),!t&&(0,l.jsx)("div",{className:"w-full h-[300px]",children:(0,l.jsx)(c.A,{position:"relative"})}),t&&!t?.[0]&&(0,l.jsxs)("a",{href:`/admin/${e?.id}/databases/${a.db_slug}/tables/${s.tableName}/entries/add-entry`,className:"border-dotted border-slate-200 border-2 p-4 w-full flex-col rounded flex items-center hover:border-primary/50 gap-2",children:[(0,l.jsx)("span",{className:"text-slate-400 text-center",children:"No Entries Added Yet."}),(0,l.jsx)(m.A,{}),(0,l.jsx)("span",{className:"text-center",children:"Add an Entry to this Table"})]}),y&&y>10?(0,l.jsx)("a",{href:`/admin/${e?.id}/databases/${a.db_slug}/tables/${s.tableName}/entries`,className:"button outlined gray w-full -mb-4",children:(0,l.jsx)("span",{children:"See All Entries"})}):null,t?.[0]&&f&&(0,l.jsxs)("a",{className:"button flex gap-2 items-center w-full justify-center mt-2 more-padding",href:`/admin/${e?.id}/databases/${a.db_slug}/tables/${s.tableName}/entries/add-entry`,style:{borderStyle:"dotted",borderWidth:"2px"},children:[(0,l.jsx)("span",{className:"symbol",children:"+"}),(0,l.jsx)("span",{children:"Add New Entry"})]})]})]})})}d=(h.then?(await h)():h)[0],s()}catch(e){s(e)}})},1346:(e,t,a)=>{a.d(t,{A:()=>l});var s=a(8732);function l({radioValues:e,name:t,onChangeHandler:a,setAlert:l,flexRow:n,labelColor:r,baseText:i,smallText:d,className:c}){try{return(0,s.jsx)("div",{className:"flex items-start gap-4 flex-wrap "+(n?"":" flex-col ")+(c||""),children:e.map((e,n)=>{let{payload:c,title:o,jsx:u}=e,m=!1===c?c:c||o||null;return(0,s.jsxs)("div",{className:"flex items-center gap-2",children:[(0,s.jsx)("input",{className:"m-0"+(i?" w-5 h-5":d?" w-4 h-4":" w-6 h-6"),type:"radio",defaultChecked:!!e.default,name:t,id:t+"_"+m,onChange:t=>{l&&l(null),e.onChangeHandler?e.onChangeHandler(t):a&&a(t)},value:"string"==typeof m?m:void 0}),u||(0,s.jsx)("label",{htmlFor:t+"_"+m,className:"text-lg m-0 "+(r||"text-slate-800")+(i?" text-base":d?" text-sm":" text-lg"),children:o})]},n+1)})})}catch(e){return console.log("ERROR in FormRadio =>",e),(0,s.jsx)("div",{children:"Form Radio Error"})}}a(82015)},51275:(e,t,a)=>{a.d(t,{A:()=>r});var s=a(8732),l=a(82015),n=a.n(l);function r({table:e,database:t,user:a,activeDbClone:l}){let r=e?.childTableDbFullName?e.childTableDbFullName.replace(new RegExp(/datasquirel_user_\d+_/),""):null,i=e.childTableDbFullName===t.db_full_name;return r?(0,s.jsxs)("span",{className:"info small green gap-1"+(t?.active_clone?" -mt-6":""),style:{fontWeight:"500"},children:["This Table is an active clone of"," ",(0,s.jsx)("a",{href:`/admin/${a?.id}/databases/${r}/tables/${e.childTableName}`,target:"_blank",className:"button gray outlined small-text",style:{display:"inline-block",fontSize:"12px",padding:"3px 7px"},children:(0,s.jsx)("b",{children:e.childTableName})})," ","in"," ",i?(0,s.jsxs)("span",{children:["this current database(",(0,s.jsx)("b",{children:t.db_name}),")."]}):(0,s.jsxs)(n().Fragment,{children:[(0,s.jsx)("a",{href:`/admin/${a?.id}/databases/${r}`,target:"_blank",className:"button gray outlined small-text",style:{display:"inline-block",fontSize:"12px",padding:"3px 7px"},children:(0,s.jsx)("b",{children:r})}),(0,s.jsx)("span",{children:"database."})]})," ",t?.active_clone?"":"Only Foreign keys and Entries can be updated."]}):(0,s.jsx)(n().Fragment,{})}},78704:(e,t,a)=>{a.a(e,async(e,s)=>{try{a.r(t),a.d(t,{TableEntriesContext:()=>y,default:()=>j,getServerSideProps:()=>N});var l=a(8732),n=a(82015),r=a.n(n),i=a(29021),d=a.n(i),c=a(92702),o=a(25562),u=a(61925),m=a(55880),h=a(52854),b=a(22873),x=a(77047),p=a(71934),f=a(43363),g=e([u,m]);[u,m]=g.then?(await g)():g;let y=r().createContext({});function j({user:e,database:t,table:a,dbSchemaData:s,entries:n,query:i,confirmedDelegetedUser:d,entriesCount:c}){let h=`${a.tableFullName} Table Entries | Datasquirel`,b=(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)("title",{children:h}),(0,l.jsx)("meta",{name:"description",content:"All Entries in table"})]}),[x,p]=r().useState(null),[g,j]=r().useState(n),N=r().useRef("title"),w=r().useRef(null),v=r().useRef([]),S=r().useRef([]);return(0,l.jsx)(o.A,{head:b,user:e,extraHeaderContent:(0,l.jsx)(m.A,{database:t,query:i,confirmedDelegetedUser:d,user:e}),children:(0,l.jsxs)(y.Provider,{value:{user:e,database:t,table:a,dbSchemaData:s,entries:n,targetEntry:x,setTargetEntry:p,richTextEditors:v,jsonTextEditors:S,query:i,confirmedDelegetedUser:d,activeEntries:g,setActiveEntries:j,targetField:N,searchTerm:w,entriesCount:c},children:[(0,l.jsx)(u.A,{}),(0,l.jsx)(f.A,{snug:!0})]})})}async function N({req:e,res:t,query:a}){let s=await (0,c.default)(e,t);if(!s?.logged_in_status)return{redirect:{destination:"/login",permanent:!1}};try{let t=a.single,l=await (0,b.A)({request:e,user:s,databaseSlug:t,query:a});if(!l)throw Error("No Db Object Found!");let{dbUserId:n}=l,r=await (0,p.A)(`SELECT * FROM user_databases WHERE user_id='${n}' AND db_slug='${a.single}'`),i=r[0].db_full_name;if(!r||!r[0])return{redirect:{destination:`/admin/${s?.id}/databases`,permanent:!1}};let c=JSON.parse(d().readFileSync(`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${n}/main.json`,"utf-8")),o=(c.filter(e=>e.dbFullName===r[0].db_full_name)[0]?.tables).filter(e=>e.tableName===a.single_table)[0],u=await (0,h.A)({queryString:`SELECT * FROM \`${r[0].db_full_name}\`.\`${o.tableName}\` ORDER BY id DESC LIMIT 20`,tableSchema:o,database:i});console.log("tableEntries",u);let m=await (0,h.A)({queryString:`SELECT COUNT(id) AS count FROM \`${r[0].db_full_name}\`.\`${o.tableName}\``,tableSchema:o,database:i});return{props:{user:s,database:r[0],table:o,dbSchemaData:c,entries:u,query:a,confirmedDelegetedUser:l,entriesCount:m?.[0]?.count||0}}}catch(e){return(0,x.A)({component:"[single_table]/entries-page-component/getServerSideProps/catch-error",message:e.message,user:s}),{props:{user:s,database:null,table:null,entries:[]}}}}s()}catch(e){s(e)}})}};