pocketbase-react 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -69,7 +69,7 @@ const mobileRedirectURL = "expo://..." // for example
69
69
 
70
70
  ## Caveats
71
71
  ```tsx
72
- import { useAppContent } = "pocketbase-react";
72
+ import { useAppContent, useAuth } from "pocketbase-react";
73
73
  ```
74
74
  ### Records
75
75
  Reading the records value directly accesses the Redux Store.
@@ -173,17 +173,4 @@ await actions.updateEmail(email: string);
173
173
  Delete user
174
174
  ```tsx
175
175
  await actions.deleteUser(id: string);
176
- ```
177
-
178
-
179
- ## Development
180
-
181
- By using
182
- ```sh
183
- npm run build
184
- # or
185
- yarn build
186
- ```
187
- You will get a folder called dist, which you can use to replace node_modules/pocketbase-react/dist in your React project.
188
-
189
- ***Maybe someone finds a better way to integrate the package in development with a project***
176
+ ```
@@ -5976,11 +5976,12 @@
5976
5976
  return context;
5977
5977
  };
5978
5978
 
5979
- const ContentContext = react.createContext(null);
5979
+ const ContentContext = react.createContext({});
5980
5980
  const ContentProvider = props => {
5981
5981
  const client = useClientContext();
5982
5982
  const dispatch = useAppDispatch;
5983
5983
  const [collections, _] = react.useState(props.collections || []);
5984
+ const [subscribed, setSubscribed] = react.useState([]);
5984
5985
  const actions = {
5985
5986
  subscribe: async collectionName => {
5986
5987
  await (client == null ? void 0 : client.realtime.subscribe(collectionName, event => {
@@ -5995,13 +5996,19 @@
5995
5996
  dispatch(deleteRecord$1(collectionName, event.record));
5996
5997
  break;
5997
5998
  }
5999
+ }).then(() => {
6000
+ setSubscribed([...subscribed, collectionName]);
5998
6001
  }).catch(_error => {}));
5999
6002
  },
6000
6003
  unsubscribe: collectionName => {
6001
6004
  if (collectionName) {
6002
- client == null ? void 0 : client.realtime.unsubscribe(collectionName).catch(_error => {});
6005
+ client == null ? void 0 : client.realtime.unsubscribe(collectionName).then(() => {
6006
+ setSubscribed(subscribed.filter(name => name !== collectionName));
6007
+ }).catch(_error => {});
6003
6008
  } else {
6004
- client == null ? void 0 : client.realtime.unsubscribe().catch(_error => {});
6009
+ client == null ? void 0 : client.realtime.unsubscribe().then(() => {
6010
+ setSubscribed([]);
6011
+ }).catch(_error => {});
6005
6012
  }
6006
6013
  },
6007
6014
  fetch: async collectionName => {
@@ -6028,7 +6035,10 @@
6028
6035
  return () => actions.unsubscribe();
6029
6036
  }, [collections]);
6030
6037
  return /*#__PURE__*/react.createElement(ContentContext.Provider, {
6031
- value: actions
6038
+ value: {
6039
+ actions,
6040
+ subscribed
6041
+ }
6032
6042
  }, props.children);
6033
6043
  };
6034
6044
 
@@ -6039,7 +6049,7 @@
6039
6049
  }, props.children);
6040
6050
  };
6041
6051
 
6042
- const AuthContext = react.createContext(null);
6052
+ const AuthContext = react.createContext({});
6043
6053
  const AuthProvider = props => {
6044
6054
  const client = useClientContext();
6045
6055
  const [authProviders, setAuthProviders] = react.useState();
@@ -6231,20 +6241,25 @@
6231
6241
  const context = react.useContext(ContentContext);
6232
6242
  react.useEffect(() => {
6233
6243
  if (initialFetch) {
6234
- context == null ? void 0 : context.fetch(collectionName);
6244
+ context.actions.fetch(collectionName);
6235
6245
  }
6236
6246
  }, [collectionName, initialFetch]);
6247
+ const [isSubscribed, setIsSubscribed] = react.useState(false);
6248
+ react.useEffect(() => {
6249
+ setIsSubscribed(context.subscribed.includes(collectionName));
6250
+ }, [collectionName, context.subscribed]);
6237
6251
  const actions = {
6238
- subscribe: async () => await (context == null ? void 0 : context.subscribe(collectionName)),
6239
- unsubscribe: () => context == null ? void 0 : context.unsubscribe(collectionName),
6240
- fetch: async () => await (context == null ? void 0 : context.fetch(collectionName)),
6241
- create: async record => await (context == null ? void 0 : context.create(collectionName, record)),
6242
- update: async (id, record) => await (context == null ? void 0 : context.update(collectionName, id, record)),
6243
- delete: async id => await (context == null ? void 0 : context.delete(collectionName, id))
6252
+ subscribe: async () => await context.actions.subscribe(collectionName),
6253
+ unsubscribe: () => context.actions.unsubscribe(collectionName),
6254
+ fetch: async () => await context.actions.fetch(collectionName),
6255
+ create: async record => await context.actions.create(collectionName, record),
6256
+ update: async (id, record) => await context.actions.update(collectionName, id, record),
6257
+ delete: async id => await context.actions.delete(collectionName, id)
6244
6258
  };
6245
6259
  return {
6246
6260
  records,
6247
- actions
6261
+ actions,
6262
+ isSubscribed
6248
6263
  };
6249
6264
  }
6250
6265
 
@@ -25,4 +25,4 @@
25
25
  * This source code is licensed under the MIT license found in the
26
26
  * LICENSE file in the root directory of this source tree.
27
27
  */
28
- var Xr,Gr=Symbol.for("react.element"),Yr=Symbol.for("react.portal"),Qr=Symbol.for("react.fragment"),Zr=Symbol.for("react.strict_mode"),en=Symbol.for("react.profiler"),tn=Symbol.for("react.provider"),rn=Symbol.for("react.context"),nn=Symbol.for("react.server_context"),on=Symbol.for("react.forward_ref"),un=Symbol.for("react.suspense"),cn=Symbol.for("react.suspense_list"),an=Symbol.for("react.memo"),fn=Symbol.for("react.lazy"),sn=Symbol.for("react.offscreen");function ln(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case Gr:switch(e=e.type){case Qr:case en:case Zr:case un:case cn:return e;default:switch(e=e&&e.$$typeof){case nn:case rn:case on:case fn:case an:case tn:return e;default:return t}}case Yr:return t}}}Xr=Symbol.for("react.module.reference");var pn={ContextConsumer:rn,ContextProvider:tn,Element:Gr,ForwardRef:on,Fragment:Qr,Lazy:fn,Memo:an,Portal:Yr,Profiler:en,StrictMode:Zr,Suspense:un,SuspenseList:cn,isAsyncMode:function(){return!1},isConcurrentMode:function(){return!1},isContextConsumer:function(e){return ln(e)===rn},isContextProvider:function(e){return ln(e)===tn},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===Gr},isForwardRef:function(e){return ln(e)===on},isFragment:function(e){return ln(e)===Qr},isLazy:function(e){return ln(e)===fn},isMemo:function(e){return ln(e)===an},isPortal:function(e){return ln(e)===Yr},isProfiler:function(e){return ln(e)===en},isStrictMode:function(e){return ln(e)===Zr},isSuspense:function(e){return ln(e)===un},isSuspenseList:function(e){return ln(e)===cn},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===Qr||e===en||e===Zr||e===un||e===cn||e===sn||"object"==typeof e&&null!==e&&(e.$$typeof===fn||e.$$typeof===an||e.$$typeof===tn||e.$$typeof===rn||e.$$typeof===on||e.$$typeof===Xr||void 0!==e.getModuleId)},typeOf:ln};u((function(e){e.exports=pn}));const yn={notify(){},get:()=>[]};function dn(e,t){let r,n=yn;function o(){u.onStateChange&&u.onStateChange()}function i(){r||(r=t?t.addNestedSub(o):e.subscribe(o),n=function(){const e=mr();let t=null,r=null;return{clear(){t=null,r=null},notify(){e((()=>{let e=t;for(;e;)e.callback(),e=e.next}))},get(){let e=[],r=t;for(;r;)e.push(r),r=r.next;return e},subscribe(e){let n=!0,o=r={callback:e,next:null,prev:r};return o.prev?o.prev.next=o:t=o,function(){n&&null!==t&&(n=!1,o.next?o.next.prev=o.prev:r=o.prev,o.prev?o.prev.next=o.next:t=o.next)}}}}())}const u={addNestedSub:function(e){return i(),n.subscribe(e)},notifyNestedSubs:function(){n.notify()},handleChangeWrapper:o,isSubscribed:function(){return!!r},trySubscribe:i,tryUnsubscribe:function(){r&&(r(),r=void 0,n.clear(),n=yn)},getListeners:()=>n};return u}const bn=!("undefined"==typeof window||void 0===window.document||void 0===window.document.createElement)?z.useLayoutEffect:z.useEffect;function vn({store:e,context:t,children:r,serverState:n}){const o=z.useMemo((()=>{const t=dn(e);return{store:e,subscription:t,getServerState:n?()=>n:void 0}}),[e,n]),i=z.useMemo((()=>e.getState()),[e]);bn((()=>{const{subscription:t}=o;return t.onStateChange=t.notifyNestedSubs,t.trySubscribe(),i!==e.getState()&&t.notifyNestedSubs(),()=>{t.tryUnsubscribe(),t.onStateChange=void 0}}),[o,i]);return z.createElement((t||gr).Provider,{value:o},r)}wr=vr.useSyncExternalStoreWithSelector,hr=t.unstable_batchedUpdates;var hn=e=>{if("[object Object]"!==Object.prototype.toString.call(e))return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype};const{hasOwnProperty:mn}=Object.prototype,{propertyIsEnumerable:gn}=Object,On=(e,t,r)=>Object.defineProperty(e,t,{value:r,writable:!0,enumerable:!0,configurable:!0}),wn=i,Sn={concatArrays:!1,ignoreUndefined:!1},Pn=e=>{const t=[];for(const r in e)mn.call(e,r)&&t.push(r);if(Object.getOwnPropertySymbols){const r=Object.getOwnPropertySymbols(e);for(const n of r)gn.call(e,n)&&t.push(n)}return t};function En(e){return Array.isArray(e)?function(e){const t=e.slice(0,0);return Pn(e).forEach((r=>{On(t,r,En(e[r]))})),t}(e):hn(e)?function(e){const t=null===Object.getPrototypeOf(e)?Object.create(null):{};return Pn(e).forEach((r=>{On(t,r,En(e[r]))})),t}(e):e}const jn=(e,t,r,n)=>(r.forEach((r=>{void 0===t[r]&&n.ignoreUndefined||On(e,r,r in e&&e[r]!==Object.getPrototypeOf(e)?_n(e[r],t[r],n):En(t[r]))})),e);function _n(e,t,r){return r.concatArrays&&Array.isArray(e)&&Array.isArray(t)?((e,t,r)=>{let n=e.slice(0,0),o=0;return[e,t].forEach((t=>{const i=[];for(let r=0;t.length>r;r++)mn.call(t,r)&&(i.push(r+""),On(n,o++,t===e?t[r]:En(t[r])));n=jn(n,t,Pn(t).filter((e=>!i.includes(e))),r)})),n})(e,t,r):hn(t)&&hn(e)?jn(e,t,Pn(t),r):En(t)}const Rn=function(...e){const t=_n(En(Sn),this!==wn&&this||{},Sn);let r={_:{}};for(const n of e)if(void 0!==n){if(!hn(n))throw new TypeError("`"+n+"` is not an Option Object");r=_n(r,{_:n},t)}return r._}.bind({concatArrays:!0,ignoreUndefined:!0});function xn(e,t){return new Promise(((r,n)=>{try{const n=e();null==t||t(null,n),r(n)}catch(e){null==t||t(e),n(e)}}))}function kn(e,t,r){return Promise.all(e).then((e=>{var n;const o=null!==(n=null==r?void 0:r(e))&&void 0!==n?n:null;return null==t||t(null,o),Promise.resolve(o)}),(e=>(null==t||t(e),Promise.reject(e))))}const An={getItem:(e,t)=>xn((()=>window.localStorage.getItem(e)),t),setItem:(e,t,r)=>xn((()=>window.localStorage.setItem(e,t)),r),removeItem:(e,t)=>xn((()=>window.localStorage.removeItem(e)),t),mergeItem:(e,t,r)=>xn((()=>function(e,t){const r=window.localStorage.getItem(e);if(r){const n=JSON.parse(r),o=JSON.parse(t),i=JSON.stringify(Rn(n,o));window.localStorage.setItem(e,i)}else window.localStorage.setItem(e,t)}(e,t)),r),clear:e=>xn((()=>window.localStorage.clear()),e),getAllKeys:e=>xn((()=>{const e=window.localStorage.length,t=[];for(let r=0;e>r;r+=1){const e=window.localStorage.key(r)||"";t.push(e)}return t}),e),flushGetRequests:()=>{},multiGet:(e,t)=>kn(e.map((e=>An.getItem(e))),t,(t=>t.map(((t,r)=>[e[r],t])))),multiSet:(e,t)=>kn(e.map((e=>An.setItem(e[0],e[1]))),t),multiRemove:(e,t)=>kn(e.map((e=>An.removeItem(e))),t),multiMerge:(e,t)=>kn(e.map((e=>An.mergeItem(e[0],e[1]))),t)};class Cn{static async get(e){return"undefined"!=typeof document?localStorage.getItem(e):await An.getItem(e)}static async set(e,t){return"undefined"!=typeof document?localStorage.setItem(e,t):await An.setItem(e,t)}static async remove(e){return"undefined"!=typeof document?localStorage.removeItem(e):await An.removeItem(e)}}const Dn={key:"root",storage:{getItem:async(e,...t)=>await Cn.get(e),setItem:async(e,t,...r)=>Cn.set(e,t),removeItem:async(e,...t)=>Cn.remove(e)}},$n=ae({reducer:function(e,t){var r=void 0!==e.version?e.version:-1,n=void 0===e.stateReconciler?Mt:e.stateReconciler,o=e.getStoredState||zt,i=void 0!==e.timeout?e.timeout:5e3,u=null,c=!1,a=!0,f=function(e){return e._persist.rehydrated&&u&&!a&&u.update(e),e};return function(s,l){var p=s||{},y=p._persist,d=Bt(p,["_persist"]);if(l.type===Dt){var b=!1,v=function(t,r){b||(l.rehydrate(e.key,t,r),b=!0)};if(i&&setTimeout((function(){!b&&v(void 0,Error('redux-persist: persist timed out for persist key "'.concat(e.key,'"')))}),i),a=!1,u||(u=Lt(e)),y)return Kt({},t(d,l),{_persist:y});if("function"!=typeof l.rehydrate||"function"!=typeof l.register)throw Error("redux-persist: either rehydrate or register is not a function on the PERSIST action. This can happen if the action is being replayed. This is an unexplored use case, please open an issue and we will figure out a resolution.");return l.register(e.key),o(e).then((function(t){var n=e.migrate||function(e,t){return Promise.resolve(e)};n(t,r).then((function(e){v(e)}),(function(e){v(void 0,e)}))}),(function(e){v(void 0,e)})),Kt({},t(d,l),{_persist:{version:r,rehydrated:!1}})}if(l.type===$t)return c=!0,l.result(function(e){var t=e.storage,r="".concat(void 0!==e.keyPrefix?e.keyPrefix:xt).concat(e.key);return t.removeItem(r,Wt)}(e)),Kt({},t(d,l),{_persist:y});if(l.type===kt)return l.result(u&&u.flush()),Kt({},t(d,l),{_persist:y});if(l.type===Ct)a=!0;else if(l.type===At){if(c)return Kt({},d,{_persist:Kt({},y,{rehydrated:!0})});if(l.key===e.key){var h=t(d,l),m=l.payload,g=Kt({},!1!==n&&void 0!==m?n(m,s,h,e):h,{_persist:Kt({},y,{rehydrated:!0})});return f(g)}}if(!y)return t(s,l);var O=t(d,l);return O===d?s:f(Kt({},O,{_persist:y}))}}(Dn,he)}),In=function(e){var t,r=Rt(),n=e||{},o=n.reducer,i=void 0===o?void 0:o,u=n.middleware,c=void 0===u?r():u,a=n.devTools,f=void 0===a||a,s=n.preloadedState,l=void 0===s?void 0:s,p=n.enhancers,y=void 0===p?void 0:p;if("function"==typeof i)t=i;else{if(!function(e){if("object"!=typeof e||null===e)return!1;var t=Object.getPrototypeOf(e);if(null===t)return!0;for(var r=t;null!==Object.getPrototypeOf(r);)r=Object.getPrototypeOf(r);return t===r}(i))throw Error('"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers');t=ae(i)}var d=c;"function"==typeof d&&(d=d(r));var b=se.apply(void 0,d),v=fe;f&&(v=jt(function(e,t){for(var r in t||(t={}))St.call(t,r)&&Et(e,r,t[r]);if(wt)for(var n=0,o=wt(t);o.length>n;n++)Pt.call(t,r=o[n])&&Et(e,r,t[r]);return e}({trace:!1},"object"==typeof f&&f)));var h=[b];return Array.isArray(y)?h=gt([b],y):"function"==typeof y&&(h=y(h)),ce(t,l,v.apply(void 0,h))}({reducer:$n,middleware:[ht]}),Tn=In.dispatch,Un=Er,Nn=function(e,t,r){var n=r||!1,o=ce(Zt,Qt,t&&t.enhancer?t.enhancer:void 0),i=function(e){o.dispatch({type:It,key:e})},u=function(t,r,i){var u={type:At,payload:r,err:i,key:t};e.dispatch(u),o.dispatch(u),n&&c.getState().bootstrapped&&(n(),n=!1)},c=Gt({},o,{purge:function(){var t=[];return e.dispatch({type:$t,result:function(e){t.push(e)}}),Promise.all(t)},flush:function(){var t=[];return e.dispatch({type:kt,result:function(e){t.push(e)}}),Promise.all(t)},pause:function(){e.dispatch({type:Ct})},persist:function(){e.dispatch({type:Dt,register:i,rehydrate:u})}});return t&&t.manualPersist||c.persist(),c}(In),Mn=()=>z.useContext(zn),Ln=z.createContext(null),Fn=e=>{const t=Mn(),r=Tn,[n,o]=z.useState(e.collections||[]),i={subscribe:async e=>{await(null==t?void 0:t.realtime.subscribe(e,(t=>{switch(t.action){case"create":r(X(e,t.record));break;case"update":r(Y(e,t.record));break;case"delete":r(G(e,t.record))}})).catch((e=>{})))},unsubscribe:e=>{e?null==t||t.realtime.unsubscribe(e).catch((e=>{})):null==t||t.realtime.unsubscribe().catch((e=>{}))},fetch:async e=>{const n=await(null==t?void 0:t.records.getFullList(e,200).catch((e=>{})));r(H(e,n))},create:async(e,r)=>{await(null==t?void 0:t.records.create(e,r).catch((e=>{})))},update:async(e,r,n)=>{await(null==t?void 0:t.records.update(e,r,n).catch((e=>{})))},delete:async(e,r)=>{await(null==t?void 0:t.records.delete(e,r).catch((e=>{})))}};return z.useEffect((()=>(n&&n.forEach((e=>{i.fetch(e),i.subscribe(e)})),()=>i.unsubscribe())),[n]),z.createElement(Ln.Provider,{value:i},e.children)},zn=z.createContext(null),Vn=e=>z.createElement(zn.Provider,{value:e.client},e.children),Wn=z.createContext(null),qn=e=>{const t=Mn(),[r,n]=z.useState(),o={registerWithEmail:async(e,r)=>{await(null==t?void 0:t.users.create({email:e,password:r,passwordConfirm:r}))},signInWithEmail:async(e,r)=>{await(null==t?void 0:t.users.authViaEmail(e,r))},signInWithProvider:async t=>{const n=null==r?void 0:r.find((e=>e.name===t)),o=(null==n?void 0:n.authUrl)+typeof document!="undefined"?e.webRedirectUrl:e.mobileRedirectUrl;await e.openURL(o),await Cn.set("provider",JSON.stringify(r))},submitProviderResult:async r=>{const n=new URLSearchParams(r.split("?")[1]),o=n.get("code"),i=n.get("state"),u=await Cn.get("provider");if(u){const r=JSON.parse(u),n=null==r?void 0:r.find((e=>e.state===i));n&&o&&await(null==t?void 0:t.users.authViaOAuth2(n.name,o,n.codeVerifier,"undefined"!=typeof document?e.webRedirectUrl:e.mobileRedirectUrl))}},signOut:()=>{null==t||t.authStore.clear()},sendPasswordResetEmail:async e=>{await(null==t?void 0:t.users.requestPasswordReset(e))},sendEmailVerification:async e=>{await(null==t?void 0:t.users.requestVerification(e))},updateProfile:async(e,r)=>{await(null==t?void 0:t.records.update("profiles",e,r))},updateEmail:async e=>{await(null==t?void 0:t.users.requestEmailChange(e))},deleteUser:async e=>{await(null==t?void 0:t.users.delete(e))}};return z.useEffect((()=>{(async()=>{const e=await(null==t?void 0:t.users.listAuthMethods());n(null==e?void 0:e.authProviders)})()}),[e.webRedirectUrl,e.mobileRedirectUrl]),z.createElement(Wn.Provider,{value:o},e.children)};function Kn(e){return Kn="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},Kn(e)}function Jn(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function Bn(e,t){for(var r=0;t.length>r;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function Hn(e,t){return!t||"object"!==Kn(t)&&"function"!=typeof t?Gn(e):t}function Xn(e){return Xn=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},Xn(e)}function Gn(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function Yn(e,t){return Yn=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},Yn(e,t)}function Qn(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var Zn=function(e){function t(){var e,r;Jn(this,t);for(var n=arguments.length,o=Array(n),i=0;n>i;i++)o[i]=arguments[i];return Qn(Gn(r=Hn(this,(e=Xn(t)).call.apply(e,[this].concat(o)))),"state",{bootstrapped:!1}),Qn(Gn(r),"_unsubscribe",void 0),Qn(Gn(r),"handlePersistorState",(function(){r.props.persistor.getState().bootstrapped&&(r.props.onBeforeLift?Promise.resolve(r.props.onBeforeLift()).finally((function(){return r.setState({bootstrapped:!0})})):r.setState({bootstrapped:!0}),r._unsubscribe&&r._unsubscribe())})),r}var r,n,o;return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&Yn(e,t)}(t,e),r=t,(n=[{key:"componentDidMount",value:function(){this._unsubscribe=this.props.persistor.subscribe(this.handlePersistorState),this.handlePersistorState()}},{key:"componentWillUnmount",value:function(){this._unsubscribe&&this._unsubscribe()}},{key:"render",value:function(){return"function"==typeof this.props.children?this.props.children(this.state.bootstrapped):this.state.bootstrapped?this.props.children:this.props.loading}}])&&Bn(r.prototype,n),o&&Bn(r,o),t}(z.PureComponent);Qn(Zn,"defaultProps",{children:null,loading:null});const eo=z.createContext(null);e.ADD_RECORD=W,e.ADD_RECORDS=q,e.AuthContext=Wn,e.AuthProvider=qn,e.ClientContext=zn,e.ClientProvider=Vn,e.ContentContext=Ln,e.ContentProvider=Fn,e.DELETE_RECORD=J,e.DELETE_RECORDS=B,e.Pocketbase=e=>{const t=new o.default(e.serverURL);return t?z.createElement(Vn,{client:t},z.createElement(vn,{store:In},z.createElement(Zn,{persistor:Nn},z.createElement(qn,{webRedirectUrl:e.webRedirectUrl,mobileRedirectUrl:e.mobileRedirectUrl,openURL:e.openURL},z.createElement(Fn,{collections:e.initialCollections},e.children))))):null},e.PocketbaseContext=eo,e.SET_RECORDS=V,e.UPDATE_RECORD=K,e.appReducer=he,e.persistConfig=Dn,e.persistor=Nn,e.recordsAction=Q,e.store=In,e.useAppContent=function(e,t=!1){var r;const n=null!=(r=Un((t=>t.reducer.records[e])))?r:[],o=z.useContext(Ln);return z.useEffect((()=>{t&&(null==o||o.fetch(e))}),[e,t]),{records:n,actions:{subscribe:async()=>await(null==o?void 0:o.subscribe(e)),unsubscribe:()=>null==o?void 0:o.unsubscribe(e),fetch:async()=>await(null==o?void 0:o.fetch(e)),create:async t=>await(null==o?void 0:o.create(e,t)),update:async(t,r)=>await(null==o?void 0:o.update(e,t,r)),delete:async t=>await(null==o?void 0:o.delete(e,t))}}},e.useAppDispatch=Tn,e.useAppSelector=Un,e.useAuth=function(){return{actions:z.useContext(Wn)}},e.useClientContext=Mn,Object.defineProperty(e,"__esModule",{value:!0})}));
28
+ var Xr,Gr=Symbol.for("react.element"),Yr=Symbol.for("react.portal"),Qr=Symbol.for("react.fragment"),Zr=Symbol.for("react.strict_mode"),en=Symbol.for("react.profiler"),tn=Symbol.for("react.provider"),rn=Symbol.for("react.context"),nn=Symbol.for("react.server_context"),on=Symbol.for("react.forward_ref"),un=Symbol.for("react.suspense"),cn=Symbol.for("react.suspense_list"),an=Symbol.for("react.memo"),fn=Symbol.for("react.lazy"),sn=Symbol.for("react.offscreen");function ln(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case Gr:switch(e=e.type){case Qr:case en:case Zr:case un:case cn:return e;default:switch(e=e&&e.$$typeof){case nn:case rn:case on:case fn:case an:case tn:return e;default:return t}}case Yr:return t}}}Xr=Symbol.for("react.module.reference");var pn={ContextConsumer:rn,ContextProvider:tn,Element:Gr,ForwardRef:on,Fragment:Qr,Lazy:fn,Memo:an,Portal:Yr,Profiler:en,StrictMode:Zr,Suspense:un,SuspenseList:cn,isAsyncMode:function(){return!1},isConcurrentMode:function(){return!1},isContextConsumer:function(e){return ln(e)===rn},isContextProvider:function(e){return ln(e)===tn},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===Gr},isForwardRef:function(e){return ln(e)===on},isFragment:function(e){return ln(e)===Qr},isLazy:function(e){return ln(e)===fn},isMemo:function(e){return ln(e)===an},isPortal:function(e){return ln(e)===Yr},isProfiler:function(e){return ln(e)===en},isStrictMode:function(e){return ln(e)===Zr},isSuspense:function(e){return ln(e)===un},isSuspenseList:function(e){return ln(e)===cn},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===Qr||e===en||e===Zr||e===un||e===cn||e===sn||"object"==typeof e&&null!==e&&(e.$$typeof===fn||e.$$typeof===an||e.$$typeof===tn||e.$$typeof===rn||e.$$typeof===on||e.$$typeof===Xr||void 0!==e.getModuleId)},typeOf:ln};u((function(e){e.exports=pn}));const yn={notify(){},get:()=>[]};function dn(e,t){let r,n=yn;function o(){u.onStateChange&&u.onStateChange()}function i(){r||(r=t?t.addNestedSub(o):e.subscribe(o),n=function(){const e=mr();let t=null,r=null;return{clear(){t=null,r=null},notify(){e((()=>{let e=t;for(;e;)e.callback(),e=e.next}))},get(){let e=[],r=t;for(;r;)e.push(r),r=r.next;return e},subscribe(e){let n=!0,o=r={callback:e,next:null,prev:r};return o.prev?o.prev.next=o:t=o,function(){n&&null!==t&&(n=!1,o.next?o.next.prev=o.prev:r=o.prev,o.prev?o.prev.next=o.next:t=o.next)}}}}())}const u={addNestedSub:function(e){return i(),n.subscribe(e)},notifyNestedSubs:function(){n.notify()},handleChangeWrapper:o,isSubscribed:function(){return!!r},trySubscribe:i,tryUnsubscribe:function(){r&&(r(),r=void 0,n.clear(),n=yn)},getListeners:()=>n};return u}const bn=!("undefined"==typeof window||void 0===window.document||void 0===window.document.createElement)?z.useLayoutEffect:z.useEffect;function vn({store:e,context:t,children:r,serverState:n}){const o=z.useMemo((()=>{const t=dn(e);return{store:e,subscription:t,getServerState:n?()=>n:void 0}}),[e,n]),i=z.useMemo((()=>e.getState()),[e]);bn((()=>{const{subscription:t}=o;return t.onStateChange=t.notifyNestedSubs,t.trySubscribe(),i!==e.getState()&&t.notifyNestedSubs(),()=>{t.tryUnsubscribe(),t.onStateChange=void 0}}),[o,i]);return z.createElement((t||gr).Provider,{value:o},r)}wr=vr.useSyncExternalStoreWithSelector,hr=t.unstable_batchedUpdates;var hn=e=>{if("[object Object]"!==Object.prototype.toString.call(e))return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype};const{hasOwnProperty:mn}=Object.prototype,{propertyIsEnumerable:gn}=Object,On=(e,t,r)=>Object.defineProperty(e,t,{value:r,writable:!0,enumerable:!0,configurable:!0}),wn=i,Sn={concatArrays:!1,ignoreUndefined:!1},Pn=e=>{const t=[];for(const r in e)mn.call(e,r)&&t.push(r);if(Object.getOwnPropertySymbols){const r=Object.getOwnPropertySymbols(e);for(const n of r)gn.call(e,n)&&t.push(n)}return t};function En(e){return Array.isArray(e)?function(e){const t=e.slice(0,0);return Pn(e).forEach((r=>{On(t,r,En(e[r]))})),t}(e):hn(e)?function(e){const t=null===Object.getPrototypeOf(e)?Object.create(null):{};return Pn(e).forEach((r=>{On(t,r,En(e[r]))})),t}(e):e}const jn=(e,t,r,n)=>(r.forEach((r=>{void 0===t[r]&&n.ignoreUndefined||On(e,r,r in e&&e[r]!==Object.getPrototypeOf(e)?_n(e[r],t[r],n):En(t[r]))})),e);function _n(e,t,r){return r.concatArrays&&Array.isArray(e)&&Array.isArray(t)?((e,t,r)=>{let n=e.slice(0,0),o=0;return[e,t].forEach((t=>{const i=[];for(let r=0;t.length>r;r++)mn.call(t,r)&&(i.push(r+""),On(n,o++,t===e?t[r]:En(t[r])));n=jn(n,t,Pn(t).filter((e=>!i.includes(e))),r)})),n})(e,t,r):hn(t)&&hn(e)?jn(e,t,Pn(t),r):En(t)}const Rn=function(...e){const t=_n(En(Sn),this!==wn&&this||{},Sn);let r={_:{}};for(const n of e)if(void 0!==n){if(!hn(n))throw new TypeError("`"+n+"` is not an Option Object");r=_n(r,{_:n},t)}return r._}.bind({concatArrays:!0,ignoreUndefined:!0});function xn(e,t){return new Promise(((r,n)=>{try{const n=e();null==t||t(null,n),r(n)}catch(e){null==t||t(e),n(e)}}))}function kn(e,t,r){return Promise.all(e).then((e=>{var n;const o=null!==(n=null==r?void 0:r(e))&&void 0!==n?n:null;return null==t||t(null,o),Promise.resolve(o)}),(e=>(null==t||t(e),Promise.reject(e))))}const An={getItem:(e,t)=>xn((()=>window.localStorage.getItem(e)),t),setItem:(e,t,r)=>xn((()=>window.localStorage.setItem(e,t)),r),removeItem:(e,t)=>xn((()=>window.localStorage.removeItem(e)),t),mergeItem:(e,t,r)=>xn((()=>function(e,t){const r=window.localStorage.getItem(e);if(r){const n=JSON.parse(r),o=JSON.parse(t),i=JSON.stringify(Rn(n,o));window.localStorage.setItem(e,i)}else window.localStorage.setItem(e,t)}(e,t)),r),clear:e=>xn((()=>window.localStorage.clear()),e),getAllKeys:e=>xn((()=>{const e=window.localStorage.length,t=[];for(let r=0;e>r;r+=1){const e=window.localStorage.key(r)||"";t.push(e)}return t}),e),flushGetRequests:()=>{},multiGet:(e,t)=>kn(e.map((e=>An.getItem(e))),t,(t=>t.map(((t,r)=>[e[r],t])))),multiSet:(e,t)=>kn(e.map((e=>An.setItem(e[0],e[1]))),t),multiRemove:(e,t)=>kn(e.map((e=>An.removeItem(e))),t),multiMerge:(e,t)=>kn(e.map((e=>An.mergeItem(e[0],e[1]))),t)};class Cn{static async get(e){return"undefined"!=typeof document?localStorage.getItem(e):await An.getItem(e)}static async set(e,t){return"undefined"!=typeof document?localStorage.setItem(e,t):await An.setItem(e,t)}static async remove(e){return"undefined"!=typeof document?localStorage.removeItem(e):await An.removeItem(e)}}const Dn={key:"root",storage:{getItem:async(e,...t)=>await Cn.get(e),setItem:async(e,t,...r)=>Cn.set(e,t),removeItem:async(e,...t)=>Cn.remove(e)}},$n=ae({reducer:function(e,t){var r=void 0!==e.version?e.version:-1,n=void 0===e.stateReconciler?Mt:e.stateReconciler,o=e.getStoredState||zt,i=void 0!==e.timeout?e.timeout:5e3,u=null,c=!1,a=!0,f=function(e){return e._persist.rehydrated&&u&&!a&&u.update(e),e};return function(s,l){var p=s||{},y=p._persist,d=Bt(p,["_persist"]);if(l.type===Dt){var b=!1,v=function(t,r){b||(l.rehydrate(e.key,t,r),b=!0)};if(i&&setTimeout((function(){!b&&v(void 0,Error('redux-persist: persist timed out for persist key "'.concat(e.key,'"')))}),i),a=!1,u||(u=Lt(e)),y)return Kt({},t(d,l),{_persist:y});if("function"!=typeof l.rehydrate||"function"!=typeof l.register)throw Error("redux-persist: either rehydrate or register is not a function on the PERSIST action. This can happen if the action is being replayed. This is an unexplored use case, please open an issue and we will figure out a resolution.");return l.register(e.key),o(e).then((function(t){var n=e.migrate||function(e,t){return Promise.resolve(e)};n(t,r).then((function(e){v(e)}),(function(e){v(void 0,e)}))}),(function(e){v(void 0,e)})),Kt({},t(d,l),{_persist:{version:r,rehydrated:!1}})}if(l.type===$t)return c=!0,l.result(function(e){var t=e.storage,r="".concat(void 0!==e.keyPrefix?e.keyPrefix:xt).concat(e.key);return t.removeItem(r,Wt)}(e)),Kt({},t(d,l),{_persist:y});if(l.type===kt)return l.result(u&&u.flush()),Kt({},t(d,l),{_persist:y});if(l.type===Ct)a=!0;else if(l.type===At){if(c)return Kt({},d,{_persist:Kt({},y,{rehydrated:!0})});if(l.key===e.key){var h=t(d,l),m=l.payload,g=Kt({},!1!==n&&void 0!==m?n(m,s,h,e):h,{_persist:Kt({},y,{rehydrated:!0})});return f(g)}}if(!y)return t(s,l);var O=t(d,l);return O===d?s:f(Kt({},O,{_persist:y}))}}(Dn,he)}),In=function(e){var t,r=Rt(),n=e||{},o=n.reducer,i=void 0===o?void 0:o,u=n.middleware,c=void 0===u?r():u,a=n.devTools,f=void 0===a||a,s=n.preloadedState,l=void 0===s?void 0:s,p=n.enhancers,y=void 0===p?void 0:p;if("function"==typeof i)t=i;else{if(!function(e){if("object"!=typeof e||null===e)return!1;var t=Object.getPrototypeOf(e);if(null===t)return!0;for(var r=t;null!==Object.getPrototypeOf(r);)r=Object.getPrototypeOf(r);return t===r}(i))throw Error('"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers');t=ae(i)}var d=c;"function"==typeof d&&(d=d(r));var b=se.apply(void 0,d),v=fe;f&&(v=jt(function(e,t){for(var r in t||(t={}))St.call(t,r)&&Et(e,r,t[r]);if(wt)for(var n=0,o=wt(t);o.length>n;n++)Pt.call(t,r=o[n])&&Et(e,r,t[r]);return e}({trace:!1},"object"==typeof f&&f)));var h=[b];return Array.isArray(y)?h=gt([b],y):"function"==typeof y&&(h=y(h)),ce(t,l,v.apply(void 0,h))}({reducer:$n,middleware:[ht]}),Tn=In.dispatch,Un=Er,Nn=function(e,t,r){var n=r||!1,o=ce(Zt,Qt,t&&t.enhancer?t.enhancer:void 0),i=function(e){o.dispatch({type:It,key:e})},u=function(t,r,i){var u={type:At,payload:r,err:i,key:t};e.dispatch(u),o.dispatch(u),n&&c.getState().bootstrapped&&(n(),n=!1)},c=Gt({},o,{purge:function(){var t=[];return e.dispatch({type:$t,result:function(e){t.push(e)}}),Promise.all(t)},flush:function(){var t=[];return e.dispatch({type:kt,result:function(e){t.push(e)}}),Promise.all(t)},pause:function(){e.dispatch({type:Ct})},persist:function(){e.dispatch({type:Dt,register:i,rehydrate:u})}});return t&&t.manualPersist||c.persist(),c}(In),Mn=()=>z.useContext(zn),Ln=z.createContext({}),Fn=e=>{const t=Mn(),r=Tn,[n,o]=z.useState(e.collections||[]),[i,u]=z.useState([]),c={subscribe:async e=>{await(null==t?void 0:t.realtime.subscribe(e,(t=>{switch(t.action){case"create":r(X(e,t.record));break;case"update":r(Y(e,t.record));break;case"delete":r(G(e,t.record))}})).then((()=>{u([...i,e])})).catch((e=>{})))},unsubscribe:e=>{e?null==t||t.realtime.unsubscribe(e).then((()=>{u(i.filter((t=>t!==e)))})).catch((e=>{})):null==t||t.realtime.unsubscribe().then((()=>{u([])})).catch((e=>{}))},fetch:async e=>{const n=await(null==t?void 0:t.records.getFullList(e,200).catch((e=>{})));r(H(e,n))},create:async(e,r)=>{await(null==t?void 0:t.records.create(e,r).catch((e=>{})))},update:async(e,r,n)=>{await(null==t?void 0:t.records.update(e,r,n).catch((e=>{})))},delete:async(e,r)=>{await(null==t?void 0:t.records.delete(e,r).catch((e=>{})))}};return z.useEffect((()=>(n&&n.forEach((e=>{c.fetch(e),c.subscribe(e)})),()=>c.unsubscribe())),[n]),z.createElement(Ln.Provider,{value:{actions:c,subscribed:i}},e.children)},zn=z.createContext(null),Vn=e=>z.createElement(zn.Provider,{value:e.client},e.children),Wn=z.createContext({}),qn=e=>{const t=Mn(),[r,n]=z.useState(),o={registerWithEmail:async(e,r)=>{await(null==t?void 0:t.users.create({email:e,password:r,passwordConfirm:r}))},signInWithEmail:async(e,r)=>{await(null==t?void 0:t.users.authViaEmail(e,r))},signInWithProvider:async t=>{const n=null==r?void 0:r.find((e=>e.name===t)),o=(null==n?void 0:n.authUrl)+typeof document!="undefined"?e.webRedirectUrl:e.mobileRedirectUrl;await e.openURL(o),await Cn.set("provider",JSON.stringify(r))},submitProviderResult:async r=>{const n=new URLSearchParams(r.split("?")[1]),o=n.get("code"),i=n.get("state"),u=await Cn.get("provider");if(u){const r=JSON.parse(u),n=null==r?void 0:r.find((e=>e.state===i));n&&o&&await(null==t?void 0:t.users.authViaOAuth2(n.name,o,n.codeVerifier,"undefined"!=typeof document?e.webRedirectUrl:e.mobileRedirectUrl))}},signOut:()=>{null==t||t.authStore.clear()},sendPasswordResetEmail:async e=>{await(null==t?void 0:t.users.requestPasswordReset(e))},sendEmailVerification:async e=>{await(null==t?void 0:t.users.requestVerification(e))},updateProfile:async(e,r)=>{await(null==t?void 0:t.records.update("profiles",e,r))},updateEmail:async e=>{await(null==t?void 0:t.users.requestEmailChange(e))},deleteUser:async e=>{await(null==t?void 0:t.users.delete(e))}};return z.useEffect((()=>{(async()=>{const e=await(null==t?void 0:t.users.listAuthMethods());n(null==e?void 0:e.authProviders)})()}),[e.webRedirectUrl,e.mobileRedirectUrl]),z.createElement(Wn.Provider,{value:o},e.children)};function Kn(e){return Kn="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},Kn(e)}function Jn(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function Bn(e,t){for(var r=0;t.length>r;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function Hn(e,t){return!t||"object"!==Kn(t)&&"function"!=typeof t?Gn(e):t}function Xn(e){return Xn=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},Xn(e)}function Gn(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function Yn(e,t){return Yn=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},Yn(e,t)}function Qn(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var Zn=function(e){function t(){var e,r;Jn(this,t);for(var n=arguments.length,o=Array(n),i=0;n>i;i++)o[i]=arguments[i];return Qn(Gn(r=Hn(this,(e=Xn(t)).call.apply(e,[this].concat(o)))),"state",{bootstrapped:!1}),Qn(Gn(r),"_unsubscribe",void 0),Qn(Gn(r),"handlePersistorState",(function(){r.props.persistor.getState().bootstrapped&&(r.props.onBeforeLift?Promise.resolve(r.props.onBeforeLift()).finally((function(){return r.setState({bootstrapped:!0})})):r.setState({bootstrapped:!0}),r._unsubscribe&&r._unsubscribe())})),r}var r,n,o;return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&Yn(e,t)}(t,e),r=t,(n=[{key:"componentDidMount",value:function(){this._unsubscribe=this.props.persistor.subscribe(this.handlePersistorState),this.handlePersistorState()}},{key:"componentWillUnmount",value:function(){this._unsubscribe&&this._unsubscribe()}},{key:"render",value:function(){return"function"==typeof this.props.children?this.props.children(this.state.bootstrapped):this.state.bootstrapped?this.props.children:this.props.loading}}])&&Bn(r.prototype,n),o&&Bn(r,o),t}(z.PureComponent);Qn(Zn,"defaultProps",{children:null,loading:null});const eo=z.createContext(null);e.ADD_RECORD=W,e.ADD_RECORDS=q,e.AuthContext=Wn,e.AuthProvider=qn,e.ClientContext=zn,e.ClientProvider=Vn,e.ContentContext=Ln,e.ContentProvider=Fn,e.DELETE_RECORD=J,e.DELETE_RECORDS=B,e.Pocketbase=e=>{const t=new o.default(e.serverURL);return t?z.createElement(Vn,{client:t},z.createElement(vn,{store:In},z.createElement(Zn,{persistor:Nn},z.createElement(qn,{webRedirectUrl:e.webRedirectUrl,mobileRedirectUrl:e.mobileRedirectUrl,openURL:e.openURL},z.createElement(Fn,{collections:e.initialCollections},e.children))))):null},e.PocketbaseContext=eo,e.SET_RECORDS=V,e.UPDATE_RECORD=K,e.appReducer=he,e.persistConfig=Dn,e.persistor=Nn,e.recordsAction=Q,e.store=In,e.useAppContent=function(e,t=!1){var r;const n=null!=(r=Un((t=>t.reducer.records[e])))?r:[],o=z.useContext(Ln);z.useEffect((()=>{t&&o.actions.fetch(e)}),[e,t]);const[i,u]=z.useState(!1);return z.useEffect((()=>{u(o.subscribed.includes(e))}),[e,o.subscribed]),{records:n,actions:{subscribe:async()=>await o.actions.subscribe(e),unsubscribe:()=>o.actions.unsubscribe(e),fetch:async()=>await o.actions.fetch(e),create:async t=>await o.actions.create(e,t),update:async(t,r)=>await o.actions.update(e,t,r),delete:async t=>await o.actions.delete(e,t)},isSubscribed:i}},e.useAppDispatch=Tn,e.useAppSelector=Un,e.useAuth=function(){return{actions:z.useContext(Wn)}},e.useClientContext=Mn,Object.defineProperty(e,"__esModule",{value:!0})}));
@@ -29,7 +29,7 @@ export interface AuthActions {
29
29
  updateEmail: UpdateEmailType;
30
30
  deleteUser: DeleteUserType;
31
31
  }
32
- export declare const AuthContext: React.Context<AuthActions | null>;
32
+ export declare const AuthContext: React.Context<AuthActions>;
33
33
  export declare type AuthProviderProps = {
34
34
  children: React.ReactNode;
35
35
  webRedirectUrl: string;
@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
  import { createContext } from 'react';
3
3
  import { useClientContext } from '../hooks/useClientContext';
4
4
  import { StorageService } from '../service/Storage';
5
- export const AuthContext = createContext(null);
5
+ export const AuthContext = createContext({});
6
6
  export const AuthProvider = props => {
7
7
  const client = useClientContext();
8
8
  const [authProviders, setAuthProviders] = React.useState();
@@ -13,7 +13,11 @@ interface ContentActions {
13
13
  update: UpdateType;
14
14
  delete: DeleteType;
15
15
  }
16
- export declare const ContentContext: React.Context<ContentActions | null>;
16
+ interface ContentContext {
17
+ actions: ContentActions;
18
+ subscribed: string[];
19
+ }
20
+ export declare const ContentContext: React.Context<ContentContext>;
17
21
  export declare type ContentProviderProps = {
18
22
  children: React.ReactNode;
19
23
  collections?: string[];
@@ -3,11 +3,12 @@ import * as React from 'react';
3
3
  import { createContext, useEffect } from 'react';
4
4
  import { useClientContext } from '../hooks/useClientContext';
5
5
  import { recordsAction } from '../store/actions';
6
- export const ContentContext = createContext(null);
6
+ export const ContentContext = createContext({});
7
7
  export const ContentProvider = props => {
8
8
  const client = useClientContext();
9
9
  const dispatch = store.useAppDispatch;
10
10
  const [collections, _] = React.useState(props.collections || []);
11
+ const [subscribed, setSubscribed] = React.useState([]);
11
12
  const actions = {
12
13
  subscribe: async collectionName => {
13
14
  await (client == null ? void 0 : client.realtime.subscribe(collectionName, event => {
@@ -24,15 +25,19 @@ export const ContentProvider = props => {
24
25
  default:
25
26
  break;
26
27
  }
28
+ }).then(() => {
29
+ setSubscribed([...subscribed, collectionName]);
27
30
  }).catch(_error => {}));
28
31
  },
29
32
  unsubscribe: collectionName => {
30
33
  if (collectionName) {
31
- client == null ? void 0 : client.realtime.unsubscribe(collectionName).catch(_error => {});
32
- ;
34
+ client == null ? void 0 : client.realtime.unsubscribe(collectionName).then(() => {
35
+ setSubscribed(subscribed.filter(name => name !== collectionName));
36
+ }).catch(_error => {});
33
37
  } else {
34
- client == null ? void 0 : client.realtime.unsubscribe().catch(_error => {});
35
- ;
38
+ client == null ? void 0 : client.realtime.unsubscribe().then(() => {
39
+ setSubscribed([]);
40
+ }).catch(_error => {});
36
41
  }
37
42
  },
38
43
  fetch: async collectionName => {
@@ -59,6 +64,9 @@ export const ContentProvider = props => {
59
64
  return () => actions.unsubscribe();
60
65
  }, [collections]);
61
66
  return /*#__PURE__*/React.createElement(ContentContext.Provider, {
62
- value: actions
67
+ value: {
68
+ actions,
69
+ subscribed
70
+ }
63
71
  }, props.children);
64
72
  };
@@ -16,4 +16,5 @@ export interface Actions {
16
16
  export declare function useAppContent<T extends Record>(collectionName: string, initialFetch?: boolean): {
17
17
  records: T[];
18
18
  actions: Actions;
19
+ isSubscribed: boolean;
19
20
  };
@@ -1,4 +1,4 @@
1
- import { useContext, useEffect } from 'react';
1
+ import { useContext, useEffect, useState } from 'react';
2
2
  import * as store from '../store';
3
3
  import { ContentContext } from '../context';
4
4
  export function useAppContent(collectionName, initialFetch = false) {
@@ -7,19 +7,24 @@ export function useAppContent(collectionName, initialFetch = false) {
7
7
  const context = useContext(ContentContext);
8
8
  useEffect(() => {
9
9
  if (initialFetch) {
10
- context == null ? void 0 : context.fetch(collectionName);
10
+ context.actions.fetch(collectionName);
11
11
  }
12
12
  }, [collectionName, initialFetch]);
13
+ const [isSubscribed, setIsSubscribed] = useState(false);
14
+ useEffect(() => {
15
+ setIsSubscribed(context.subscribed.includes(collectionName));
16
+ }, [collectionName, context.subscribed]);
13
17
  const actions = {
14
- subscribe: async () => await (context == null ? void 0 : context.subscribe(collectionName)),
15
- unsubscribe: () => context == null ? void 0 : context.unsubscribe(collectionName),
16
- fetch: async () => await (context == null ? void 0 : context.fetch(collectionName)),
17
- create: async record => await (context == null ? void 0 : context.create(collectionName, record)),
18
- update: async (id, record) => await (context == null ? void 0 : context.update(collectionName, id, record)),
19
- delete: async id => await (context == null ? void 0 : context.delete(collectionName, id))
18
+ subscribe: async () => await context.actions.subscribe(collectionName),
19
+ unsubscribe: () => context.actions.unsubscribe(collectionName),
20
+ fetch: async () => await context.actions.fetch(collectionName),
21
+ create: async record => await context.actions.create(collectionName, record),
22
+ update: async (id, record) => await context.actions.update(collectionName, id, record),
23
+ delete: async id => await context.actions.delete(collectionName, id)
20
24
  };
21
25
  return {
22
26
  records,
23
- actions
27
+ actions,
28
+ isSubscribed
24
29
  };
25
30
  }
@@ -1,4 +1,4 @@
1
1
  import { AuthActions } from '../context/auth';
2
2
  export declare function useAuth(): {
3
- actions: AuthActions | null;
3
+ actions: AuthActions;
4
4
  };
@@ -7,7 +7,7 @@ var _useClientContext = require("../hooks/useClientContext");
7
7
  var _Storage = require("../service/Storage");
8
8
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
9
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
10
- const AuthContext = (0, React.createContext)(null);
10
+ const AuthContext = (0, React.createContext)({});
11
11
  exports.AuthContext = AuthContext;
12
12
  const AuthProvider = props => {
13
13
  const client = (0, _useClientContext.useClientContext)();
@@ -8,12 +8,13 @@ var _useClientContext = require("../hooks/useClientContext");
8
8
  var _actions = require("../store/actions");
9
9
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
10
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
- const ContentContext = (0, React.createContext)(null);
11
+ const ContentContext = (0, React.createContext)({});
12
12
  exports.ContentContext = ContentContext;
13
13
  const ContentProvider = props => {
14
14
  const client = (0, _useClientContext.useClientContext)();
15
15
  const dispatch = store.useAppDispatch;
16
16
  const [collections, _] = React.useState(props.collections || []);
17
+ const [subscribed, setSubscribed] = React.useState([]);
17
18
  const actions = {
18
19
  subscribe: async collectionName => {
19
20
  await (client == null ? void 0 : client.realtime.subscribe(collectionName, event => {
@@ -30,15 +31,19 @@ const ContentProvider = props => {
30
31
  default:
31
32
  break;
32
33
  }
34
+ }).then(() => {
35
+ setSubscribed([...subscribed, collectionName]);
33
36
  }).catch(_error => {}));
34
37
  },
35
38
  unsubscribe: collectionName => {
36
39
  if (collectionName) {
37
- client == null ? void 0 : client.realtime.unsubscribe(collectionName).catch(_error => {});
38
- ;
40
+ client == null ? void 0 : client.realtime.unsubscribe(collectionName).then(() => {
41
+ setSubscribed(subscribed.filter(name => name !== collectionName));
42
+ }).catch(_error => {});
39
43
  } else {
40
- client == null ? void 0 : client.realtime.unsubscribe().catch(_error => {});
41
- ;
44
+ client == null ? void 0 : client.realtime.unsubscribe().then(() => {
45
+ setSubscribed([]);
46
+ }).catch(_error => {});
42
47
  }
43
48
  },
44
49
  fetch: async collectionName => {
@@ -65,7 +70,10 @@ const ContentProvider = props => {
65
70
  return () => actions.unsubscribe();
66
71
  }, [collections]);
67
72
  return /*#__PURE__*/React.createElement(ContentContext.Provider, {
68
- value: actions
73
+ value: {
74
+ actions,
75
+ subscribed
76
+ }
69
77
  }, props.children);
70
78
  };
71
79
  exports.ContentProvider = ContentProvider;
@@ -13,19 +13,24 @@ function useAppContent(collectionName, initialFetch = false) {
13
13
  const context = (0, _react.useContext)(_context.ContentContext);
14
14
  (0, _react.useEffect)(() => {
15
15
  if (initialFetch) {
16
- context == null ? void 0 : context.fetch(collectionName);
16
+ context.actions.fetch(collectionName);
17
17
  }
18
18
  }, [collectionName, initialFetch]);
19
+ const [isSubscribed, setIsSubscribed] = (0, _react.useState)(false);
20
+ (0, _react.useEffect)(() => {
21
+ setIsSubscribed(context.subscribed.includes(collectionName));
22
+ }, [collectionName, context.subscribed]);
19
23
  const actions = {
20
- subscribe: async () => await (context == null ? void 0 : context.subscribe(collectionName)),
21
- unsubscribe: () => context == null ? void 0 : context.unsubscribe(collectionName),
22
- fetch: async () => await (context == null ? void 0 : context.fetch(collectionName)),
23
- create: async record => await (context == null ? void 0 : context.create(collectionName, record)),
24
- update: async (id, record) => await (context == null ? void 0 : context.update(collectionName, id, record)),
25
- delete: async id => await (context == null ? void 0 : context.delete(collectionName, id))
24
+ subscribe: async () => await context.actions.subscribe(collectionName),
25
+ unsubscribe: () => context.actions.unsubscribe(collectionName),
26
+ fetch: async () => await context.actions.fetch(collectionName),
27
+ create: async record => await context.actions.create(collectionName, record),
28
+ update: async (id, record) => await context.actions.update(collectionName, id, record),
29
+ delete: async id => await context.actions.delete(collectionName, id)
26
30
  };
27
31
  return {
28
32
  records,
29
- actions
33
+ actions,
34
+ isSubscribed
30
35
  };
31
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pocketbase-react",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Unofficial React SDK (React, React Native, Expo) for interacting with the PocketBase JS SDK",
5
5
  "keywords": [
6
6
  "pocketbase",
@@ -37,7 +37,7 @@ export interface AuthActions {
37
37
  deleteUser: DeleteUserType;
38
38
  }
39
39
 
40
- export const AuthContext = createContext<AuthActions | null>(null);
40
+ export const AuthContext = createContext<AuthActions>({} as AuthActions);
41
41
 
42
42
  export type AuthProviderProps = {
43
43
  children: React.ReactNode;
@@ -21,7 +21,12 @@ interface ContentActions {
21
21
  delete: DeleteType;
22
22
  }
23
23
 
24
- export const ContentContext = createContext<ContentActions | null>(null);
24
+ interface ContentContext {
25
+ actions: ContentActions;
26
+ subscribed: string[];
27
+ }
28
+
29
+ export const ContentContext = createContext<ContentContext>({} as ContentContext);
25
30
 
26
31
  export type ContentProviderProps = {
27
32
  children: React.ReactNode;
@@ -37,6 +42,7 @@ export const ContentProvider = (props: ContentProviderProps) => {
37
42
  const client = useClientContext();
38
43
  const dispatch = store.useAppDispatch;
39
44
  const [collections, _] = React.useState<string[]>(props.collections || []);
45
+ const [subscribed, setSubscribed] = React.useState<string[]>([]);
40
46
 
41
47
  const actions: ContentActions = {
42
48
  subscribe: async (collectionName: string) => {
@@ -54,13 +60,25 @@ export const ContentProvider = (props: ContentProviderProps) => {
54
60
  default:
55
61
  break;
56
62
  }
57
- }).catch((_error) => {});
63
+ })
64
+ .then(() => {
65
+ setSubscribed([...subscribed, collectionName]);
66
+ })
67
+ .catch((_error) => {});
58
68
  },
59
69
  unsubscribe: (collectionName?: string) => {
60
70
  if (collectionName) {
61
- client?.realtime.unsubscribe(collectionName).catch((_error) => {});;
71
+ client?.realtime.unsubscribe(collectionName)
72
+ .then(() => {
73
+ setSubscribed(subscribed.filter((name) => name !== collectionName));
74
+ })
75
+ .catch((_error) => {});
62
76
  } else {
63
- client?.realtime.unsubscribe().catch((_error) => {});;
77
+ client?.realtime.unsubscribe()
78
+ .then(() => {
79
+ setSubscribed([]);
80
+ })
81
+ .catch((_error) => {});
64
82
  }
65
83
  },
66
84
  fetch: async (collectionName: string) => {
@@ -89,6 +107,9 @@ export const ContentProvider = (props: ContentProviderProps) => {
89
107
  }, [collections]);
90
108
 
91
109
  return (
92
- <ContentContext.Provider value={actions}>{props.children}</ContentContext.Provider>
110
+ <ContentContext.Provider value={{
111
+ actions,
112
+ subscribed,
113
+ }}>{props.children}</ContentContext.Provider>
93
114
  );
94
115
  };
@@ -1,4 +1,4 @@
1
- import { useContext, useEffect } from 'react';
1
+ import { useContext, useEffect, useState } from 'react';
2
2
  import * as store from '../store';
3
3
  import { ContentContext } from '../context';
4
4
  import { Record } from '../interfaces/Record';
@@ -22,23 +22,30 @@ export interface Actions {
22
22
  export function useAppContent<T extends Record>(
23
23
  collectionName: string,
24
24
  initialFetch: boolean = false
25
- ): {records: T[], actions: Actions} {
25
+ ): {records: T[], actions: Actions, isSubscribed: boolean} {
26
26
  const records = (store.useAppSelector((state) => state.reducer.records[collectionName]) ?? []) as T[];
27
27
  const context = useContext(ContentContext);
28
28
 
29
29
  useEffect(() => {
30
30
  if (initialFetch) {
31
- context?.fetch(collectionName);
31
+ context.actions.fetch(collectionName);
32
32
  }
33
33
  }, [collectionName, initialFetch]);
34
34
 
35
+ const [isSubscribed, setIsSubscribed] = useState(false);
36
+
37
+ useEffect(() => {
38
+ setIsSubscribed(context.subscribed.includes(collectionName));
39
+ }, [collectionName, context.subscribed])
40
+
41
+
35
42
  const actions: Actions = {
36
- subscribe: async () => await context?.subscribe(collectionName),
37
- unsubscribe: () => context?.unsubscribe(collectionName),
38
- fetch: async () => await context?.fetch(collectionName),
39
- create: async (record: {}) => await context?.create(collectionName, record),
40
- update: async (id: string, record: {}) => await context?.update(collectionName, id, record),
41
- delete: async (id: string) => await context?.delete(collectionName, id),
43
+ subscribe: async () => await context.actions.subscribe(collectionName),
44
+ unsubscribe: () => context.actions.unsubscribe(collectionName),
45
+ fetch: async () => await context.actions.fetch(collectionName),
46
+ create: async (record: {}) => await context.actions.create(collectionName, record),
47
+ update: async (id: string, record: {}) => await context.actions.update(collectionName, id, record),
48
+ delete: async (id: string) => await context.actions.delete(collectionName, id),
42
49
  };
43
- return { records, actions };
50
+ return { records, actions, isSubscribed };
44
51
  }
@@ -1,7 +1,7 @@
1
1
  import { useContext } from 'react';
2
2
  import { AuthActions, AuthContext } from '../context/auth';
3
3
 
4
- export function useAuth(): { actions: AuthActions | null } {
4
+ export function useAuth(): { actions: AuthActions } {
5
5
  const actions = useContext(AuthContext);
6
6
  return { actions };
7
7
  }