Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@magicbell/react-headless

Package Overview
Dependencies
Maintainers
2
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@magicbell/react-headless - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

3

dist/index.d.ts

@@ -10,1 +10,4 @@ export { default as MagicBellProvider } from './components/MagicBellProvider';

export { secondsToDate, toDate, toUnix } from './lib/date';
export { default as clientSettings } from './stores/clientSettings';
export { default as useConfig } from './stores/config';
export { useNotificationStoresCollection } from './stores/notifications';

2

dist/magicbell-react-headless.esm.js

@@ -1,2 +0,2 @@

import t,{useEffect as e,useState as r}from"react";import n from"zustand/vanilla";import o from"zustand";import i from"humps";import a from"axios";import s from"immer";import u from"ramda/src/findIndex";import c from"ramda/src/mergeRight";import f from"ramda/src/propEq";import d from"ramda/src/uniqBy";import*as l from"ably";import m from"mitt";import v from"dompurify";import h from"ramda/src/isNil";import p from"dayjs";function A(){return(A=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function P(t,e){return(P=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function y(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var S=n(function(){return{apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}});function g(){var t=S.getState(),e=t.apiSecret,r=t.userEmail,n=t.userExternalId,o=t.userKey,i={"X-MAGICBELL-CLIENT-ID":t.clientId,"X-MAGICBELL-API-KEY":t.apiKey};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),o&&(i["X-MAGICBELL-USER-KEY"]=o),n&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=n),i}function k(t,e,r,n){var o=S.getState().serverURL,i=g();return a({method:t,url:e,data:r,params:n,headers:i,baseURL:o}).then(function(t){return t.data},function(t){throw t})}function U(t,e){return void 0===e&&(e={}),k("get",t,void 0,e)}function E(t,e,r){return void 0===e&&(e={}),void 0===r&&(r={}),k("post",t,e,r)}var w=function(){function t(t){void 0===t&&(t="/config"),this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}return t.prototype.get=function(){try{return Promise.resolve(U(this.remotePathOrUrl)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},t}(),R=o(function(t,e){return{channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new w,fetch:function(){try{var r=e();return Promise.resolve(r._repository.get()).then(function(e){t(A({},e,{lastFetchedAt:Date.now()}))})}catch(t){return Promise.reject(t)}}}});function O(t){return c({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}var C=["notifications"],L=function(t){var e,r;function n(e){return void 0===e&&(e="/notifications"),t.call(this,e)||this}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,P(e,r);var o=n.prototype;return o.markAsRead=function(t){return E(this.remotePathOrUrl+"/"+t+"/read").then(function(){return!0}).catch(function(){return!1})},o.markAsUnread=function(t){return E(this.remotePathOrUrl+"/"+t+"/unread").then(function(){return!0}).catch(function(){return!1})},o.markAllAsSeen=function(){return E(this.remotePathOrUrl+"/seen").then(function(){return!0}).catch(function(){return!1})},o.markAllAsRead=function(){return E(this.remotePathOrUrl+"/read").then(function(){return!0}).catch(function(){return!1})},n}(function(){function t(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}var e=t.prototype;return e.get=function(t){try{return Promise.resolve(U(this.remotePathOrUrl+"/"+t)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.findBy=function(t){try{return Promise.resolve(U(this.remotePathOrUrl,t)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.create=function(t){try{return Promise.resolve(E(this.remotePathOrUrl,t)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.update=function(t,e){return void 0===r&&(r={}),k("put",this.remotePathOrUrl+"/"+t,e,r);var r},e.delete=function(t){return(e=this.remotePathOrUrl+"/"+t,void 0===r&&(r={}),k("delete",e,void 0,r)).then(function(){return!0}).catch(function(){return!1});var e,r},t}()),x=o(function(t,e){return{stores:{},_repository:new L,setStore:function(e,r,n){void 0===r&&(r={}),void 0===n&&(n={}),t(s(function(t){t.stores[e]=O(A({},n,{context:r}))}))},fetchStore:function(r,n,o){void 0===n&&(n={}),void 0===o&&(o={});try{var i=e(),a=i._repository,u=i.stores[r];return Promise.resolve(function(){if(u)return Promise.resolve(a.findBy(A({},u.context,n))).then(function(e){t(s(function(t){t.stores[r]=function(t,e,r){void 0===r&&(r={reset:!1});var n=e.notifications,o=y(e,C),i=r.reset?n:d(function(t){return t.id},[].concat(t.notifications,n));return A({context:t.context,notifications:i},o)}(u,A({},e,{lastFetchedAt:new Date}),o)}))});throw new Error("Store not found. Define a store with the "+r+" ID")}())}catch(t){return Promise.reject(t)}},fetchAllStores:function(t,r){void 0===t&&(t={}),void 0===r&&(r={});try{var n=e(),o=n.stores,i=n.fetchStore;for(var a in o)i(a,t,r);return Promise.resolve()}catch(t){return Promise.reject(t)}},markNotificationAsSeen:function(r){var n=e().stores;t(s(function(t){for(var e in n){var o=n[e],i=o.notifications,a=o.unseenCount,s=u(f("id",r),i);s>-1&&(i[s].seenAt||(t.stores[e].unseenCount=Math.max(0,a-1),t.stores[e].notifications[s]=c(i[s],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:function(r){var n=e(),o=n.stores,i=n._repository.markAsRead(r);return t(s(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,s=u(f("id",r),i);s>-1&&(t.stores[e].unreadCount=Math.max(0,a-1),t.stores[e].notifications[s]=c(i[s],{readAt:Date.now()/1e3}))}})),i},markNotificationAsUnread:function(r){var n=e(),o=n.stores,i=n._repository.markAsUnread(r);return t(s(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,s=u(f("id",r),i);s>-1&&(t.stores[e].unreadCount=a+1,t.stores[e].notifications[s]=c(i[s],{readAt:null}))}})),i},deleteNotification:function(r,n){void 0===n&&(n={});var o=e(),i=o.stores,a=o._repository,c=!1===n.persist?Promise.resolve(!0):a.delete(r);return t(s(function(t){for(var e in i){var n=i[e],o=n.notifications,a=n.total,s=n.unseenCount,c=n.unreadCount,d=u(f("id",r),o);if(d>-1){var l=o[d];l.seenAt||(t.stores[e].unseenCount=Math.max(0,s-1)),l.readAt||(t.stores[e].unreadCount=Math.max(0,c-1)),t.stores[e].total=Math.max(0,a-1),t.stores[e].notifications.splice(d,1)}}})),c},markAllAsSeen:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsSeen():Promise.resolve(!0);return t(s(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unseenCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=c(r,{seenAt:Date.now()/1e3})})};for(var n in o)e(n)})),i},markAllAsRead:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsRead():Promise.resolve(!0);return t(s(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unreadCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=c(r,{readAt:Date.now()/1e3})})};for(var n in o)e(n)})),i}}}),I=m();function M(t){var e=S.getState().clientId,r=t.name.replace(/\//gi,"."),n=t.data;return n.client_id&&n.client_id===e?Promise.resolve():"string"==typeof n.id?(new L).get(n.id).then(function(t){I.emit(r,t.notification)}):(I.emit(r,n),Promise.resolve())}function b(t,r){e(function(){return I.on(t,r),function(){I.off(t,r)}},[])}function N(){var t,r=x(),n=function(){return r.fetchAllStores({page:1},{reset:!0})};return t=R(),e(function(){return t.ws?function(t){var e=t.channel,r=function(t){var e=S.getState().serverURL+"/"+t.authUrl,r=g();return new l.Realtime({authUrl:e,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),n=function(){return I.emit("wakeup")};r.connection.on("disconnected",n),r.connection.on("suspended",n);var o=r.channels.get(e);return o.subscribe(M),function(){o.unsubscribe(M),o.detach(),r.connection.off("disconnected"),r.connection.off("suspended"),r.close()}}(t.ws):function(){}},[t.ws]),b("wakeup",n),b("notifications.new",function(){return r.fetchAllStores({page:1})}),b("notifications.seen.all",function(){return r.markAllAsSeen({persist:!1})}),b("notifications.read.all",function(){return r.markAllAsRead({persist:!1})}),b("notifications.read",n),b("notifications.unread",n),b("notifications.delete",function(t){return r.deleteNotification(t.id,{persist:!1})}),null}var _=["serverURL"],j=["children","stores"];function D(t){var e=t.serverURL,r=y(t,_);return e&&(r.serverURL=e),S.setState(r),r}function K(n){var o=n.children,i=n.stores,a=void 0===i?[{id:"default",defaultQueryParams:{}}]:i,s=y(n,j);r(function(){return D(s)}),r(function(){return function(t){var e={};return t.forEach(function(t){var r=t.defaults;e[t.id]=O(A({context:t.defaultQueryParams},void 0===r?{}:r))}),x.setState({stores:e}),e}(a)});var u=R();return e(function(){u.fetch()},[]),t.createElement(t.Fragment,null,t.createElement(N,null),o)}function B(t){void 0===t&&(t="default");var r=x(),n=r.stores,o=r.fetchStore,i=r.markAllAsSeen,a=r.markAllAsRead,s=R(),u=n[t];if(!u)throw new Error("Store not found. Define a store with the "+t+" ID");var c=function(e,r){return o(t,e,r)};return e(function(){s.lastFetchedAt&&!u.lastFetchedAt&&c({page:1})},[s.lastFetchedAt]),A({},u,{isEmpty:0===u.notifications.length,hasNextPage:u.currentPage<u.totalPages,fetch:c,fetchNextPage:function(e,r){return void 0===e&&(e={}),o(t,A({},e,{page:u.currentPage+1}),r)},markAllAsSeen:i,markAllAsRead:a})}function z(t){var e=B((void 0===t?{}:t).storeId);return A({},e,{markAllAsSeen:function(){return e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)}})}function F(t){return t?X(1e3*t):null}function X(t){return p(t)}function G(t){return p(t).unix()}function T(t){var e=x(),r=e.markNotificationAsRead,n=e.markNotificationAsSeen,o=e.markNotificationAsUnread,i=e.deleteNotification,a=function(t){if(h(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),s=t.content?v.sanitize(t.content):t.content;return A({},t,{customAttributes:a,readAt:F(t.readAt),seenAt:F(t.seenAt),sentAt:F(t.sentAt),archivedAt:F(t.archivedAt),isSeen:!h(t.seenAt),isRead:!h(t.readAt),isArchived:!h(t.archivedAt),sanitizedContent:s,markAsSeen:function(){return n(t.id)},markAsRead:function(){return r(t.id)},markAsUnread:function(){return o(t.id)},delete:function(){return i(t.id)}})}function q(t,r){e(function(){return function(){r?r(t):t.markAsSeen()}},[])}function J(t,e){var r=T(t);return q(r,e),r}export{K as MagicBellProvider,N as RealtimeListener,F as secondsToDate,X as toDate,G as toUnix,z as useBell,b as useMagicBellEvent,J as useNotification,T as useNotificationFactory,q as useNotificationUnmount,B as useNotifications};
import t,{useEffect as e,useState as r}from"react";import n from"zustand/vanilla";import o from"zustand";import i from"humps";import a from"axios";import s from"immer";import u from"ramda/src/findIndex";import c from"ramda/src/mergeRight";import f from"ramda/src/propEq";import d from"ramda/src/uniqBy";import*as l from"ably";import m from"mitt";import v from"dompurify";import h from"ramda/src/isNil";import p from"dayjs";function A(){return(A=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function P(t,e){return(P=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function y(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var S=n(function(){return{apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}});function g(){var t=S.getState(),e=t.apiSecret,r=t.userEmail,n=t.userExternalId,o=t.userKey,i={"X-MAGICBELL-CLIENT-ID":t.clientId,"X-MAGICBELL-API-KEY":t.apiKey};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),o&&(i["X-MAGICBELL-USER-KEY"]=o),n&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=n),i}function k(t,e,r,n){var o=S.getState().serverURL,i=g();return a({method:t,url:e,data:r,params:n,headers:i,baseURL:o}).then(function(t){return t.data},function(t){throw t})}function U(t,e){return void 0===e&&(e={}),k("get",t,void 0,e)}function E(t,e,r){return void 0===e&&(e={}),void 0===r&&(r={}),k("post",t,e,r)}var w=function(){function t(t){void 0===t&&(t="/config"),this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}return t.prototype.get=function(){try{return Promise.resolve(U(this.remotePathOrUrl)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},t}(),R=o(function(t,e){return{channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new w,fetch:function(){try{var r=e();return Promise.resolve(r._repository.get()).then(function(e){t(A({},e,{lastFetchedAt:Date.now()}))})}catch(t){return Promise.reject(t)}}}});function O(t){return c({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}var C=["notifications"],L=function(t){var e,r;function n(e){return void 0===e&&(e="/notifications"),t.call(this,e)||this}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,P(e,r);var o=n.prototype;return o.markAsRead=function(t){return E(this.remotePathOrUrl+"/"+t+"/read").then(function(){return!0}).catch(function(){return!1})},o.markAsUnread=function(t){return E(this.remotePathOrUrl+"/"+t+"/unread").then(function(){return!0}).catch(function(){return!1})},o.markAllAsSeen=function(){return E(this.remotePathOrUrl+"/seen").then(function(){return!0}).catch(function(){return!1})},o.markAllAsRead=function(){return E(this.remotePathOrUrl+"/read").then(function(){return!0}).catch(function(){return!1})},n}(function(){function t(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}var e=t.prototype;return e.get=function(t){try{return Promise.resolve(U(this.remotePathOrUrl+"/"+t)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.findBy=function(t){try{return Promise.resolve(U(this.remotePathOrUrl,t)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.create=function(t){try{return Promise.resolve(E(this.remotePathOrUrl,t)).then(function(t){return i.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.update=function(t,e){return void 0===r&&(r={}),k("put",this.remotePathOrUrl+"/"+t,e,r);var r},e.delete=function(t){return(e=this.remotePathOrUrl+"/"+t,void 0===r&&(r={}),k("delete",e,void 0,r)).then(function(){return!0}).catch(function(){return!1});var e,r},t}()),x=o(function(t,e){return{stores:{},_repository:new L,setStore:function(e,r,n){void 0===r&&(r={}),void 0===n&&(n={}),t(s(function(t){t.stores[e]=O(A({},n,{context:r}))}))},fetchStore:function(r,n,o){void 0===n&&(n={}),void 0===o&&(o={});try{var i=e(),a=i._repository,u=i.stores[r];return Promise.resolve(function(){if(u)return Promise.resolve(a.findBy(A({},u.context,n))).then(function(e){t(s(function(t){t.stores[r]=function(t,e,r){void 0===r&&(r={reset:!1});var n=e.notifications,o=y(e,C),i=r.reset?n:d(function(t){return t.id},[].concat(t.notifications,n));return A({context:t.context,notifications:i},o)}(u,A({},e,{lastFetchedAt:new Date}),o)}))});throw new Error("Store not found. Define a store with the "+r+" ID")}())}catch(t){return Promise.reject(t)}},fetchAllStores:function(t,r){void 0===t&&(t={}),void 0===r&&(r={});try{var n=e(),o=n.stores,i=n.fetchStore;for(var a in o)i(a,t,r);return Promise.resolve()}catch(t){return Promise.reject(t)}},markNotificationAsSeen:function(r){var n=e().stores;t(s(function(t){for(var e in n){var o=n[e],i=o.notifications,a=o.unseenCount,s=u(f("id",r),i);s>-1&&(i[s].seenAt||(t.stores[e].unseenCount=Math.max(0,a-1),t.stores[e].notifications[s]=c(i[s],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:function(r){var n=e(),o=n.stores,i=n._repository.markAsRead(r);return t(s(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,s=u(f("id",r),i);s>-1&&(t.stores[e].unreadCount=Math.max(0,a-1),t.stores[e].notifications[s]=c(i[s],{readAt:Date.now()/1e3}))}})),i},markNotificationAsUnread:function(r){var n=e(),o=n.stores,i=n._repository.markAsUnread(r);return t(s(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,s=u(f("id",r),i);s>-1&&(t.stores[e].unreadCount=a+1,t.stores[e].notifications[s]=c(i[s],{readAt:null}))}})),i},deleteNotification:function(r,n){void 0===n&&(n={});var o=e(),i=o.stores,a=o._repository,c=!1===n.persist?Promise.resolve(!0):a.delete(r);return t(s(function(t){for(var e in i){var n=i[e],o=n.notifications,a=n.total,s=n.unseenCount,c=n.unreadCount,d=u(f("id",r),o);if(d>-1){var l=o[d];l.seenAt||(t.stores[e].unseenCount=Math.max(0,s-1)),l.readAt||(t.stores[e].unreadCount=Math.max(0,c-1)),t.stores[e].total=Math.max(0,a-1),t.stores[e].notifications.splice(d,1)}}})),c},markAllAsSeen:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsSeen():Promise.resolve(!0);return t(s(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unseenCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=c(r,{seenAt:Date.now()/1e3})})};for(var n in o)e(n)})),i},markAllAsRead:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsRead():Promise.resolve(!0);return t(s(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unreadCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=c(r,{readAt:Date.now()/1e3})})};for(var n in o)e(n)})),i}}}),I=m();function M(t){var e=S.getState().clientId,r=t.name.replace(/\//gi,"."),n=t.data;return n.client_id&&n.client_id===e?Promise.resolve():"string"==typeof n.id?(new L).get(n.id).then(function(t){I.emit(r,t.notification)}):(I.emit(r,n),Promise.resolve())}function b(t,r){e(function(){return I.on(t,r),function(){I.off(t,r)}},[])}function N(){var t,r=x(),n=function(){return r.fetchAllStores({page:1},{reset:!0})};return t=R(),e(function(){return t.ws?function(t){var e=t.channel,r=function(t){var e=S.getState().serverURL+"/"+t.authUrl,r=g();return new l.Realtime({authUrl:e,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),n=function(){return I.emit("wakeup")};r.connection.on("disconnected",n),r.connection.on("suspended",n);var o=r.channels.get(e);return o.subscribe(M),function(){o.unsubscribe(M),o.detach(),r.connection.off("disconnected"),r.connection.off("suspended"),r.close()}}(t.ws):function(){}},[t.ws]),b("wakeup",n),b("notifications.new",function(){return r.fetchAllStores({page:1})}),b("notifications.seen.all",function(){return r.markAllAsSeen({persist:!1})}),b("notifications.read.all",function(){return r.markAllAsRead({persist:!1})}),b("notifications.read",n),b("notifications.unread",n),b("notifications.delete",function(t){return r.deleteNotification(t.id,{persist:!1})}),null}var _=["serverURL"],j=["children","stores"];function D(t){var e=t.serverURL,r=y(t,_);return e&&(r.serverURL=e),S.setState(r),r}function K(n){var o=n.children,i=n.stores,a=void 0===i?[{id:"default",defaultQueryParams:{}}]:i,s=y(n,j);r(function(){return D(s)}),r(function(){return function(t){var e={};return t.forEach(function(t){var r=t.defaults;e[t.id]=O(A({context:t.defaultQueryParams},void 0===r?{}:r))}),x.setState({stores:e}),e}(a)});var u=R();return e(function(){u.fetch()},[]),t.createElement(t.Fragment,null,t.createElement(N,null),o)}function B(t){void 0===t&&(t="default");var r=x(),n=r.stores,o=r.fetchStore,i=r.markAllAsSeen,a=r.markAllAsRead,s=R(),u=n[t];if(!u)throw new Error("Store not found. Define a store with the "+t+" ID");var c=function(e,r){return o(t,e,r)};return e(function(){s.lastFetchedAt&&!u.lastFetchedAt&&c({page:1})},[s.lastFetchedAt]),A({},u,{isEmpty:0===u.notifications.length,hasNextPage:u.currentPage<u.totalPages,fetch:c,fetchNextPage:function(e,r){return void 0===e&&(e={}),o(t,A({},e,{page:u.currentPage+1}),r)},markAllAsSeen:i,markAllAsRead:a})}function z(t){var e=B((void 0===t?{}:t).storeId);return A({},e,{markAllAsSeen:function(){return e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)}})}function F(t){return t?X(1e3*t):null}function X(t){return p(t)}function G(t){return p(t).unix()}function T(t){var e=x(),r=e.markNotificationAsRead,n=e.markNotificationAsSeen,o=e.markNotificationAsUnread,i=e.deleteNotification,a=function(t){if(h(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),s=t.content?v.sanitize(t.content):t.content;return A({},t,{customAttributes:a,readAt:F(t.readAt),seenAt:F(t.seenAt),sentAt:F(t.sentAt),archivedAt:F(t.archivedAt),isSeen:!h(t.seenAt),isRead:!h(t.readAt),isArchived:!h(t.archivedAt),sanitizedContent:s,markAsSeen:function(){return n(t.id)},markAsRead:function(){return r(t.id)},markAsUnread:function(){return o(t.id)},delete:function(){return i(t.id)}})}function q(t,r){e(function(){return function(){r?r(t):t.markAsSeen()}},[])}function J(t,e){var r=T(t);return q(r,e),r}export{K as MagicBellProvider,N as RealtimeListener,S as clientSettings,F as secondsToDate,X as toDate,G as toUnix,z as useBell,R as useConfig,b as useMagicBellEvent,J as useNotification,T as useNotificationFactory,x as useNotificationStoresCollection,q as useNotificationUnmount,B as useNotifications};
//# sourceMappingURL=magicbell-react-headless.esm.js.map

@@ -1,2 +0,2 @@

var t=require("react"),e=require("zustand/vanilla"),r=require("zustand"),n=require("humps"),o=require("axios"),i=require("immer"),a=require("ramda/src/findIndex"),u=require("ramda/src/mergeRight"),s=require("ramda/src/propEq"),c=require("ramda/src/uniqBy"),f=require("ably"),d=require("mitt"),l=require("dompurify"),v=require("ramda/src/isNil"),h=require("dayjs");function m(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function p(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,n.get?n:{enumerable:!0,get:function(){return t[r]}})}}),e.default=t,e}var A=m(t),P=m(e),y=m(r),g=m(n),S=m(o),E=m(i),k=m(a),U=m(u),x=m(s),O=m(c),w=p(f),R=m(d),b=m(l),C=m(v),L=m(h);function M(){return(M=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function I(t,e){return(I=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function N(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var j=P.default(function(){return{apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}});function _(){var t=j.getState(),e=t.apiSecret,r=t.userEmail,n=t.userExternalId,o=t.userKey,i={"X-MAGICBELL-CLIENT-ID":t.clientId,"X-MAGICBELL-API-KEY":t.apiKey};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),o&&(i["X-MAGICBELL-USER-KEY"]=o),n&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=n),i}function q(t,e,r,n){var o=j.getState().serverURL,i=_();return S.default({method:t,url:e,data:r,params:n,headers:i,baseURL:o}).then(function(t){return t.data},function(t){throw t})}function D(t,e){return void 0===e&&(e={}),q("get",t,void 0,e)}function B(t,e,r){return void 0===e&&(e={}),void 0===r&&(r={}),q("post",t,e,r)}var K=function(){function t(t){void 0===t&&(t="/config"),this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}return t.prototype.get=function(){try{return Promise.resolve(D(this.remotePathOrUrl)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},t}(),z=y.default(function(t,e){return{channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new K,fetch:function(){try{var r=e();return Promise.resolve(r._repository.get()).then(function(e){t(M({},e,{lastFetchedAt:Date.now()}))})}catch(t){return Promise.reject(t)}}}});function F(t){return U.default({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}var X=["notifications"],G=function(t){var e,r;function n(e){return void 0===e&&(e="/notifications"),t.call(this,e)||this}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,I(e,r);var o=n.prototype;return o.markAsRead=function(t){return B(this.remotePathOrUrl+"/"+t+"/read").then(function(){return!0}).catch(function(){return!1})},o.markAsUnread=function(t){return B(this.remotePathOrUrl+"/"+t+"/unread").then(function(){return!0}).catch(function(){return!1})},o.markAllAsSeen=function(){return B(this.remotePathOrUrl+"/seen").then(function(){return!0}).catch(function(){return!1})},o.markAllAsRead=function(){return B(this.remotePathOrUrl+"/read").then(function(){return!0}).catch(function(){return!1})},n}(function(){function t(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}var e=t.prototype;return e.get=function(t){try{return Promise.resolve(D(this.remotePathOrUrl+"/"+t)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.findBy=function(t){try{return Promise.resolve(D(this.remotePathOrUrl,t)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.create=function(t){try{return Promise.resolve(B(this.remotePathOrUrl,t)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.update=function(t,e){return void 0===r&&(r={}),q("put",this.remotePathOrUrl+"/"+t,e,r);var r},e.delete=function(t){return(e=this.remotePathOrUrl+"/"+t,void 0===r&&(r={}),q("delete",e,void 0,r)).then(function(){return!0}).catch(function(){return!1});var e,r},t}()),T=y.default(function(t,e){return{stores:{},_repository:new G,setStore:function(e,r,n){void 0===r&&(r={}),void 0===n&&(n={}),t(E.default(function(t){t.stores[e]=F(M({},n,{context:r}))}))},fetchStore:function(r,n,o){void 0===n&&(n={}),void 0===o&&(o={});try{var i=e(),a=i._repository,u=i.stores[r];return Promise.resolve(function(){if(u)return Promise.resolve(a.findBy(M({},u.context,n))).then(function(e){t(E.default(function(t){t.stores[r]=function(t,e,r){void 0===r&&(r={reset:!1});var n=e.notifications,o=N(e,X),i=r.reset?n:O.default(function(t){return t.id},[].concat(t.notifications,n));return M({context:t.context,notifications:i},o)}(u,M({},e,{lastFetchedAt:new Date}),o)}))});throw new Error("Store not found. Define a store with the "+r+" ID")}())}catch(t){return Promise.reject(t)}},fetchAllStores:function(t,r){void 0===t&&(t={}),void 0===r&&(r={});try{var n=e(),o=n.stores,i=n.fetchStore;for(var a in o)i(a,t,r);return Promise.resolve()}catch(t){return Promise.reject(t)}},markNotificationAsSeen:function(r){var n=e().stores;t(E.default(function(t){for(var e in n){var o=n[e],i=o.notifications,a=o.unseenCount,u=k.default(x.default("id",r),i);u>-1&&(i[u].seenAt||(t.stores[e].unseenCount=Math.max(0,a-1),t.stores[e].notifications[u]=U.default(i[u],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:function(r){var n=e(),o=n.stores,i=n._repository.markAsRead(r);return t(E.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=k.default(x.default("id",r),i);u>-1&&(t.stores[e].unreadCount=Math.max(0,a-1),t.stores[e].notifications[u]=U.default(i[u],{readAt:Date.now()/1e3}))}})),i},markNotificationAsUnread:function(r){var n=e(),o=n.stores,i=n._repository.markAsUnread(r);return t(E.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=k.default(x.default("id",r),i);u>-1&&(t.stores[e].unreadCount=a+1,t.stores[e].notifications[u]=U.default(i[u],{readAt:null}))}})),i},deleteNotification:function(r,n){void 0===n&&(n={});var o=e(),i=o.stores,a=o._repository,u=!1===n.persist?Promise.resolve(!0):a.delete(r);return t(E.default(function(t){for(var e in i){var n=i[e],o=n.notifications,a=n.total,u=n.unseenCount,s=n.unreadCount,c=k.default(x.default("id",r),o);if(c>-1){var f=o[c];f.seenAt||(t.stores[e].unseenCount=Math.max(0,u-1)),f.readAt||(t.stores[e].unreadCount=Math.max(0,s-1)),t.stores[e].total=Math.max(0,a-1),t.stores[e].notifications.splice(c,1)}}})),u},markAllAsSeen:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsSeen():Promise.resolve(!0);return t(E.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unseenCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=U.default(r,{seenAt:Date.now()/1e3})})};for(var n in o)e(n)})),i},markAllAsRead:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsRead():Promise.resolve(!0);return t(E.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unreadCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=U.default(r,{readAt:Date.now()/1e3})})};for(var n in o)e(n)})),i}}}),J=R.default();function Q(t){var e=j.getState().clientId,r=t.name.replace(/\//gi,"."),n=t.data;return n.client_id&&n.client_id===e?Promise.resolve():"string"==typeof n.id?(new G).get(n.id).then(function(t){J.emit(r,t.notification)}):(J.emit(r,n),Promise.resolve())}function Y(e,r){t.useEffect(function(){return J.on(e,r),function(){J.off(e,r)}},[])}function H(){var e,r=T(),n=function(){return r.fetchAllStores({page:1},{reset:!0})};return e=z(),t.useEffect(function(){return e.ws?function(t){var e=t.channel,r=function(t){var e=j.getState().serverURL+"/"+t.authUrl,r=_();return new w.Realtime({authUrl:e,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),n=function(){return J.emit("wakeup")};r.connection.on("disconnected",n),r.connection.on("suspended",n);var o=r.channels.get(e);return o.subscribe(Q),function(){o.unsubscribe(Q),o.detach(),r.connection.off("disconnected"),r.connection.off("suspended"),r.close()}}(e.ws):function(){}},[e.ws]),Y("wakeup",n),Y("notifications.new",function(){return r.fetchAllStores({page:1})}),Y("notifications.seen.all",function(){return r.markAllAsSeen({persist:!1})}),Y("notifications.read.all",function(){return r.markAllAsRead({persist:!1})}),Y("notifications.read",n),Y("notifications.unread",n),Y("notifications.delete",function(t){return r.deleteNotification(t.id,{persist:!1})}),null}var V=["serverURL"],W=["children","stores"];function Z(t){var e=t.serverURL,r=N(t,V);return e&&(r.serverURL=e),j.setState(r),r}function $(e){void 0===e&&(e="default");var r=T(),n=r.stores,o=r.fetchStore,i=r.markAllAsSeen,a=r.markAllAsRead,u=z(),s=n[e];if(!s)throw new Error("Store not found. Define a store with the "+e+" ID");var c=function(t,r){return o(e,t,r)};return t.useEffect(function(){u.lastFetchedAt&&!s.lastFetchedAt&&c({page:1})},[u.lastFetchedAt]),M({},s,{isEmpty:0===s.notifications.length,hasNextPage:s.currentPage<s.totalPages,fetch:c,fetchNextPage:function(t,r){return void 0===t&&(t={}),o(e,M({},t,{page:s.currentPage+1}),r)},markAllAsSeen:i,markAllAsRead:a})}function tt(t){return t?et(1e3*t):null}function et(t){return L.default(t)}function rt(t){var e=T(),r=e.markNotificationAsRead,n=e.markNotificationAsSeen,o=e.markNotificationAsUnread,i=e.deleteNotification,a=function(t){if(C.default(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),u=t.content?b.default.sanitize(t.content):t.content;return M({},t,{customAttributes:a,readAt:tt(t.readAt),seenAt:tt(t.seenAt),sentAt:tt(t.sentAt),archivedAt:tt(t.archivedAt),isSeen:!C.default(t.seenAt),isRead:!C.default(t.readAt),isArchived:!C.default(t.archivedAt),sanitizedContent:u,markAsSeen:function(){return n(t.id)},markAsRead:function(){return r(t.id)},markAsUnread:function(){return o(t.id)},delete:function(){return i(t.id)}})}function nt(e,r){t.useEffect(function(){return function(){r?r(e):e.markAsSeen()}},[])}exports.MagicBellProvider=function(e){var r=e.children,n=e.stores,o=void 0===n?[{id:"default",defaultQueryParams:{}}]:n,i=N(e,W);t.useState(function(){return Z(i)}),t.useState(function(){return function(t){var e={};return t.forEach(function(t){var r=t.defaults;e[t.id]=F(M({context:t.defaultQueryParams},void 0===r?{}:r))}),T.setState({stores:e}),e}(o)});var a=z();return t.useEffect(function(){a.fetch()},[]),A.default.createElement(A.default.Fragment,null,A.default.createElement(H,null),r)},exports.RealtimeListener=H,exports.secondsToDate=tt,exports.toDate=et,exports.toUnix=function(t){return L.default(t).unix()},exports.useBell=function(t){var e=$((void 0===t?{}:t).storeId);return M({},e,{markAllAsSeen:function(){return e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)}})},exports.useMagicBellEvent=Y,exports.useNotification=function(t,e){var r=rt(t);return nt(r,e),r},exports.useNotificationFactory=rt,exports.useNotificationUnmount=nt,exports.useNotifications=$;
var t=require("react"),e=require("zustand/vanilla"),r=require("zustand"),n=require("humps"),o=require("axios"),i=require("immer"),a=require("ramda/src/findIndex"),u=require("ramda/src/mergeRight"),s=require("ramda/src/propEq"),c=require("ramda/src/uniqBy"),f=require("ably"),d=require("mitt"),l=require("dompurify"),v=require("ramda/src/isNil"),h=require("dayjs");function m(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function p(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,n.get?n:{enumerable:!0,get:function(){return t[r]}})}}),e.default=t,e}var A=m(t),P=m(e),y=m(r),g=m(n),S=m(o),E=m(i),x=m(a),k=m(u),U=m(s),O=m(c),w=p(f),R=m(d),C=m(l),b=m(v),L=m(h);function M(){return(M=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function I(t,e){return(I=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function N(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var j=P.default(function(){return{apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}});function _(){var t=j.getState(),e=t.apiSecret,r=t.userEmail,n=t.userExternalId,o=t.userKey,i={"X-MAGICBELL-CLIENT-ID":t.clientId,"X-MAGICBELL-API-KEY":t.apiKey};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),o&&(i["X-MAGICBELL-USER-KEY"]=o),n&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=n),i}function q(t,e,r,n){var o=j.getState().serverURL,i=_();return S.default({method:t,url:e,data:r,params:n,headers:i,baseURL:o}).then(function(t){return t.data},function(t){throw t})}function D(t,e){return void 0===e&&(e={}),q("get",t,void 0,e)}function B(t,e,r){return void 0===e&&(e={}),void 0===r&&(r={}),q("post",t,e,r)}var K=function(){function t(t){void 0===t&&(t="/config"),this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}return t.prototype.get=function(){try{return Promise.resolve(D(this.remotePathOrUrl)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},t}(),z=y.default(function(t,e){return{channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new K,fetch:function(){try{var r=e();return Promise.resolve(r._repository.get()).then(function(e){t(M({},e,{lastFetchedAt:Date.now()}))})}catch(t){return Promise.reject(t)}}}});function F(t){return k.default({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}var X=["notifications"],G=function(t){var e,r;function n(e){return void 0===e&&(e="/notifications"),t.call(this,e)||this}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,I(e,r);var o=n.prototype;return o.markAsRead=function(t){return B(this.remotePathOrUrl+"/"+t+"/read").then(function(){return!0}).catch(function(){return!1})},o.markAsUnread=function(t){return B(this.remotePathOrUrl+"/"+t+"/unread").then(function(){return!0}).catch(function(){return!1})},o.markAllAsSeen=function(){return B(this.remotePathOrUrl+"/seen").then(function(){return!0}).catch(function(){return!1})},o.markAllAsRead=function(){return B(this.remotePathOrUrl+"/read").then(function(){return!0}).catch(function(){return!1})},n}(function(){function t(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}var e=t.prototype;return e.get=function(t){try{return Promise.resolve(D(this.remotePathOrUrl+"/"+t)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.findBy=function(t){try{return Promise.resolve(D(this.remotePathOrUrl,t)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.create=function(t){try{return Promise.resolve(B(this.remotePathOrUrl,t)).then(function(t){return g.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.update=function(t,e){return void 0===r&&(r={}),q("put",this.remotePathOrUrl+"/"+t,e,r);var r},e.delete=function(t){return(e=this.remotePathOrUrl+"/"+t,void 0===r&&(r={}),q("delete",e,void 0,r)).then(function(){return!0}).catch(function(){return!1});var e,r},t}()),T=y.default(function(t,e){return{stores:{},_repository:new G,setStore:function(e,r,n){void 0===r&&(r={}),void 0===n&&(n={}),t(E.default(function(t){t.stores[e]=F(M({},n,{context:r}))}))},fetchStore:function(r,n,o){void 0===n&&(n={}),void 0===o&&(o={});try{var i=e(),a=i._repository,u=i.stores[r];return Promise.resolve(function(){if(u)return Promise.resolve(a.findBy(M({},u.context,n))).then(function(e){t(E.default(function(t){t.stores[r]=function(t,e,r){void 0===r&&(r={reset:!1});var n=e.notifications,o=N(e,X),i=r.reset?n:O.default(function(t){return t.id},[].concat(t.notifications,n));return M({context:t.context,notifications:i},o)}(u,M({},e,{lastFetchedAt:new Date}),o)}))});throw new Error("Store not found. Define a store with the "+r+" ID")}())}catch(t){return Promise.reject(t)}},fetchAllStores:function(t,r){void 0===t&&(t={}),void 0===r&&(r={});try{var n=e(),o=n.stores,i=n.fetchStore;for(var a in o)i(a,t,r);return Promise.resolve()}catch(t){return Promise.reject(t)}},markNotificationAsSeen:function(r){var n=e().stores;t(E.default(function(t){for(var e in n){var o=n[e],i=o.notifications,a=o.unseenCount,u=x.default(U.default("id",r),i);u>-1&&(i[u].seenAt||(t.stores[e].unseenCount=Math.max(0,a-1),t.stores[e].notifications[u]=k.default(i[u],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:function(r){var n=e(),o=n.stores,i=n._repository.markAsRead(r);return t(E.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=x.default(U.default("id",r),i);u>-1&&(t.stores[e].unreadCount=Math.max(0,a-1),t.stores[e].notifications[u]=k.default(i[u],{readAt:Date.now()/1e3}))}})),i},markNotificationAsUnread:function(r){var n=e(),o=n.stores,i=n._repository.markAsUnread(r);return t(E.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=x.default(U.default("id",r),i);u>-1&&(t.stores[e].unreadCount=a+1,t.stores[e].notifications[u]=k.default(i[u],{readAt:null}))}})),i},deleteNotification:function(r,n){void 0===n&&(n={});var o=e(),i=o.stores,a=o._repository,u=!1===n.persist?Promise.resolve(!0):a.delete(r);return t(E.default(function(t){for(var e in i){var n=i[e],o=n.notifications,a=n.total,u=n.unseenCount,s=n.unreadCount,c=x.default(U.default("id",r),o);if(c>-1){var f=o[c];f.seenAt||(t.stores[e].unseenCount=Math.max(0,u-1)),f.readAt||(t.stores[e].unreadCount=Math.max(0,s-1)),t.stores[e].total=Math.max(0,a-1),t.stores[e].notifications.splice(c,1)}}})),u},markAllAsSeen:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsSeen():Promise.resolve(!0);return t(E.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unseenCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=k.default(r,{seenAt:Date.now()/1e3})})};for(var n in o)e(n)})),i},markAllAsRead:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsRead():Promise.resolve(!0);return t(E.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unreadCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=k.default(r,{readAt:Date.now()/1e3})})};for(var n in o)e(n)})),i}}}),J=R.default();function Q(t){var e=j.getState().clientId,r=t.name.replace(/\//gi,"."),n=t.data;return n.client_id&&n.client_id===e?Promise.resolve():"string"==typeof n.id?(new G).get(n.id).then(function(t){J.emit(r,t.notification)}):(J.emit(r,n),Promise.resolve())}function Y(e,r){t.useEffect(function(){return J.on(e,r),function(){J.off(e,r)}},[])}function H(){var e,r=T(),n=function(){return r.fetchAllStores({page:1},{reset:!0})};return e=z(),t.useEffect(function(){return e.ws?function(t){var e=t.channel,r=function(t){var e=j.getState().serverURL+"/"+t.authUrl,r=_();return new w.Realtime({authUrl:e,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),n=function(){return J.emit("wakeup")};r.connection.on("disconnected",n),r.connection.on("suspended",n);var o=r.channels.get(e);return o.subscribe(Q),function(){o.unsubscribe(Q),o.detach(),r.connection.off("disconnected"),r.connection.off("suspended"),r.close()}}(e.ws):function(){}},[e.ws]),Y("wakeup",n),Y("notifications.new",function(){return r.fetchAllStores({page:1})}),Y("notifications.seen.all",function(){return r.markAllAsSeen({persist:!1})}),Y("notifications.read.all",function(){return r.markAllAsRead({persist:!1})}),Y("notifications.read",n),Y("notifications.unread",n),Y("notifications.delete",function(t){return r.deleteNotification(t.id,{persist:!1})}),null}var V=["serverURL"],W=["children","stores"];function Z(t){var e=t.serverURL,r=N(t,V);return e&&(r.serverURL=e),j.setState(r),r}function $(e){void 0===e&&(e="default");var r=T(),n=r.stores,o=r.fetchStore,i=r.markAllAsSeen,a=r.markAllAsRead,u=z(),s=n[e];if(!s)throw new Error("Store not found. Define a store with the "+e+" ID");var c=function(t,r){return o(e,t,r)};return t.useEffect(function(){u.lastFetchedAt&&!s.lastFetchedAt&&c({page:1})},[u.lastFetchedAt]),M({},s,{isEmpty:0===s.notifications.length,hasNextPage:s.currentPage<s.totalPages,fetch:c,fetchNextPage:function(t,r){return void 0===t&&(t={}),o(e,M({},t,{page:s.currentPage+1}),r)},markAllAsSeen:i,markAllAsRead:a})}function tt(t){return t?et(1e3*t):null}function et(t){return L.default(t)}function rt(t){var e=T(),r=e.markNotificationAsRead,n=e.markNotificationAsSeen,o=e.markNotificationAsUnread,i=e.deleteNotification,a=function(t){if(b.default(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),u=t.content?C.default.sanitize(t.content):t.content;return M({},t,{customAttributes:a,readAt:tt(t.readAt),seenAt:tt(t.seenAt),sentAt:tt(t.sentAt),archivedAt:tt(t.archivedAt),isSeen:!b.default(t.seenAt),isRead:!b.default(t.readAt),isArchived:!b.default(t.archivedAt),sanitizedContent:u,markAsSeen:function(){return n(t.id)},markAsRead:function(){return r(t.id)},markAsUnread:function(){return o(t.id)},delete:function(){return i(t.id)}})}function nt(e,r){t.useEffect(function(){return function(){r?r(e):e.markAsSeen()}},[])}exports.MagicBellProvider=function(e){var r=e.children,n=e.stores,o=void 0===n?[{id:"default",defaultQueryParams:{}}]:n,i=N(e,W);t.useState(function(){return Z(i)}),t.useState(function(){return function(t){var e={};return t.forEach(function(t){var r=t.defaults;e[t.id]=F(M({context:t.defaultQueryParams},void 0===r?{}:r))}),T.setState({stores:e}),e}(o)});var a=z();return t.useEffect(function(){a.fetch()},[]),A.default.createElement(A.default.Fragment,null,A.default.createElement(H,null),r)},exports.RealtimeListener=H,exports.clientSettings=j,exports.secondsToDate=tt,exports.toDate=et,exports.toUnix=function(t){return L.default(t).unix()},exports.useBell=function(t){var e=$((void 0===t?{}:t).storeId);return M({},e,{markAllAsSeen:function(){return e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)}})},exports.useConfig=z,exports.useMagicBellEvent=Y,exports.useNotification=function(t,e){var r=rt(t);return nt(r,e),r},exports.useNotificationFactory=rt,exports.useNotificationStoresCollection=T,exports.useNotificationUnmount=nt,exports.useNotifications=$;
//# sourceMappingURL=magicbell-react-headless.js.map

@@ -1,2 +0,2 @@

import t,{useEffect as e,useState as n}from"react";import r from"zustand/vanilla";import o from"zustand";import s from"humps";import i from"axios";import a from"immer";import c from"ramda/src/findIndex";import u from"ramda/src/mergeRight";import l from"ramda/src/propEq";import d from"ramda/src/uniqBy";import*as f from"ably";import m from"mitt";import h from"dompurify";import A from"ramda/src/isNil";import p from"dayjs";function y(){return(y=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function S(t,e){if(null==t)return{};var n,r,o={},s=Object.keys(t);for(r=0;r<s.length;r++)e.indexOf(n=s[r])>=0||(o[n]=t[n]);return o}const g=r(()=>({apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}));function v(){const{apiKey:t,apiSecret:e,clientId:n,userEmail:r,userExternalId:o,userKey:s}=g.getState(),i={"X-MAGICBELL-CLIENT-ID":n,"X-MAGICBELL-API-KEY":t};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),s&&(i["X-MAGICBELL-USER-KEY"]=s),o&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=o),i}function w(t,e,n,r){const{serverURL:o}=g.getState(),s=v();return i({method:t,url:e,data:n,params:r,headers:s,baseURL:o}).then(t=>t.data,t=>{throw t})}function P(t,e={}){return w("get",t,void 0,e)}function k(t,e={},n={}){return w("post",t,e,n)}class U{constructor(t="/config"){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}async get(){const t=await P(this.remotePathOrUrl);return s.camelizeKeys(t)}}const E=o((t,e)=>({channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new U,fetch:async()=>{const{_repository:n}=e(),r=await n.get();t(y({},r,{lastFetchedAt:Date.now()}))}}));function R(t){return u({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}const C=["notifications"];class x extends class{constructor(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}async get(t){const e=`${this.remotePathOrUrl}/${t}`,n=await P(e);return s.camelizeKeys(n)}async findBy(t){const e=await P(this.remotePathOrUrl,t);return s.camelizeKeys(e)}async create(t){const e=await k(this.remotePathOrUrl,t);return s.camelizeKeys(e)}update(t,e){return function(t,e,n={}){return w("put",t,e,n)}(`${this.remotePathOrUrl}/${t}`,e)}delete(t){return function(t,e={}){return w("delete",t,void 0,e)}(`${this.remotePathOrUrl}/${t}`).then(()=>!0).catch(()=>!1)}}{constructor(t="/notifications"){super(t)}markAsRead(t){return k(`${this.remotePathOrUrl}/${t}/read`).then(()=>!0).catch(()=>!1)}markAsUnread(t){return k(`${this.remotePathOrUrl}/${t}/unread`).then(()=>!0).catch(()=>!1)}markAllAsSeen(){return k(`${this.remotePathOrUrl}/seen`).then(()=>!0).catch(()=>!1)}markAllAsRead(){return k(`${this.remotePathOrUrl}/read`).then(()=>!0).catch(()=>!1)}}const L=o((t,e)=>({stores:{},_repository:new x,setStore:(e,n={},r={})=>{t(a(t=>{t.stores[e]=R(y({},r,{context:n}))}))},fetchStore:async(n,r={},o={})=>{const{stores:s,_repository:i}=e(),c=s[n];if(!c)throw new Error(`Store not found. Define a store with the ${n} ID`);{const e=await i.findBy(y({},c.context,r));t(a(t=>{t.stores[n]=function(t,e,n={reset:!1}){const{notifications:r}=e,o=S(e,C),s=n.reset?r:d(t=>t.id,[...t.notifications,...r]);return y({context:t.context,notifications:s},o)}(c,y({},e,{lastFetchedAt:new Date}),o)}))}},fetchAllStores:async(t={},n={})=>{const{stores:r,fetchStore:o}=e();for(const e in r)o(e,t,n)},markNotificationAsSeen:n=>{const{stores:r}=e();t(a(t=>{for(const e in r){const{notifications:o,unseenCount:s}=r[e],i=c(l("id",n),o);i>-1&&(o[i].seenAt||(t.stores[e].unseenCount=Math.max(0,s-1),t.stores[e].notifications[i]=u(o[i],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:n=>{const{stores:r,_repository:o}=e(),s=o.markAsRead(n);return t(a(t=>{for(const e in r){const{notifications:o,unreadCount:s}=r[e],i=c(l("id",n),o);i>-1&&(t.stores[e].unreadCount=Math.max(0,s-1),t.stores[e].notifications[i]=u(o[i],{readAt:Date.now()/1e3}))}})),s},markNotificationAsUnread:n=>{const{stores:r,_repository:o}=e(),s=o.markAsUnread(n);return t(a(t=>{for(const e in r){const{notifications:o,unreadCount:s}=r[e],i=c(l("id",n),o);i>-1&&(t.stores[e].unreadCount=s+1,t.stores[e].notifications[i]=u(o[i],{readAt:null}))}})),s},deleteNotification:(n,r={})=>{const{stores:o,_repository:s}=e(),i=!1===r.persist?Promise.resolve(!0):s.delete(n);return t(a(t=>{for(const e in o){const{notifications:r,total:s,unseenCount:i,unreadCount:a}=o[e],u=c(l("id",n),r);if(u>-1){const n=r[u];n.seenAt||(t.stores[e].unseenCount=Math.max(0,i-1)),n.readAt||(t.stores[e].unreadCount=Math.max(0,a-1)),t.stores[e].total=Math.max(0,s-1),t.stores[e].notifications.splice(u,1)}}})),i},markAllAsSeen:(n={persist:!0,updateModels:!0})=>{const{stores:r,_repository:o}=e(),s=!1!==n.persist?o.markAllAsSeen():Promise.resolve(!0);return t(a(t=>{for(const e in r){const{notifications:o}=r[e];t.stores[e].unseenCount=0,!1!==n.updateModels&&o.forEach((n,r)=>{t.stores[e].notifications[r]=u(n,{seenAt:Date.now()/1e3})})}})),s},markAllAsRead:(n={persist:!0,updateModels:!0})=>{const{stores:r,_repository:o}=e(),s=!1!==n.persist?o.markAllAsRead():Promise.resolve(!0);return t(a(t=>{for(const e in r){const{notifications:o}=r[e];t.stores[e].unreadCount=0,!1!==n.updateModels&&o.forEach((n,r)=>{t.stores[e].notifications[r]=u(n,{readAt:Date.now()/1e3})})}})),s}})),O=m();function I(t){const{clientId:e}=g.getState(),n=t.name.replace(/\//gi,"."),r=t.data;return r.client_id&&r.client_id===e?Promise.resolve():"string"==typeof r.id?(new x).get(r.id).then(t=>{O.emit(n,t.notification)}):(O.emit(n,r),Promise.resolve())}function M(t,n){e(()=>(O.on(t,n),()=>{O.off(t,n)}),[])}function b(){const t=L(),n=()=>t.fetchAllStores({page:1},{reset:!0});return function(){const t=E();e(()=>t.ws?function(t){const{channel:e}=t,n=function(t){const{serverURL:e}=g.getState(),n=`${e}/${t.authUrl}`,r=v();return new f.Realtime({authUrl:n,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),r=()=>O.emit("wakeup");n.connection.on("disconnected",r),n.connection.on("suspended",r);const o=n.channels.get(e);return o.subscribe(I),()=>{o.unsubscribe(I),o.detach(),n.connection.off("disconnected"),n.connection.off("suspended"),n.close()}}(t.ws):()=>{},[t.ws])}(),M("wakeup",n),M("notifications.new",()=>t.fetchAllStores({page:1})),M("notifications.seen.all",()=>t.markAllAsSeen({persist:!1})),M("notifications.read.all",()=>t.markAllAsRead({persist:!1})),M("notifications.read",n),M("notifications.unread",n),M("notifications.delete",e=>t.deleteNotification(e.id,{persist:!1})),null}const N=["serverURL"],$=["children","stores"];function D(t){let{serverURL:e}=t;const n=S(t,N);return e&&(n.serverURL=e),g.setState(n),n}function _(r){let{children:o,stores:s=[{id:"default",defaultQueryParams:{}}]}=r,i=S(r,$);n(()=>D(i)),n(()=>function(t){const e={};return t.forEach(t=>{const{defaultQueryParams:n,defaults:r={}}=t;e[t.id]=R(y({context:n},r))}),L.setState({stores:e}),e}(s));const a=E();return e(()=>{a.fetch()},[]),t.createElement(t.Fragment,null,t.createElement(b,null),o)}function K(t="default"){const{stores:n,fetchStore:r,markAllAsSeen:o,markAllAsRead:s}=L(),i=E(),a=n[t];if(!a)throw new Error(`Store not found. Define a store with the ${t} ID`);const c=(e,n)=>r(t,e,n);return e(()=>{i.lastFetchedAt&&!a.lastFetchedAt&&c({page:1})},[i.lastFetchedAt]),y({},a,{isEmpty:0===a.notifications.length,hasNextPage:a.currentPage<a.totalPages,fetch:c,fetchNextPage:(e={},n)=>r(t,y({},e,{page:a.currentPage+1}),n),markAllAsSeen:o,markAllAsRead:s})}function B({storeId:t}={}){const e=K(t);return y({},e,{markAllAsSeen:()=>e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)})}function z(t){return t?F(1e3*t):null}function F(t){return p(t)}function X(t){return p(t).unix()}function G(t){const{markNotificationAsRead:e,markNotificationAsSeen:n,markNotificationAsUnread:r,deleteNotification:o}=L(),s=function(t){if(A(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),i=t.content?h.sanitize(t.content):t.content;return y({},t,{customAttributes:s,readAt:z(t.readAt),seenAt:z(t.seenAt),sentAt:z(t.sentAt),archivedAt:z(t.archivedAt),isSeen:!A(t.seenAt),isRead:!A(t.readAt),isArchived:!A(t.archivedAt),sanitizedContent:i,markAsSeen:()=>n(t.id),markAsRead:()=>e(t.id),markAsUnread:()=>r(t.id),delete:()=>o(t.id)})}function j(t,n){e(()=>()=>{n?n(t):t.markAsSeen()},[])}function T(t,e){const n=G(t);return j(n,e),n}export{_ as MagicBellProvider,b as RealtimeListener,z as secondsToDate,F as toDate,X as toUnix,B as useBell,M as useMagicBellEvent,T as useNotification,G as useNotificationFactory,j as useNotificationUnmount,K as useNotifications};
import t,{useEffect as e,useState as n}from"react";import r from"zustand/vanilla";import o from"zustand";import s from"humps";import i from"axios";import a from"immer";import c from"ramda/src/findIndex";import u from"ramda/src/mergeRight";import l from"ramda/src/propEq";import d from"ramda/src/uniqBy";import*as f from"ably";import m from"mitt";import h from"dompurify";import A from"ramda/src/isNil";import p from"dayjs";function y(){return(y=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function S(t,e){if(null==t)return{};var n,r,o={},s=Object.keys(t);for(r=0;r<s.length;r++)e.indexOf(n=s[r])>=0||(o[n]=t[n]);return o}const g=r(()=>({apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}));function v(){const{apiKey:t,apiSecret:e,clientId:n,userEmail:r,userExternalId:o,userKey:s}=g.getState(),i={"X-MAGICBELL-CLIENT-ID":n,"X-MAGICBELL-API-KEY":t};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),s&&(i["X-MAGICBELL-USER-KEY"]=s),o&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=o),i}function w(t,e,n,r){const{serverURL:o}=g.getState(),s=v();return i({method:t,url:e,data:n,params:r,headers:s,baseURL:o}).then(t=>t.data,t=>{throw t})}function P(t,e={}){return w("get",t,void 0,e)}function k(t,e={},n={}){return w("post",t,e,n)}class U{constructor(t="/config"){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}async get(){const t=await P(this.remotePathOrUrl);return s.camelizeKeys(t)}}const E=o((t,e)=>({channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new U,fetch:async()=>{const{_repository:n}=e(),r=await n.get();t(y({},r,{lastFetchedAt:Date.now()}))}}));function R(t){return u({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}const C=["notifications"];class x extends class{constructor(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}async get(t){const e=`${this.remotePathOrUrl}/${t}`,n=await P(e);return s.camelizeKeys(n)}async findBy(t){const e=await P(this.remotePathOrUrl,t);return s.camelizeKeys(e)}async create(t){const e=await k(this.remotePathOrUrl,t);return s.camelizeKeys(e)}update(t,e){return function(t,e,n={}){return w("put",t,e,n)}(`${this.remotePathOrUrl}/${t}`,e)}delete(t){return function(t,e={}){return w("delete",t,void 0,e)}(`${this.remotePathOrUrl}/${t}`).then(()=>!0).catch(()=>!1)}}{constructor(t="/notifications"){super(t)}markAsRead(t){return k(`${this.remotePathOrUrl}/${t}/read`).then(()=>!0).catch(()=>!1)}markAsUnread(t){return k(`${this.remotePathOrUrl}/${t}/unread`).then(()=>!0).catch(()=>!1)}markAllAsSeen(){return k(`${this.remotePathOrUrl}/seen`).then(()=>!0).catch(()=>!1)}markAllAsRead(){return k(`${this.remotePathOrUrl}/read`).then(()=>!0).catch(()=>!1)}}const L=o((t,e)=>({stores:{},_repository:new x,setStore:(e,n={},r={})=>{t(a(t=>{t.stores[e]=R(y({},r,{context:n}))}))},fetchStore:async(n,r={},o={})=>{const{stores:s,_repository:i}=e(),c=s[n];if(!c)throw new Error(`Store not found. Define a store with the ${n} ID`);{const e=await i.findBy(y({},c.context,r));t(a(t=>{t.stores[n]=function(t,e,n={reset:!1}){const{notifications:r}=e,o=S(e,C),s=n.reset?r:d(t=>t.id,[...t.notifications,...r]);return y({context:t.context,notifications:s},o)}(c,y({},e,{lastFetchedAt:new Date}),o)}))}},fetchAllStores:async(t={},n={})=>{const{stores:r,fetchStore:o}=e();for(const e in r)o(e,t,n)},markNotificationAsSeen:n=>{const{stores:r}=e();t(a(t=>{for(const e in r){const{notifications:o,unseenCount:s}=r[e],i=c(l("id",n),o);i>-1&&(o[i].seenAt||(t.stores[e].unseenCount=Math.max(0,s-1),t.stores[e].notifications[i]=u(o[i],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:n=>{const{stores:r,_repository:o}=e(),s=o.markAsRead(n);return t(a(t=>{for(const e in r){const{notifications:o,unreadCount:s}=r[e],i=c(l("id",n),o);i>-1&&(t.stores[e].unreadCount=Math.max(0,s-1),t.stores[e].notifications[i]=u(o[i],{readAt:Date.now()/1e3}))}})),s},markNotificationAsUnread:n=>{const{stores:r,_repository:o}=e(),s=o.markAsUnread(n);return t(a(t=>{for(const e in r){const{notifications:o,unreadCount:s}=r[e],i=c(l("id",n),o);i>-1&&(t.stores[e].unreadCount=s+1,t.stores[e].notifications[i]=u(o[i],{readAt:null}))}})),s},deleteNotification:(n,r={})=>{const{stores:o,_repository:s}=e(),i=!1===r.persist?Promise.resolve(!0):s.delete(n);return t(a(t=>{for(const e in o){const{notifications:r,total:s,unseenCount:i,unreadCount:a}=o[e],u=c(l("id",n),r);if(u>-1){const n=r[u];n.seenAt||(t.stores[e].unseenCount=Math.max(0,i-1)),n.readAt||(t.stores[e].unreadCount=Math.max(0,a-1)),t.stores[e].total=Math.max(0,s-1),t.stores[e].notifications.splice(u,1)}}})),i},markAllAsSeen:(n={persist:!0,updateModels:!0})=>{const{stores:r,_repository:o}=e(),s=!1!==n.persist?o.markAllAsSeen():Promise.resolve(!0);return t(a(t=>{for(const e in r){const{notifications:o}=r[e];t.stores[e].unseenCount=0,!1!==n.updateModels&&o.forEach((n,r)=>{t.stores[e].notifications[r]=u(n,{seenAt:Date.now()/1e3})})}})),s},markAllAsRead:(n={persist:!0,updateModels:!0})=>{const{stores:r,_repository:o}=e(),s=!1!==n.persist?o.markAllAsRead():Promise.resolve(!0);return t(a(t=>{for(const e in r){const{notifications:o}=r[e];t.stores[e].unreadCount=0,!1!==n.updateModels&&o.forEach((n,r)=>{t.stores[e].notifications[r]=u(n,{readAt:Date.now()/1e3})})}})),s}})),O=m();function I(t){const{clientId:e}=g.getState(),n=t.name.replace(/\//gi,"."),r=t.data;return r.client_id&&r.client_id===e?Promise.resolve():"string"==typeof r.id?(new x).get(r.id).then(t=>{O.emit(n,t.notification)}):(O.emit(n,r),Promise.resolve())}function M(t,n){e(()=>(O.on(t,n),()=>{O.off(t,n)}),[])}function b(){const t=L(),n=()=>t.fetchAllStores({page:1},{reset:!0});return function(){const t=E();e(()=>t.ws?function(t){const{channel:e}=t,n=function(t){const{serverURL:e}=g.getState(),n=`${e}/${t.authUrl}`,r=v();return new f.Realtime({authUrl:n,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),r=()=>O.emit("wakeup");n.connection.on("disconnected",r),n.connection.on("suspended",r);const o=n.channels.get(e);return o.subscribe(I),()=>{o.unsubscribe(I),o.detach(),n.connection.off("disconnected"),n.connection.off("suspended"),n.close()}}(t.ws):()=>{},[t.ws])}(),M("wakeup",n),M("notifications.new",()=>t.fetchAllStores({page:1})),M("notifications.seen.all",()=>t.markAllAsSeen({persist:!1})),M("notifications.read.all",()=>t.markAllAsRead({persist:!1})),M("notifications.read",n),M("notifications.unread",n),M("notifications.delete",e=>t.deleteNotification(e.id,{persist:!1})),null}const N=["serverURL"],$=["children","stores"];function D(t){let{serverURL:e}=t;const n=S(t,N);return e&&(n.serverURL=e),g.setState(n),n}function _(r){let{children:o,stores:s=[{id:"default",defaultQueryParams:{}}]}=r,i=S(r,$);n(()=>D(i)),n(()=>function(t){const e={};return t.forEach(t=>{const{defaultQueryParams:n,defaults:r={}}=t;e[t.id]=R(y({context:n},r))}),L.setState({stores:e}),e}(s));const a=E();return e(()=>{a.fetch()},[]),t.createElement(t.Fragment,null,t.createElement(b,null),o)}function K(t="default"){const{stores:n,fetchStore:r,markAllAsSeen:o,markAllAsRead:s}=L(),i=E(),a=n[t];if(!a)throw new Error(`Store not found. Define a store with the ${t} ID`);const c=(e,n)=>r(t,e,n);return e(()=>{i.lastFetchedAt&&!a.lastFetchedAt&&c({page:1})},[i.lastFetchedAt]),y({},a,{isEmpty:0===a.notifications.length,hasNextPage:a.currentPage<a.totalPages,fetch:c,fetchNextPage:(e={},n)=>r(t,y({},e,{page:a.currentPage+1}),n),markAllAsSeen:o,markAllAsRead:s})}function B({storeId:t}={}){const e=K(t);return y({},e,{markAllAsSeen:()=>e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)})}function z(t){return t?F(1e3*t):null}function F(t){return p(t)}function X(t){return p(t).unix()}function G(t){const{markNotificationAsRead:e,markNotificationAsSeen:n,markNotificationAsUnread:r,deleteNotification:o}=L(),s=function(t){if(A(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),i=t.content?h.sanitize(t.content):t.content;return y({},t,{customAttributes:s,readAt:z(t.readAt),seenAt:z(t.seenAt),sentAt:z(t.sentAt),archivedAt:z(t.archivedAt),isSeen:!A(t.seenAt),isRead:!A(t.readAt),isArchived:!A(t.archivedAt),sanitizedContent:i,markAsSeen:()=>n(t.id),markAsRead:()=>e(t.id),markAsUnread:()=>r(t.id),delete:()=>o(t.id)})}function j(t,n){e(()=>()=>{n?n(t):t.markAsSeen()},[])}function T(t,e){const n=G(t);return j(n,e),n}export{_ as MagicBellProvider,b as RealtimeListener,g as clientSettings,z as secondsToDate,F as toDate,X as toUnix,B as useBell,E as useConfig,M as useMagicBellEvent,T as useNotification,G as useNotificationFactory,L as useNotificationStoresCollection,j as useNotificationUnmount,K as useNotifications};
//# sourceMappingURL=magicbell-react-headless.modern.js.map

@@ -1,2 +0,2 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("zustand/vanilla"),require("zustand"),require("humps"),require("axios"),require("immer"),require("ramda/src/findIndex"),require("ramda/src/mergeRight"),require("ramda/src/propEq"),require("ramda/src/uniqBy"),require("ably"),require("mitt"),require("dompurify"),require("ramda/src/isNil"),require("dayjs")):"function"==typeof define&&define.amd?define(["exports","react","zustand/vanilla","zustand","humps","axios","immer","ramda/src/findIndex","ramda/src/mergeRight","ramda/src/propEq","ramda/src/uniqBy","ably","mitt","dompurify","ramda/src/isNil","dayjs"],e):e((t||self).reactHeadless={},t.react,t.create,t.zustand,t.humps,t.axios,t.immer,t.findIndex,t.mergeRight,t.propEq,t.uniqBy,t.ably,t.mitt,t.dompurify,t.isNil,t.dayjs)}(this,function(t,e,r,n,o,i,a,u,s,c,f,d,l,m,v,h){function p(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function A(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,n.get?n:{enumerable:!0,get:function(){return t[r]}})}}),e.default=t,e}var y=p(e),P=p(r),g=p(n),S=p(o),E=p(i),k=p(a),U=p(u),O=p(s),b=p(c),w=p(f),R=A(d),x=p(l),C=p(m),I=p(v),L=p(h);function M(){return(M=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function N(t,e){return(N=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function j(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var q=P.default(function(){return{apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}});function _(){var t=q.getState(),e=t.apiSecret,r=t.userEmail,n=t.userExternalId,o=t.userKey,i={"X-MAGICBELL-CLIENT-ID":t.clientId,"X-MAGICBELL-API-KEY":t.apiKey};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),o&&(i["X-MAGICBELL-USER-KEY"]=o),n&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=n),i}function D(t,e,r,n){var o=q.getState().serverURL,i=_();return E.default({method:t,url:e,data:r,params:n,headers:i,baseURL:o}).then(function(t){return t.data},function(t){throw t})}function B(t,e){return void 0===e&&(e={}),D("get",t,void 0,e)}function z(t,e,r){return void 0===e&&(e={}),void 0===r&&(r={}),D("post",t,e,r)}var K=function(){function t(t){void 0===t&&(t="/config"),this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}return t.prototype.get=function(){try{return Promise.resolve(B(this.remotePathOrUrl)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},t}(),F=g.default(function(t,e){return{channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new K,fetch:function(){try{var r=e();return Promise.resolve(r._repository.get()).then(function(e){t(M({},e,{lastFetchedAt:Date.now()}))})}catch(t){return Promise.reject(t)}}}});function T(t){return O.default({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}var X=["notifications"],G=function(t){var e,r;function n(e){return void 0===e&&(e="/notifications"),t.call(this,e)||this}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,N(e,r);var o=n.prototype;return o.markAsRead=function(t){return z(this.remotePathOrUrl+"/"+t+"/read").then(function(){return!0}).catch(function(){return!1})},o.markAsUnread=function(t){return z(this.remotePathOrUrl+"/"+t+"/unread").then(function(){return!0}).catch(function(){return!1})},o.markAllAsSeen=function(){return z(this.remotePathOrUrl+"/seen").then(function(){return!0}).catch(function(){return!1})},o.markAllAsRead=function(){return z(this.remotePathOrUrl+"/read").then(function(){return!0}).catch(function(){return!1})},n}(function(){function t(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}var e=t.prototype;return e.get=function(t){try{return Promise.resolve(B(this.remotePathOrUrl+"/"+t)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.findBy=function(t){try{return Promise.resolve(B(this.remotePathOrUrl,t)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.create=function(t){try{return Promise.resolve(z(this.remotePathOrUrl,t)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.update=function(t,e){return void 0===r&&(r={}),D("put",this.remotePathOrUrl+"/"+t,e,r);var r},e.delete=function(t){return(e=this.remotePathOrUrl+"/"+t,void 0===r&&(r={}),D("delete",e,void 0,r)).then(function(){return!0}).catch(function(){return!1});var e,r},t}()),H=g.default(function(t,e){return{stores:{},_repository:new G,setStore:function(e,r,n){void 0===r&&(r={}),void 0===n&&(n={}),t(k.default(function(t){t.stores[e]=T(M({},n,{context:r}))}))},fetchStore:function(r,n,o){void 0===n&&(n={}),void 0===o&&(o={});try{var i=e(),a=i._repository,u=i.stores[r];return Promise.resolve(function(){if(u)return Promise.resolve(a.findBy(M({},u.context,n))).then(function(e){t(k.default(function(t){t.stores[r]=function(t,e,r){void 0===r&&(r={reset:!1});var n=e.notifications,o=j(e,X),i=r.reset?n:w.default(function(t){return t.id},[].concat(t.notifications,n));return M({context:t.context,notifications:i},o)}(u,M({},e,{lastFetchedAt:new Date}),o)}))});throw new Error("Store not found. Define a store with the "+r+" ID")}())}catch(t){return Promise.reject(t)}},fetchAllStores:function(t,r){void 0===t&&(t={}),void 0===r&&(r={});try{var n=e(),o=n.stores,i=n.fetchStore;for(var a in o)i(a,t,r);return Promise.resolve()}catch(t){return Promise.reject(t)}},markNotificationAsSeen:function(r){var n=e().stores;t(k.default(function(t){for(var e in n){var o=n[e],i=o.notifications,a=o.unseenCount,u=U.default(b.default("id",r),i);u>-1&&(i[u].seenAt||(t.stores[e].unseenCount=Math.max(0,a-1),t.stores[e].notifications[u]=O.default(i[u],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:function(r){var n=e(),o=n.stores,i=n._repository.markAsRead(r);return t(k.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=U.default(b.default("id",r),i);u>-1&&(t.stores[e].unreadCount=Math.max(0,a-1),t.stores[e].notifications[u]=O.default(i[u],{readAt:Date.now()/1e3}))}})),i},markNotificationAsUnread:function(r){var n=e(),o=n.stores,i=n._repository.markAsUnread(r);return t(k.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=U.default(b.default("id",r),i);u>-1&&(t.stores[e].unreadCount=a+1,t.stores[e].notifications[u]=O.default(i[u],{readAt:null}))}})),i},deleteNotification:function(r,n){void 0===n&&(n={});var o=e(),i=o.stores,a=o._repository,u=!1===n.persist?Promise.resolve(!0):a.delete(r);return t(k.default(function(t){for(var e in i){var n=i[e],o=n.notifications,a=n.total,u=n.unseenCount,s=n.unreadCount,c=U.default(b.default("id",r),o);if(c>-1){var f=o[c];f.seenAt||(t.stores[e].unseenCount=Math.max(0,u-1)),f.readAt||(t.stores[e].unreadCount=Math.max(0,s-1)),t.stores[e].total=Math.max(0,a-1),t.stores[e].notifications.splice(c,1)}}})),u},markAllAsSeen:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsSeen():Promise.resolve(!0);return t(k.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unseenCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=O.default(r,{seenAt:Date.now()/1e3})})};for(var n in o)e(n)})),i},markAllAsRead:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsRead():Promise.resolve(!0);return t(k.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unreadCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=O.default(r,{readAt:Date.now()/1e3})})};for(var n in o)e(n)})),i}}}),J=x.default();function Q(t){var e=q.getState().clientId,r=t.name.replace(/\//gi,"."),n=t.data;return n.client_id&&n.client_id===e?Promise.resolve():"string"==typeof n.id?(new G).get(n.id).then(function(t){J.emit(r,t.notification)}):(J.emit(r,n),Promise.resolve())}function Y(t,r){e.useEffect(function(){return J.on(t,r),function(){J.off(t,r)}},[])}function V(){var t,r=H(),n=function(){return r.fetchAllStores({page:1},{reset:!0})};return t=F(),e.useEffect(function(){return t.ws?function(t){var e=t.channel,r=function(t){var e=q.getState().serverURL+"/"+t.authUrl,r=_();return new R.Realtime({authUrl:e,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),n=function(){return J.emit("wakeup")};r.connection.on("disconnected",n),r.connection.on("suspended",n);var o=r.channels.get(e);return o.subscribe(Q),function(){o.unsubscribe(Q),o.detach(),r.connection.off("disconnected"),r.connection.off("suspended"),r.close()}}(t.ws):function(){}},[t.ws]),Y("wakeup",n),Y("notifications.new",function(){return r.fetchAllStores({page:1})}),Y("notifications.seen.all",function(){return r.markAllAsSeen({persist:!1})}),Y("notifications.read.all",function(){return r.markAllAsRead({persist:!1})}),Y("notifications.read",n),Y("notifications.unread",n),Y("notifications.delete",function(t){return r.deleteNotification(t.id,{persist:!1})}),null}var W=["serverURL"],Z=["children","stores"];function $(t){var e=t.serverURL,r=j(t,W);return e&&(r.serverURL=e),q.setState(r),r}function tt(t){void 0===t&&(t="default");var r=H(),n=r.stores,o=r.fetchStore,i=r.markAllAsSeen,a=r.markAllAsRead,u=F(),s=n[t];if(!s)throw new Error("Store not found. Define a store with the "+t+" ID");var c=function(e,r){return o(t,e,r)};return e.useEffect(function(){u.lastFetchedAt&&!s.lastFetchedAt&&c({page:1})},[u.lastFetchedAt]),M({},s,{isEmpty:0===s.notifications.length,hasNextPage:s.currentPage<s.totalPages,fetch:c,fetchNextPage:function(e,r){return void 0===e&&(e={}),o(t,M({},e,{page:s.currentPage+1}),r)},markAllAsSeen:i,markAllAsRead:a})}function et(t){return t?rt(1e3*t):null}function rt(t){return L.default(t)}function nt(t){var e=H(),r=e.markNotificationAsRead,n=e.markNotificationAsSeen,o=e.markNotificationAsUnread,i=e.deleteNotification,a=function(t){if(I.default(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),u=t.content?C.default.sanitize(t.content):t.content;return M({},t,{customAttributes:a,readAt:et(t.readAt),seenAt:et(t.seenAt),sentAt:et(t.sentAt),archivedAt:et(t.archivedAt),isSeen:!I.default(t.seenAt),isRead:!I.default(t.readAt),isArchived:!I.default(t.archivedAt),sanitizedContent:u,markAsSeen:function(){return n(t.id)},markAsRead:function(){return r(t.id)},markAsUnread:function(){return o(t.id)},delete:function(){return i(t.id)}})}function ot(t,r){e.useEffect(function(){return function(){r?r(t):t.markAsSeen()}},[])}t.MagicBellProvider=function(t){var r=t.children,n=t.stores,o=void 0===n?[{id:"default",defaultQueryParams:{}}]:n,i=j(t,Z);e.useState(function(){return $(i)}),e.useState(function(){return function(t){var e={};return t.forEach(function(t){var r=t.defaults;e[t.id]=T(M({context:t.defaultQueryParams},void 0===r?{}:r))}),H.setState({stores:e}),e}(o)});var a=F();return e.useEffect(function(){a.fetch()},[]),y.default.createElement(y.default.Fragment,null,y.default.createElement(V,null),r)},t.RealtimeListener=V,t.secondsToDate=et,t.toDate=rt,t.toUnix=function(t){return L.default(t).unix()},t.useBell=function(t){var e=tt((void 0===t?{}:t).storeId);return M({},e,{markAllAsSeen:function(){return e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)}})},t.useMagicBellEvent=Y,t.useNotification=function(t,e){var r=nt(t);return ot(r,e),r},t.useNotificationFactory=nt,t.useNotificationUnmount=ot,t.useNotifications=tt});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("zustand/vanilla"),require("zustand"),require("humps"),require("axios"),require("immer"),require("ramda/src/findIndex"),require("ramda/src/mergeRight"),require("ramda/src/propEq"),require("ramda/src/uniqBy"),require("ably"),require("mitt"),require("dompurify"),require("ramda/src/isNil"),require("dayjs")):"function"==typeof define&&define.amd?define(["exports","react","zustand/vanilla","zustand","humps","axios","immer","ramda/src/findIndex","ramda/src/mergeRight","ramda/src/propEq","ramda/src/uniqBy","ably","mitt","dompurify","ramda/src/isNil","dayjs"],e):e((t||self).reactHeadless={},t.react,t.create,t.zustand,t.humps,t.axios,t.immer,t.findIndex,t.mergeRight,t.propEq,t.uniqBy,t.ably,t.mitt,t.dompurify,t.isNil,t.dayjs)}(this,function(t,e,r,n,o,i,a,u,s,c,f,d,l,m,v,h){function p(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function A(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,n.get?n:{enumerable:!0,get:function(){return t[r]}})}}),e.default=t,e}var y=p(e),P=p(r),g=p(n),S=p(o),E=p(i),k=p(a),U=p(u),O=p(s),b=p(c),w=p(f),R=A(d),x=p(l),C=p(m),I=p(v),L=p(h);function N(){return(N=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function M(t,e){return(M=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function j(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var q=P.default(function(){return{apiKey:"",apiSecret:void 0,userEmail:void 0,userExternalId:void 0,userKey:void 0,clientId:Math.random().toString(36).substring(2)+Date.now(),serverURL:"https://api.magicbell.com"}});function _(){var t=q.getState(),e=t.apiSecret,r=t.userEmail,n=t.userExternalId,o=t.userKey,i={"X-MAGICBELL-CLIENT-ID":t.clientId,"X-MAGICBELL-API-KEY":t.apiKey};return e&&(i["X-MAGICBELL-API-SECRET"]=e),r&&(i["X-MAGICBELL-USER-EMAIL"]=r),o&&(i["X-MAGICBELL-USER-KEY"]=o),n&&(i["X-MAGICBELL-USER-EXTERNAL-ID"]=n),i}function D(t,e,r,n){var o=q.getState().serverURL,i=_();return E.default({method:t,url:e,data:r,params:n,headers:i,baseURL:o}).then(function(t){return t.data},function(t){throw t})}function B(t,e){return void 0===e&&(e={}),D("get",t,void 0,e)}function z(t,e,r){return void 0===e&&(e={}),void 0===r&&(r={}),D("post",t,e,r)}var K=function(){function t(t){void 0===t&&(t="/config"),this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}return t.prototype.get=function(){try{return Promise.resolve(B(this.remotePathOrUrl)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},t}(),F=g.default(function(t,e){return{channels:void 0,inbox:void 0,ws:void 0,lastFetchedAt:void 0,_repository:new K,fetch:function(){try{var r=e();return Promise.resolve(r._repository.get()).then(function(e){t(N({},e,{lastFetchedAt:Date.now()}))})}catch(t){return Promise.reject(t)}}}});function T(t){return O.default({context:{},total:0,totalPages:0,perPage:0,currentPage:1,unreadCount:0,unseenCount:0,notifications:[]},t)}var X=["notifications"],G=function(t){var e,r;function n(e){return void 0===e&&(e="/notifications"),t.call(this,e)||this}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,M(e,r);var o=n.prototype;return o.markAsRead=function(t){return z(this.remotePathOrUrl+"/"+t+"/read").then(function(){return!0}).catch(function(){return!1})},o.markAsUnread=function(t){return z(this.remotePathOrUrl+"/"+t+"/unread").then(function(){return!0}).catch(function(){return!1})},o.markAllAsSeen=function(){return z(this.remotePathOrUrl+"/seen").then(function(){return!0}).catch(function(){return!1})},o.markAllAsRead=function(){return z(this.remotePathOrUrl+"/read").then(function(){return!0}).catch(function(){return!1})},n}(function(){function t(t){this.remotePathOrUrl=void 0,this.remotePathOrUrl=t}var e=t.prototype;return e.get=function(t){try{return Promise.resolve(B(this.remotePathOrUrl+"/"+t)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.findBy=function(t){try{return Promise.resolve(B(this.remotePathOrUrl,t)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.create=function(t){try{return Promise.resolve(z(this.remotePathOrUrl,t)).then(function(t){return S.default.camelizeKeys(t)})}catch(t){return Promise.reject(t)}},e.update=function(t,e){return void 0===r&&(r={}),D("put",this.remotePathOrUrl+"/"+t,e,r);var r},e.delete=function(t){return(e=this.remotePathOrUrl+"/"+t,void 0===r&&(r={}),D("delete",e,void 0,r)).then(function(){return!0}).catch(function(){return!1});var e,r},t}()),H=g.default(function(t,e){return{stores:{},_repository:new G,setStore:function(e,r,n){void 0===r&&(r={}),void 0===n&&(n={}),t(k.default(function(t){t.stores[e]=T(N({},n,{context:r}))}))},fetchStore:function(r,n,o){void 0===n&&(n={}),void 0===o&&(o={});try{var i=e(),a=i._repository,u=i.stores[r];return Promise.resolve(function(){if(u)return Promise.resolve(a.findBy(N({},u.context,n))).then(function(e){t(k.default(function(t){t.stores[r]=function(t,e,r){void 0===r&&(r={reset:!1});var n=e.notifications,o=j(e,X),i=r.reset?n:w.default(function(t){return t.id},[].concat(t.notifications,n));return N({context:t.context,notifications:i},o)}(u,N({},e,{lastFetchedAt:new Date}),o)}))});throw new Error("Store not found. Define a store with the "+r+" ID")}())}catch(t){return Promise.reject(t)}},fetchAllStores:function(t,r){void 0===t&&(t={}),void 0===r&&(r={});try{var n=e(),o=n.stores,i=n.fetchStore;for(var a in o)i(a,t,r);return Promise.resolve()}catch(t){return Promise.reject(t)}},markNotificationAsSeen:function(r){var n=e().stores;t(k.default(function(t){for(var e in n){var o=n[e],i=o.notifications,a=o.unseenCount,u=U.default(b.default("id",r),i);u>-1&&(i[u].seenAt||(t.stores[e].unseenCount=Math.max(0,a-1),t.stores[e].notifications[u]=O.default(i[u],{seenAt:Date.now()/1e3})))}}))},markNotificationAsRead:function(r){var n=e(),o=n.stores,i=n._repository.markAsRead(r);return t(k.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=U.default(b.default("id",r),i);u>-1&&(t.stores[e].unreadCount=Math.max(0,a-1),t.stores[e].notifications[u]=O.default(i[u],{readAt:Date.now()/1e3}))}})),i},markNotificationAsUnread:function(r){var n=e(),o=n.stores,i=n._repository.markAsUnread(r);return t(k.default(function(t){for(var e in o){var n=o[e],i=n.notifications,a=n.unreadCount,u=U.default(b.default("id",r),i);u>-1&&(t.stores[e].unreadCount=a+1,t.stores[e].notifications[u]=O.default(i[u],{readAt:null}))}})),i},deleteNotification:function(r,n){void 0===n&&(n={});var o=e(),i=o.stores,a=o._repository,u=!1===n.persist?Promise.resolve(!0):a.delete(r);return t(k.default(function(t){for(var e in i){var n=i[e],o=n.notifications,a=n.total,u=n.unseenCount,s=n.unreadCount,c=U.default(b.default("id",r),o);if(c>-1){var f=o[c];f.seenAt||(t.stores[e].unseenCount=Math.max(0,u-1)),f.readAt||(t.stores[e].unreadCount=Math.max(0,s-1)),t.stores[e].total=Math.max(0,a-1),t.stores[e].notifications.splice(c,1)}}})),u},markAllAsSeen:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsSeen():Promise.resolve(!0);return t(k.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unseenCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=O.default(r,{seenAt:Date.now()/1e3})})};for(var n in o)e(n)})),i},markAllAsRead:function(r){void 0===r&&(r={persist:!0,updateModels:!0});var n=e(),o=n.stores,i=!1!==r.persist?n._repository.markAllAsRead():Promise.resolve(!0);return t(k.default(function(t){var e=function(e){var n=o[e].notifications;t.stores[e].unreadCount=0,!1!==r.updateModels&&n.forEach(function(r,n){t.stores[e].notifications[n]=O.default(r,{readAt:Date.now()/1e3})})};for(var n in o)e(n)})),i}}}),J=x.default();function Q(t){var e=q.getState().clientId,r=t.name.replace(/\//gi,"."),n=t.data;return n.client_id&&n.client_id===e?Promise.resolve():"string"==typeof n.id?(new G).get(n.id).then(function(t){J.emit(r,t.notification)}):(J.emit(r,n),Promise.resolve())}function Y(t,r){e.useEffect(function(){return J.on(t,r),function(){J.off(t,r)}},[])}function V(){var t,r=H(),n=function(){return r.fetchAllStores({page:1},{reset:!0})};return t=F(),e.useEffect(function(){return t.ws?function(t){var e=t.channel,r=function(t){var e=q.getState().serverURL+"/"+t.authUrl,r=_();return new R.Realtime({authUrl:e,authHeaders:r,authMethod:"POST",log:{level:0},transports:["web_socket"]})}(t),n=function(){return J.emit("wakeup")};r.connection.on("disconnected",n),r.connection.on("suspended",n);var o=r.channels.get(e);return o.subscribe(Q),function(){o.unsubscribe(Q),o.detach(),r.connection.off("disconnected"),r.connection.off("suspended"),r.close()}}(t.ws):function(){}},[t.ws]),Y("wakeup",n),Y("notifications.new",function(){return r.fetchAllStores({page:1})}),Y("notifications.seen.all",function(){return r.markAllAsSeen({persist:!1})}),Y("notifications.read.all",function(){return r.markAllAsRead({persist:!1})}),Y("notifications.read",n),Y("notifications.unread",n),Y("notifications.delete",function(t){return r.deleteNotification(t.id,{persist:!1})}),null}var W=["serverURL"],Z=["children","stores"];function $(t){var e=t.serverURL,r=j(t,W);return e&&(r.serverURL=e),q.setState(r),r}function tt(t){void 0===t&&(t="default");var r=H(),n=r.stores,o=r.fetchStore,i=r.markAllAsSeen,a=r.markAllAsRead,u=F(),s=n[t];if(!s)throw new Error("Store not found. Define a store with the "+t+" ID");var c=function(e,r){return o(t,e,r)};return e.useEffect(function(){u.lastFetchedAt&&!s.lastFetchedAt&&c({page:1})},[u.lastFetchedAt]),N({},s,{isEmpty:0===s.notifications.length,hasNextPage:s.currentPage<s.totalPages,fetch:c,fetchNextPage:function(e,r){return void 0===e&&(e={}),o(t,N({},e,{page:s.currentPage+1}),r)},markAllAsSeen:i,markAllAsRead:a})}function et(t){return t?rt(1e3*t):null}function rt(t){return L.default(t)}function nt(t){var e=H(),r=e.markNotificationAsRead,n=e.markNotificationAsSeen,o=e.markNotificationAsUnread,i=e.deleteNotification,a=function(t){if(I.default(t))return null;if("string"==typeof t)try{return JSON.parse(t)}catch(t){console.warn('"customAttributes" is not valid JSON')}return t}(t.customAttributes),u=t.content?C.default.sanitize(t.content):t.content;return N({},t,{customAttributes:a,readAt:et(t.readAt),seenAt:et(t.seenAt),sentAt:et(t.sentAt),archivedAt:et(t.archivedAt),isSeen:!I.default(t.seenAt),isRead:!I.default(t.readAt),isArchived:!I.default(t.archivedAt),sanitizedContent:u,markAsSeen:function(){return n(t.id)},markAsRead:function(){return r(t.id)},markAsUnread:function(){return o(t.id)},delete:function(){return i(t.id)}})}function ot(t,r){e.useEffect(function(){return function(){r?r(t):t.markAsSeen()}},[])}t.MagicBellProvider=function(t){var r=t.children,n=t.stores,o=void 0===n?[{id:"default",defaultQueryParams:{}}]:n,i=j(t,Z);e.useState(function(){return $(i)}),e.useState(function(){return function(t){var e={};return t.forEach(function(t){var r=t.defaults;e[t.id]=T(N({context:t.defaultQueryParams},void 0===r?{}:r))}),H.setState({stores:e}),e}(o)});var a=F();return e.useEffect(function(){a.fetch()},[]),y.default.createElement(y.default.Fragment,null,y.default.createElement(V,null),r)},t.RealtimeListener=V,t.clientSettings=q,t.secondsToDate=et,t.toDate=rt,t.toUnix=function(t){return L.default(t).unix()},t.useBell=function(t){var e=tt((void 0===t?{}:t).storeId);return N({},e,{markAllAsSeen:function(){return e.unseenCount>0?e.markAllAsSeen({updateModels:!1}):Promise.resolve(!0)}})},t.useConfig=F,t.useMagicBellEvent=Y,t.useNotification=function(t,e){var r=nt(t);return ot(r,e),r},t.useNotificationFactory=nt,t.useNotificationStoresCollection=H,t.useNotificationUnmount=ot,t.useNotifications=tt});
//# sourceMappingURL=magicbell-react-headless.umd.js.map
{
"name": "@magicbell/react-headless",
"version": "2.0.1",
"version": "2.0.2",
"description": "Hooks to build a notification inbox",

@@ -61,3 +61,3 @@ "author": "MagicBell <bot@magicbell.io> (https://magicbell.com/)",

"@storybook/react": "^6.3.8",
"@testing-library/react": "^12.0.0",
"@testing-library/react": "^12.1.0",
"@testing-library/react-hooks": "^7.0.2",

@@ -80,3 +80,3 @@ "@types/dompurify": "^2.2.3",

"husky": "^7.0.2",
"jest": "^27.1.1",
"jest": "^27.2.0",
"jest-watch-typeahead": "^0.6.4",

@@ -93,3 +93,3 @@ "lint-staged": "^10.5.4",

"tslib": "^2.3.1",
"typescript": "^4.4.2",
"typescript": "^4.4.3",
"utility-types": "^3.10.0"

@@ -100,3 +100,3 @@ },

"axios": "^0.21.4",
"dayjs": "^1.10.6",
"dayjs": "^1.10.7",
"dompurify": "^2.3.1",

@@ -103,0 +103,0 @@ "humps": "^2.0.1",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc