UNPKG

@flows/js

Version:

Flows JS SDK - Build native product growth experiences, your way

2 lines (1 loc) 16.2 kB
"use strict";var flows_js=(()=>{var U=Object.defineProperty,oo=Object.defineProperties,eo=Object.getOwnPropertyDescriptor,ro=Object.getOwnPropertyDescriptors,no=Object.getOwnPropertyNames,pt=Object.getOwnPropertySymbols;var at=Object.prototype.hasOwnProperty,io=Object.prototype.propertyIsEnumerable;var ft=(t,o,e)=>o in t?U(t,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[o]=e,m=(t,o)=>{for(var e in o||(o={}))at.call(o,e)&&ft(t,e,o[e]);if(pt)for(var e of pt(o))io.call(o,e)&&ft(t,e,o[e]);return t},d=(t,o)=>oo(t,ro(o));var so=(t,o)=>{for(var e in o)U(t,e,{get:o[e],enumerable:!0})},po=(t,o,e,r)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of no(o))!at.call(t,n)&&n!==e&&U(t,n,{get:()=>o[n],enumerable:!(r=eo(o,n))||r.enumerable});return t};var fo=t=>po(U({},"__esModule",{value:!0}),t);var Ao={};so(Ao,{addFloatingBlocksChangeListener:()=>wo,addSlotBlocksChangeListener:()=>Bo,getCurrentFloatingBlocks:()=>ko,getCurrentSlotBlocks:()=>Qt,init:()=>ho,resetAllWorkflowsProgress:()=>bo,resetWorkflowProgress:()=>Io,startWorkflow:()=>So});var ct=(t,{body:o,method:e,version:r})=>fetch(t,{method:e,headers:{"Content-Type":"application/json","x-flows-version":r},body:o?JSON.stringify(o):void 0}).then(async n=>{var p;let i=await n.text(),s=i?JSON.parse(i):void 0;if(!n.ok){let f=s;throw new Error((p=f==null?void 0:f.message)!=null?p:n.statusText)}return s}),_=(t,o)=>({getBlocks:e=>ct(`${t}/v2/sdk/blocks`,{method:"POST",body:e,version:o}),sendEvent:e=>ct(`${t}/v2/sdk/events`,{method:"POST",body:e,version:o})});var F=(t,o)=>{let e=new Set([...o.exitedBlockIds,...o.updatedBlocks.map(r=>r.id)]);return[...t.filter(r=>!e.has(r.id)),...o.updatedBlocks]};function mt(t){var o;return typeof t=="string"||typeof t=="symbol"?t:Object.is((o=t==null?void 0:t.valueOf)==null?void 0:o.call(t),-0)?"-0":String(t)}function lt(t){let o=[],e=t.length;if(e===0)return o;let r=0,n="",i="",s=!1;for(t.charCodeAt(0)===46&&(o.push(""),r++);r<e;){let p=t[r];i?p==="\\"&&r+1<e?(r++,n+=t[r]):p===i?i="":n+=p:s?p==='"'||p==="'"?i=p:p==="]"?(s=!1,o.push(n),n=""):n+=p:p==="["?(s=!0,n&&(o.push(n),n="")):p==="."?n&&(o.push(n),n=""):n+=p,r++}return n&&o.push(n),o}function q(t){return t!==null&&(typeof t=="object"||typeof t=="function")}function ut(t,o){return t===o||Number.isNaN(t)&&Number.isNaN(o)}var ao=/^(?:0|[1-9]\d*)$/;function dt(t,o=Number.MAX_SAFE_INTEGER){switch(typeof t){case"number":return Number.isInteger(t)&&t>=0&&t<o;case"symbol":return!1;case"string":return ao.test(t)}}function xt(t){return typeof t=="symbol"||t instanceof Symbol}var co=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,mo=/^\w*$/;function gt(t,o){return Array.isArray(t)?!1:typeof t=="number"||typeof t=="boolean"||t==null||xt(t)?!0:typeof t=="string"&&(mo.test(t)||!co.test(t))||o!=null&&Object.hasOwn(o,t)}var ht=(t,o,e)=>{let r=t[o];(!(Object.hasOwn(t,o)&&ut(r,e))||e===void 0&&!(o in t))&&(t[o]=e)};function vt(t,o,e,r){if(t==null&&!q(t))return t;let n=gt(o,t)?[o]:Array.isArray(o)?o:typeof o=="string"?lt(o):[o],i=t;for(let s=0;s<n.length&&i!=null;s++){let p=mt(n[s]),f;if(s===n.length-1)f=e(i[p]);else{let l=i[p],c=r(l);f=c!==void 0?c:q(l)?l:dt(n[s+1])?[]:{}}ht(i,p,f),i=i[p]}return t}function z(t,o,e){return vt(t,o,()=>e,()=>{})}var yt=({block:t,exitNodeCb:o,removeBlock:e,setStateMemory:r})=>{var p,f,l;let n=({properties:c,parentKey:u})=>{var st;let g=m({},c);return Object.entries(c).forEach(([R,L])=>{Array.isArray(L)&&(g[R]=L.map((O,Yt)=>typeof O=="object"?n({properties:O,parentKey:[u,R,Yt].filter(to=>to!==void 0).join(".")}):O))}),delete g.f__exit_nodes,(st=c.f__exit_nodes)==null||st.forEach(R=>{let L=()=>o([u,R].filter(O=>O!==void 0).join("."));g[R]=L}),g},i=n({properties:t.data});for(let c of(p=t.propertyMeta)!=null?p:[])if(c.type==="state-memory"){let u={value:(f=c.value)!=null?f:!1,setValue:g=>{r(c.key,g)},triggers:(l=c.triggers)!=null?l:[]};z(i,c.key,u)}let s=t.exitNodes.reduce((c,u)=>{let g=()=>(e(t.id),o(u));return c[u]=g,c},{});return m(m({__flows:{id:t.id,key:t.key,workflowId:t.workflowId}},i),s)};var lo="#22262d",uo="#fff",kt="%cFlows%c",wt=`color:${uo};background:${lo};padding:2px 4px;border-radius:4px`,Bt={error:(t,...o)=>{console.error(`${kt} ${t}`,wt,"",...o)},warn:(t,...o)=>{console.warn(`${kt} ${t}`,wt,"",...o)}};var K=(t,o)=>o?Array.isArray(o)?o.some(e=>K(t,e)):typeof t!="string"?!1:new RegExp(o).test(t):!0,G=(t,o)=>o===void 0?!0:Array.isArray(o)?o.some(e=>G(t,e)):t===o,H=(t,o)=>o===void 0?!0:Array.isArray(o)?o.every(e=>H(t,e)):t!==o;var J=(t,o)=>o?Array.isArray(o)?o.some(e=>J(t,e)):typeof t!="string"?!1:t.includes(o):!0,Z=(t,o)=>o?Array.isArray(o)?o.every(e=>Z(t,e)):typeof t!="string"?!1:!t.includes(o):!0;var A=({operator:t,pathname:o,value:e})=>t==="eq"?G(o,e):t==="ne"?H(o,e):t==="contains"?J(o,e):t==="notContains"?Z(o,e):t==="regex"?K(o,e):!0,bt=({eventTarget:t,value:o})=>o?Array.from(document.querySelectorAll(o)).some(e=>e.contains(t)):!1;var It=()=>window.location.pathname+window.location.search;var St=t=>{var o;if(!(!t||t==="disabled"))return t==="automatic"?(o=navigator.languages.at(0))!=null?o:navigator.language:t};var X=(t,o)=>new Proxy(t,{get(e,r,n){return r==="props"&&o(t.id),Reflect.get(e,r,n)}});var xo=Symbol.for("preact-signals");function Y(){if(P>1)P--;else{for(var t,o=!1;M!==void 0;){var e=M;for(M=void 0,Q++;e!==void 0;){var r=e.o;if(e.o=void 0,e.f&=-3,!(8&e.f)&&Et(e))try{e.c()}catch(n){o||(t=n,o=!0)}e=r}}if(Q=0,P--,o)throw t}}var a=void 0;function At(t){var o=a;a=void 0;try{return t()}finally{a=o}}var M=void 0,P=0,Q=0,j=0;function Pt(t){if(a!==void 0){var o=t.n;if(o===void 0||o.t!==a)return o={i:0,S:t,p:a.s,n:void 0,t:a,e:void 0,x:void 0,r:o},a.s!==void 0&&(a.s.n=o),a.s=o,t.n=o,32&a.f&&t.S(o),o;if(o.i===-1)return o.i=0,o.n!==void 0&&(o.n.p=o.p,o.p!==void 0&&(o.p.n=o.n),o.p=a.s,o.n=void 0,a.s.n=o,a.s=o),o}}function x(t,o){this.v=t,this.i=0,this.n=void 0,this.t=void 0,this.W=o==null?void 0:o.watched,this.Z=o==null?void 0:o.unwatched}x.prototype.brand=xo;x.prototype.h=function(){return!0};x.prototype.S=function(t){var o=this,e=this.t;e!==t&&t.e===void 0&&(t.x=e,this.t=t,e!==void 0?e.e=t:At(function(){var r;(r=o.W)==null||r.call(o)}))};x.prototype.U=function(t){var o=this;if(this.t!==void 0){var e=t.e,r=t.x;e!==void 0&&(e.x=r,t.e=void 0),r!==void 0&&(r.e=e,t.x=void 0),t===this.t&&(this.t=r,r===void 0&&At(function(){var n;(n=o.Z)==null||n.call(o)}))}};x.prototype.subscribe=function(t){var o=this;return w(function(){var e=o.value,r=a;a=void 0;try{t(e)}finally{a=r}})};x.prototype.valueOf=function(){return this.value};x.prototype.toString=function(){return this.value+""};x.prototype.toJSON=function(){return this.value};x.prototype.peek=function(){var t=a;a=void 0;try{return this.value}finally{a=t}};Object.defineProperty(x.prototype,"value",{get:function(){var t=Pt(this);return t!==void 0&&(t.i=this.i),this.v},set:function(t){if(t!==this.v){if(Q>100)throw new Error("Cycle detected");this.v=t,this.i++,j++,P++;try{for(var o=this.t;o!==void 0;o=o.x)o.t.N()}finally{Y()}}}});function E(t,o){return new x(t,o)}function Et(t){for(var o=t.s;o!==void 0;o=o.n)if(o.S.i!==o.i||!o.S.h()||o.S.i!==o.i)return!0;return!1}function Tt(t){for(var o=t.s;o!==void 0;o=o.n){var e=o.S.n;if(e!==void 0&&(o.r=e),o.S.n=o,o.i=-1,o.n===void 0){t.s=o;break}}}function Rt(t){for(var o=t.s,e=void 0;o!==void 0;){var r=o.p;o.i===-1?(o.S.U(o),r!==void 0&&(r.n=o.n),o.n!==void 0&&(o.n.p=r)):e=o,o.S.n=o.r,o.r!==void 0&&(o.r=void 0),o=r}t.s=e}function b(t,o){x.call(this,void 0),this.x=t,this.s=void 0,this.g=j-1,this.f=4,this.W=o==null?void 0:o.watched,this.Z=o==null?void 0:o.unwatched}b.prototype=new x;b.prototype.h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===j))return!0;if(this.g=j,this.f|=1,this.i>0&&!Et(this))return this.f&=-2,!0;var t=a;try{Tt(this),a=this;var o=this.x();(16&this.f||this.v!==o||this.i===0)&&(this.v=o,this.f&=-17,this.i++)}catch(e){this.v=e,this.f|=16,this.i++}return a=t,Rt(this),this.f&=-2,!0};b.prototype.S=function(t){if(this.t===void 0){this.f|=36;for(var o=this.s;o!==void 0;o=o.n)o.S.S(o)}x.prototype.S.call(this,t)};b.prototype.U=function(t){if(this.t!==void 0&&(x.prototype.U.call(this,t),this.t===void 0)){this.f&=-33;for(var o=this.s;o!==void 0;o=o.n)o.S.U(o)}};b.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var t=this.t;t!==void 0;t=t.x)t.t.N()}};Object.defineProperty(b.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var t=Pt(this);if(this.h(),t!==void 0&&(t.i=this.i),16&this.f)throw this.v;return this.v}});function k(t,o){return new b(t,o)}function Ot(t){var o=t.u;if(t.u=void 0,typeof o=="function"){P++;var e=a;a=void 0;try{o()}catch(r){throw t.f&=-2,t.f|=8,tt(t),r}finally{a=e,Y()}}}function tt(t){for(var o=t.s;o!==void 0;o=o.n)o.S.U(o);t.x=void 0,t.s=void 0,Ot(t)}function go(t){if(a!==this)throw new Error("Out-of-order effect");Rt(this),a=t,this.f&=-2,8&this.f&&tt(this),Y()}function T(t){this.x=t,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32}T.prototype.c=function(){var t=this.S();try{if(8&this.f||this.x===void 0)return;var o=this.x();typeof o=="function"&&(this.u=o)}finally{t()}};T.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,Ot(this),Tt(this),P++;var t=a;return a=this,go.bind(this,t)};T.prototype.N=function(){2&this.f||(this.f|=2,this.o=M,M=this)};T.prototype.d=function(){this.f|=8,1&this.f||tt(this)};T.prototype.dispose=function(){this.d()};function w(t){var o=new T(t);try{o.c()}catch(e){throw o.d(),e}return o.d.bind(o)}var V=E(),B=E(),I=E(null),v=k(()=>{var t;return(t=I.value)!=null?t:[]}),C=E([]),W=t=>{I.value=v.value.filter(o=>o.id!==t)},Mt=(t,o)=>{I.value=v.value.map(e=>e.id===t?o(e):e)},y=E([]);w(()=>{let o=v.value.filter(i=>i.type==="tour"),e=y.peek(),r=new Map(e.map(i=>[i.blockId,i])),n=o.map(i=>{var f,l;let s=r.get(i.id),p=(l=(f=s==null?void 0:s.currentBlockIndex)!=null?f:i.currentTourIndex)!=null?l:0;return{blockId:i.id,currentBlockIndex:p}});y.value=n});var Wt="@flows/js";var Dt="1.10.3";var $=`${Wt}@${Dt}`;var h=async t=>{let o=V.value;if(!o)return;let{environment:e,organizationId:r,userId:n,apiUrl:i}=o;await _(i,$).sendEvent(d(m({},t),{environment:e,organizationId:r,userId:n}))},Nt=new Set,ot=async t=>{Nt.has(t)||(Nt.add(t),await h({name:"block-activated",blockId:t}))};var Lt=(t,o)=>{y.value=y.value.map(e=>e.blockId===t?o(e):e)},Ut=(t,o)=>{if(o===0)return;let r=o-1;Lt(t.id,n=>d(m({},n),{currentBlockIndex:r})),h({name:"tour-update",blockId:t.id,properties:{currentTourIndex:r}})},N=(t,o)=>{var r,n;if(o===((n=(r=t.tourBlocks)==null?void 0:r.length)!=null?n:1)-1)W(t.id),h({name:"transition",blockId:t.id,propertyKey:"complete"});else{let i=o+1;Lt(t.id,s=>d(m({},s),{currentBlockIndex:i})),h({name:"tour-update",blockId:t.id,properties:{currentTourIndex:i}})}},_t=t=>{W(t),h({name:"transition",blockId:t,propertyKey:"cancel"})},jt=t=>{let o=It(),e=new Map(v.value.map(r=>[r.id,r]));y.value.forEach(r=>{var p,f,l;let n=e.get(r.blockId);if(!n)return;let i=(p=n.tourBlocks)==null?void 0:p.at(r.currentBlockIndex);if(!i)return;let s=i.tourWait;if(s&&s.interaction==="click"){let c=A({pathname:o,operator:(f=s.page)==null?void 0:f.operator,value:(l=s.page)==null?void 0:l.value});bt({eventTarget:t,value:s.element})&&c&&N(n,r.currentBlockIndex)}})},D=new Map;w(()=>{let t=B.value,o=v.value,e=y.value,r=new Map(o.map(n=>[n.id,n]));e.forEach(n=>{var l,c,u;let i=r.get(n.blockId);if(!i)return;let s=(l=i.tourBlocks)==null?void 0:l.at(n.currentBlockIndex);if(!s)return;let p=D.get(n.blockId);p&&p.stepId!==s.id&&(clearTimeout(p.timeoutId),D.delete(n.blockId));let f=s.tourWait;if(f&&(f.interaction==="navigation"&&A({pathname:t,operator:(c=f.page)==null?void 0:c.operator,value:(u=f.page)==null?void 0:u.value})&&N(i,n.currentBlockIndex),f.interaction==="delay"&&f.ms!==void 0&&!D.has(n.blockId))){let g=window.setTimeout(()=>{N(i,n.currentBlockIndex),D.delete(n.blockId)},f.ms);D.set(n.blockId,{timeoutId:g,stepId:s.id})}})});var Vt=t=>{let o=t.target;!o||!(o instanceof Element)||jt(o)};var et=0,$t=t=>{let o=()=>{let e=new WebSocket(t.url),r=()=>{t.onOpen(),et=0},n=()=>{setTimeout(()=>{o()},Math.min(1e3*4**et,12e4)),et+=1};return e.addEventListener("message",t.onMessage),e.addEventListener("open",r),e.addEventListener("close",n),{disconnect:()=>{e.removeEventListener("message",t.onMessage),e.removeEventListener("open",r),e.removeEventListener("close",n),e.readyState===WebSocket.CONNECTING?e.addEventListener("open",()=>{e.close()}):e.close()}}};return o()};var S=null,Ft=t=>{let{environment:o,organizationId:e,userId:r,apiUrl:n}=t,i={environment:o,organizationId:e,userId:r},s=`${n.replace("https://","wss://").replace("http://","ws://")}/ws/sdk/block-updates?${new URLSearchParams(i).toString()}`,p=()=>{_(n,$).getBlocks(d(m({},i),{language:St(t.language),userProperties:t.userProperties})).then(c=>{var g;let u=C.value.reduce(F,c.blocks);I.value=u,C.value=[],(g=c.meta)!=null&&g.usage_limited&&(S==null||S())}).catch(c=>{Bt.error("Failed to load blocks",c)})},f=c=>{let u=JSON.parse(c.data);I.value?I.value=F(v.value,u):C.value=[...C.value,u]};S==null||S(),S=$t({url:s,onMessage:f,onOpen:p}).disconnect};var qt=[],zt=t=>{qt.forEach(({type:o,handler:e})=>{document.removeEventListener(o,e,!0)}),t.forEach(({type:o,handler:e})=>{document.addEventListener(o,e,!0)}),qt=t};var rt=null,ho=t=>{var p;let o=(p=t.apiUrl)!=null?p:"https://api.flows-cloud.com";V.value=d(m({},t),{apiUrl:o});let{environment:e,organizationId:r,userId:n,userProperties:i,language:s}=t;Ft({apiUrl:o,environment:e,organizationId:r,userId:n,userProperties:i,language:s}),rt!==null&&clearInterval(rt),rt=window.setInterval(()=>{B.value!==window.location.pathname&&(B.value=window.location.pathname)},250),zt([{type:"click",handler:Vt}])};var nt=t=>{if(!t.componentType)return[];let e=yt({block:t,removeBlock:W,exitNodeCb:n=>h({name:"transition",blockId:t.id,propertyKey:n}),setStateMemory:async(n,i)=>{Mt(t.id,s=>{var p;return d(m({},s),{propertyMeta:(p=s.propertyMeta)==null?void 0:p.map(f=>f.type==="state-memory"&&f.key===n?d(m({},f),{value:i}):f)})}),await h({name:"set-state-memory",blockId:t.id,propertyKey:n,properties:{value:i}})}}),r={id:t.id,type:"component",component:t.componentType,props:e};return X(r,ot)},it=(t,o)=>{let e=t.tourBlocks;if(!(e!=null&&e.length))return[];let r=e.at(o);if(!(r!=null&&r.componentType))return[];let n=o===0,i=()=>{Ut(t,o)},s=()=>{N(t,o)},p=()=>{_t(t.id)},f={id:r.id,tourBlockId:t.id,type:"tour-component",component:r.componentType,props:d(m({__flows:{id:r.id,key:r.key,workflowId:r.workflowId}},r.data),{previous:n?void 0:i,continue:s,cancel:p})};return X(f,ot)};var Gt=k(()=>v.value.filter(t=>A({pathname:B.value,operator:t.page_targeting_operator,value:t.page_targeting_values}))),Ht=k(()=>{let t=new Map(v.value.map(o=>[o.id,o]));return y.value.filter(o=>{var n;let e=t.get(o.blockId),r=(n=e==null?void 0:e.tourBlocks)==null?void 0:n.at(o.currentBlockIndex);return A({pathname:B.value,operator:r==null?void 0:r.page_targeting_operator,value:r==null?void 0:r.page_targeting_values})}).flatMap(o=>{let e=t.get(o.blockId);return e?d(m({},o),{block:e}):[]})}),Jt=k(()=>{let t=Gt.value.filter(e=>!e.slottable).flatMap(nt),o=Ht.value.filter(e=>{var n;let r=(n=e.block.tourBlocks)==null?void 0:n.at(e.currentBlockIndex);return!(r!=null&&r.slottable)}).flatMap(e=>it(e.block,e.currentBlockIndex));return[...t,...o]}),vo=k(()=>Gt.value.filter(t=>t.slottable)),Zt=t=>"type"in t,Kt=t=>{var e,r,n;if(Zt(t))return(e=t.slotIndex)!=null?e:0;let o=(r=t.block.tourBlocks)==null?void 0:r.at(t.currentBlockIndex);return(n=o==null?void 0:o.slotIndex)!=null?n:0},Xt=new Map,yo=t=>{let o=k(()=>{let e=vo.value.filter(i=>i.slottable&&i.slotId===t),r=Ht.value.filter(i=>{var p;let s=(p=i.block.tourBlocks)==null?void 0:p.at(i.currentBlockIndex);return(s==null?void 0:s.slottable)&&s.slotId===t});return[...e,...r].sort((i,s)=>Kt(i)-Kt(s)).flatMap(i=>Zt(i)?nt(i):it(i.block,i.currentBlockIndex))});return Xt.set(t,o),o},ko=()=>Jt.value,Qt=t=>{var o,e;return(e=(o=Xt.get(t))==null?void 0:o.value)!=null?e:yo(t).value},wo=t=>w(()=>{t(Jt.value)}),Bo=(t,o)=>w(()=>{o(Qt(t))}),bo=()=>h({name:"reset-progress"}),Io=t=>h({name:"reset-progress",workflowId:t}),So=t=>h({name:"workflow-start",blockKey:t});return fo(Ao);})();