@@ -185,7 +185,7 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
185185type PredicateOpts = {
186186 location ?: string | undefined ;
187187 filters ?: SelectFilterParams | undefined ;
188- debug : DebugState ;
188+ debug ? : DebugState ;
189189} ;
190190
191191const predicate = (
@@ -268,13 +268,15 @@ export class KnockGuideClient {
268268 ) {
269269 const {
270270 trackLocationFromWindow = true ,
271+ // TODO: Remove this option once we ship guide toolbar v2, and offload
272+ // as much debugging specific logic and responsibilities to toolbar.
273+ trackDebugParams = false ,
271274 throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL ,
272275 } = options ;
273276 const win = checkForWindow ( ) ;
274277
275278 const location = trackLocationFromWindow ? win ?. location ?. href : undefined ;
276-
277- const debug = detectDebugParams ( ) ;
279+ const debug = trackDebugParams ? detectDebugParams ( ) : undefined ;
278280
279281 this . store = new Store < StoreState > ( {
280282 guideGroups : [ ] ,
@@ -410,8 +412,8 @@ export class KnockGuideClient {
410412 const params = {
411413 ...this . targetParams ,
412414 user_id : this . knock . userId ,
413- force_all_guides : debugState . forcedGuideKey ? true : undefined ,
414- preview_session_id : debugState . previewSessionId || undefined ,
415+ force_all_guides : debugState ? .forcedGuideKey ? true : undefined ,
416+ preview_session_id : debugState ? .previewSessionId || undefined ,
415417 } ;
416418
417419 const newChannel = this . socket . channel ( this . socketChannelTopic , params ) ;
@@ -557,6 +559,39 @@ export class KnockGuideClient {
557559 }
558560 }
559561
562+ setDebug ( debugOpts ?: Omit < DebugState , "debugging" > ) {
563+ this . knock . log ( "[Guide] .setDebug()" ) ;
564+ const shouldRefetch = ! this . store . state . debug ?. debugging ;
565+
566+ this . store . setState ( ( state ) => ( {
567+ ...state ,
568+ debug : { ...debugOpts , debugging : true } ,
569+ } ) ) ;
570+
571+ if ( shouldRefetch ) {
572+ this . knock . log (
573+ `[Guide] Start debugging, refetching guides and resubscribing to the websocket channel` ,
574+ ) ;
575+ this . fetch ( ) ;
576+ this . subscribe ( ) ;
577+ }
578+ }
579+
580+ unsetDebug ( ) {
581+ this . knock . log ( "[Guide] .unsetDebug()" ) ;
582+ const shouldRefetch = this . store . state . debug ?. debugging ;
583+
584+ this . store . setState ( ( state ) => ( { ...state , debug : undefined } ) ) ;
585+
586+ if ( shouldRefetch ) {
587+ this . knock . log (
588+ `[Guide] Stop debugging, refetching guides and resubscribing to the websocket channel` ,
589+ ) ;
590+ this . fetch ( ) ;
591+ this . subscribe ( ) ;
592+ }
593+ }
594+
560595 //
561596 // Store selector
562597 //
@@ -920,7 +955,7 @@ export class KnockGuideClient {
920955 // Get the next unarchived step.
921956 getStep ( ) {
922957 // If debugging this guide, return the first step regardless of archive status
923- if ( self . store . state . debug . forcedGuideKey === this . key ) {
958+ if ( self . store . state . debug ? .forcedGuideKey === this . key ) {
924959 return this . steps [ 0 ] ;
925960 }
926961
@@ -977,7 +1012,7 @@ export class KnockGuideClient {
9771012
9781013 // Append debug params
9791014 const debugState = this . store . state . debug ;
980- if ( debugState . forcedGuideKey ) {
1015+ if ( debugState ? .forcedGuideKey || debugState ?. debugging ) {
9811016 combinedParams . force_all_guides = true ;
9821017 }
9831018
@@ -1146,8 +1181,15 @@ export class KnockGuideClient {
11461181
11471182 this . knock . log ( `[Guide] Detected a location change: ${ href } ` ) ;
11481183
1184+ if ( ! this . options . trackDebugParams ) {
1185+ this . setLocation ( href ) ;
1186+ return ;
1187+ }
1188+
1189+ // TODO: Remove below once we ship toolbar v2.
1190+
11491191 // If entering debug mode, fetch all guides.
1150- const currentDebugParams = this . store . state . debug ;
1192+ const currentDebugParams = this . store . state . debug || { } ;
11511193 const newDebugParams = detectDebugParams ( ) ;
11521194
11531195 this . setLocation ( href , { debug : newDebugParams } ) ;
0 commit comments