@@ -182,7 +182,7 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
182182type PredicateOpts = {
183183 location ?: string | undefined ;
184184 filters ?: SelectFilterParams | undefined ;
185- debug : DebugState ;
185+ debug ? : DebugState ;
186186} ;
187187
188188const predicate = (
@@ -265,13 +265,15 @@ export class KnockGuideClient {
265265 ) {
266266 const {
267267 trackLocationFromWindow = true ,
268+ // TODO: Remove this option once we ship guide toolbar v2, and offload
269+ // as much debugging specific logic and responsibilities to toolbar.
270+ trackDebugParams = false ,
268271 throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL ,
269272 } = options ;
270273 const win = checkForWindow ( ) ;
271274
272275 const location = trackLocationFromWindow ? win ?. location ?. href : undefined ;
273-
274- const debug = detectDebugParams ( ) ;
276+ const debug = trackDebugParams ? detectDebugParams ( ) : undefined ;
275277
276278 this . store = new Store < StoreState > ( {
277279 guideGroups : [ ] ,
@@ -407,8 +409,8 @@ export class KnockGuideClient {
407409 const params = {
408410 ...this . targetParams ,
409411 user_id : this . knock . userId ,
410- force_all_guides : debugState . forcedGuideKey ? true : undefined ,
411- preview_session_id : debugState . previewSessionId || undefined ,
412+ force_all_guides : debugState ? .forcedGuideKey ? true : undefined ,
413+ preview_session_id : debugState ? .previewSessionId || undefined ,
412414 } ;
413415
414416 const newChannel = this . socket . channel ( this . socketChannelTopic , params ) ;
@@ -554,6 +556,39 @@ export class KnockGuideClient {
554556 }
555557 }
556558
559+ setDebug ( debugOpts ?: Omit < DebugState , "debugging" > ) {
560+ this . knock . log ( "[Guide] .setDebug()" ) ;
561+ const shouldRefetch = ! this . store . state . debug ?. debugging ;
562+
563+ this . store . setState ( ( state ) => ( {
564+ ...state ,
565+ debug : { ...debugOpts , debugging : true } ,
566+ } ) ) ;
567+
568+ if ( shouldRefetch ) {
569+ this . knock . log (
570+ `[Guide] Start debugging, refetching guides and resubscribing to the websocket channel` ,
571+ ) ;
572+ this . fetch ( ) ;
573+ this . subscribe ( ) ;
574+ }
575+ }
576+
577+ unsetDebug ( ) {
578+ this . knock . log ( "[Guide] .unsetDebug()" ) ;
579+ const shouldRefetch = this . store . state . debug ?. debugging ;
580+
581+ this . store . setState ( ( state ) => ( { ...state , debug : undefined } ) ) ;
582+
583+ if ( shouldRefetch ) {
584+ this . knock . log (
585+ `[Guide] Stop debugging, refetching guides and resubscribing to the websocket channel` ,
586+ ) ;
587+ this . fetch ( ) ;
588+ this . subscribe ( ) ;
589+ }
590+ }
591+
557592 //
558593 // Store selector
559594 //
@@ -917,7 +952,7 @@ export class KnockGuideClient {
917952 // Get the next unarchived step.
918953 getStep ( ) {
919954 // If debugging this guide, return the first step regardless of archive status
920- if ( self . store . state . debug . forcedGuideKey === this . key ) {
955+ if ( self . store . state . debug ? .forcedGuideKey === this . key ) {
921956 return this . steps [ 0 ] ;
922957 }
923958
@@ -974,7 +1009,7 @@ export class KnockGuideClient {
9741009
9751010 // Append debug params
9761011 const debugState = this . store . state . debug ;
977- if ( debugState . forcedGuideKey ) {
1012+ if ( debugState ? .forcedGuideKey || debugState ?. debugging ) {
9781013 combinedParams . force_all_guides = true ;
9791014 }
9801015
@@ -1143,8 +1178,15 @@ export class KnockGuideClient {
11431178
11441179 this . knock . log ( `[Guide] Detected a location change: ${ href } ` ) ;
11451180
1181+ if ( ! this . options . trackDebugParams ) {
1182+ this . setLocation ( href ) ;
1183+ return ;
1184+ }
1185+
1186+ // TODO: Remove below once we ship toolbar v2.
1187+
11461188 // If entering debug mode, fetch all guides.
1147- const currentDebugParams = this . store . state . debug ;
1189+ const currentDebugParams = this . store . state . debug || { } ;
11481190 const newDebugParams = detectDebugParams ( ) ;
11491191
11501192 this . setLocation ( href , { debug : newDebugParams } ) ;
0 commit comments