@knocklabs/client 0.16.2 → 0.16.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.16.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 49f791b: fix: guard against undefined window object in ssr
8
+
9
+ ## 0.16.3
10
+
11
+ ### Patch Changes
12
+
13
+ - 79204f0: fix: export useGuides hook to react package
14
+
3
15
  ## 0.16.2
4
16
 
5
17
  ### Patch Changes
package/dist/cjs/api.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var a=Object.defineProperty;var n=(t,e,s)=>e in t?a(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var r=(t,e,s)=>n(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("axios"),l=require("axios-retry"),c=require("phoenix"),i=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},d=i(u),o=i(l);class h{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null,this.axiosClient=d.default.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken,"X-Knock-Client":this.getKnockClientHeader()}}),typeof window<"u"&&(this.socket=new c.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o.default(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.default.exponentialDelay})}async makeRequest(e){try{const s=await this.axiosClient(e);return{statusCode:s.status<300?"ok":"error",body:s.data,error:void 0,status:s.status}}catch(s){return console.error(s),{statusCode:"error",status:500,body:void 0,error:s}}}canRetryRequest(e){return o.default.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}getKnockClientHeader(){return"Knock/ClientJS 0.16.2"}}exports.default=h;
1
+ "use strict";var a=Object.defineProperty;var n=(t,e,s)=>e in t?a(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var r=(t,e,s)=>n(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("axios"),l=require("axios-retry"),c=require("phoenix"),i=t=>t&&typeof t=="object"&&"default"in t?t:{default:t},d=i(u),o=i(l);class h{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null,this.axiosClient=d.default.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken,"X-Knock-Client":this.getKnockClientHeader()}}),typeof window<"u"&&(this.socket=new c.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o.default(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.default.exponentialDelay})}async makeRequest(e){try{const s=await this.axiosClient(e);return{statusCode:s.status<300?"ok":"error",body:s.data,error:void 0,status:s.status}}catch(s){return console.error(s),{statusCode:"error",status:500,body:void 0,error:s}}}canRetryRequest(e){return o.default.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}getKnockClientHeader(){return"Knock/ClientJS 0.16.4"}}exports.default=h;
2
2
  //# sourceMappingURL=api.js.map
@@ -1,2 +1,2 @@
1
- "use strict";var m=Object.defineProperty;var f=(u,e,t)=>e in u?m(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var d=(u,e,t)=>f(u,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@tanstack/store"),S=require("urlpattern-polyfill"),c=require("./helpers.js"),v=50,G=30*1e3,p=u=>`/v1/users/${u}/guides`,g=(u,e={})=>{const t=new c.SelectionResult,s=c.findDefaultGroup(u.guideGroups);if(!s)return t;const r=s.display_sequence,i=u.location;for(const[n,o]of r.entries()){const a=u.guides[o];!a||!_(a,{location:i,filters:e})||t.set(n,a)}return t.metadata={guideGroup:s},t},_=(u,{location:e,filters:t={}})=>{if(t.type&&t.type!==u.type||t.key&&t.key!==u.key||u.steps.every(r=>!!r.message.archived_at))return!1;const s=u.activation_location_rules||[];return!(s.length>0&&e&&!s.reduce((i,n)=>{if(i===!1)return!1;switch(n.directive){case"allow":return i===!0||n.pattern.test(e)?!0:void 0;case"block":return n.pattern.test(e)?!1:i}},void 0))};class C{constructor(e,t,s={},r={}){d(this,"store");d(this,"socket");d(this,"socketChannel");d(this,"socketChannelTopic");d(this,"socketEventTypes",["guide.added","guide.updated","guide.removed","guide_group.added","guide_group.updated"]);d(this,"pushStateFn");d(this,"replaceStateFn");d(this,"stage");d(this,"counterIntervalId");d(this,"handleLocationChange",()=>{const e=window.location.href;this.store.state.location!==e&&(this.knock.log(`[Guide] Handle Location change: ${e}`),this.setLocation(e))});this.knock=e,this.channelId=t,this.targetParams=s,this.options=r;const{trackLocationFromWindow:i=!0}=r,n=i?window==null?void 0:window.location.href:void 0;this.store=new y.Store({guideGroups:[],guideGroupDisplayLogs:{},guides:{},queries:{},location:n,counter:0});const{socket:o}=this.knock.client();this.socket=o,this.socketChannelTopic=`guides:${t}`,i&&this.listenForLocationChangesFromWindow(),this.startCounterInterval(),this.knock.log("[Guide] Initialized a guide client")}incrementCounter(){this.knock.log("[Guide] Incrementing the counter"),this.store.setState(e=>({...e,counter:e.counter+1}))}startCounterInterval(){const{throttleCheckInterval:e=G}=this.options;this.counterIntervalId=setInterval(()=>{this.knock.log("[Guide] Counter interval tick"),!(this.stage&&this.stage.status!=="closed")&&this.incrementCounter()},e)}clearCounterInterval(){this.counterIntervalId&&(clearInterval(this.counterIntervalId),this.counterIntervalId=void 0)}cleanup(){this.unsubscribe(),this.removeEventListeners(),this.clearGroupStage(),this.clearCounterInterval()}async fetch(e){this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Loading all eligible guides");const t=this.buildQueryParams(e==null?void 0:e.filters),s=this.formatQueryKey(t),r=this.store.state.queries[s];if(r)return r;this.store.setState(n=>({...n,queries:{...n.queries,[s]:{status:"loading"}}}));let i;try{const n=await this.knock.user.getGuides(this.channelId,t);i={status:"ok"};const{entries:o,guide_groups:a,guide_group_display_logs:l}=n;this.store.setState(h=>({...h,guideGroups:(a==null?void 0:a.length)>0?a:[c.mockDefaultGroup(o)],guideGroupDisplayLogs:l,guides:c.byKey(o.map(k=>this.localCopy(k))),queries:{...h.queries,[s]:i}}))}catch(n){i={status:"error",error:n},this.store.setState(o=>({...o,queries:{...o.queries,[s]:i}}))}return i}subscribe(){if(!this.socket)return;this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Subscribing to real time updates"),this.socket.isConnected()||this.socket.connect(),this.socketChannel&&this.unsubscribe();const e={...this.targetParams,user_id:this.knock.userId},t=this.socket.channel(this.socketChannelTopic,e);for(const s of this.socketEventTypes)t.on(s,r=>this.handleSocketEvent(r));["closed","errored"].includes(t.state)&&t.join(),this.socketChannel=t}unsubscribe(){if(this.socketChannel){this.knock.log("[Guide] Unsubscribing from real time updates");for(const e of this.socketEventTypes)this.socketChannel.off(e);this.socketChannel.leave(),this.socketChannel=void 0}}handleSocketEvent(e){const{event:t,data:s}=e;switch(t){case"guide.added":return this.addOrReplaceGuide(e);case"guide.updated":return s.eligible?this.addOrReplaceGuide(e):this.removeGuide(e);case"guide.removed":return this.removeGuide(e);case"guide_group.added":case"guide_group.updated":return this.addOrReplaceGuideGroup(e);default:return}}setLocation(e){this.clearGroupStage(),this.store.setState(t=>({...t,location:e}))}selectGuides(e,t={}){if(Object.keys(e.guides).length===0)return[];this.knock.log(`[Guide] Selecting guides for: ${c.formatFilters(t)}`);const s=g(e,t);return s.size===0?(this.knock.log("[Guide] Selection returned zero result"),[]):[...s.values()]}selectGuide(e,t={}){if(Object.keys(e.guides).length===0)return;this.knock.log(`[Guide] Selecting a guide for: ${c.formatFilters(t)}`);const s=g(e,t);if(s.size===0){this.knock.log("[Guide] Selection returned zero result");return}const[r,i]=[...s][0];if(i.bypass_global_group_limit)return i;const n=c.findDefaultGroup(e.guideGroups),o=e.guideGroupDisplayLogs[c.DEFAULT_GROUP_KEY];if(!(n&&n.display_interval&&o&&c.checkIfThrottled(o,n.display_interval)))switch(this.stage||(this.stage=this.openGroupStage()),this.stage.status){case"open":{this.knock.log(`[Guide] Addng to the group stage: ${i.key}`),this.stage.ordered[r]=i.key;return}case"patch":return this.knock.log(`[Guide] Patching the group stage: ${i.key}`),this.stage.ordered[r]=i.key,this.stage.resolved===i.key?i:void 0;case"closed":return this.stage.resolved===i.key?i:void 0}}openGroupStage(){this.knock.log("[Guide] Opening a new group stage");const{orderResolutionDuration:e=v}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.stage={status:"open",ordered:[],timeoutId:t},this.stage}closePendingGroupStage(){if(!(!this.stage||this.stage.status==="closed"))return this.knock.log("[Guide] Closing the current group stage"),this.ensureClearTimeout(),this.stage={...this.stage,status:"closed",resolved:this.stage.ordered.find(e=>e!==void 0),timeoutId:null},this.stage}patchClosedGroupStage(){var s;if(((s=this.stage)==null?void 0:s.status)!=="closed")return;this.knock.log("[Guide] Patching the current group stage");const{orderResolutionDuration:e=0}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.ensureClearTimeout(),this.stage={...this.stage,status:"patch",ordered:[],timeoutId:t},this.stage}clearGroupStage(){this.stage&&(this.knock.log("[Guide] Clearing the current group stage"),this.ensureClearTimeout(),this.stage=void 0)}ensureClearTimeout(){var e;(e=this.stage)!=null&&e.timeoutId&&clearTimeout(this.stage.timeoutId)}_selectGuide(e,t={}){return this.openGroupStage(),this.selectGuide(e,t),this.closePendingGroupStage(),this.selectGuide(e,t)}async markAsSeen(e,t){if(t.message.seen_at)return;this.knock.log(`[Guide] Marking as seen (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{seen_at:new Date().toISOString()});if(!s)return;const r={...this.buildEngagementEventBaseParams(e,s),content:s.content,data:this.targetParams.data,tenant:this.targetParams.tenant};return this.knock.user.markGuideStepAs("seen",r),s}async markAsInteracted(e,t,s){this.knock.log(`[Guide] Marking as interacted (Guide key: ${e.key}, Step ref:${t.ref})`);const r=new Date().toISOString(),i=this.setStepMessageAttrs(e.key,t.ref,{read_at:r,interacted_at:r});if(!i)return;const n={...this.buildEngagementEventBaseParams(e,i),metadata:s};return this.knock.user.markGuideStepAs("interacted",n),i}async markAsArchived(e,t){if(t.message.archived_at)return;this.knock.log(`[Guide] Marking as archived (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{archived_at:new Date().toISOString()});if(!s)return;const r=this.buildEngagementEventBaseParams(e,s);return this.knock.user.markGuideStepAs("archived",{...r,unthrottled:e.bypass_global_group_limit}),s}localCopy(e){const t=this,s={...e,getStep(){return this.steps.find(r=>!r.message.archived_at)}};return s.getStep=s.getStep.bind(s),s.steps=e.steps.map(({message:r,...i})=>{const n={...i,message:{...r},markAsSeen(){if(!this.message.seen_at)return t.markAsSeen(s,this)},markAsInteracted({metadata:o}={}){return t.markAsInteracted(s,this,o)},markAsArchived(){if(!this.message.archived_at)return t.markAsArchived(s,this)}};return n.markAsSeen=n.markAsSeen.bind(n),n.markAsInteracted=n.markAsInteracted.bind(n),n.markAsArchived=n.markAsArchived.bind(n),n}),s.activation_location_rules=e.activation_location_rules.map(r=>({...r,pattern:new S.URLPattern({pathname:r.pathname})})),s}buildQueryParams(e={}){const t={...this.targetParams,...e};let s=Object.fromEntries(Object.entries(t).filter(([r,i])=>i!=null));return s=s.data?{...s,data:JSON.stringify(s.data)}:s,s}formatQueryKey(e){const s=Object.keys(e).sort().map(i=>`${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`).join("&"),r=p(this.knock.userId);return s?`${r}?${s}`:r}setStepMessageAttrs(e,t,s){let r;return s.archived_at&&this.clearGroupStage(),this.store.setState(i=>{const n=i.guides[e];if(!n)return i;const o=n.steps.map(h=>(h.ref!==t||(h.message={...h.message,...s},r=h),h));n.steps=o;const a={...i.guides,[n.key]:n},l=s.archived_at&&!n.bypass_global_group_limit?{...i.guideGroupDisplayLogs,[c.DEFAULT_GROUP_KEY]:s.archived_at}:i.guideGroupDisplayLogs;return{...i,guides:a,guideGroupDisplayLogs:l}}),r}buildEngagementEventBaseParams(e,t){return{message_id:t.message.id,channel_id:e.channel_id,guide_key:e.key,guide_id:e.id,guide_step_ref:t.ref}}addOrReplaceGuide({data:e}){this.patchClosedGroupStage();const t=this.localCopy(e.guide);this.store.setState(s=>{const r={...s.guides,[t.key]:t};return{...s,guides:r}})}removeGuide({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const{[e.guide.key]:s,...r}=t.guides;return{...t,guides:r}})}addOrReplaceGuideGroup({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const s=[e.guide_group],r=e.guide_group.display_sequence_unthrottled||[],i=e.guide_group.display_sequence_throttled||[];let n=t.guides;return n=r.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!0};return{...o,[a]:l}},n),n=i.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!1};return{...o,[a]:l}},n),{...t,guides:n,guideGroups:s}})}listenForLocationChangesFromWindow(){if(window!=null&&window.history){window.addEventListener("popstate",this.handleLocationChange),window.addEventListener("hashchange",this.handleLocationChange);const e=window.history.pushState,t=window.history.replaceState;window.history.pushState=new Proxy(e,{apply:(s,r,i)=>{Reflect.apply(s,r,i),setTimeout(()=>{this.handleLocationChange()},0)}}),window.history.replaceState=new Proxy(t,{apply:(s,r,i)=>{Reflect.apply(s,r,i),setTimeout(()=>{this.handleLocationChange()},0)}}),this.pushStateFn=e,this.replaceStateFn=t}else this.knock.log("[Guide] Unable to access the `window.history` object to detect location changes")}removeEventListeners(){window.removeEventListener("popstate",this.handleLocationChange),window.removeEventListener("hashchange",this.handleLocationChange),this.pushStateFn&&(window.history.pushState=this.pushStateFn,this.pushStateFn=void 0),this.replaceStateFn&&(window.history.replaceState=this.replaceStateFn,this.replaceStateFn=void 0)}}exports.KnockGuideClient=C;exports.guidesApiRootPath=p;
1
+ "use strict";var m=Object.defineProperty;var y=(u,e,t)=>e in u?m(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var c=(u,e,t)=>y(u,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@tanstack/store"),v=require("urlpattern-polyfill"),d=require("./helpers.js"),G=50,_=30*1e3,g=()=>{if(typeof window<"u")return window},k=u=>`/v1/users/${u}/guides`,p=(u,e={})=>{const t=new d.SelectionResult,s=d.findDefaultGroup(u.guideGroups);if(!s)return t;const n=s.display_sequence,i=u.location;for(const[r,o]of n.entries()){const a=u.guides[o];!a||!C(a,{location:i,filters:e})||t.set(r,a)}return t.metadata={guideGroup:s},t},C=(u,{location:e,filters:t={}})=>{if(t.type&&t.type!==u.type||t.key&&t.key!==u.key||u.steps.every(n=>!!n.message.archived_at))return!1;const s=u.activation_location_rules||[];return!(s.length>0&&e&&!s.reduce((i,r)=>{if(i===!1)return!1;switch(r.directive){case"allow":return i===!0||r.pattern.test(e)?!0:void 0;case"block":return r.pattern.test(e)?!1:i}},void 0))};class b{constructor(e,t,s={},n={}){c(this,"store");c(this,"socket");c(this,"socketChannel");c(this,"socketChannelTopic");c(this,"socketEventTypes",["guide.added","guide.updated","guide.removed","guide_group.added","guide_group.updated"]);c(this,"pushStateFn");c(this,"replaceStateFn");c(this,"stage");c(this,"counterIntervalId");c(this,"handleLocationChange",()=>{const e=g();if(!(e!=null&&e.location))return;const t=e.location.href;this.store.state.location!==t&&(this.knock.log(`[Guide] Handle Location change: ${t}`),this.setLocation(t))});this.knock=e,this.channelId=t,this.targetParams=s,this.options=n;const{trackLocationFromWindow:i=!0}=n,r=g(),o=i?r==null?void 0:r.location.href:void 0;this.store=new S.Store({guideGroups:[],guideGroupDisplayLogs:{},guides:{},queries:{},location:o,counter:0});const{socket:a}=this.knock.client();this.socket=a,this.socketChannelTopic=`guides:${t}`,i&&this.listenForLocationChangesFromWindow(),this.startCounterInterval(),this.knock.log("[Guide] Initialized a guide client")}incrementCounter(){this.knock.log("[Guide] Incrementing the counter"),this.store.setState(e=>({...e,counter:e.counter+1}))}startCounterInterval(){const{throttleCheckInterval:e=_}=this.options;this.counterIntervalId=setInterval(()=>{this.knock.log("[Guide] Counter interval tick"),!(this.stage&&this.stage.status!=="closed")&&this.incrementCounter()},e)}clearCounterInterval(){this.counterIntervalId&&(clearInterval(this.counterIntervalId),this.counterIntervalId=void 0)}cleanup(){this.unsubscribe(),this.removeEventListeners(),this.clearGroupStage(),this.clearCounterInterval()}async fetch(e){this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Loading all eligible guides");const t=this.buildQueryParams(e==null?void 0:e.filters),s=this.formatQueryKey(t),n=this.store.state.queries[s];if(n)return n;this.store.setState(r=>({...r,queries:{...r.queries,[s]:{status:"loading"}}}));let i;try{const r=await this.knock.user.getGuides(this.channelId,t);i={status:"ok"};const{entries:o,guide_groups:a,guide_group_display_logs:l}=r;this.store.setState(h=>({...h,guideGroups:(a==null?void 0:a.length)>0?a:[d.mockDefaultGroup(o)],guideGroupDisplayLogs:l||{},guides:d.byKey(o.map(f=>this.localCopy(f))),queries:{...h.queries,[s]:i}}))}catch(r){i={status:"error",error:r},this.store.setState(o=>({...o,queries:{...o.queries,[s]:i}}))}return i}subscribe(){if(!this.socket)return;this.knock.failIfNotAuthenticated(),this.knock.log("[Guide] Subscribing to real time updates"),this.socket.isConnected()||this.socket.connect(),this.socketChannel&&this.unsubscribe();const e={...this.targetParams,user_id:this.knock.userId},t=this.socket.channel(this.socketChannelTopic,e);for(const s of this.socketEventTypes)t.on(s,n=>this.handleSocketEvent(n));["closed","errored"].includes(t.state)&&t.join(),this.socketChannel=t}unsubscribe(){if(this.socketChannel){this.knock.log("[Guide] Unsubscribing from real time updates");for(const e of this.socketEventTypes)this.socketChannel.off(e);this.socketChannel.leave(),this.socketChannel=void 0}}handleSocketEvent(e){const{event:t,data:s}=e;switch(t){case"guide.added":return this.addOrReplaceGuide(e);case"guide.updated":return s.eligible?this.addOrReplaceGuide(e):this.removeGuide(e);case"guide.removed":return this.removeGuide(e);case"guide_group.added":case"guide_group.updated":return this.addOrReplaceGuideGroup(e);default:return}}setLocation(e){this.clearGroupStage(),this.store.setState(t=>({...t,location:e}))}selectGuides(e,t={}){if(Object.keys(e.guides).length===0)return[];this.knock.log(`[Guide] Selecting guides for: ${d.formatFilters(t)}`);const s=p(e,t);return s.size===0?(this.knock.log("[Guide] Selection returned zero result"),[]):[...s.values()]}selectGuide(e,t={}){if(Object.keys(e.guides).length===0)return;this.knock.log(`[Guide] Selecting a guide for: ${d.formatFilters(t)}`);const s=p(e,t);if(s.size===0){this.knock.log("[Guide] Selection returned zero result");return}const[n,i]=[...s][0];if(i.bypass_global_group_limit)return i;const r=d.findDefaultGroup(e.guideGroups),o=e.guideGroupDisplayLogs[d.DEFAULT_GROUP_KEY];if(!(r&&r.display_interval&&o&&d.checkIfThrottled(o,r.display_interval)))switch(this.stage||(this.stage=this.openGroupStage()),this.stage.status){case"open":{this.knock.log(`[Guide] Addng to the group stage: ${i.key}`),this.stage.ordered[n]=i.key;return}case"patch":return this.knock.log(`[Guide] Patching the group stage: ${i.key}`),this.stage.ordered[n]=i.key,this.stage.resolved===i.key?i:void 0;case"closed":return this.stage.resolved===i.key?i:void 0}}openGroupStage(){this.knock.log("[Guide] Opening a new group stage");const{orderResolutionDuration:e=G}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.stage={status:"open",ordered:[],timeoutId:t},this.stage}closePendingGroupStage(){if(!(!this.stage||this.stage.status==="closed"))return this.knock.log("[Guide] Closing the current group stage"),this.ensureClearTimeout(),this.stage={...this.stage,status:"closed",resolved:this.stage.ordered.find(e=>e!==void 0),timeoutId:null},this.stage}patchClosedGroupStage(){var s;if(((s=this.stage)==null?void 0:s.status)!=="closed")return;this.knock.log("[Guide] Patching the current group stage");const{orderResolutionDuration:e=0}=this.options,t=setTimeout(()=>{this.closePendingGroupStage(),this.incrementCounter()},e);return this.ensureClearTimeout(),this.stage={...this.stage,status:"patch",ordered:[],timeoutId:t},this.stage}clearGroupStage(){this.stage&&(this.knock.log("[Guide] Clearing the current group stage"),this.ensureClearTimeout(),this.stage=void 0)}ensureClearTimeout(){var e;(e=this.stage)!=null&&e.timeoutId&&clearTimeout(this.stage.timeoutId)}_selectGuide(e,t={}){return this.openGroupStage(),this.selectGuide(e,t),this.closePendingGroupStage(),this.selectGuide(e,t)}async markAsSeen(e,t){if(t.message.seen_at)return;this.knock.log(`[Guide] Marking as seen (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{seen_at:new Date().toISOString()});if(!s)return;const n={...this.buildEngagementEventBaseParams(e,s),content:s.content,data:this.targetParams.data,tenant:this.targetParams.tenant};return this.knock.user.markGuideStepAs("seen",n),s}async markAsInteracted(e,t,s){this.knock.log(`[Guide] Marking as interacted (Guide key: ${e.key}, Step ref:${t.ref})`);const n=new Date().toISOString(),i=this.setStepMessageAttrs(e.key,t.ref,{read_at:n,interacted_at:n});if(!i)return;const r={...this.buildEngagementEventBaseParams(e,i),metadata:s};return this.knock.user.markGuideStepAs("interacted",r),i}async markAsArchived(e,t){if(t.message.archived_at)return;this.knock.log(`[Guide] Marking as archived (Guide key: ${e.key}, Step ref:${t.ref})`);const s=this.setStepMessageAttrs(e.key,t.ref,{archived_at:new Date().toISOString()});if(!s)return;const n=this.buildEngagementEventBaseParams(e,s);return this.knock.user.markGuideStepAs("archived",{...n,unthrottled:e.bypass_global_group_limit}),s}localCopy(e){const t=this,s={...e,getStep(){return this.steps.find(n=>!n.message.archived_at)}};return s.getStep=s.getStep.bind(s),s.steps=e.steps.map(({message:n,...i})=>{const r={...i,message:{...n},markAsSeen(){if(!this.message.seen_at)return t.markAsSeen(s,this)},markAsInteracted({metadata:o}={}){return t.markAsInteracted(s,this,o)},markAsArchived(){if(!this.message.archived_at)return t.markAsArchived(s,this)}};return r.markAsSeen=r.markAsSeen.bind(r),r.markAsInteracted=r.markAsInteracted.bind(r),r.markAsArchived=r.markAsArchived.bind(r),r}),s.activation_location_rules=e.activation_location_rules.map(n=>({...n,pattern:new v.URLPattern({pathname:n.pathname})})),s}buildQueryParams(e={}){const t={...this.targetParams,...e};let s=Object.fromEntries(Object.entries(t).filter(([n,i])=>i!=null));return s=s.data?{...s,data:JSON.stringify(s.data)}:s,s}formatQueryKey(e){const s=Object.keys(e).sort().map(i=>`${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`).join("&"),n=k(this.knock.userId);return s?`${n}?${s}`:n}setStepMessageAttrs(e,t,s){let n;return s.archived_at&&this.clearGroupStage(),this.store.setState(i=>{const r=i.guides[e];if(!r)return i;const o=r.steps.map(h=>(h.ref!==t||(h.message={...h.message,...s},n=h),h));r.steps=o;const a={...i.guides,[r.key]:r},l=s.archived_at&&!r.bypass_global_group_limit?{...i.guideGroupDisplayLogs,[d.DEFAULT_GROUP_KEY]:s.archived_at}:i.guideGroupDisplayLogs;return{...i,guides:a,guideGroupDisplayLogs:l}}),n}buildEngagementEventBaseParams(e,t){return{message_id:t.message.id,channel_id:e.channel_id,guide_key:e.key,guide_id:e.id,guide_step_ref:t.ref}}addOrReplaceGuide({data:e}){this.patchClosedGroupStage();const t=this.localCopy(e.guide);this.store.setState(s=>{const n={...s.guides,[t.key]:t};return{...s,guides:n}})}removeGuide({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const{[e.guide.key]:s,...n}=t.guides;return{...t,guides:n}})}addOrReplaceGuideGroup({data:e}){this.patchClosedGroupStage(),this.store.setState(t=>{const s=[e.guide_group],n=e.guide_group.display_sequence_unthrottled||[],i=e.guide_group.display_sequence_throttled||[];let r=t.guides;return r=n.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!0};return{...o,[a]:l}},r),r=i.reduce((o,a)=>{if(!o[a])return o;const l={...o[a],bypass_global_group_limit:!1};return{...o,[a]:l}},r),{...t,guides:r,guideGroups:s}})}listenForLocationChangesFromWindow(){const e=g();if(e!=null&&e.history){e.addEventListener("popstate",this.handleLocationChange),e.addEventListener("hashchange",this.handleLocationChange);const t=e.history.pushState,s=e.history.replaceState;e.history.pushState=new Proxy(t,{apply:(n,i,r)=>{Reflect.apply(n,i,r),setTimeout(()=>{this.handleLocationChange()},0)}}),e.history.replaceState=new Proxy(s,{apply:(n,i,r)=>{Reflect.apply(n,i,r),setTimeout(()=>{this.handleLocationChange()},0)}}),this.pushStateFn=t,this.replaceStateFn=s}else this.knock.log("[Guide] Unable to access the `window.history` object to detect location changes")}removeEventListeners(){const e=g();e!=null&&e.history&&(e.removeEventListener("popstate",this.handleLocationChange),e.removeEventListener("hashchange",this.handleLocationChange),this.pushStateFn&&(e.history.pushState=this.pushStateFn,this.pushStateFn=void 0),this.replaceStateFn&&(e.history.replaceState=this.replaceStateFn,this.replaceStateFn=void 0))}}exports.KnockGuideClient=b;exports.guidesApiRootPath=k;
2
2
  //# sourceMappingURL=client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n\n const location = trackLocationFromWindow\n ? window?.location.href\n : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const {\n entries,\n guide_groups: groups,\n guide_group_display_logs: guideGroupDisplayLogs,\n } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs,\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n newChannel.join();\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const href = window.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n if (window?.history) {\n // 1. Listen for browser back/forward button clicks.\n window.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n window.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = window.history.pushState;\n const replaceStateFn = window.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n window.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n window.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n window.removeEventListener(\"popstate\", this.handleLocationChange);\n window.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n window.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n window.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guideGroupDisplayLogs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":"kVA6CMA,EAAoC,GAIpCC,EAAqC,GAAK,IAEnCC,EAAqBC,GAChC,aAAaA,CAAM,UAEfC,EAAS,CAACC,EAAmBC,EAA8B,KAAO,CAEhE,MAAAC,EAAS,IAAIC,kBAEbC,EAAeC,EAAAA,iBAAiBL,EAAM,WAAW,EACnD,GAAA,CAACI,EAAqB,OAAAF,EAE1B,MAAMI,EAAkBF,EAAa,iBAC/BG,EAAWP,EAAM,SAEvB,SAAW,CAACQ,EAAOC,CAAQ,IAAKH,EAAgB,UAAW,CACnD,MAAAI,EAAQV,EAAM,OAAOS,CAAQ,EAC/B,CAACC,GAGD,CADaC,EAAUD,EAAO,CAAE,SAAAH,EAAU,QAAAN,EAAS,GAGhDC,EAAA,IAAIM,EAAOE,CAAK,CAAA,CAGlB,OAAAR,EAAA,SAAW,CAAE,WAAYE,CAAa,EACtCF,CACT,EAOMS,EAAY,CAChBD,EACA,CAAE,SAAAH,EAAU,QAAAN,EAAU,MACnB,CASC,GARAA,EAAQ,MAAQA,EAAQ,OAASS,EAAM,MAIvCT,EAAQ,KAAOA,EAAQ,MAAQS,EAAM,KAIrCA,EAAM,MAAM,MAAOE,GAAM,CAAC,CAACA,EAAE,QAAQ,WAAW,EAC3C,MAAA,GAGH,MAAAC,EAAgBH,EAAM,2BAA6B,CAAC,EAEtD,MAAA,EAAAG,EAAc,OAAS,GAAKN,GA4B1B,CA3BYM,EAAc,OAA4B,CAACC,EAAKC,IAAS,CAGnE,GAAAD,IAAQ,GAAc,MAAA,GAK1B,OAAQC,EAAK,UAAW,CACtB,IAAK,QAGC,OAAAD,IAAQ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AKsD,IAAQ,CACtC,GAAI,CAACtD,EAAIsD,CAAG,EAAU,OAAAtD,EACtB,MAAMJ,EAAQ,CAAE,GAAGI,EAAIsD,CAAG,EAAG,0BAA2B,EAAM,EAC9D,MAAO,CAAE,GAAGtD,EAAK,CAACsD,CAAG,EAAG1D,CAAM,GAC7B+D,CAAM,EAEF,CAAE,GAAGzE,EAAO,OAAAyE,EAAQ,YAAAE,CAAY,CAAA,CACxC,CAAA,CAYK,oCAAqC,CAC3C,GAAI,qBAAQ,QAAS,CAEZ,OAAA,iBAAiB,WAAY,KAAK,oBAAoB,EAGtD,OAAA,iBAAiB,aAAc,KAAK,oBAAoB,EAGzD,MAAAG,EAAc,OAAO,QAAQ,UAC7BC,EAAiB,OAAO,QAAQ,aAGtC,OAAO,QAAQ,UAAY,IAAI,MAAMD,EAAa,CAChD,MAAO,CAACE,EAAQC,EAASC,IAAS,CACxB,QAAA,MAAMF,EAAQC,EAASC,CAAI,EACnC,WAAW,IAAM,CACf,KAAK,qBAAqB,GACzB,CAAC,CAAA,CACN,CACD,EACD,OAAO,QAAQ,aAAe,IAAI,MAAMH,EAAgB,CACtD,MAAO,CAACC,EAAQC,EAASC,IAAS,CACxB,QAAA,MAAMF,EAAQC,EAASC,CAAI,EACnC,WAAW,IAAM,CACf,KAAK,qBAAqB,GACzB,CAAC,CAAA,CACN,CACD,EAGD,KAAK,YAAcJ,EACnB,KAAK,eAAiBC,CAAA,MAEtB,KAAK,MAAM,IACT,iFACF,CACF,CAGM,sBAAuB,CACtB,OAAA,oBAAoB,WAAY,KAAK,oBAAoB,EACzD,OAAA,oBAAoB,aAAc,KAAK,oBAAoB,EAE9D,KAAK,cACA,OAAA,QAAQ,UAAY,KAAK,YAChC,KAAK,YAAc,QAEjB,KAAK,iBACA,OAAA,QAAQ,aAAe,KAAK,eACnC,KAAK,eAAiB,OACxB,CAEJ"}
1
+ {"version":3,"file":"client.js","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\n// Return the global window object if defined, so to safely guard against SSR.\nconst checkForWindow = () => {\n if (typeof window !== \"undefined\") {\n return window;\n }\n};\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n const win = checkForWindow();\n\n const location = trackLocationFromWindow ? win?.location.href : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n newChannel.join();\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const win = checkForWindow();\n if (!win?.location) return;\n\n const href = win.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n const win = checkForWindow();\n if (win?.history) {\n // 1. Listen for browser back/forward button clicks.\n win.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n win.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = win.history.pushState;\n const replaceStateFn = win.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n win.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n win.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n const win = checkForWindow();\n if (!win?.history) return;\n\n win.removeEventListener(\"popstate\", this.handleLocationChange);\n win.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n win.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n win.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","checkForWindow","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","win","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","guideGroupDisplayLogs","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":"kVA6CMA,EAAoC,GAIpCC,EAAqC,GAAK,IAG1CC,EAAiB,IAAM,CACvB,GAAA,OAAO,OAAW,IACb,OAAA,MAEX,EAEaC,EAAqBC,GAChC,aAAaA,CAAM,UAEfC,EAAS,CAACC,EAAmBC,EAA8B,KAAO,CAEhE,MAAAC,EAAS,IAAIC,kBAEbC,EAAeC,EAAAA,iBAAiBL,EAAM,WAAW,EACnD,GAAA,CAACI,EAAqB,OAAAF,EAE1B,MAAMI,EAAkBF,EAAa,iBAC/BG,EAAWP,EAAM,SAEvB,SAAW,CAACQ,EAAOC,CAAQ,IAAKH,EAAgB,UAAW,CACnD,MAAAI,EAAQV,EAAM,OAAOS,CAAQ,EAC/B,CAACC,GAGD,CADaC,EAAUD,EAAO,CAAE,SAAAH,EAAU,QAAAN,EAAS,GAGhDC,EAAA,IAAIM,EAAOE,CAAK,CAAA,CAGlB,OAAAR,EAAA,SAAW,CAAE,WAAYE,CAA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ASC,CAAI,EACnC,WAAW,IAAM,CACf,KAAK,qBAAqB,GACzB,CAAC,CAAA,CACN,CACD,EACD9D,EAAI,QAAQ,aAAe,IAAI,MAAM2D,EAAgB,CACnD,MAAO,CAACC,EAAQC,EAASC,IAAS,CACxB,QAAA,MAAMF,EAAQC,EAASC,CAAI,EACnC,WAAW,IAAM,CACf,KAAK,qBAAqB,GACzB,CAAC,CAAA,CACN,CACD,EAGD,KAAK,YAAcJ,EACnB,KAAK,eAAiBC,CAAA,MAEtB,KAAK,MAAM,IACT,iFACF,CACF,CAGM,sBAAuB,CAC7B,MAAM3D,EAAM1B,EAAe,EACtB0B,GAAA,MAAAA,EAAK,UAENA,EAAA,oBAAoB,WAAY,KAAK,oBAAoB,EACzDA,EAAA,oBAAoB,aAAc,KAAK,oBAAoB,EAE3D,KAAK,cACHA,EAAA,QAAQ,UAAY,KAAK,YAC7B,KAAK,YAAc,QAEjB,KAAK,iBACHA,EAAA,QAAQ,aAAe,KAAK,eAChC,KAAK,eAAiB,QACxB,CAEJ"}
package/dist/esm/api.mjs CHANGED
@@ -53,7 +53,7 @@ class k {
53
53
  return o.isNetworkError(e) ? !0 : e.response ? e.response.status >= 500 && e.response.status <= 599 || e.response.status === 429 : !1;
54
54
  }
55
55
  getKnockClientHeader() {
56
- return "Knock/ClientJS 0.16.2";
56
+ return "Knock/ClientJS 0.16.4";
57
57
  }
58
58
  }
59
59
  export {
@@ -1,40 +1,43 @@
1
- var f = Object.defineProperty;
2
- var S = (u, e, t) => e in u ? f(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
- var d = (u, e, t) => S(u, typeof e != "symbol" ? e + "" : e, t);
4
- import { Store as y } from "@tanstack/store";
5
- import { URLPattern as v } from "urlpattern-polyfill";
6
- import { byKey as G, mockDefaultGroup as _, formatFilters as l, findDefaultGroup as k, DEFAULT_GROUP_KEY as g, checkIfThrottled as w, SelectionResult as C } from "./helpers.mjs";
7
- const b = 50, I = 30 * 1e3, A = (u) => `/v1/users/${u}/guides`, p = (u, e = {}) => {
8
- const t = new C(), s = k(u.guideGroups);
1
+ var y = Object.defineProperty;
2
+ var S = (u, e, t) => e in u ? y(u, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : u[e] = t;
3
+ var c = (u, e, t) => S(u, typeof e != "symbol" ? e + "" : e, t);
4
+ import { Store as v } from "@tanstack/store";
5
+ import { URLPattern as G } from "urlpattern-polyfill";
6
+ import { byKey as _, mockDefaultGroup as C, formatFilters as g, findDefaultGroup as m, DEFAULT_GROUP_KEY as p, checkIfThrottled as b, SelectionResult as I } from "./helpers.mjs";
7
+ const A = 50, L = 30 * 1e3, l = () => {
8
+ if (typeof window < "u")
9
+ return window;
10
+ }, E = (u) => `/v1/users/${u}/guides`, k = (u, e = {}) => {
11
+ const t = new I(), s = m(u.guideGroups);
9
12
  if (!s) return t;
10
- const r = s.display_sequence, i = u.location;
11
- for (const [n, o] of r.entries()) {
13
+ const n = s.display_sequence, i = u.location;
14
+ for (const [r, o] of n.entries()) {
12
15
  const a = u.guides[o];
13
- !a || !L(a, { location: i, filters: e }) || t.set(n, a);
16
+ !a || !T(a, { location: i, filters: e }) || t.set(r, a);
14
17
  }
15
18
  return t.metadata = { guideGroup: s }, t;
16
- }, L = (u, { location: e, filters: t = {} }) => {
17
- if (t.type && t.type !== u.type || t.key && t.key !== u.key || u.steps.every((r) => !!r.message.archived_at))
19
+ }, T = (u, { location: e, filters: t = {} }) => {
20
+ if (t.type && t.type !== u.type || t.key && t.key !== u.key || u.steps.every((n) => !!n.message.archived_at))
18
21
  return !1;
19
22
  const s = u.activation_location_rules || [];
20
- return !(s.length > 0 && e && !s.reduce((i, n) => {
23
+ return !(s.length > 0 && e && !s.reduce((i, r) => {
21
24
  if (i === !1) return !1;
22
- switch (n.directive) {
25
+ switch (r.directive) {
23
26
  case "allow":
24
- return i === !0 || n.pattern.test(e) ? !0 : void 0;
27
+ return i === !0 || r.pattern.test(e) ? !0 : void 0;
25
28
  case "block":
26
- return n.pattern.test(e) ? !1 : i;
29
+ return r.pattern.test(e) ? !1 : i;
27
30
  }
28
31
  }, void 0));
29
32
  };
30
- class F {
31
- constructor(e, t, s = {}, r = {}) {
32
- d(this, "store");
33
+ class O {
34
+ constructor(e, t, s = {}, n = {}) {
35
+ c(this, "store");
33
36
  // Phoenix channels for real time guide updates over websocket
34
- d(this, "socket");
35
- d(this, "socketChannel");
36
- d(this, "socketChannelTopic");
37
- d(this, "socketEventTypes", [
37
+ c(this, "socket");
38
+ c(this, "socketChannel");
39
+ c(this, "socketChannelTopic");
40
+ c(this, "socketEventTypes", [
38
41
  "guide.added",
39
42
  "guide.updated",
40
43
  "guide.removed",
@@ -42,38 +45,40 @@ class F {
42
45
  "guide_group.updated"
43
46
  ]);
44
47
  // Original history methods to monkey patch, or restore in cleanups.
45
- d(this, "pushStateFn");
46
- d(this, "replaceStateFn");
48
+ c(this, "pushStateFn");
49
+ c(this, "replaceStateFn");
47
50
  // Guides that are competing to render are "staged" first without rendering
48
51
  // and ranked based on its relative order in the group over a duration of time
49
52
  // to resolve and render the prevailing one.
50
- d(this, "stage");
51
- d(this, "counterIntervalId");
53
+ c(this, "stage");
54
+ c(this, "counterIntervalId");
52
55
  // Define as an arrow func property to always bind this to the class instance.
53
- d(this, "handleLocationChange", () => {
54
- const e = window.location.href;
55
- this.store.state.location !== e && (this.knock.log(`[Guide] Handle Location change: ${e}`), this.setLocation(e));
56
+ c(this, "handleLocationChange", () => {
57
+ const e = l();
58
+ if (!(e != null && e.location)) return;
59
+ const t = e.location.href;
60
+ this.store.state.location !== t && (this.knock.log(`[Guide] Handle Location change: ${t}`), this.setLocation(t));
56
61
  });
57
- this.knock = e, this.channelId = t, this.targetParams = s, this.options = r;
58
- const { trackLocationFromWindow: i = !0 } = r, n = i ? window == null ? void 0 : window.location.href : void 0;
59
- this.store = new y({
62
+ this.knock = e, this.channelId = t, this.targetParams = s, this.options = n;
63
+ const { trackLocationFromWindow: i = !0 } = n, r = l(), o = i ? r == null ? void 0 : r.location.href : void 0;
64
+ this.store = new v({
60
65
  guideGroups: [],
61
66
  guideGroupDisplayLogs: {},
62
67
  guides: {},
63
68
  queries: {},
64
- location: n,
69
+ location: o,
65
70
  // Increment to update the state store and trigger re-selection.
66
71
  counter: 0
67
72
  });
68
- const { socket: o } = this.knock.client();
69
- this.socket = o, this.socketChannelTopic = `guides:${t}`, i && this.listenForLocationChangesFromWindow(), this.startCounterInterval(), this.knock.log("[Guide] Initialized a guide client");
73
+ const { socket: a } = this.knock.client();
74
+ this.socket = a, this.socketChannelTopic = `guides:${t}`, i && this.listenForLocationChangesFromWindow(), this.startCounterInterval(), this.knock.log("[Guide] Initialized a guide client");
70
75
  }
71
76
  incrementCounter() {
72
77
  this.knock.log("[Guide] Incrementing the counter"), this.store.setState((e) => ({ ...e, counter: e.counter + 1 }));
73
78
  }
74
79
  startCounterInterval() {
75
80
  const {
76
- throttleCheckInterval: e = I
81
+ throttleCheckInterval: e = L
77
82
  } = this.options;
78
83
  this.counterIntervalId = setInterval(() => {
79
84
  this.knock.log("[Guide] Counter interval tick"), !(this.stage && this.stage.status !== "closed") && this.incrementCounter();
@@ -87,31 +92,27 @@ class F {
87
92
  }
88
93
  async fetch(e) {
89
94
  this.knock.failIfNotAuthenticated(), this.knock.log("[Guide] Loading all eligible guides");
90
- const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), r = this.store.state.queries[s];
91
- if (r)
92
- return r;
93
- this.store.setState((n) => ({
94
- ...n,
95
- queries: { ...n.queries, [s]: { status: "loading" } }
95
+ const t = this.buildQueryParams(e == null ? void 0 : e.filters), s = this.formatQueryKey(t), n = this.store.state.queries[s];
96
+ if (n)
97
+ return n;
98
+ this.store.setState((r) => ({
99
+ ...r,
100
+ queries: { ...r.queries, [s]: { status: "loading" } }
96
101
  }));
97
102
  let i;
98
103
  try {
99
- const n = await this.knock.user.getGuides(this.channelId, t);
104
+ const r = await this.knock.user.getGuides(this.channelId, t);
100
105
  i = { status: "ok" };
101
- const {
102
- entries: o,
103
- guide_groups: a,
104
- guide_group_display_logs: h
105
- } = n;
106
- this.store.setState((c) => ({
107
- ...c,
108
- guideGroups: (a == null ? void 0 : a.length) > 0 ? a : [_(o)],
109
- guideGroupDisplayLogs: h,
110
- guides: G(o.map((m) => this.localCopy(m))),
111
- queries: { ...c.queries, [s]: i }
106
+ const { entries: o, guide_groups: a, guide_group_display_logs: h } = r;
107
+ this.store.setState((d) => ({
108
+ ...d,
109
+ guideGroups: (a == null ? void 0 : a.length) > 0 ? a : [C(o)],
110
+ guideGroupDisplayLogs: h || {},
111
+ guides: _(o.map((f) => this.localCopy(f))),
112
+ queries: { ...d.queries, [s]: i }
112
113
  }));
113
- } catch (n) {
114
- i = { status: "error", error: n }, this.store.setState((o) => ({
114
+ } catch (r) {
115
+ i = { status: "error", error: r }, this.store.setState((o) => ({
115
116
  ...o,
116
117
  queries: { ...o.queries, [s]: i }
117
118
  }));
@@ -123,7 +124,7 @@ class F {
123
124
  this.knock.failIfNotAuthenticated(), this.knock.log("[Guide] Subscribing to real time updates"), this.socket.isConnected() || this.socket.connect(), this.socketChannel && this.unsubscribe();
124
125
  const e = { ...this.targetParams, user_id: this.knock.userId }, t = this.socket.channel(this.socketChannelTopic, e);
125
126
  for (const s of this.socketEventTypes)
126
- t.on(s, (r) => this.handleSocketEvent(r));
127
+ t.on(s, (n) => this.handleSocketEvent(n));
127
128
  ["closed", "errored"].includes(t.state) && t.join(), this.socketChannel = t;
128
129
  }
129
130
  unsubscribe() {
@@ -159,34 +160,34 @@ class F {
159
160
  selectGuides(e, t = {}) {
160
161
  if (Object.keys(e.guides).length === 0)
161
162
  return [];
162
- this.knock.log(`[Guide] Selecting guides for: ${l(t)}`);
163
- const s = p(e, t);
163
+ this.knock.log(`[Guide] Selecting guides for: ${g(t)}`);
164
+ const s = k(e, t);
164
165
  return s.size === 0 ? (this.knock.log("[Guide] Selection returned zero result"), []) : [...s.values()];
165
166
  }
166
167
  selectGuide(e, t = {}) {
167
168
  if (Object.keys(e.guides).length === 0)
168
169
  return;
169
- this.knock.log(`[Guide] Selecting a guide for: ${l(t)}`);
170
- const s = p(e, t);
170
+ this.knock.log(`[Guide] Selecting a guide for: ${g(t)}`);
171
+ const s = k(e, t);
171
172
  if (s.size === 0) {
172
173
  this.knock.log("[Guide] Selection returned zero result");
173
174
  return;
174
175
  }
175
- const [r, i] = [...s][0];
176
+ const [n, i] = [...s][0];
176
177
  if (i.bypass_global_group_limit)
177
178
  return i;
178
- const n = k(e.guideGroups), o = e.guideGroupDisplayLogs[g];
179
- if (!(n && n.display_interval && o && w(
179
+ const r = m(e.guideGroups), o = e.guideGroupDisplayLogs[p];
180
+ if (!(r && r.display_interval && o && b(
180
181
  o,
181
- n.display_interval
182
+ r.display_interval
182
183
  )))
183
184
  switch (this.stage || (this.stage = this.openGroupStage()), this.stage.status) {
184
185
  case "open": {
185
- this.knock.log(`[Guide] Addng to the group stage: ${i.key}`), this.stage.ordered[r] = i.key;
186
+ this.knock.log(`[Guide] Addng to the group stage: ${i.key}`), this.stage.ordered[n] = i.key;
186
187
  return;
187
188
  }
188
189
  case "patch":
189
- return this.knock.log(`[Guide] Patching the group stage: ${i.key}`), this.stage.ordered[r] = i.key, this.stage.resolved === i.key ? i : void 0;
190
+ return this.knock.log(`[Guide] Patching the group stage: ${i.key}`), this.stage.ordered[n] = i.key, this.stage.resolved === i.key ? i : void 0;
190
191
  case "closed":
191
192
  return this.stage.resolved === i.key ? i : void 0;
192
193
  }
@@ -194,7 +195,7 @@ class F {
194
195
  openGroupStage() {
195
196
  this.knock.log("[Guide] Opening a new group stage");
196
197
  const {
197
- orderResolutionDuration: e = b
198
+ orderResolutionDuration: e = A
198
199
  } = this.options, t = setTimeout(() => {
199
200
  this.closePendingGroupStage(), this.incrementCounter();
200
201
  }, e);
@@ -261,7 +262,7 @@ class F {
261
262
  seen_at: (/* @__PURE__ */ new Date()).toISOString()
262
263
  });
263
264
  if (!s) return;
264
- const r = {
265
+ const n = {
265
266
  ...this.buildEngagementEventBaseParams(e, s),
266
267
  content: s.content,
267
268
  data: this.targetParams.data,
@@ -269,23 +270,23 @@ class F {
269
270
  };
270
271
  return this.knock.user.markGuideStepAs(
271
272
  "seen",
272
- r
273
+ n
273
274
  ), s;
274
275
  }
275
276
  async markAsInteracted(e, t, s) {
276
277
  this.knock.log(
277
278
  `[Guide] Marking as interacted (Guide key: ${e.key}, Step ref:${t.ref})`
278
279
  );
279
- const r = (/* @__PURE__ */ new Date()).toISOString(), i = this.setStepMessageAttrs(e.key, t.ref, {
280
- read_at: r,
281
- interacted_at: r
280
+ const n = (/* @__PURE__ */ new Date()).toISOString(), i = this.setStepMessageAttrs(e.key, t.ref, {
281
+ read_at: n,
282
+ interacted_at: n
282
283
  });
283
284
  if (!i) return;
284
- const n = {
285
+ const r = {
285
286
  ...this.buildEngagementEventBaseParams(e, i),
286
287
  metadata: s
287
288
  };
288
- return this.knock.user.markGuideStepAs("interacted", n), i;
289
+ return this.knock.user.markGuideStepAs("interacted", r), i;
289
290
  }
290
291
  async markAsArchived(e, t) {
291
292
  if (t.message.archived_at) return;
@@ -296,11 +297,11 @@ class F {
296
297
  archived_at: (/* @__PURE__ */ new Date()).toISOString()
297
298
  });
298
299
  if (!s) return;
299
- const r = this.buildEngagementEventBaseParams(e, s);
300
+ const n = this.buildEngagementEventBaseParams(e, s);
300
301
  return this.knock.user.markGuideStepAs(
301
302
  "archived",
302
303
  {
303
- ...r,
304
+ ...n,
304
305
  unthrottled: e.bypass_global_group_limit
305
306
  }
306
307
  ), s;
@@ -313,13 +314,13 @@ class F {
313
314
  ...e,
314
315
  // Get the next unarchived step.
315
316
  getStep() {
316
- return this.steps.find((r) => !r.message.archived_at);
317
+ return this.steps.find((n) => !n.message.archived_at);
317
318
  }
318
319
  };
319
- return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: r, ...i }) => {
320
- const n = {
320
+ return s.getStep = s.getStep.bind(s), s.steps = e.steps.map(({ message: n, ...i }) => {
321
+ const r = {
321
322
  ...i,
322
- message: { ...r },
323
+ message: { ...n },
323
324
  markAsSeen() {
324
325
  if (!this.message.seen_at)
325
326
  return t.markAsSeen(s, this);
@@ -332,17 +333,17 @@ class F {
332
333
  return t.markAsArchived(s, this);
333
334
  }
334
335
  };
335
- return n.markAsSeen = n.markAsSeen.bind(n), n.markAsInteracted = n.markAsInteracted.bind(n), n.markAsArchived = n.markAsArchived.bind(n), n;
336
- }), s.activation_location_rules = e.activation_location_rules.map((r) => ({
337
- ...r,
338
- pattern: new v({ pathname: r.pathname })
336
+ return r.markAsSeen = r.markAsSeen.bind(r), r.markAsInteracted = r.markAsInteracted.bind(r), r.markAsArchived = r.markAsArchived.bind(r), r;
337
+ }), s.activation_location_rules = e.activation_location_rules.map((n) => ({
338
+ ...n,
339
+ pattern: new G({ pathname: n.pathname })
339
340
  })), s;
340
341
  }
341
342
  buildQueryParams(e = {}) {
342
343
  const t = { ...this.targetParams, ...e };
343
344
  let s = Object.fromEntries(
344
345
  Object.entries(t).filter(
345
- ([r, i]) => i != null
346
+ ([n, i]) => i != null
346
347
  )
347
348
  );
348
349
  return s = s.data ? { ...s, data: JSON.stringify(s.data) } : s, s;
@@ -350,22 +351,22 @@ class F {
350
351
  formatQueryKey(e) {
351
352
  const s = Object.keys(e).sort().map(
352
353
  (i) => `${encodeURIComponent(i)}=${encodeURIComponent(e[i])}`
353
- ).join("&"), r = A(this.knock.userId);
354
- return s ? `${r}?${s}` : r;
354
+ ).join("&"), n = E(this.knock.userId);
355
+ return s ? `${n}?${s}` : n;
355
356
  }
356
357
  setStepMessageAttrs(e, t, s) {
357
- let r;
358
+ let n;
358
359
  return s.archived_at && this.clearGroupStage(), this.store.setState((i) => {
359
- const n = i.guides[e];
360
- if (!n) return i;
361
- const o = n.steps.map((c) => (c.ref !== t || (c.message = { ...c.message, ...s }, r = c), c));
362
- n.steps = o;
363
- const a = { ...i.guides, [n.key]: n }, h = s.archived_at && !n.bypass_global_group_limit ? {
360
+ const r = i.guides[e];
361
+ if (!r) return i;
362
+ const o = r.steps.map((d) => (d.ref !== t || (d.message = { ...d.message, ...s }, n = d), d));
363
+ r.steps = o;
364
+ const a = { ...i.guides, [r.key]: r }, h = s.archived_at && !r.bypass_global_group_limit ? {
364
365
  ...i.guideGroupDisplayLogs,
365
- [g]: s.archived_at
366
+ [p]: s.archived_at
366
367
  } : i.guideGroupDisplayLogs;
367
368
  return { ...i, guides: a, guideGroupDisplayLogs: h };
368
- }), r;
369
+ }), n;
369
370
  }
370
371
  buildEngagementEventBaseParams(e, t) {
371
372
  return {
@@ -380,61 +381,63 @@ class F {
380
381
  this.patchClosedGroupStage();
381
382
  const t = this.localCopy(e.guide);
382
383
  this.store.setState((s) => {
383
- const r = { ...s.guides, [t.key]: t };
384
- return { ...s, guides: r };
384
+ const n = { ...s.guides, [t.key]: t };
385
+ return { ...s, guides: n };
385
386
  });
386
387
  }
387
388
  removeGuide({ data: e }) {
388
389
  this.patchClosedGroupStage(), this.store.setState((t) => {
389
- const { [e.guide.key]: s, ...r } = t.guides;
390
- return { ...t, guides: r };
390
+ const { [e.guide.key]: s, ...n } = t.guides;
391
+ return { ...t, guides: n };
391
392
  });
392
393
  }
393
394
  addOrReplaceGuideGroup({
394
395
  data: e
395
396
  }) {
396
397
  this.patchClosedGroupStage(), this.store.setState((t) => {
397
- const s = [e.guide_group], r = e.guide_group.display_sequence_unthrottled || [], i = e.guide_group.display_sequence_throttled || [];
398
- let n = t.guides;
399
- return n = r.reduce((o, a) => {
398
+ const s = [e.guide_group], n = e.guide_group.display_sequence_unthrottled || [], i = e.guide_group.display_sequence_throttled || [];
399
+ let r = t.guides;
400
+ return r = n.reduce((o, a) => {
400
401
  if (!o[a]) return o;
401
402
  const h = { ...o[a], bypass_global_group_limit: !0 };
402
403
  return { ...o, [a]: h };
403
- }, n), n = i.reduce((o, a) => {
404
+ }, r), r = i.reduce((o, a) => {
404
405
  if (!o[a]) return o;
405
406
  const h = { ...o[a], bypass_global_group_limit: !1 };
406
407
  return { ...o, [a]: h };
407
- }, n), { ...t, guides: n, guideGroups: s };
408
+ }, r), { ...t, guides: r, guideGroups: s };
408
409
  });
409
410
  }
410
411
  listenForLocationChangesFromWindow() {
411
- if (window != null && window.history) {
412
- window.addEventListener("popstate", this.handleLocationChange), window.addEventListener("hashchange", this.handleLocationChange);
413
- const e = window.history.pushState, t = window.history.replaceState;
414
- window.history.pushState = new Proxy(e, {
415
- apply: (s, r, i) => {
416
- Reflect.apply(s, r, i), setTimeout(() => {
412
+ const e = l();
413
+ if (e != null && e.history) {
414
+ e.addEventListener("popstate", this.handleLocationChange), e.addEventListener("hashchange", this.handleLocationChange);
415
+ const t = e.history.pushState, s = e.history.replaceState;
416
+ e.history.pushState = new Proxy(t, {
417
+ apply: (n, i, r) => {
418
+ Reflect.apply(n, i, r), setTimeout(() => {
417
419
  this.handleLocationChange();
418
420
  }, 0);
419
421
  }
420
- }), window.history.replaceState = new Proxy(t, {
421
- apply: (s, r, i) => {
422
- Reflect.apply(s, r, i), setTimeout(() => {
422
+ }), e.history.replaceState = new Proxy(s, {
423
+ apply: (n, i, r) => {
424
+ Reflect.apply(n, i, r), setTimeout(() => {
423
425
  this.handleLocationChange();
424
426
  }, 0);
425
427
  }
426
- }), this.pushStateFn = e, this.replaceStateFn = t;
428
+ }), this.pushStateFn = t, this.replaceStateFn = s;
427
429
  } else
428
430
  this.knock.log(
429
431
  "[Guide] Unable to access the `window.history` object to detect location changes"
430
432
  );
431
433
  }
432
434
  removeEventListeners() {
433
- window.removeEventListener("popstate", this.handleLocationChange), window.removeEventListener("hashchange", this.handleLocationChange), this.pushStateFn && (window.history.pushState = this.pushStateFn, this.pushStateFn = void 0), this.replaceStateFn && (window.history.replaceState = this.replaceStateFn, this.replaceStateFn = void 0);
435
+ const e = l();
436
+ e != null && e.history && (e.removeEventListener("popstate", this.handleLocationChange), e.removeEventListener("hashchange", this.handleLocationChange), this.pushStateFn && (e.history.pushState = this.pushStateFn, this.pushStateFn = void 0), this.replaceStateFn && (e.history.replaceState = this.replaceStateFn, this.replaceStateFn = void 0));
434
437
  }
435
438
  }
436
439
  export {
437
- F as KnockGuideClient,
438
- A as guidesApiRootPath
440
+ O as KnockGuideClient,
441
+ E as guidesApiRootPath
439
442
  };
440
443
  //# sourceMappingURL=client.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.mjs","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n\n const location = trackLocationFromWindow\n ? window?.location.href\n : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const {\n entries,\n guide_groups: groups,\n guide_group_display_logs: guideGroupDisplayLogs,\n } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs,\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n newChannel.join();\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const href = window.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n if (window?.history) {\n // 1. Listen for browser back/forward button clicks.\n window.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n window.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = window.history.pushState;\n const replaceStateFn = window.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n window.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n window.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n window.removeEventListener(\"popstate\", this.handleLocationChange);\n window.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n window.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n window.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guideGroupDisplayLogs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":";;;;;;AA6CA,MAAMA,IAAoC,IAIpCC,IAAqC,KAAK,KAEnCC,IAAoB,CAACC,MAChC,aAAaA,CAAM,WAEfC,IAAS,CAACC,GAAmBC,IAA8B,OAAO;AAEhE,QAAAC,IAAS,IAAIC,EAAgB,GAE7BC,IAAeC,EAAiBL,EAAM,WAAW;AACnD,MAAA,CAACI,EAAqB,QAAAF;AAE1B,QAAMI,IAAkBF,EAAa,kBAC/BG,IAAWP,EAAM;AAEvB,aAAW,CAACQ,GAAOC,CAAQ,KAAKH,EAAgB,WAAW;AACnD,UAAAI,IAAQV,EAAM,OAAOS,CAAQ;AAInC,IAHI,CAACC,KAGD,CADaC,EAAUD,GAAO,EAAE,UAAAH,GAAU,SAAAN,GAAS,KAGhDC,EAA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KAAK,oBAAoB,GAGtD,OAAA,iBAAiB,cAAc,KAAK,oBAAoB;AAGzD,YAAAG,IAAc,OAAO,QAAQ,WAC7BC,IAAiB,OAAO,QAAQ;AAGtC,aAAO,QAAQ,YAAY,IAAI,MAAMD,GAAa;AAAA,QAChD,OAAO,CAACE,GAAQC,GAASC,MAAS;AACxB,kBAAA,MAAMF,GAAQC,GAASC,CAAI,GACnC,WAAW,MAAM;AACf,iBAAK,qBAAqB;AAAA,aACzB,CAAC;AAAA,QAAA;AAAA,MACN,CACD,GACD,OAAO,QAAQ,eAAe,IAAI,MAAMH,GAAgB;AAAA,QACtD,OAAO,CAACC,GAAQC,GAASC,MAAS;AACxB,kBAAA,MAAMF,GAAQC,GAASC,CAAI,GACnC,WAAW,MAAM;AACf,iBAAK,qBAAqB;AAAA,aACzB,CAAC;AAAA,QAAA;AAAA,MACN,CACD,GAGD,KAAK,cAAcJ,GACnB,KAAK,iBAAiBC;AAAA,IAAA;AAEtB,WAAK,MAAM;AAAA,QACT;AAAA,MACF;AAAA,EACF;AAAA,EAGM,uBAAuB;AACtB,WAAA,oBAAoB,YAAY,KAAK,oBAAoB,GACzD,OAAA,oBAAoB,cAAc,KAAK,oBAAoB,GAE9D,KAAK,gBACA,OAAA,QAAQ,YAAY,KAAK,aAChC,KAAK,cAAc,SAEjB,KAAK,mBACA,OAAA,QAAQ,eAAe,KAAK,gBACnC,KAAK,iBAAiB;AAAA,EACxB;AAEJ;"}
1
+ {"version":3,"file":"client.mjs","sources":["../../../../src/clients/guide/client.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\nimport { Channel, Socket } from \"phoenix\";\nimport { URLPattern } from \"urlpattern-polyfill\";\n\nimport Knock from \"../../knock\";\n\nimport {\n DEFAULT_GROUP_KEY,\n SelectionResult,\n byKey,\n checkIfThrottled,\n findDefaultGroup,\n formatFilters,\n mockDefaultGroup,\n} from \"./helpers\";\nimport {\n ConstructorOpts,\n GetGuidesQueryParams,\n GetGuidesResponse,\n GroupStage,\n GuideAddedEvent,\n GuideData,\n GuideGroupAddedEvent,\n GuideGroupUpdatedEvent,\n GuideRemovedEvent,\n GuideSocketEvent,\n GuideStepData,\n GuideUpdatedEvent,\n KnockGuide,\n KnockGuideStep,\n MarkAsArchivedParams,\n MarkAsInteractedParams,\n MarkAsSeenParams,\n MarkGuideAsResponse,\n QueryFilterParams,\n QueryStatus,\n SelectFilterParams,\n StepMessageState,\n StoreState,\n TargetParams,\n} from \"./types\";\n\n// How long to wait until we resolve the guides order and determine the\n// prevailing guide.\nconst DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds\n\n// How often we should increment the counter to refresh the store state and\n// trigger subscribed callbacks.\nconst DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds\n\n// Return the global window object if defined, so to safely guard against SSR.\nconst checkForWindow = () => {\n if (typeof window !== \"undefined\") {\n return window;\n }\n};\n\nexport const guidesApiRootPath = (userId: string | undefined | null) =>\n `/v1/users/${userId}/guides`;\n\nconst select = (state: StoreState, filters: SelectFilterParams = {}) => {\n // A map of selected guides as values, with its order index as keys.\n const result = new SelectionResult();\n\n const defaultGroup = findDefaultGroup(state.guideGroups);\n if (!defaultGroup) return result;\n\n const displaySequence = defaultGroup.display_sequence;\n const location = state.location;\n\n for (const [index, guideKey] of displaySequence.entries()) {\n const guide = state.guides[guideKey];\n if (!guide) continue;\n\n const affirmed = predicate(guide, { location, filters });\n if (!affirmed) continue;\n\n result.set(index, guide);\n }\n\n result.metadata = { guideGroup: defaultGroup };\n return result;\n};\n\ntype PredicateOpts = {\n location?: string | undefined;\n filters?: SelectFilterParams | undefined;\n};\n\nconst predicate = (\n guide: KnockGuide,\n { location, filters = {} }: PredicateOpts,\n) => {\n if (filters.type && filters.type !== guide.type) {\n return false;\n }\n\n if (filters.key && filters.key !== guide.key) {\n return false;\n }\n\n if (guide.steps.every((s) => !!s.message.archived_at)) {\n return false;\n }\n\n const locationRules = guide.activation_location_rules || [];\n\n if (locationRules.length > 0 && location) {\n const allowed = locationRules.reduce<boolean | undefined>((acc, rule) => {\n // Any matched block rule prevails so no need to evaluate further\n // as soon as there is one.\n if (acc === false) return false;\n\n // At this point we either have a matched allow rule (acc is true),\n // or no matched rule found yet (acc is undefined).\n\n switch (rule.directive) {\n case \"allow\": {\n // No need to evaluate more allow rules once we matched one\n // since any matched allowed rule means allow.\n if (acc === true) return true;\n\n const matched = rule.pattern.test(location);\n return matched ? true : undefined;\n }\n\n case \"block\": {\n // Always test block rules (unless already matched to block)\n // because they'd prevail over matched allow rules.\n const matched = rule.pattern.test(location);\n return matched ? false : acc;\n }\n }\n }, undefined);\n\n if (!allowed) return false;\n }\n\n return true;\n};\n\nexport class KnockGuideClient {\n public store: Store<StoreState, (state: StoreState) => StoreState>;\n\n // Phoenix channels for real time guide updates over websocket\n private socket: Socket | undefined;\n private socketChannel: Channel | undefined;\n private socketChannelTopic: string;\n private socketEventTypes = [\n \"guide.added\",\n \"guide.updated\",\n \"guide.removed\",\n \"guide_group.added\",\n \"guide_group.updated\",\n ];\n\n // Original history methods to monkey patch, or restore in cleanups.\n private pushStateFn: History[\"pushState\"] | undefined;\n private replaceStateFn: History[\"replaceState\"] | undefined;\n\n // Guides that are competing to render are \"staged\" first without rendering\n // and ranked based on its relative order in the group over a duration of time\n // to resolve and render the prevailing one.\n private stage: GroupStage | undefined;\n\n private counterIntervalId: ReturnType<typeof setInterval> | undefined;\n\n constructor(\n readonly knock: Knock,\n readonly channelId: string,\n readonly targetParams: TargetParams = {},\n readonly options: ConstructorOpts = {},\n ) {\n const { trackLocationFromWindow = true } = options;\n const win = checkForWindow();\n\n const location = trackLocationFromWindow ? win?.location.href : undefined;\n\n this.store = new Store<StoreState>({\n guideGroups: [],\n guideGroupDisplayLogs: {},\n guides: {},\n queries: {},\n location,\n // Increment to update the state store and trigger re-selection.\n counter: 0,\n });\n\n // In server environments we might not have a socket connection.\n const { socket: maybeSocket } = this.knock.client();\n this.socket = maybeSocket;\n this.socketChannelTopic = `guides:${channelId}`;\n\n if (trackLocationFromWindow) {\n this.listenForLocationChangesFromWindow();\n }\n\n // Start the counter loop to increment at an interval.\n this.startCounterInterval();\n\n this.knock.log(\"[Guide] Initialized a guide client\");\n }\n\n private incrementCounter() {\n this.knock.log(\"[Guide] Incrementing the counter\");\n this.store.setState((state) => ({ ...state, counter: state.counter + 1 }));\n }\n\n private startCounterInterval() {\n const {\n throttleCheckInterval: delay = DEFAULT_COUNTER_INCREMENT_INTERVAL,\n } = this.options;\n\n this.counterIntervalId = setInterval(() => {\n this.knock.log(\"[Guide] Counter interval tick\");\n if (this.stage && this.stage.status !== \"closed\") return;\n\n this.incrementCounter();\n }, delay);\n }\n\n private clearCounterInterval() {\n if (this.counterIntervalId) {\n clearInterval(this.counterIntervalId);\n this.counterIntervalId = undefined;\n }\n }\n\n cleanup() {\n this.unsubscribe();\n this.removeEventListeners();\n this.clearGroupStage();\n this.clearCounterInterval();\n }\n\n async fetch(opts?: { filters?: QueryFilterParams }) {\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Loading all eligible guides\");\n\n const queryParams = this.buildQueryParams(opts?.filters);\n const queryKey = this.formatQueryKey(queryParams);\n\n // If already fetched before, then noop.\n const maybeQueryStatus = this.store.state.queries[queryKey];\n if (maybeQueryStatus) {\n return maybeQueryStatus;\n }\n\n // Mark this query status as loading.\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: { status: \"loading\" } },\n }));\n\n let queryStatus: QueryStatus;\n try {\n const data = await this.knock.user.getGuides<\n GetGuidesQueryParams,\n GetGuidesResponse\n >(this.channelId, queryParams);\n queryStatus = { status: \"ok\" };\n\n const { entries, guide_groups: groups, guide_group_display_logs } = data;\n\n this.store.setState((state) => ({\n ...state,\n guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],\n guideGroupDisplayLogs: guide_group_display_logs || {},\n guides: byKey(entries.map((g) => this.localCopy(g))),\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n } catch (e) {\n queryStatus = { status: \"error\", error: e as Error };\n\n this.store.setState((state) => ({\n ...state,\n queries: { ...state.queries, [queryKey]: queryStatus },\n }));\n }\n\n return queryStatus;\n }\n\n subscribe() {\n if (!this.socket) return;\n this.knock.failIfNotAuthenticated();\n this.knock.log(\"[Guide] Subscribing to real time updates\");\n\n // Ensure a live socket connection if not yet connected.\n if (!this.socket.isConnected()) {\n this.socket.connect();\n }\n\n // If there's an existing connected channel, then disconnect.\n if (this.socketChannel) {\n this.unsubscribe();\n }\n\n // Join the channel topic and subscribe to supported events.\n const params = { ...this.targetParams, user_id: this.knock.userId };\n const newChannel = this.socket.channel(this.socketChannelTopic, params);\n\n for (const eventType of this.socketEventTypes) {\n newChannel.on(eventType, (payload) => this.handleSocketEvent(payload));\n }\n\n if ([\"closed\", \"errored\"].includes(newChannel.state)) {\n newChannel.join();\n }\n\n // Track the joined channel.\n this.socketChannel = newChannel;\n }\n\n unsubscribe() {\n if (!this.socketChannel) return;\n this.knock.log(\"[Guide] Unsubscribing from real time updates\");\n\n // Unsubscribe from the socket events and leave the channel.\n for (const eventType of this.socketEventTypes) {\n this.socketChannel.off(eventType);\n }\n this.socketChannel.leave();\n\n // Unset the channel.\n this.socketChannel = undefined;\n }\n\n private handleSocketEvent(payload: GuideSocketEvent) {\n const { event, data } = payload;\n\n switch (event) {\n case \"guide.added\":\n return this.addOrReplaceGuide(payload);\n\n case \"guide.updated\":\n return data.eligible\n ? this.addOrReplaceGuide(payload)\n : this.removeGuide(payload);\n\n case \"guide.removed\":\n return this.removeGuide(payload);\n\n case \"guide_group.added\":\n case \"guide_group.updated\":\n return this.addOrReplaceGuideGroup(payload);\n\n default:\n return;\n }\n }\n\n setLocation(href: string) {\n // Make sure to clear out the stage.\n this.clearGroupStage();\n\n this.store.setState((state) => ({ ...state, location: href }));\n }\n\n //\n // Store selector\n //\n\n selectGuides(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return [];\n }\n this.knock.log(`[Guide] Selecting guides for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return [];\n }\n\n // Return all selected guides, since we cannot apply the one-at-a-time limit\n // or throttle settings, but rather defer to the caller to decide which ones\n // to render. Note\n return [...result.values()];\n }\n\n selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n if (Object.keys(state.guides).length === 0) {\n return undefined;\n }\n this.knock.log(`[Guide] Selecting a guide for: ${formatFilters(filters)}`);\n\n const result = select(state, filters);\n\n if (result.size === 0) {\n this.knock.log(\"[Guide] Selection returned zero result\");\n return undefined;\n }\n\n const [index, guide] = [...result][0]!;\n\n // If a guide ignores the group limit, then return immediately to render\n // always.\n if (guide.bypass_global_group_limit) {\n return guide;\n }\n\n // Check if inside the throttle window (i.e. throttled) and if so stop and\n // return undefined.\n const defaultGroup = findDefaultGroup(state.guideGroups);\n const throttleWindowStartedAt =\n state.guideGroupDisplayLogs[DEFAULT_GROUP_KEY];\n\n if (\n defaultGroup &&\n defaultGroup.display_interval &&\n throttleWindowStartedAt\n ) {\n const throttled = checkIfThrottled(\n throttleWindowStartedAt,\n defaultGroup.display_interval,\n );\n if (throttled) return undefined;\n }\n\n // Starting here to the end of this method represents the core logic of how\n // \"group stage\" works. It provides a mechanism for 1) figuring out which\n // guide components are about to render on a page, 2) determining which\n // among them ranks highest in the configured display sequence, and 3)\n // returning only the prevailing guide to render at a time.\n //\n // Imagine N number of components that use the `useGuide()` hook which\n // calls this `selectGuide()` method, and the logic works like this:\n // * The first time this method is called, we don't have an \"open\" group\n // stage, so we open one (this occurs when a new page/route is rendering).\n // * While it is open, we record which guide was selected and its order\n // index from each call, but we do NOT return any guide to render yet.\n // * When a group stage opens, it schedules a timer to close itself. How\n // long this timer waits is configurable. Note, `setTimeout` with 0\n // delay seems to work well for React apps, where we \"yield\" to React\n // for one render cycle and close the group right after.\n // * When a group stage closes, we evaluate which guides were selected and\n // recorded, then determine the winning guide (i.e. the one with the\n // lowest order index value).\n // * Then increment the internal counter to trigger a store state update,\n // which allows `useGuide()` and `selectGuide()` to re-run. This second\n // round of `selectGuide()` calls, occurring when the group stage is\n // closed, results in returning the prevailing guide.\n // * Whenever a user navigates to a new page, we repeat the same process\n // above.\n // * There's a third status called \"patch,\" which is for handling real-time\n // updates received from the API. It's similar to the \"open\" to \"closed\"\n // flow, except we keep the resolved guide in place while we recalculate.\n // This is done so that we don't cause flickers or CLS.\n if (!this.stage) {\n this.stage = this.openGroupStage(); // Assign here to make tsc happy\n }\n\n switch (this.stage.status) {\n case \"open\": {\n this.knock.log(`[Guide] Addng to the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return undefined;\n }\n\n case \"patch\": {\n this.knock.log(`[Guide] Patching the group stage: ${guide.key}`);\n this.stage.ordered[index] = guide.key;\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n\n case \"closed\": {\n return this.stage.resolved === guide.key ? guide : undefined;\n }\n }\n }\n\n private openGroupStage() {\n this.knock.log(\"[Guide] Opening a new group stage\");\n\n const {\n orderResolutionDuration: delay = DEFAULT_ORDER_RESOLUTION_DURATION,\n } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n this.stage = {\n status: \"open\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n // Close the current non-closed stage to resolve the prevailing guide up next\n // for display amongst the ones that have been staged.\n private closePendingGroupStage() {\n if (!this.stage || this.stage.status === \"closed\") return;\n\n this.knock.log(\"[Guide] Closing the current group stage\");\n\n // Should have been cleared already since this method should be called as a\n // callback to a setTimeout, but just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"closed\",\n resolved: this.stage.ordered.find((x) => x !== undefined),\n timeoutId: null,\n };\n\n return this.stage;\n }\n\n // Set the current closed stage status to \"patch\" to allow re-running\n // selections and re-building a group stage with the latest/updated state,\n // while keeping the currently resolved guide in place so that it stays\n // rendered until we are ready to resolve the updated stage and re-render.\n // Note, must be called ahead of updating the state store.\n private patchClosedGroupStage() {\n if (this.stage?.status !== \"closed\") return;\n\n this.knock.log(\"[Guide] Patching the current group stage\");\n\n const { orderResolutionDuration: delay = 0 } = this.options;\n\n const timeoutId = setTimeout(() => {\n this.closePendingGroupStage();\n this.incrementCounter();\n }, delay);\n\n // Just to be safe.\n this.ensureClearTimeout();\n\n this.stage = {\n ...this.stage,\n status: \"patch\",\n ordered: [],\n timeoutId,\n };\n\n return this.stage;\n }\n\n private clearGroupStage() {\n if (!this.stage) return;\n\n this.knock.log(\"[Guide] Clearing the current group stage\");\n\n this.ensureClearTimeout();\n this.stage = undefined;\n }\n\n private ensureClearTimeout() {\n if (this.stage?.timeoutId) {\n clearTimeout(this.stage.timeoutId);\n }\n }\n\n // Test helper that opens and closes the group stage to return the select\n // result immediately.\n private _selectGuide(state: StoreState, filters: SelectFilterParams = {}) {\n this.openGroupStage();\n\n this.selectGuide(state, filters);\n this.closePendingGroupStage();\n\n return this.selectGuide(state, filters);\n }\n\n //\n // Engagement event handlers\n //\n // Make an optimistic update on the client side first, then send an engagement\n // event to the backend.\n //\n\n async markAsSeen(guide: GuideData, step: GuideStepData) {\n if (step.message.seen_at) return;\n\n this.knock.log(\n `[Guide] Marking as seen (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n seen_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n content: updatedStep.content,\n data: this.targetParams.data,\n tenant: this.targetParams.tenant,\n };\n\n this.knock.user.markGuideStepAs<MarkAsSeenParams, MarkGuideAsResponse>(\n \"seen\",\n params,\n );\n\n return updatedStep;\n }\n\n async markAsInteracted(\n guide: GuideData,\n step: GuideStepData,\n metadata?: GenericData,\n ) {\n this.knock.log(\n `[Guide] Marking as interacted (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const ts = new Date().toISOString();\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n read_at: ts,\n interacted_at: ts,\n });\n if (!updatedStep) return;\n\n const params = {\n ...this.buildEngagementEventBaseParams(guide, updatedStep),\n metadata,\n };\n\n this.knock.user.markGuideStepAs<\n MarkAsInteractedParams,\n MarkGuideAsResponse\n >(\"interacted\", params);\n\n return updatedStep;\n }\n\n async markAsArchived(guide: GuideData, step: GuideStepData) {\n if (step.message.archived_at) return;\n\n this.knock.log(\n `[Guide] Marking as archived (Guide key: ${guide.key}, Step ref:${step.ref})`,\n );\n\n const updatedStep = this.setStepMessageAttrs(guide.key, step.ref, {\n archived_at: new Date().toISOString(),\n });\n if (!updatedStep) return;\n\n const params = this.buildEngagementEventBaseParams(guide, updatedStep);\n\n this.knock.user.markGuideStepAs<MarkAsArchivedParams, MarkGuideAsResponse>(\n \"archived\",\n {\n ...params,\n unthrottled: guide.bypass_global_group_limit,\n },\n );\n\n return updatedStep;\n }\n\n //\n // Helpers\n //\n\n private localCopy(remoteGuide: GuideData) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n\n // Build a local copy with helper methods added.\n const localGuide = {\n ...remoteGuide,\n // Get the next unarchived step.\n getStep() {\n return this.steps.find((s) => !s.message.archived_at);\n },\n } as KnockGuide;\n\n localGuide.getStep = localGuide.getStep.bind(localGuide);\n\n localGuide.steps = remoteGuide.steps.map(({ message, ...rest }) => {\n const localStep = {\n ...rest,\n message: { ...message },\n markAsSeen() {\n // Send a seen event if it has not been previously seen.\n if (this.message.seen_at) return;\n return self.markAsSeen(localGuide, this);\n },\n markAsInteracted({ metadata }: { metadata?: GenericData } = {}) {\n // Always send an interaction event through.\n return self.markAsInteracted(localGuide, this, metadata);\n },\n markAsArchived() {\n // Send an archived event if it has not been previously archived.\n if (this.message.archived_at) return;\n return self.markAsArchived(localGuide, this);\n },\n };\n\n // Bind all engagement action handler methods to the local step object so\n // they can operate on itself.\n localStep.markAsSeen = localStep.markAsSeen.bind(localStep);\n localStep.markAsInteracted = localStep.markAsInteracted.bind(localStep);\n localStep.markAsArchived = localStep.markAsArchived.bind(localStep);\n\n return localStep;\n });\n\n localGuide.activation_location_rules =\n remoteGuide.activation_location_rules.map((rule) => {\n return {\n ...rule,\n pattern: new URLPattern({ pathname: rule.pathname }),\n };\n });\n\n return localGuide;\n }\n\n private buildQueryParams(filterParams: QueryFilterParams = {}) {\n // Combine the target params with the given filter params.\n const combinedParams = { ...this.targetParams, ...filterParams };\n\n // Prune out any keys that have an undefined or null value.\n let params = Object.fromEntries(\n Object.entries(combinedParams).filter(\n ([_k, v]) => v !== undefined && v !== null,\n ),\n );\n\n // Encode target data as a JSON string, if provided.\n params = params.data\n ? { ...params, data: JSON.stringify(params.data) }\n : params;\n\n return params as GetGuidesQueryParams;\n }\n\n private formatQueryKey(queryParams: GenericData) {\n const sortedKeys = Object.keys(queryParams).sort();\n\n const queryStr = sortedKeys\n .map(\n (key) =>\n `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`,\n )\n .join(\"&\");\n\n const basePath = guidesApiRootPath(this.knock.userId);\n return queryStr ? `${basePath}?${queryStr}` : basePath;\n }\n\n private setStepMessageAttrs(\n guideKey: string,\n stepRef: string,\n attrs: Partial<StepMessageState>,\n ) {\n let updatedStep: KnockGuideStep | undefined;\n\n // If we are marking as archived, clear the group stage so we can render\n // the next guide in the group.\n if (attrs.archived_at) {\n this.clearGroupStage();\n }\n\n this.store.setState((state) => {\n const guide = state.guides[guideKey];\n if (!guide) return state;\n\n const steps = guide.steps.map((step) => {\n if (step.ref !== stepRef) return step;\n\n // Mutate in place and maintain the same obj ref so to make it easier\n // to use in hook deps.\n step.message = { ...step.message, ...attrs };\n updatedStep = step;\n\n return step;\n });\n // Mutate in place and maintain the same obj ref.\n guide.steps = steps;\n const guides = { ...state.guides, [guide.key]: guide };\n\n // If the guide is subject to throttled settings and we are marking as\n // archived, then update the display logs to start a new throttle window.\n const guideGroupDisplayLogs =\n attrs.archived_at && !guide.bypass_global_group_limit\n ? {\n ...state.guideGroupDisplayLogs,\n [DEFAULT_GROUP_KEY]: attrs.archived_at,\n }\n : state.guideGroupDisplayLogs;\n\n return { ...state, guides, guideGroupDisplayLogs };\n });\n\n return updatedStep;\n }\n\n private buildEngagementEventBaseParams(\n guide: GuideData,\n step: GuideStepData,\n ) {\n return {\n message_id: step.message.id,\n channel_id: guide.channel_id,\n guide_key: guide.key,\n guide_id: guide.id,\n guide_step_ref: step.ref,\n };\n }\n\n private addOrReplaceGuide({ data }: GuideAddedEvent | GuideUpdatedEvent) {\n this.patchClosedGroupStage();\n\n const guide = this.localCopy(data.guide);\n\n this.store.setState((state) => {\n const guides = { ...state.guides, [guide.key]: guide };\n\n return { ...state, guides };\n });\n }\n\n private removeGuide({ data }: GuideUpdatedEvent | GuideRemovedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n const { [data.guide.key]: _, ...rest } = state.guides;\n return { ...state, guides: rest };\n });\n }\n\n private addOrReplaceGuideGroup({\n data,\n }: GuideGroupAddedEvent | GuideGroupUpdatedEvent) {\n this.patchClosedGroupStage();\n\n this.store.setState((state) => {\n // Currently we only support a single default global group, so we can just\n // update the list with the added/updated group.\n const guideGroups = [data.guide_group];\n\n // A guide group event can include lists of unthrottled vs throttled guide\n // keys which we can use to bulk update the guides in the store already.\n const unthrottled = data.guide_group.display_sequence_unthrottled || [];\n const throttled = data.guide_group.display_sequence_throttled || [];\n\n let guides = state.guides;\n\n guides = unthrottled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: true };\n return { ...acc, [key]: guide };\n }, guides);\n\n guides = throttled.reduce((acc, key) => {\n if (!acc[key]) return acc;\n const guide = { ...acc[key], bypass_global_group_limit: false };\n return { ...acc, [key]: guide };\n }, guides);\n\n return { ...state, guides, guideGroups };\n });\n }\n\n // Define as an arrow func property to always bind this to the class instance.\n private handleLocationChange = () => {\n const win = checkForWindow();\n if (!win?.location) return;\n\n const href = win.location.href;\n if (this.store.state.location === href) return;\n\n this.knock.log(`[Guide] Handle Location change: ${href}`);\n this.setLocation(href);\n };\n\n private listenForLocationChangesFromWindow() {\n const win = checkForWindow();\n if (win?.history) {\n // 1. Listen for browser back/forward button clicks.\n win.addEventListener(\"popstate\", this.handleLocationChange);\n\n // 2. Listen for hash changes in case it's used for routing.\n win.addEventListener(\"hashchange\", this.handleLocationChange);\n\n // 3. Monkey-patch history methods to catch programmatic navigation.\n const pushStateFn = win.history.pushState;\n const replaceStateFn = win.history.replaceState;\n\n // Use setTimeout to allow the browser state to potentially settle.\n win.history.pushState = new Proxy(pushStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n win.history.replaceState = new Proxy(replaceStateFn, {\n apply: (target, history, args) => {\n Reflect.apply(target, history, args);\n setTimeout(() => {\n this.handleLocationChange();\n }, 0);\n },\n });\n\n // 4. Keep refs to the original handlers so we can restore during cleanup.\n this.pushStateFn = pushStateFn;\n this.replaceStateFn = replaceStateFn;\n } else {\n this.knock.log(\n \"[Guide] Unable to access the `window.history` object to detect location changes\",\n );\n }\n }\n\n private removeEventListeners() {\n const win = checkForWindow();\n if (!win?.history) return;\n\n win.removeEventListener(\"popstate\", this.handleLocationChange);\n win.removeEventListener(\"hashchange\", this.handleLocationChange);\n\n if (this.pushStateFn) {\n win.history.pushState = this.pushStateFn;\n this.pushStateFn = undefined;\n }\n if (this.replaceStateFn) {\n win.history.replaceState = this.replaceStateFn;\n this.replaceStateFn = undefined;\n }\n }\n}\n"],"names":["DEFAULT_ORDER_RESOLUTION_DURATION","DEFAULT_COUNTER_INCREMENT_INTERVAL","checkForWindow","guidesApiRootPath","userId","select","state","filters","result","SelectionResult","defaultGroup","findDefaultGroup","displaySequence","location","index","guideKey","guide","predicate","s","locationRules","acc","rule","KnockGuideClient","knock","channelId","targetParams","options","__publicField","win","href","trackLocationFromWindow","Store","maybeSocket","delay","opts","queryParams","queryKey","maybeQueryStatus","queryStatus","data","entries","groups","guide_group_display_logs","mockDefaultGroup","byKey","g","e","params","newChannel","eventType","payload","event","formatFilters","throttleWindowStartedAt","DEFAULT_GROUP_KEY","checkIfThrottled","timeoutId","x","_a","step","updatedStep","metadata","ts","remoteGuide","self","localGuide","message","rest","localStep","URLPattern","filterParams","combinedParams","_k","v","queryStr","key","basePath","stepRef","attrs","steps","guides","guideGroupDisplayLogs","_","guideGroups","unthrottled","throttled","pushStateFn","replaceStateFn","target","history","args"],"mappings":";;;;;;AA6CA,MAAMA,IAAoC,IAIpCC,IAAqC,KAAK,KAG1CC,IAAiB,MAAM;AACvB,MAAA,OAAO,SAAW;AACb,WAAA;AAEX,GAEaC,IAAoB,CAACC,MAChC,aAAaA,CAAM,WAEfC,IAAS,CAACC,GAAmBC,IAA8B,OAAO;AAEhE,QAAAC,IAAS,IAAIC,EAAgB,GAE7BC,IAAeC,EAAiBL,EAAM,WAAW;AACnD,MAAA,CAACI,EAAqB,QAAAF;AAE1B,QAAMI,IAAkBF,EAAa,kBAC/BG,IAAWP,EAAM;AAEvB,aAAW,CAACQ,GAAOC,CAAQ,KAAKH,EAAgB,WAAW;AACnD,UAAAI,IAAQV,EAAM,OAAOS,CAAQ;AAInC,IAHI,CAACC,KAGD,CADaC,EAAUD,GAAO,EAAE,UAAAH,GAAU,SAAAN,GAAS,KAGhDC,EAAA,IAAIM,GAAOE,CAAK;AAAA,EAAA;AAGlB,SAAAR,EAAA,WAAW,EAAE,YAAYE,EAAa,GACtCF;AACT,GAOMS,IAAY,CAChBD,GACA,EAAE,UAAAH,GAAU,SAAAN,IAAU,SACnB;AASC,MARAA,EAAQ,QAAQA,EAAQ,SAASS,EAAM,QAIvCT,EAAQ,OAAOA,EAAQ,QAAQS,EAAM,OAIrCA,EAAM,MAAM,MAAM,CAACE,MAAM,CAAC,CAACA,EAAE,QAAQ,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CAAC,SAAS;IAuDjB,OAAO,CAAC,gBAAgB;IAmBxB,OAAO,CAAC,cAAc;IActB,OAAO,CAAC,mBAAmB;IA+C3B,OAAO,CAAC,8BAA8B;IAatC,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,sBAAsB;IAkC9B,OAAO,CAAC,oBAAoB,CAS1B;IAEF,OAAO,CAAC,kCAAkC;IAyC1C,OAAO,CAAC,oBAAoB;CAgB7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knocklabs/client",
3
- "version": "0.16.2",
3
+ "version": "0.16.4",
4
4
  "description": "The clientside library for interacting with Knock",
5
5
  "homepage": "https://github.com/knocklabs/javascript/tree/main/packages/client",
6
6
  "author": "@knocklabs",
@@ -55,7 +55,7 @@
55
55
  "@codecov/vite-plugin": "^1.9.1",
56
56
  "@types/jsonwebtoken": "^9.0.10",
57
57
  "@typescript-eslint/eslint-plugin": "^8.32.0",
58
- "@typescript-eslint/parser": "^8.32.1",
58
+ "@typescript-eslint/parser": "^8.39.1",
59
59
  "cross-env": "^7.0.3",
60
60
  "crypto": "^1.0.1",
61
61
  "eslint": "^8.56.0",
@@ -70,7 +70,7 @@
70
70
  "dependencies": {
71
71
  "@babel/runtime": "^7.27.1",
72
72
  "@knocklabs/types": "^0.1.5",
73
- "@tanstack/store": "^0.7.1",
73
+ "@tanstack/store": "^0.7.2",
74
74
  "@types/phoenix": "^1.6.6",
75
75
  "axios": "^1.11.0",
76
76
  "axios-retry": "^4.5.0",
@@ -49,6 +49,13 @@ const DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds
49
49
  // trigger subscribed callbacks.
50
50
  const DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds
51
51
 
52
+ // Return the global window object if defined, so to safely guard against SSR.
53
+ const checkForWindow = () => {
54
+ if (typeof window !== "undefined") {
55
+ return window;
56
+ }
57
+ };
58
+
52
59
  export const guidesApiRootPath = (userId: string | undefined | null) =>
53
60
  `/v1/users/${userId}/guides`;
54
61
 
@@ -166,10 +173,9 @@ export class KnockGuideClient {
166
173
  readonly options: ConstructorOpts = {},
167
174
  ) {
168
175
  const { trackLocationFromWindow = true } = options;
176
+ const win = checkForWindow();
169
177
 
170
- const location = trackLocationFromWindow
171
- ? window?.location.href
172
- : undefined;
178
+ const location = trackLocationFromWindow ? win?.location.href : undefined;
173
179
 
174
180
  this.store = new Store<StoreState>({
175
181
  guideGroups: [],
@@ -255,16 +261,12 @@ export class KnockGuideClient {
255
261
  >(this.channelId, queryParams);
256
262
  queryStatus = { status: "ok" };
257
263
 
258
- const {
259
- entries,
260
- guide_groups: groups,
261
- guide_group_display_logs: guideGroupDisplayLogs,
262
- } = data;
264
+ const { entries, guide_groups: groups, guide_group_display_logs } = data;
263
265
 
264
266
  this.store.setState((state) => ({
265
267
  ...state,
266
268
  guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],
267
- guideGroupDisplayLogs,
269
+ guideGroupDisplayLogs: guide_group_display_logs || {},
268
270
  guides: byKey(entries.map((g) => this.localCopy(g))),
269
271
  queries: { ...state.queries, [queryKey]: queryStatus },
270
272
  }));
@@ -864,7 +866,10 @@ export class KnockGuideClient {
864
866
 
865
867
  // Define as an arrow func property to always bind this to the class instance.
866
868
  private handleLocationChange = () => {
867
- const href = window.location.href;
869
+ const win = checkForWindow();
870
+ if (!win?.location) return;
871
+
872
+ const href = win.location.href;
868
873
  if (this.store.state.location === href) return;
869
874
 
870
875
  this.knock.log(`[Guide] Handle Location change: ${href}`);
@@ -872,19 +877,20 @@ export class KnockGuideClient {
872
877
  };
873
878
 
874
879
  private listenForLocationChangesFromWindow() {
875
- if (window?.history) {
880
+ const win = checkForWindow();
881
+ if (win?.history) {
876
882
  // 1. Listen for browser back/forward button clicks.
877
- window.addEventListener("popstate", this.handleLocationChange);
883
+ win.addEventListener("popstate", this.handleLocationChange);
878
884
 
879
885
  // 2. Listen for hash changes in case it's used for routing.
880
- window.addEventListener("hashchange", this.handleLocationChange);
886
+ win.addEventListener("hashchange", this.handleLocationChange);
881
887
 
882
888
  // 3. Monkey-patch history methods to catch programmatic navigation.
883
- const pushStateFn = window.history.pushState;
884
- const replaceStateFn = window.history.replaceState;
889
+ const pushStateFn = win.history.pushState;
890
+ const replaceStateFn = win.history.replaceState;
885
891
 
886
892
  // Use setTimeout to allow the browser state to potentially settle.
887
- window.history.pushState = new Proxy(pushStateFn, {
893
+ win.history.pushState = new Proxy(pushStateFn, {
888
894
  apply: (target, history, args) => {
889
895
  Reflect.apply(target, history, args);
890
896
  setTimeout(() => {
@@ -892,7 +898,7 @@ export class KnockGuideClient {
892
898
  }, 0);
893
899
  },
894
900
  });
895
- window.history.replaceState = new Proxy(replaceStateFn, {
901
+ win.history.replaceState = new Proxy(replaceStateFn, {
896
902
  apply: (target, history, args) => {
897
903
  Reflect.apply(target, history, args);
898
904
  setTimeout(() => {
@@ -912,15 +918,18 @@ export class KnockGuideClient {
912
918
  }
913
919
 
914
920
  private removeEventListeners() {
915
- window.removeEventListener("popstate", this.handleLocationChange);
916
- window.removeEventListener("hashchange", this.handleLocationChange);
921
+ const win = checkForWindow();
922
+ if (!win?.history) return;
923
+
924
+ win.removeEventListener("popstate", this.handleLocationChange);
925
+ win.removeEventListener("hashchange", this.handleLocationChange);
917
926
 
918
927
  if (this.pushStateFn) {
919
- window.history.pushState = this.pushStateFn;
928
+ win.history.pushState = this.pushStateFn;
920
929
  this.pushStateFn = undefined;
921
930
  }
922
931
  if (this.replaceStateFn) {
923
- window.history.replaceState = this.replaceStateFn;
932
+ win.history.replaceState = this.replaceStateFn;
924
933
  this.replaceStateFn = undefined;
925
934
  }
926
935
  }