1+ import { KnockGuide } from "@knocklabs/client" ;
2+
13import { checkForWindow } from "../../../../../modules/core" ;
24
35// Use this param to start Toolbar and enter into a debugging session when
46// it is present and set to true.
57const TOOLBAR_QUERY_PARAM = "knock_guide_toolbar" ;
68
9+ // Optional, when present pin/focus on this guide.
10+ const GUIDE_KEY_PARAM = "focused_guide_key" ;
11+
712// Use this key to read and write the run config data.
813const LOCAL_STORAGE_KEY = "knock_guide_debug" ;
914
10- type ToolbarV2RunConfig = {
15+ export type ToolbarV2RunConfig = {
1116 isVisible : boolean ;
17+ focusedGuideKeys ?: Record < KnockGuide [ "key" ] , true > ;
1218} ;
1319
14- export const getRunConfig = ( ) : ToolbarV2RunConfig | undefined => {
20+ export const getRunConfig = ( ) : ToolbarV2RunConfig => {
21+ const fallback = { isVisible : false } ;
22+
1523 const win = checkForWindow ( ) ;
1624 if ( ! win || ! win . location ) {
17- return undefined ;
25+ return fallback ;
1826 }
1927
2028 const urlSearchParams = new URLSearchParams ( win . location . search ) ;
2129 const toolbarParamValue = urlSearchParams . get ( TOOLBAR_QUERY_PARAM ) ;
30+ const guideKeyParamValue = urlSearchParams . get ( GUIDE_KEY_PARAM ) ;
2231
2332 // If toolbar param detected in the URL, write to local storage before
2433 // returning.
2534 if ( toolbarParamValue !== null ) {
26- const config = {
35+ const config : ToolbarV2RunConfig = {
2736 isVisible : toolbarParamValue === "true" ,
2837 } ;
38+ if ( guideKeyParamValue ) {
39+ config . focusedGuideKeys = { [ guideKeyParamValue ] : true } ;
40+ }
41+
2942 writeRunConfigLS ( config ) ;
3043 return config ;
3144 }
3245
3346 // If not detected, check local storage for a persisted run config. If not
3447 // present then fall back to a default config.
35- return (
36- readRunConfigLS ( ) || {
37- isVisible : false ,
38- }
39- ) ;
48+ return readRunConfigLS ( ) || fallback ;
4049} ;
4150
4251const writeRunConfigLS = ( config : ToolbarV2RunConfig ) => {
4352 const win = checkForWindow ( ) ;
53+ if ( ! win || ! win . localStorage ) return ;
54+
4455 try {
45- win ? .localStorage ? .setItem ( LOCAL_STORAGE_KEY , JSON . stringify ( config ) ) ;
56+ win . localStorage . setItem ( LOCAL_STORAGE_KEY , JSON . stringify ( config ) ) ;
4657 } catch {
4758 // localStorage may be unavailable (e.g. private browsing)
4859 }
4960} ;
5061
5162const readRunConfigLS = ( ) : ToolbarV2RunConfig | undefined => {
5263 const win = checkForWindow ( ) ;
64+ if ( ! win || ! win . localStorage ) return undefined ;
65+
5366 try {
54- const stored = win ? .localStorage ? .getItem ( LOCAL_STORAGE_KEY ) ;
67+ const stored = win . localStorage . getItem ( LOCAL_STORAGE_KEY ) ;
5568 if ( stored ) {
5669 return JSON . parse ( stored ) ;
5770 }
@@ -63,8 +76,10 @@ const readRunConfigLS = (): ToolbarV2RunConfig | undefined => {
6376
6477export const clearRunConfigLS = ( ) => {
6578 const win = checkForWindow ( ) ;
79+ if ( ! win || ! win . localStorage ) return ;
80+
6681 try {
67- win ? .localStorage ? .removeItem ( LOCAL_STORAGE_KEY ) ;
82+ win . localStorage . removeItem ( LOCAL_STORAGE_KEY ) ;
6883 } catch {
6984 // localStorage may be unavailable (e.g. private browsing)
7085 }
0 commit comments