blast-client
Version:
blast client
2 lines • 10.2 kB
JavaScript
var G=Object.defineProperty,Y=Object.defineProperties;var j=Object.getOwnPropertyDescriptors;var W=Object.getOwnPropertySymbols;var Q=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable;var M=(r,e,t)=>e in r?G(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,O=(r,e)=>{for(var t in e||(e={}))Q.call(e,t)&&M(r,t,e[t]);if(W)for(var t of W(e))Z.call(e,t)&&M(r,t,e[t]);return r},R=(r,e)=>Y(r,j(e));var l=(r,e,t)=>new Promise((o,i)=>{var n=c=>{try{a(t.next(c))}catch(p){i(p)}},s=c=>{try{a(t.throw(c))}catch(p){i(p)}},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 U=ee(void 0);function L({children:r}){let[e,t]=re([]),o=n=>{t([...e,n])},i=n=>{t(s=>s.filter(a=>a.alertId!==n))};return ie(U.Provider,{value:{alerts:e,addAlert:o,removeAlert:i},children:r})}var y=()=>{let r=te(U);if(!r)throw new Error("error");return r};import z from"js-cookie";function w(r){return z.get(r)}function x(r,e,t){z.set(r,e,R(O({},t),{secure:!0}))}import{v4 as oe}from"uuid";var E=()=>{let r=w("user_id");return r||(r=oe(),x("user_id",r,{path:"/",secure:!0})),r};import{useEffect as ne}from"react";import{v4 as se}from"uuid";function ae(){let{addAlert:r}=y();ne(()=>{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:se(),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 le,useMemo as de,useState as H}from"react";import{v4 as ce}from"uuid";var F=r=>new CustomEvent("new-subscribe",{detail:r});var k=class{constructor(e,t,o){this.swPath=e;this.publicKey=t;this.scope=o;this.initialized=!1;this.clickHandlerAttached=!1}getOrCreateUserId(){let e=w("user_id");return e||(e=ce(),x("user_id",e,{path:"/"})),e}requestNotificationPermission(){return l(this,null,function*(){return Notification.requestPermission()})}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}trigger(e,t=1,o=0){if(!this.onMessage)return;let i=0,n=()=>{var a;i++,(a=this.onMessage)==null||a.call(this,{type:e,subscribe_id:this.subscribeId}),i>=t&&s!==void 0&&clearInterval(s)};n();let s;o>0&&t>1&&(s=window.setInterval(n,o))}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();this.subscribeId=s;let a={sub:i.toJSON(),userAgent:navigator.userAgent,userId:s,subscribeId:this.subscribeId};window.dispatchEvent(F(a)),yield t(a.sub,a.userAgent,a.userId)}return i})}init(e,t,o,i){return l(this,null,function*(){if(!this.initialized){this.initialized=!0,this.onMessage=t;try{if(!("serviceWorker"in navigator)||!("PushManager"in window))return;let n=Notification.permission;if(n==="denied"){this.trigger("denied");return}if(n==="default"&&(n=yield this.requestNotificationPermission()),n!=="granted"){this.trigger("denied");return}let s=yield this.registerServiceWorker();yield this.getOrCreateSubscription(s,e),this.trigger("subscribed",i!=null?i:1,o!=null?o:0)}catch(n){console.error("Service Worker init error:",n),this.trigger("error")}}})}clickNotification(e,t=!1){return l(this,null,function*(){t&&console.log("Click notification plugin init"),!(!("serviceWorker"in navigator)||!("PushManager"in window))&&(this.clickHandlerAttached||(this.clickHandlerAttached=!0,navigator.serviceWorker.addEventListener("message",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;try{let{data:a,ok:c}=yield e(n,s);c||console.error(`[Error adding click rate]: ${(a==null?void 0:a.message)||a}`)}finally{this.clickHandlerAttached=!1}}}))))})}};var I=class{constructor(e){this.sw=e}clickNotification(e,t=!1){return l(this,null,function*(){return yield this.sw.clickNotification(e,t)})}};var A=class{constructor(e,t,o){this.sw=null;this.plugins=null;typeof window!="undefined"&&(this.sw=new k(e,t,o),this.plugins=new I(this.sw))}run(e,t,o,i){return l(this,null,function*(){if(!this.sw)return new Error("BlastClient can only be run in the browser.");try{return yield this.sw.init(e,t,o,i),"Auto inject complete"}catch(n){return n}})}};function ue(r){let{swPath:e,publicKey:t,scope:o,subscribeUser:i,time:n,repeatCount:s}=r,[a,c]=H("idle"),[p,d]=H(null),m=de(()=>typeof window=="undefined"?null:new A(e,t,o),[e,t,o]);return le(()=>{if(!m)return;let b=!1;return c("initializing"),m.run(i,r.onMessage,n,s).then(h=>{b||(h instanceof Error?(d(h),c("error")):c("ready"))}).catch(h=>{b||(d(h),c("error"))}),()=>{b=!0}},[m,i,n,s]),{client:m,status:a,error:p}}import{useEffect as N,useRef as S,useState as V}from"react";import{motion as K,AnimatePresence as fe}from"framer-motion";import{createPortal as ge}from"react-dom";import{useCallback as pe,useLayoutEffect as me}from"react";var v=typeof document=="undefined"?null:document.getElementById("blast-root"),C=()=>{let r=pe(()=>{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)},[]);me(()=>{r()},[r])};import{jsx as f,jsxs as be}from"react/jsx-runtime";var $=({dialogs:r,addGets:e})=>{let[t,o]=V(0),[i,n]=V(!0),s=S(()=>{}),a=S(()=>{}),c=S(!1),p=E(),d=r[t],m=()=>{n(!1)},b=()=>{t<r.length-1?o(u=>u+1):m()};s.current=m,a.current=b,N(()=>{let u=()=>s.current();return window.addEventListener("dialog-close",u),()=>window.removeEventListener("dialog-close",u)},[]),N(()=>{console.log(`[Dialog Helper] Showing dialog: ${d.dialogId||`dialog-${t}`}`)},[d]),N(()=>{if(!d||!p||c.current)return;l(null,null,function*(){yield e(d.dialogId,p),c.current=!0})},[d,p]);let h=u=>{if(u==="post_message"){let P=()=>s.current(),X=()=>a.current();window.dispatchEvent(new CustomEvent("dialog-message",{detail:{dialogId:d.dialogId||`dialog-${t}`,close:P,next:X}}))}else b()},B=()=>f(fe,{children:i&&f(K.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:be(K.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:[f("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)]"}),f("h2",{className:"text-2xl font-extrabold tracking-wide",children:d.title}),f("p",{className:"text-sm text-white/80 pt-2",children:d.content}),f("div",{className:"flex justify-end gap-3 pt-6 flex-wrap",children:d.buttons.map((u,P)=>f("button",{onClick:()=>h(u.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:u.content},P))})]})})});return v?ge(B(),v):B()};import{Fragment as he,jsx as q}from"react/jsx-runtime";function T({dialogs:r,addGets:e}){return C(),r!=null&&r.length?q($,{dialogs:r,addGets:e}):q(he,{})}import{useEffect as ve}from"react";import{motion as ye,AnimatePresence as we}from"framer-motion";import{createPortal as xe}from"react-dom";import{jsx as g,jsxs as D}from"react/jsx-runtime";var J=({renderAlert:r})=>{let{removeAlert:e,alerts:t}=y();ve(()=>{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=()=>g(we,{children:t.map((i,n)=>r?g("div",{className:"fixed top-5 right-5 z-[9999999]",style:{maxWidth:"320px"},children:r(i)},i.alertId):D(ye.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:[D("div",{className:"flex-grow pr-4",children:[g("h5",{className:"mb-1 text-lg font-semibold tracking-wide",children:i.title}),g("p",{className:"text-sm opacity-80",children:i.content})]}),g("div",{className:"flex w-12 items-center justify-center text-purple-300",children:D("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:[g("path",{d:"M10.268 21a2 2 0 0 0 3.464 0"}),g("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 v?xe(o(),v):o()};import{jsx as ke}from"react/jsx-runtime";function _(){return C(),ke(J,{})}export{_ as Alert,L as BlastProvider,T as Dialog,ue as useBlast,ae as usePushNotificationListener,E as useUserId};
//# sourceMappingURL=main.mjs.map