@knocklabs/client 0.21.11 → 0.21.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/cjs/api.js +1 -1
- package/dist/cjs/clients/guide/client.js +1 -1
- package/dist/cjs/clients/guide/client.js.map +1 -1
- package/dist/cjs/clients/guide/types.js.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/esm/api.mjs +1 -1
- package/dist/esm/clients/guide/client.mjs +88 -193
- package/dist/esm/clients/guide/client.mjs.map +1 -1
- package/dist/esm/clients/guide/types.mjs.map +1 -1
- package/dist/esm/index.mjs +12 -13
- package/dist/types/clients/guide/client.d.ts +1 -8
- package/dist/types/clients/guide/client.d.ts.map +1 -1
- package/dist/types/clients/guide/index.d.ts +1 -1
- package/dist/types/clients/guide/index.d.ts.map +1 -1
- package/dist/types/clients/guide/types.d.ts +2 -7
- package/dist/types/clients/guide/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/clients/guide/client.ts +2 -172
- package/src/clients/guide/index.ts +1 -5
- package/src/clients/guide/types.ts +2 -10
|
@@ -30,7 +30,6 @@ import {
|
|
|
30
30
|
GuideData,
|
|
31
31
|
GuideGroupAddedEvent,
|
|
32
32
|
GuideGroupUpdatedEvent,
|
|
33
|
-
GuideLivePreviewUpdatedEvent,
|
|
34
33
|
GuideRemovedEvent,
|
|
35
34
|
GuideSocketEvent,
|
|
36
35
|
GuideStepData,
|
|
@@ -64,14 +63,6 @@ const DEFAULT_COUNTER_INCREMENT_INTERVAL = 30 * 1000; // in milliseconds
|
|
|
64
63
|
// Maximum number of retry attempts for channel subscription
|
|
65
64
|
const SUBSCRIBE_RETRY_LIMIT = 3;
|
|
66
65
|
|
|
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
|
-
|
|
75
66
|
// Return the global window object if defined, so to safely guard against SSR.
|
|
76
67
|
const checkForWindow = () => {
|
|
77
68
|
if (typeof window !== "undefined") {
|
|
@@ -82,76 +73,6 @@ const checkForWindow = () => {
|
|
|
82
73
|
export const guidesApiRootPath = (userId: string | undefined | null) =>
|
|
83
74
|
`/v1/users/${userId}/guides`;
|
|
84
75
|
|
|
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
|
-
|
|
155
76
|
type SelectQueryMetadata = {
|
|
156
77
|
limit: SelectQueryLimit;
|
|
157
78
|
opts: SelectGuideOpts;
|
|
@@ -239,7 +160,7 @@ const predicate = (
|
|
|
239
160
|
};
|
|
240
161
|
|
|
241
162
|
export const checkActivatable = (
|
|
242
|
-
guide: KnockGuide,
|
|
163
|
+
guide: Pick<KnockGuide, "activation_url_rules" | "activation_url_patterns">,
|
|
243
164
|
location: string | undefined,
|
|
244
165
|
) => {
|
|
245
166
|
const url = location ? newUrl(location) : undefined;
|
|
@@ -272,7 +193,6 @@ export class KnockGuideClient {
|
|
|
272
193
|
"guide.removed",
|
|
273
194
|
"guide_group.added",
|
|
274
195
|
"guide_group.updated",
|
|
275
|
-
"guide.live_preview_updated",
|
|
276
196
|
];
|
|
277
197
|
private subscribeRetryCount = 0;
|
|
278
198
|
|
|
@@ -295,15 +215,11 @@ export class KnockGuideClient {
|
|
|
295
215
|
) {
|
|
296
216
|
const {
|
|
297
217
|
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,
|
|
301
218
|
throttleCheckInterval = DEFAULT_COUNTER_INCREMENT_INTERVAL,
|
|
302
219
|
} = options;
|
|
303
220
|
const win = checkForWindow();
|
|
304
221
|
|
|
305
222
|
const location = trackLocationFromWindow ? win?.location?.href : undefined;
|
|
306
|
-
const debug = trackDebugParams ? detectDebugParams() : undefined;
|
|
307
223
|
|
|
308
224
|
this.store = new Store<StoreState>({
|
|
309
225
|
guideGroups: [],
|
|
@@ -315,7 +231,6 @@ export class KnockGuideClient {
|
|
|
315
231
|
location,
|
|
316
232
|
// Increment to update the state store and trigger re-selection.
|
|
317
233
|
counter: 0,
|
|
318
|
-
debug,
|
|
319
234
|
});
|
|
320
235
|
|
|
321
236
|
// In server environments we might not have a socket connection.
|
|
@@ -530,9 +445,6 @@ export class KnockGuideClient {
|
|
|
530
445
|
case "guide_group.updated":
|
|
531
446
|
return this.addOrReplaceGuideGroup(payload);
|
|
532
447
|
|
|
533
|
-
case "guide.live_preview_updated":
|
|
534
|
-
return this.updatePreviewGuide(payload);
|
|
535
|
-
|
|
536
448
|
default:
|
|
537
449
|
return;
|
|
538
450
|
}
|
|
@@ -560,45 +472,6 @@ export class KnockGuideClient {
|
|
|
560
472
|
});
|
|
561
473
|
}
|
|
562
474
|
|
|
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
|
-
|
|
602
475
|
setDebug(debugOpts?: Omit<DebugState, "debugging">) {
|
|
603
476
|
this.knock.log("[Guide] .setDebug()");
|
|
604
477
|
|
|
@@ -1383,15 +1256,6 @@ export class KnockGuideClient {
|
|
|
1383
1256
|
});
|
|
1384
1257
|
}
|
|
1385
1258
|
|
|
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
|
-
|
|
1395
1259
|
// Define as an arrow func property to always bind this to the class instance.
|
|
1396
1260
|
private handleLocationChange = () => {
|
|
1397
1261
|
this.knock.log(`[Guide] .handleLocationChange`);
|
|
@@ -1403,43 +1267,9 @@ export class KnockGuideClient {
|
|
|
1403
1267
|
|
|
1404
1268
|
this.knock.log(`[Guide] Detected a location change: ${href}`);
|
|
1405
1269
|
|
|
1406
|
-
|
|
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);
|
|
1432
1271
|
};
|
|
1433
1272
|
|
|
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
|
-
|
|
1443
1273
|
private listenForLocationChangesFromWindow() {
|
|
1444
1274
|
const win = checkForWindow();
|
|
1445
1275
|
if (win?.history && win?.addEventListener) {
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
KnockGuideClient,
|
|
3
|
-
DEBUG_QUERY_PARAMS,
|
|
4
|
-
checkActivatable,
|
|
5
|
-
} from "./client";
|
|
1
|
+
export { KnockGuideClient, checkActivatable } from "./client";
|
|
6
2
|
export { checkStateIfThrottled } from "./helpers";
|
|
7
3
|
export type { ToolbarV2RunConfig } from "./helpers";
|
|
8
4
|
export type {
|
|
@@ -161,8 +161,7 @@ type SocketEventType =
|
|
|
161
161
|
| "guide.updated"
|
|
162
162
|
| "guide.removed"
|
|
163
163
|
| "guide_group.added"
|
|
164
|
-
| "guide_group.updated"
|
|
165
|
-
| "guide.live_preview_updated";
|
|
164
|
+
| "guide_group.updated";
|
|
166
165
|
|
|
167
166
|
type SocketEventPayload<E extends SocketEventType, D> = {
|
|
168
167
|
topic: string;
|
|
@@ -195,18 +194,12 @@ export type GuideGroupUpdatedEvent = SocketEventPayload<
|
|
|
195
194
|
{ guide_group: GuideGroupData }
|
|
196
195
|
>;
|
|
197
196
|
|
|
198
|
-
export type GuideLivePreviewUpdatedEvent = SocketEventPayload<
|
|
199
|
-
"guide.live_preview_updated",
|
|
200
|
-
{ guide: GuideData; eligible: boolean }
|
|
201
|
-
>;
|
|
202
|
-
|
|
203
197
|
export type GuideSocketEvent =
|
|
204
198
|
| GuideAddedEvent
|
|
205
199
|
| GuideUpdatedEvent
|
|
206
200
|
| GuideRemovedEvent
|
|
207
201
|
| GuideGroupAddedEvent
|
|
208
|
-
| GuideGroupUpdatedEvent
|
|
209
|
-
| GuideLivePreviewUpdatedEvent;
|
|
202
|
+
| GuideGroupUpdatedEvent;
|
|
210
203
|
|
|
211
204
|
//
|
|
212
205
|
// Guide client
|
|
@@ -285,7 +278,6 @@ export type TargetParams = {
|
|
|
285
278
|
|
|
286
279
|
export type ConstructorOpts = {
|
|
287
280
|
trackLocationFromWindow?: boolean;
|
|
288
|
-
trackDebugParams?: boolean;
|
|
289
281
|
orderResolutionDuration?: number;
|
|
290
282
|
throttleCheckInterval?: number;
|
|
291
283
|
};
|