UNPKG

blast-client

Version:

blast client

2 lines 9.28 kB
var G=Object.defineProperty,Y=Object.defineProperties;var j=Object.getOwnPropertyDescriptors;var R=Object.getOwnPropertySymbols;var Q=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable;var W=(r,e,t)=>e in r?G(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,L=(r,e)=>{for(var t in e||(e={}))Q.call(e,t)&&W(r,t,e[t]);if(R)for(var t of R(e))Z.call(e,t)&&W(r,t,e[t]);return r},U=(r,e)=>Y(r,j(e));var l=(r,e,t)=>new Promise((o,i)=>{var n=c=>{try{a(t.next(c))}catch(u){i(u)}},s=c=>{try{a(t.throw(c))}catch(u){i(u)}},a=c=>c.done?o(c.value):Promise.resolve(c.value).then(n,s);a((t=t.apply(r,e)).next())});import{createContext as ee,useContext as te,useState as re}from"react";import{jsx as ie}from"react/jsx-runtime";var O=ee(void 0);function B({children:r}){let[e,t]=re([]),o=n=>{t([...e,n])},i=n=>{t(s=>s.filter(a=>a.alertId!==n))};return ie(O.Provider,{value:{alerts:e,addAlert:o,removeAlert:i},children:r})}var v=()=>{let r=te(O);if(!r)throw new Error("error");return r};import{v4 as oe}from"uuid";var F=r=>new CustomEvent("new-subscribe",{detail:r});import M from"js-cookie";function b(r){return M.get(r)}function h(r,e,t){M.set(r,e,U(L({},t),{secure:!0}))}var w=class{constructor(e,t,o){this.swPath=e;this.publicKey=t;this.scope=o;this.hasClickHandler=!1}getOrCreateUserId(){let e=b("user_id");return e||(e=oe(),h("user_id",e,{path:"/"})),e}requestNotificationPermission(){return l(this,null,function*(){return yield Notification.requestPermission()})}registerServiceWorker(){return l(this,null,function*(){var t;let e=yield navigator.serviceWorker.register(this.swPath,{scope:(t=this.scope)!=null?t:"/"});if(e.update(),e.installing||e.waiting){let o=e.installing||e.waiting;yield new Promise(i=>{o==null||o.addEventListener("statechange",()=>{o.state==="activated"&&i()})})}else e.active||(yield navigator.serviceWorker.ready);return e})}getOrCreateSubscription(e,t){return l(this,null,function*(){let o=yield e.pushManager.getSubscription(),i=yield e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:this.urlBase64ToUint8Array(this.publicKey)});if(!o||o.endpoint!==i.endpoint){let s=this.getOrCreateUserId();window.dispatchEvent(F({sub:i.toJSON(),userAgent:navigator.userAgent,userId:s})),yield t(i.toJSON(),navigator.userAgent,s)}return i})}urlBase64ToUint8Array(e){let t="=".repeat((4-e.length%4)%4),o=(e+t).replace(/-/g,"+").replace(/_/g,"/"),i=window.atob(o),n=new Uint8Array(i.length);for(let s=0;s<i.length;++s)n[s]=i.charCodeAt(s);return n}init(e,t,o,i){return l(this,null,function*(){let n=s=>{if(t){t(s);let a=Math.min(i!=null?i:1,4);if(o&&typeof o=="number"&&o>0&&a>1){let c=1,u=setInterval(()=>{c++,t(s),c>=a&&clearInterval(u)},o)}}};try{if(!("serviceWorker"in navigator)||!("PushManager"in window))return;let s=Notification.permission;if(s==="denied"){n("denied");return}let a=s;if(s==="default"&&(a=yield this.requestNotificationPermission()),a!=="granted"){n("denied");return}let c=yield this.registerServiceWorker();yield this.getOrCreateSubscription(c,e),n("subscribed")}catch(s){console.error("Service Worker init error:",s),n("error")}})}clickNotification(e){return l(this,null,function*(){if(!("serviceWorker"in navigator)||!("PushManager"in window)||this.hasClickHandler)return;let t=o=>l(this,null,function*(){var i;if(((i=o.data)==null?void 0:i.type)==="NOTIFICATION_CLICKED"){let{notificationId:n,subscribeId:s}=o.data.notification_data;this.hasClickHandler=!0;let{data:a,ok:c}=yield e(n,s);if(this.hasClickHandler=!1,!c)return console.error(`[ Error to add click rate ]: ${(a==null?void 0:a.message)||a}`)}});navigator.serviceWorker.addEventListener("message",t)})}};var y=class{constructor(e){this.sw=e}clickNotification(e){return l(this,null,function*(){return yield this.sw.clickNotification(e)})}};var N=class{constructor(e,t,o){this.sw=new w(e,t,o),this.plugins=new y(this.sw)}run(e,t,o,i){return l(this,null,function*(){try{return yield this.sw.init(e,t,o,i),"Auto inject complete"}catch(n){return n}})}};import{v4 as ne}from"uuid";var x=()=>{let r=b("user_id");return r||(r=ne(),h("user_id",r,{path:"/",secure:!0})),r};import{useEffect as se}from"react";import{v4 as ae}from"uuid";function H(){let{addAlert:r}=v();se(()=>{var t;let e=o=>{var i;if(((i=o.data)==null?void 0:i.type)==="PUSH_RECEIVED"){let{notification:n}=o.data;r({alertId:ae(),content:n.body,title:n.title,timeout:5e3})}};return(t=navigator.serviceWorker)==null||t.addEventListener("message",e),()=>{var o;(o=navigator.serviceWorker)==null||o.removeEventListener("message",e)}},[])}import{useEffect as A,useRef as I,useState as V}from"react";import{motion as z,AnimatePresence as de}from"framer-motion";import{createPortal as ue}from"react-dom";import{useCallback as ce,useLayoutEffect as le}from"react";var g=typeof document=="undefined"?null:document.getElementById("blast-root"),k=()=>{let r=ce(()=>{if(typeof window=="undefined"||typeof document=="undefined"||typeof document.body=="undefined"||document.getElementById("blast-root"))return;let t=document.createElement("div");t.id="blast-root",document.body.appendChild(t)},[]);le(()=>{r()},[r])};import{jsx as m,jsxs as pe}from"react/jsx-runtime";var J=({dialogs:r,addGets:e})=>{let[t,o]=V(0),[i,n]=V(!0),s=I(()=>{}),a=I(()=>{}),c=I(!1),u=x(),p=r[t],T=()=>{n(!1)},D=()=>{t<r.length-1?o(d=>d+1):T()};s.current=T,a.current=D,A(()=>{let d=()=>s.current();return window.addEventListener("dialog-close",d),()=>window.removeEventListener("dialog-close",d)},[]),A(()=>{console.log(`[Dialog Helper] Showing dialog: ${p.dialogId||`dialog-${t}`}`)},[p]),A(()=>{if(!p||!u||c.current)return;l(null,null,function*(){yield e(p.dialogId,u),c.current=!0})},[p,u]);let q=d=>{if(d==="post_message"){let C=()=>s.current(),X=()=>a.current();window.dispatchEvent(new CustomEvent("dialog-message",{detail:{dialogId:p.dialogId||`dialog-${t}`,close:C,next:X}}))}else D()},_=()=>m(de,{children:i&&m(z.div,{dir:"ltr",className:"fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4",initial:{opacity:0},animate:{opacity:1},exit:{opacity:0},children:pe(z.div,{className:"relative w-full max-w-lg rounded-3xl p-6 backdrop-blur-xl bg-white/10 dark:bg-white/5 ring-1 ring-white/20 text-white shadow-2xl overflow-hidden",initial:{scale:.95,y:40,opacity:0},animate:{scale:1,y:0,opacity:1},exit:{scale:.95,y:40,opacity:0},transition:{type:"spring",stiffness:260,damping:20},children:[m("div",{className:"absolute inset-0 rounded-3xl pointer-events-none z-[-1] before:absolute before:inset-0 before:rounded-3xl before:border before:border-white/20 before:backdrop-blur-md before:shadow-[0_0_30px_rgba(255,255,255,0.1)]"}),m("h2",{className:"text-2xl font-extrabold tracking-wide",children:p.title}),m("p",{className:"text-sm text-white/80 pt-2",children:p.content}),m("div",{className:"flex justify-end gap-3 pt-6 flex-wrap",children:p.buttons.map((d,C)=>m("button",{onClick:()=>q(d.event),className:"relative z-10 px-5 py-2.5 rounded-xl bg-gradient-to-br from-blue-500 to-indigo-600 hover:from-blue-600 hover:to-indigo-700 transition-all duration-300 shadow-lg shadow-indigo-800/30 font-medium text-white",children:d.content},C))})]})})});return g?ue(_(),g):_()};import{Fragment as me,jsx as K}from"react/jsx-runtime";function P({dialogs:r,addGets:e}){return k(),r!=null&&r.length?K(J,{dialogs:r,addGets:e}):K(me,{})}import{useEffect as fe}from"react";import{motion as ge,AnimatePresence as ve}from"framer-motion";import{createPortal as be}from"react-dom";import{jsx as f,jsxs as E}from"react/jsx-runtime";var $=({renderAlert:r})=>{let{removeAlert:e,alerts:t}=v();fe(()=>{t.forEach(i=>{if(i.timeout===Number.POSITIVE_INFINITY)return;let n=setTimeout(()=>{e(i.alertId)},i.timeout);return()=>clearTimeout(n)})},[t,e]);let o=()=>f(ve,{children:t.map((i,n)=>r?f("div",{className:"fixed top-5 right-5 z-[9999999]",style:{maxWidth:"320px"},children:r(i)},i.alertId):E(ge.div,{initial:{opacity:0,x:50,scale:.9},animate:{opacity:1,x:0,scale:1},exit:{opacity:0,x:50,scale:.9},transition:{type:"spring",stiffness:260,damping:20,duration:.5},className:"fixed top-5 right-5 z-[9999999] flex w-[320px] max-w-full items-center rounded-lg border border-purple-500 bg-gradient-to-r from-purple-900/30 to-black/60 p-4 text-white shadow-lg backdrop-blur-lg max-sm:right-auto max-sm:left-1/2 max-sm:w-[95%] max-sm:-translate-x-1/2",children:[E("div",{className:"flex-grow pr-4",children:[f("h5",{className:"mb-1 text-lg font-semibold tracking-wide",children:i.title}),f("p",{className:"text-sm opacity-80",children:i.content})]}),f("div",{className:"flex w-12 items-center justify-center text-purple-300",children:E("svg",{xmlns:"http://www.w3.org/2000/svg",width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",className:"lucide lucide-bell h-7 w-7",children:[f("path",{d:"M10.268 21a2 2 0 0 0 3.464 0"}),f("path",{d:"M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326"})]})})]},i.alertId))});return g?be(o(),g):o()};import{jsx as he}from"react/jsx-runtime";function S(){return k(),he($,{})}export{S as Alert,N as BlastClient,B as BlastProvider,P as Dialog,H as usePushNotificationListener,x as useUserId}; //# sourceMappingURL=main.mjs.map