Skip to content

Commit ffa7bd2

Browse files
authored
chore: clean up guide toolbar v1 code (#960)
1 parent 0862e8e commit ffa7bd2

13 files changed

Lines changed: 16 additions & 503 deletions

File tree

.changeset/slimy-dogs-play.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@knocklabs/react-core": patch
3+
"@knocklabs/client": patch
4+
"@knocklabs/react": patch
5+
---
6+
7+
[Guide] Remove guide toolbar v1 support

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

Lines changed: 2 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
GuideData,
3131
GuideGroupAddedEvent,
3232
GuideGroupUpdatedEvent,
33-
GuideLivePreviewUpdatedEvent,
3433
GuideRemovedEvent,
3534
GuideSocketEvent,
3635
GuideStepData,
@@ -64,14 +63,6 @@ const DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds
6463
// Maximum number of retry attempts for channel subscription
6564
const SUBSCRIBE_RETRY_LIMIT = 3;
6665

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-
7566
// Return the global window object if defined, so to safely guard against SSR.
7667
const checkForWindow = () => {
7768
if (typeof window !== "undefined") {
@@ -82,76 +73,6 @@ const checkForWindow = () => {
8273
export const guidesApiRootPath = (userId: string | undefined | null) =>
8374
`/v1/users/${userId}/guides`;
8475

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-
15576
type SelectQueryMetadata = {
15677
limit: SelectQueryLimit;
15778
opts: SelectGuideOpts;
@@ -239,7 +160,7 @@ const predicate = (
239160
};
240161

241162
export const checkActivatable = (
242-
guide: KnockGuide,
163+
guide: Pick<KnockGuide, "activation_url_rules" | "activation_url_patterns">,
243164
location: string | undefined,
244165
) => {
245166
const url = location ? newUrl(location) : undefined;
@@ -272,7 +193,6 @@ export class KnockGuideClient {
272193
"guide.removed",
273194
"guide_group.added",
274195
"guide_group.updated",
275-
"guide.live_preview_updated",
276196
];
277197
private subscribeRetryCount = 0;
278198

@@ -295,15 +215,11 @@ export class KnockGuideClient {
295215
) {
296216
const {
297217
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,
301218
throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL,
302219
} = options;
303220
const win = checkForWindow();
304221

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

308224
this.store = new Store<StoreState>({
309225
guideGroups: [],
@@ -315,7 +231,6 @@ export class KnockGuideClient {
315231
location,
316232
// Increment to update the state store and trigger re-selection.
317233
counter: 0,
318-
debug,
319234
});
320235

321236
// In server environments we might not have a socket connection.
@@ -530,9 +445,6 @@ export class KnockGuideClient {
530445
case "guide_group.updated":
531446
return this.addOrReplaceGuideGroup(payload);
532447

533-
case "guide.live_preview_updated":
534-
return this.updatePreviewGuide(payload);
535-
536448
default:
537449
return;
538450
}
@@ -560,45 +472,6 @@ export class KnockGuideClient {
560472
});
561473
}
562474

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-
602475
setDebug(debugOpts?: Omit<DebugState, "debugging">) {
603476
this.knock.log("[Guide] .setDebug()");
604477

@@ -1383,15 +1256,6 @@ export class KnockGuideClient {
13831256
});
13841257
}
13851258

1386-
private updatePreviewGuide({ data }: GuideLivePreviewUpdatedEvent) {
1387-
const guide = this.localCopy(data.guide);
1388-
1389-
this.store.setState((state) => {
1390-
const previewGuides = { ...state.previewGuides, [guide.key]: guide };
1391-
return { ...state, previewGuides };
1392-
});
1393-
}
1394-
13951259
// Define as an arrow func property to always bind this to the class instance.
13961260
private handleLocationChange = () => {
13971261
this.knock.log(`[Guide] .handleLocationChange`);
@@ -1403,43 +1267,9 @@ export class KnockGuideClient {
14031267

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

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

1434-
// Returns whether debug params have changed. For guide key, we only check
1435-
// presence since the exact value has no impact on fetch/subscribe
1436-
private checkDebugStateChanged(a: DebugState, b: DebugState): boolean {
1437-
return (
1438-
Boolean(a.forcedGuideKey) !== Boolean(b.forcedGuideKey) ||
1439-
a.previewSessionId !== b.previewSessionId
1440-
);
1441-
}
1442-
14431273
private listenForLocationChangesFromWindow() {
14441274
const win = checkForWindow();
14451275
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: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ type SocketEventType =
161161
| "guide.updated"
162162
| "guide.removed"
163163
| "guide_group.added"
164-
| "guide_group.updated"
165-
| "guide.live_preview_updated";
164+
| "guide_group.updated";
166165

167166
type SocketEventPayload<E extends SocketEventType, D> = {
168167
topic: string;
@@ -195,18 +194,12 @@ export type GuideGroupUpdatedEvent = SocketEventPayload<
195194
{ guide_group: GuideGroupData }
196195
>;
197196

198-
export type GuideLivePreviewUpdatedEvent = SocketEventPayload<
199-
"guide.live_preview_updated",
200-
{ guide: GuideData; eligible: boolean }
201-
>;
202-
203197
export type GuideSocketEvent =
204198
| GuideAddedEvent
205199
| GuideUpdatedEvent
206200
| GuideRemovedEvent
207201
| GuideGroupAddedEvent
208-
| GuideGroupUpdatedEvent
209-
| GuideLivePreviewUpdatedEvent;
202+
| GuideGroupUpdatedEvent;
210203

211204
//
212205
// Guide client
@@ -285,7 +278,6 @@ export type TargetParams = {
285278

286279
export type ConstructorOpts = {
287280
trackLocationFromWindow?: boolean;
288-
trackDebugParams?: boolean;
289281
orderResolutionDuration?: number;
290282
throttleCheckInterval?: number;
291283
};

0 commit comments

Comments
 (0)