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