Skip to content

Commit 6a5d677

Browse files
committed
clean up guide toolbar v1 code
1 parent f99231d commit 6a5d677

12 files changed

Lines changed: 7 additions & 485 deletions

File tree

packages/client/src/clients/guide/client.ts

Lines changed: 1 addition & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ const DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds
6464
// Maximum number of retry attempts for channel subscription
6565
const SUBSCRIBE_RETRY_LIMIT = 3;
6666

67-
// Debug query param keys
68-
export const DEBUG_QUERY_PARAMS = {
69-
GUIDE_KEY: "knock_guide_key",
70-
PREVIEW_SESSION_ID: "knock_preview_session_id",
71-
};
72-
73-
const DEBUG_STORAGE_KEY = "knock_guide_debug";
74-
7567
// Return the global window object if defined, so to safely guard against SSR.
7668
const checkForWindow = () => {
7769
if (typeof window !== "undefined") {
@@ -82,76 +74,6 @@ const checkForWindow = () => {
8274
export const guidesApiRootPath = (userId: string | undefined | null) =>
8375
`/v1/users/${userId}/guides`;
8476

85-
// Detect debug params from URL or local storage
86-
const detectDebugParams = (): DebugState => {
87-
const win = checkForWindow();
88-
if (!win || !win.location) {
89-
return { forcedGuideKey: null, previewSessionId: null };
90-
}
91-
92-
const urlParams = new URLSearchParams(win.location.search);
93-
const urlGuideKey = urlParams.get(DEBUG_QUERY_PARAMS.GUIDE_KEY);
94-
const urlPreviewSessionId = urlParams.get(
95-
DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID,
96-
);
97-
98-
// If URL params exist, persist them to localStorage and return them
99-
if (urlGuideKey || urlPreviewSessionId) {
100-
if (win.localStorage) {
101-
try {
102-
const debugState = {
103-
forcedGuideKey: urlGuideKey,
104-
previewSessionId: urlPreviewSessionId,
105-
};
106-
win.localStorage.setItem(DEBUG_STORAGE_KEY, JSON.stringify(debugState));
107-
} catch {
108-
// Silently fail in privacy mode
109-
}
110-
}
111-
return {
112-
forcedGuideKey: urlGuideKey,
113-
previewSessionId: urlPreviewSessionId,
114-
};
115-
}
116-
117-
// Check local storage if no URL params
118-
let storedGuideKey = null;
119-
let storedPreviewSessionId = null;
120-
121-
if (win.localStorage) {
122-
try {
123-
const storedDebugState = win.localStorage.getItem(DEBUG_STORAGE_KEY);
124-
if (storedDebugState) {
125-
const parsedDebugState = safeJsonParseDebugParams(storedDebugState);
126-
storedGuideKey = parsedDebugState.forcedGuideKey;
127-
storedPreviewSessionId = parsedDebugState.previewSessionId;
128-
}
129-
} catch {
130-
// Silently fail in privacy mode
131-
}
132-
}
133-
134-
return {
135-
forcedGuideKey: storedGuideKey,
136-
previewSessionId: storedPreviewSessionId,
137-
};
138-
};
139-
140-
const safeJsonParseDebugParams = (value: string): DebugState => {
141-
try {
142-
const parsed = JSON.parse(value);
143-
return {
144-
forcedGuideKey: parsed?.forcedGuideKey ?? null,
145-
previewSessionId: parsed?.previewSessionId ?? null,
146-
};
147-
} catch {
148-
return {
149-
forcedGuideKey: null,
150-
previewSessionId: null,
151-
};
152-
}
153-
};
154-
15577
type SelectQueryMetadata = {
15678
limit: SelectQueryLimit;
15779
opts: SelectGuideOpts;
@@ -295,15 +217,11 @@ export class KnockGuideClient {
295217
) {
296218
const {
297219
trackLocationFromWindow = true,
298-
// TODO(KNO-11523): Remove once we ship guide toolbar v2, and offload as
299-
// much debugging specific logic and responsibilities to toolbar.
300-
trackDebugParams = false,
301220
throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL,
302221
} = options;
303222
const win = checkForWindow();
304223

305224
const location = trackLocationFromWindow ? win?.location?.href : undefined;
306-
const debug = trackDebugParams ? detectDebugParams() : undefined;
307225

308226
this.store = new Store<StoreState>({
309227
guideGroups: [],
@@ -315,7 +233,6 @@ export class KnockGuideClient {
315233
location,
316234
// Increment to update the state store and trigger re-selection.
317235
counter: 0,
318-
debug,
319236
});
320237

321238
// In server environments we might not have a socket connection.
@@ -560,45 +477,6 @@ export class KnockGuideClient {
560477
});
561478
}
562479

563-
exitDebugMode() {
564-
this.knock.log("[Guide] Exiting debug mode");
565-
566-
// Clear localStorage debug params
567-
const win = checkForWindow();
568-
if (win?.localStorage) {
569-
try {
570-
win.localStorage.removeItem(DEBUG_STORAGE_KEY);
571-
} catch {
572-
// Silently fail in privacy mode
573-
}
574-
}
575-
576-
// Clear debug state from store
577-
this.store.setState((state) => ({
578-
...state,
579-
debug: {
580-
forcedGuideKey: null,
581-
previewSessionId: null,
582-
focusedGuideKeys: {},
583-
},
584-
previewGuides: {}, // Clear preview guides when exiting debug mode
585-
}));
586-
587-
// Remove URL query params if present
588-
// Only update the URL if params need to be cleared to avoid unnecessary navigations
589-
if (win?.location) {
590-
const url = new URL(win.location.href);
591-
if (
592-
url.searchParams.has(DEBUG_QUERY_PARAMS.GUIDE_KEY) ||
593-
url.searchParams.has(DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID)
594-
) {
595-
url.searchParams.delete(DEBUG_QUERY_PARAMS.GUIDE_KEY);
596-
url.searchParams.delete(DEBUG_QUERY_PARAMS.PREVIEW_SESSION_ID);
597-
win.location.href = url.toString();
598-
}
599-
}
600-
}
601-
602480
setDebug(debugOpts?: Omit<DebugState, "debugging">) {
603481
this.knock.log("[Guide] .setDebug()");
604482

@@ -1399,43 +1277,9 @@ export class KnockGuideClient {
13991277

14001278
this.knock.log(`[Guide] Detected a location change: ${href}`);
14011279

1402-
if (!this.options.trackDebugParams) {
1403-
this.setLocation(href);
1404-
return;
1405-
}
1406-
1407-
// TODO(KNO-11523): Remove below once we ship toolbar v2.
1408-
1409-
// If entering debug mode, fetch all guides.
1410-
const currentDebugParams = this.store.state.debug || {};
1411-
const newDebugParams = detectDebugParams();
1412-
1413-
this.setLocation(href, { debug: newDebugParams });
1414-
1415-
// If debug state has changed, refetch guides and resubscribe to the websocket channel
1416-
const debugStateChanged = this.checkDebugStateChanged(
1417-
currentDebugParams,
1418-
newDebugParams,
1419-
);
1420-
1421-
if (debugStateChanged) {
1422-
this.knock.log(
1423-
`[Guide] Debug state changed, refetching guides and resubscribing to the websocket channel`,
1424-
);
1425-
this.fetch();
1426-
this.subscribe();
1427-
}
1280+
this.setLocation(href);
14281281
};
14291282

1430-
// Returns whether debug params have changed. For guide key, we only check
1431-
// presence since the exact value has no impact on fetch/subscribe
1432-
private checkDebugStateChanged(a: DebugState, b: DebugState): boolean {
1433-
return (
1434-
Boolean(a.forcedGuideKey) !== Boolean(b.forcedGuideKey) ||
1435-
a.previewSessionId !== b.previewSessionId
1436-
);
1437-
}
1438-
14391283
private listenForLocationChangesFromWindow() {
14401284
const win = checkForWindow();
14411285
if (win?.history && win?.addEventListener) {

packages/client/src/clients/guide/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
export {
2-
KnockGuideClient,
3-
DEBUG_QUERY_PARAMS,
4-
checkActivatable,
5-
} from "./client";
1+
export { KnockGuideClient, checkActivatable } from "./client";
62
export { checkStateIfThrottled } from "./helpers";
73
export type { ToolbarV2RunConfig } from "./helpers";
84
export type {

packages/client/src/clients/guide/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ export type TargetParams = {
285285

286286
export type ConstructorOpts = {
287287
trackLocationFromWindow?: boolean;
288-
trackDebugParams?: boolean;
289288
orderResolutionDuration?: number;
290289
throttleCheckInterval?: number;
291290
};

packages/client/test/clients/guide/guide.test.ts

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ describe("KnockGuideClient", () => {
166166
queries: {},
167167
location: undefined,
168168
counter: 0,
169-
debug: undefined,
170169
});
171170
});
172171

@@ -195,7 +194,6 @@ describe("KnockGuideClient", () => {
195194
queries: {},
196195
location: "https://example.com",
197196
counter: 0,
198-
debug: undefined,
199197
});
200198
});
201199

@@ -212,33 +210,7 @@ describe("KnockGuideClient", () => {
212210
queries: {},
213211
location: undefined,
214212
counter: 0,
215-
debug: undefined,
216-
});
217-
});
218-
219-
test("handles localStorage errors gracefully during initialization", () => {
220-
const mockLocalStorageWithErrors = {
221-
getItem: vi.fn().mockImplementation(() => {
222-
throw new Error("Privacy mode or quota exceeded");
223-
}),
224-
setItem: vi.fn().mockImplementation(() => {
225-
throw new Error("Privacy mode or quota exceeded");
226-
}),
227-
};
228-
229-
vi.stubGlobal("window", {
230-
location: {
231-
search:
232-
"?knock_guide_key=test_guide&knock_preview_session_id=session123",
233-
},
234-
localStorage: mockLocalStorageWithErrors,
235213
});
236-
237-
expect(() => {
238-
new KnockGuideClient(mockKnock, channelId, {}, { trackDebugParams: true });
239-
}).not.toThrow();
240-
241-
expect(mockLocalStorageWithErrors.setItem).toHaveBeenCalled();
242214
});
243215

244216
test("starts the counter interval clock and sets the interval id", () => {
@@ -3714,102 +3686,6 @@ describe("KnockGuideClient", () => {
37143686
expect(windowWithHistory.history.replaceState).toBe(originalReplaceState);
37153687
});
37163688

3717-
test("handleLocationChange calls subscribe when entering debug mode", () => {
3718-
const mockLocalStorage = {
3719-
getItem: vi.fn(),
3720-
setItem: vi.fn(),
3721-
};
3722-
3723-
vi.stubGlobal("window", {
3724-
...mockWindow,
3725-
location: {
3726-
href: "https://example.com/dashboard?knock_guide_key=test_guide",
3727-
search: "?knock_guide_key=test_guide",
3728-
},
3729-
localStorage: mockLocalStorage,
3730-
});
3731-
3732-
const client = new KnockGuideClient(
3733-
mockKnock,
3734-
channelId,
3735-
{},
3736-
{ trackLocationFromWindow: true, trackDebugParams: true },
3737-
);
3738-
3739-
client.store.state.debug = { forcedGuideKey: null };
3740-
client.store.state.location = "https://example.com/dashboard";
3741-
3742-
const subscribeSpy = vi
3743-
.spyOn(client, "subscribe")
3744-
.mockImplementation(() => {});
3745-
3746-
const fetchSpy = vi
3747-
.spyOn(client, "fetch")
3748-
.mockImplementation(() => Promise.resolve({ status: "ok" }));
3749-
3750-
client["handleLocationChange"]();
3751-
3752-
expect(fetchSpy).toHaveBeenCalled();
3753-
expect(subscribeSpy).toHaveBeenCalled();
3754-
3755-
// Should persist debug parameters to localStorage when detected in URL
3756-
expect(mockLocalStorage.setItem).toHaveBeenCalledWith(
3757-
"knock_guide_debug",
3758-
JSON.stringify({
3759-
forcedGuideKey: "test_guide",
3760-
previewSessionId: null,
3761-
}),
3762-
);
3763-
});
3764-
3765-
test("handleLocationChange calls subscribe when exiting debug mode", () => {
3766-
const mockLocalStorage = {
3767-
getItem: vi.fn().mockImplementation((key: string) => {
3768-
// Simulate localStorage having stored debug values from previous session
3769-
if (key === "knock_guide_debug") {
3770-
return JSON.stringify({
3771-
forcedGuideKey: "stored_guide",
3772-
previewSessionId: "stored_session",
3773-
});
3774-
}
3775-
return null;
3776-
}),
3777-
setItem: vi.fn(),
3778-
};
3779-
3780-
vi.stubGlobal("window", {
3781-
...mockWindow,
3782-
location: {
3783-
href: "https://example.com/dashboard",
3784-
search: "",
3785-
},
3786-
localStorage: mockLocalStorage,
3787-
});
3788-
3789-
const client = new KnockGuideClient(
3790-
mockKnock,
3791-
channelId,
3792-
{},
3793-
{ trackLocationFromWindow: true, trackDebugParams: true },
3794-
);
3795-
3796-
client.store.state.debug = { forcedGuideKey: "test_guide" };
3797-
client.store.state.location =
3798-
"https://example.com/dashboard?knock_guide_key=test_guide";
3799-
3800-
const subscribeSpy = vi
3801-
.spyOn(client, "subscribe")
3802-
.mockImplementation(() => {});
3803-
3804-
client["handleLocationChange"]();
3805-
3806-
expect(subscribeSpy).toHaveBeenCalled();
3807-
3808-
// Should read from localStorage when no URL parameters are present
3809-
expect(mockLocalStorage.getItem).toHaveBeenCalledWith(
3810-
"knock_guide_debug",
3811-
);
3812-
});
38133689
});
38143690

38153691
describe("private methods", () => {

0 commit comments

Comments
 (0)