tinybase
Version:
A reactive data store and sync engine.
2 lines (1 loc) • 9.01 kB
JavaScript
import{jsx as e,jsxs as t,Fragment as l}from"react/jsx-runtime";import r from"react";import{CellView as n,ResultCellView as s,ValueView as a}from"../ui-react/index.js";const o=e=>typeof e,c=o(""),i=o(!0),d=o(0),u=o(o),h="Result",m="Ids",C="Table",p="Row",b=p+"Count",f=p+m,I="Sorted"+p+m,g="Cell",x=g+m,y="Value",N=y+m,v="currentTarget",w="value",T="extra",k=e=>(t,l,r)=>e(t)?r?.():l(t),S=globalThis,R=Math.min,A=isFinite,B=e=>null==e,q=e=>void 0===e,P=e=>!1===e,E=k(B),L=k(q),O=e=>o(e)==c,V=e=>o(e)==u,j=e=>Array.isArray(e),M=e=>e.length,F=()=>{},H=e=>e,$=(e,t)=>e.every(t),_=(e,t)=>M(e)===M(t)&&$(e,(e,l)=>t[l]===e),z=(e,t)=>j(e)&&j(t)?_(e,t):e===t,D=(e,t)=>e.map(t),G=Object,J=e=>G.getPrototypeOf(e),K=G.entries,Q=e=>!B(e)&&E(J(e),e=>e==G.prototype||B(J(e)),()=>!0),U=G.keys,W=(e=[])=>G.fromEntries(e),X=(e,t)=>D(K(e),([e,l])=>t(l,e)),Y=(e,t,l=(e,t)=>e===t)=>{const r=K(e);return M(r)===M(U(t))&&$(r,([e,r])=>Q(r)?!!Q(t[e])&&Y(t[e],r):l(r,t[e]))},{PureComponent:Z,createContext:ee,useCallback:te,useContext:le,useEffect:re,useLayoutEffect:ne,useMemo:se,useRef:ae,useState:oe,useSyncExternalStore:ce}=r,ie=(e,...t)=>q(e)?{}:e(...t),de="tinybase_uirc",ue=S[de]?S[de]:S[de]=ee([]),he=(e,t)=>{const l=((e,t)=>{const l=le(ue);return q(e)?l[2*t]:O(e)?((e,t)=>L(e,e=>e[t]))(l[2*t+1],e):e})(e,t);return q(e)||O(e)?l:e},me=[],Ce=[{},[],[me,void 0,me],{},void 0,void 0,!1,0],pe=[Y,_,([e,t,l],[r,n,s])=>t===n&&_(e,r)&&_(l,s),(e,t)=>Y(e,t,z),z],be=(e,t)=>e===t,fe=(e,t,l,r=me)=>{const n=ae(Ce[l]),s=te(()=>{const s=t?.[(6==l?"has":"get")+e]?.(...r)??Ce[l];return(pe[l]??be)(s,n.current)?n.current:n.current=s},[t,l,e,...r]),a=te(n=>((e,t,...l)=>{const r=e?.["add"+t+"Listener"]?.(...l);return()=>e?.delListener?.(r)})(t,(6==l?"Has":"")+e,...r,n),[t,l,e,...r]);return ce(a,s,s)},Ie=(e,t,l,r,n,s,...a)=>((e,t,l,r=me,n=F,s=me,a,...o)=>te(r=>L(e,e=>L(l(r,e),l=>n(e[a+t](...ge(o,e,r),l),l))),[e,t,...r,...s,a,...xe(o)]))(ye(e),t,l,r,n,s,"set",...a),ge=(e,t,l)=>D(e,e=>V(e)?e(l,t):e),xe=e=>{return t=e=>!V(e),e.filter(t);var t},ye=e=>he(e,0),Ne=(e,t)=>fe(C+x,ye(t),1,[e]),ve=(e,t)=>fe(f,ye(t),1,[e]),we=(e,t,l,r,n,s)=>((e,t,l,r,n,s)=>fe(I,ye(s),1,[e,t,l,r,n]))(...Q(e)?[e.tableId,e.cellId,e.descending??!1,e.offset??0,e.limit,t]:[e,t,l,r,n,s]),Te=(e,t,l,r)=>fe(g,ye(r),5,[e,t,l]),ke=(e,t)=>fe(y,ye(t),5,[e]),Se=(e,t,l,r,n,s,a,o)=>Ie(s,g,r,n,a,o,e,t,l),Re=(e,t,l,r,n,s)=>Ie(r,y,t,l,n,s,e),Ae=e=>he(e,2),Be=(e,t,l)=>fe("Slice"+f,Ae(l),1,[e,t]),qe=e=>he(e,3),Pe=e=>he(e,4),Ee=(e,t)=>fe(h+C+x,Pe(t),1,[e]),Le=(e,t)=>fe(h+f,Pe(t),1,[e]),Oe=(e,t,l,r=0,n,s)=>fe(h+I,Pe(s),1,[e,t,l,r,n]),Ve=(e,t,l,r)=>e==c?t:e==d?l:e==i?r:null,je=(e,t)=>se(()=>({store:e,tableId:t}),[e,t]),Me=(e,t)=>se(()=>({queries:e,queryId:t}),[e,t]),Fe=(e,t,l)=>{const r=te(e,t);return l?r:void 0},He=(...e)=>se(()=>e,e),$e=(e,t,l)=>se(()=>{const r=t??e;return n=j(r)?W(D(r,e=>[e,e])):r,s=(e,t)=>({label:t,component:l,...O(e)?{label:e}:e}),W(X(n,(e,t)=>[t,s(e,t)]));var n,s},[t,l,e]),_e="editable",ze=(t=[],l,r=0)=>D(t,({component:t},n)=>e("td",{className:T,children:e(t,{...l})},De(n,r))),De=(e,t)=>(t?">":"<")+e,Ge=(t=[],l=0)=>D(t,({label:t},r)=>e("th",{className:T,children:t},De(r,l))),Je=({cellId:e,sort:[l,r],label:n=e??"",onClick:s})=>t("th",{onClick:Fe(()=>s?.(e),[s,e],s),className:q(r)||l!=e?void 0:`sorted ${r?"de":"a"}scending`,children:[q(r)||l!=e?null:(r?"↓":"↑")+" ",n]}),Ke=({className:l,headerRow:r,idColumn:n,params:[s,a,o,c,i,d,u,h]})=>t("table",{className:l,children:[h?e("caption",{children:h}):null,!1===r?null:e("thead",{children:t("tr",{children:[Ge(c),!1===n?null:e(Je,{sort:d??[],label:"Id",onClick:u}),X(s,({label:t},l)=>e(Je,{cellId:l,label:t,sort:d??[],onClick:u},l)),Ge(i,1)]})}),e("tbody",{children:D(o,l=>{const r={...a,rowId:l};return t("tr",{children:[ze(c,r),P(n)?null:e("th",{title:l,children:l}),X(s,({component:t,getComponentProps:n},s)=>e("td",{children:e(t,{...ie(n,l,s),...r,cellId:s})},s)),ze(i,r,1)]},l)})})]}),Qe=({thing:l,onThingChange:r,className:n,hasSchema:s,showType:a=!0})=>{const[u,h]=oe(),[m,C]=oe(),[p,b]=oe(),[f,I]=oe(),[g,x]=oe();m!==l&&(h((e=>{if(null===e)return"null";const t=o(e);return(e=>e==c||e==i)(t)||t==d&&A(e)?t:void 0})(l)),C(l),b(l+""),I(Number(l)||0),x(!!l));const y=te((e,t)=>{t(e),C(e),r(e)},[r]),N=te(()=>{if(!s?.()){const e=Ve(u,d,i,c),t=Ve(e,p,f,g);h(e),C(t),r(t)}},[s,r,p,f,g,u]),T=Ve(u,e("input",{value:p,onChange:te(e=>y(e[v][w]+"",b),[y])},u),e("input",{type:"number",value:f,onChange:te(e=>y(Number(e[v][w]||0),I),[y])},u),e("input",{type:"checkbox",checked:g,onChange:te(e=>y(!!e[v].checked,x),[y])},u));return t("div",{className:n,children:[a&&T?e("button",{title:u,className:u,onClick:N,children:u}):null,T]})},Ue=({tableId:t,rowId:l,cellId:r,store:n,className:s,showType:a})=>{const[o,c]=((e,t,l,r)=>[Te(e,t,l,r),Se(e,t,l,H,[],r)])(t,l,r,n);return e(Qe,{thing:o,onThingChange:c,className:s??_e+g,showType:a,hasSchema:ye(n)?.hasTablesSchema})},We=({valueId:t,store:l,className:r,showType:n})=>{const[s,a]=((e,t)=>[ke(e,t),Re(e,H,[],t)])(t,l);return e(Qe,{thing:s,onThingChange:a,className:r??_e+y,showType:n,hasSchema:ye(l)?.hasValuesSchema})},Xe=(e,t)=>D(Ne(e,t),t=>e+"."+t),Ye=({localRowId:r,params:[n,s,a,o,c,i,d,u,h]})=>{const m=((e,t,l)=>fe("RemoteRowId",qe(l),5,[e,t]))(c,r,i),C={tableId:a??"",rowId:r,store:d};return t("tr",{children:[ze(u,C),P(n)?null:t(l,{children:[e("th",{title:r,children:r}),e("th",{title:m,children:m})]}),X(s,({component:t,getComponentProps:l},n)=>{const[s,c]=((e,t="",l)=>e.split(t,l))(n,".",2),i=s===a?r:s===o?m:void 0;return q(i)?null:e("td",{children:e(t,{...ie(l,i,c),store:d,tableId:s,rowId:i,cellId:c})},n)}),ze(h,C,1)]})},Ze=({relationshipId:r,relationships:s,editable:a,customCells:o,extraCellsBefore:c,extraCellsAfter:i,className:d,headerRow:u,idColumn:h=!0})=>{const[m,C,p,b]=((e,t)=>[e,e?.getStore(),e?.getLocalTableId(t),e?.getRemoteTableId(t)])(qe(s),r),f=$e([...Xe(p,C),...Xe(b,C)],o,a?Ue:n),I=He(h,f,p,b,r,m,C,c,i);return t("table",{className:d,children:[P(u)?null:e("thead",{children:t("tr",{children:[Ge(c),P(h)?null:t(l,{children:[t("th",{children:[p,".Id"]}),t("th",{children:[b,".Id"]})]}),X(f,({label:t},l)=>e("th",{children:t},l)),Ge(i,1)]})}),e("tbody",{children:D(ve(p,C),t=>e(Ye,{localRowId:t,params:I},t))})]})},et=(t,l=!1,r,n=0,s,a,o,c)=>{const[[i,d,u],h]=oe([t,l,n]),m=te(e=>{h(e),c?.(e)},[c]),C=Fe(e=>m([e,e==i&&!d,u]),[m,i,d,u],r),p=te(e=>m([i,d,e]),[m,i,d]),b=!0===o?tt:o;return[[i,d,u],C,se(()=>P(o)?null:e(b,{offset:u,limit:s,total:a,onChange:p}),[o,b,u,s,a,p])]},tt=({onChange:r,total:n,offset:s=0,limit:a=n,singular:o="row",plural:c=o+"s"})=>{(s>n||s<0)&&(s=0,r(0));const i=Fe(()=>r(s-a),[r,s,a],s>0),d=Fe(()=>r(s+a),[r,s,a],s+a<n);return t(l,{children:[n>a&&t(l,{children:[e("button",{className:"previous",disabled:0==s,onClick:i,children:"←"}),e("button",{className:"next",disabled:s+a>=n,onClick:d,children:"→"}),s+1," to ",R(n,s+a)," of "]}),n," ",1!=n?c:o]})},lt=({queryId:t,cellId:l,descending:r,offset:n,limit:a,queries:o,sortOnClick:c,paginator:i=!1,customCells:d,extraCellsBefore:u,extraCellsAfter:m,onChange:C,...p})=>{const[f,I,g]=et(l,r,c,n,a,((e,t)=>fe(h+b,Pe(t),7,[e]))(t,o),i,C);return e(Ke,{...p,params:He($e(Ee(t,o),d,s),Me(o,t),Oe(t,...f,a,o),u,m,f,I,g)})},rt=({queryId:t,queries:l,customCells:r,extraCellsBefore:n,extraCellsAfter:a,...o})=>e(Ke,{...o,params:He($e(Ee(t,l),r,s),Me(l,t),Le(t,l),n,a)}),nt=({indexId:t,sliceId:l,indexes:r,editable:s,customCells:a,extraCellsBefore:o,extraCellsAfter:c,...i})=>{const[d,u,h]=((e,t)=>[e,e?.getStore(),e?.getTableId(t)])(Ae(r),t);return e(Ke,{...i,params:He($e(Ne(h,u),a,s?Ue:n),je(u,h),Be(t,l,d),o,c)})},st=({tableId:t,cellId:l,descending:r,offset:s,limit:a,store:o,editable:c,sortOnClick:i,paginator:d=!1,onChange:u,customCells:h,extraCellsBefore:m,extraCellsAfter:C,...p})=>{const[f,I,g]=et(l,r,i,s,a,((e,t)=>fe(b,ye(t),7,[e]))(t,o),d,u);return e(Ke,{...p,params:He($e(Ne(t,o),h,c?Ue:n),je(o,t),we(t,...f,a,o),m,C,f,I,g)})},at=({tableId:t,store:l,editable:r,customCells:s,extraCellsBefore:a,extraCellsAfter:o,...c})=>e(Ke,{...c,params:He($e(Ne(t,l),s,r?Ue:n),je(l,t),ve(t,l),a,o)}),ot=(t=[],l,r=0)=>D(t,({component:t},n)=>e("td",{className:T,children:e(t,{...l})},De(n,r))),ct=({store:l,editable:r=!1,valueComponent:n=(r?We:a),getValueComponentProps:s,extraCellsBefore:o,extraCellsAfter:c,className:i,headerRow:d,idColumn:u})=>{return t("table",{className:i,children:[!1===d?null:e("thead",{children:t("tr",{children:[Ge(o),!1===u?null:e("th",{children:"Id"}),e("th",{children:y}),Ge(c,1)]})}),e("tbody",{children:D((h=l,fe(N,ye(h),1)),r=>{const a={valueId:r,store:l};return t("tr",{children:[ot(o,a),P(u)?null:e("th",{title:r,children:r}),e("td",{children:e(n,{...ie(s,r),...a})}),ot(c,a,1)]},r)})})]});var h};export{Ue as EditableCellView,We as EditableValueView,Ye as RelationshipInHtmlRow,Ze as RelationshipInHtmlTable,lt as ResultSortedTableInHtmlTable,rt as ResultTableInHtmlTable,nt as SliceInHtmlTable,st as SortedTableInHtmlTable,tt as SortedTablePaginator,at as TableInHtmlTable,ct as ValuesInHtmlTable,et as useSortingAndPagination};