piral-notifications
Version:
Plugin for triggering notifications in Piral.
2 lines (1 loc) • 2.88 kB
JavaScript
var piralNotifications=(()=>{var E=Object.create;var c=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,B=Object.prototype.hasOwnProperty;var r=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(o,i)=>(typeof require<"u"?require:o)[i]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});var y=(t,o)=>{for(var i in o)c(t,i,{get:o[i],enumerable:!0})},O=(t,o,i,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of w(o))!B.call(t,n)&&n!==i&&c(t,n,{get:()=>o[n],enumerable:!(a=I(o,n))||a.enumerable});return t};var g=(t,o,i)=>(i=t!=null?E(D(t)):{},O(o||!t||!t.__esModule?c(i,"default",{value:t,enumerable:!0}):i,t)),F=t=>O(c({},"__esModule",{value:!0}),t);var q={};y(q,{Notifications:()=>m,PiralNotificationsHost:()=>C,PiralNotificationsToast:()=>d,createNotificationsApi:()=>j});var N={};y(N,{closeNotification:()=>v,openNotification:()=>k});var p=r("piral-core");function k(t,o){t.dispatch(i=>({...i,notifications:(0,p.prependItem)(i.notifications,o)}))}function v(t,o){t.dispatch(i=>({...i,notifications:(0,p.excludeOn)(i.notifications,a=>a.id===o.id)}))}var x=r("react"),f=r("piral-core");var T=g(r("react")),b=r("piral-core"),h=t=>T.createElement("div",{className:"piral-notifications-host",key:"default_notifications"},t.children),R=({children:t})=>(0,b.defaultRender)(t);var l=g(r("react")),A=r("piral-core");var u=r("piral-core"),C=(0,u.getPiralComponent)("NotificationsHost"),d=(0,u.getPiralComponent)("NotificationsToast");var m=()=>{let t=(0,A.useGlobalState)(o=>o.notifications);return l.createElement(C,null,t.map(({component:o,close:i,options:a,id:n})=>l.createElement(d,{onClose:i,options:a,key:n},l.createElement(o,{onClose:i,options:a}))))};m.displayName="Notifications";function $(t){return(0,x.isValidElement)(t)}function M(t){if(typeof t=="string"){let o=t;return()=>(0,f.defaultRender)(o)}else if((0,x.isValidElement)(t)){let o=t;return()=>o}return t}function G(t,o,i,a,n={}){let s={...a,...n},e={id:o,component:M(i),options:s,close(){setTimeout(()=>t.closeNotification(e),0)}};return typeof s.autoClose=="number"&&s.autoClose>0&&setTimeout(e.close,s.autoClose),e}function V(t,o,i){let a=[],n=0;for(let{content:s,options:e}of o)a.push(G(t,`global-${n++}`,s,i,e));return a}function _(t){return o=>({...o,components:{NotificationsHost:h,NotificationsToast:R,...o.components},notifications:t})}function j(t={}){let{defaultOptions:o={},selectId:i=()=>`${~~(Math.random()*1e4)}`,messages:a=[]}=t;return n=>(n.defineActions(N),n.dispatch((0,f.withAll)(_(V(n,a,o)),(0,f.withRootExtension)("piral-notifications",m))),s=>({showNotification(e,S){let H=typeof e=="string"||$(e)?e:(0,f.withApi)(n,e,s,"extension"),P=G(n,i(),H,o,S);return n.openNotification(P),P.close}}))}return F(q);})();