Skip to content

Commit df348dd

Browse files
committed
wip: take 2 of adding selectable status
1 parent bedf313 commit df348dd

7 files changed

Lines changed: 244 additions & 41 deletions

File tree

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

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
SelectFilterParams,
4747
SelectGuideOpts,
4848
SelectGuidesOpts,
49+
SelectQueryParams,
4950
StepMessageState,
5051
StoreState,
5152
TargetParams,
@@ -179,7 +180,7 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
179180
result.set(index, guide);
180181
}
181182

182-
result.metadata = { guideGroup: defaultGroup };
183+
result.metadata = { guideGroup: defaultGroup, filters };
183184
return result;
184185
};
185186

@@ -613,7 +614,22 @@ export class KnockGuideClient {
613614
`[Guide] .selectGuides (filters: ${formatFilters(filters)}; state: ${formatState(state)})`,
614615
);
615616

616-
const selectedGuide = this.selectGuide(state, filters, opts);
617+
const selectedGuide = this.selectGuide(state, filters, {
618+
...opts,
619+
// Do not record this selectGuide() query, since this is a short to check
620+
// throttling, and we should be recording the actual selectGuides query
621+
// (as needed).
622+
recordSelectQuery: false,
623+
});
624+
625+
// Record AFTER the selectGuide() query to ensure we have a group stage open
626+
// to be able to record.
627+
const { recordSelectQuery = !!state.debug?.debugging } = opts;
628+
this.maybeRecordSelectQuery(
629+
{ ...filters, limit: "one" },
630+
{ ...opts, recordSelectQuery },
631+
);
632+
617633
if (!selectedGuide) {
618634
return [];
619635
}
@@ -653,32 +669,6 @@ export class KnockGuideClient {
653669
return undefined;
654670
}
655671

656-
const result = select(state, filters);
657-
658-
if (result.size === 0) {
659-
this.knock.log("[Guide] Selection found zero result");
660-
return undefined;
661-
}
662-
663-
const [index, guide] = [...result][0]!;
664-
this.knock.log(
665-
`[Guide] Selection found: \`${guide.key}\` (total: ${result.size})`,
666-
);
667-
668-
// If a guide ignores the group limit, then return immediately to render
669-
// always.
670-
if (guide.bypass_global_group_limit) {
671-
this.knock.log(`[Guide] Returning the unthrottled guide: ${guide.key}`);
672-
return guide;
673-
}
674-
675-
// Check if inside the throttle window (i.e. throttled) and if so stop and
676-
// return undefined unless explicitly given the option to include throttled.
677-
if (!opts.includeThrottled && checkStateIfThrottled(state)) {
678-
this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);
679-
return undefined;
680-
}
681-
682672
// Starting here to the end of this method represents the core logic of how
683673
// "group stage" works. It provides a mechanism for 1) figuring out which
684674
// guide components are about to render on a page, 2) determining which
@@ -712,6 +702,40 @@ export class KnockGuideClient {
712702
this.stage = this.openGroupStage(); // Assign here to make tsc happy
713703
}
714704

705+
// Must come AFTER we ensure a group stage exists, so we can record select
706+
// queries.
707+
const { recordSelectQuery = !!state.debug?.debugging } = opts;
708+
this.maybeRecordSelectQuery(
709+
{ ...filters, limit: "one" },
710+
{ ...opts, recordSelectQuery },
711+
);
712+
713+
const result = select(state, filters);
714+
715+
if (result.size === 0) {
716+
this.knock.log("[Guide] Selection found zero result");
717+
return undefined;
718+
}
719+
720+
const [index, guide] = [...result][0]!;
721+
this.knock.log(
722+
`[Guide] Selection found: \`${guide.key}\` (total: ${result.size})`,
723+
);
724+
725+
// If a guide ignores the group limit, then return immediately to render
726+
// always.
727+
if (guide.bypass_global_group_limit) {
728+
this.knock.log(`[Guide] Returning the unthrottled guide: ${guide.key}`);
729+
return guide;
730+
}
731+
732+
// Check if inside the throttle window (i.e. throttled) and if so stop and
733+
// return undefined unless explicitly given the option to include throttled.
734+
if (!opts.includeThrottled && checkStateIfThrottled(state)) {
735+
this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);
736+
return undefined;
737+
}
738+
715739
switch (this.stage.status) {
716740
case "open": {
717741
this.knock.log(`[Guide] Adding to the group stage: ${guide.key}`);
@@ -740,6 +764,40 @@ export class KnockGuideClient {
740764
}
741765
}
742766

767+
private maybeRecordSelectQuery(
768+
params: SelectQueryParams,
769+
opts: SelectGuideOpts,
770+
) {
771+
if (!opts.recordSelectQuery) return;
772+
if (!this.stage || this.stage.status === "closed") return;
773+
if (!params.key && !params.type) return;
774+
775+
// Deep merge into the query logs:
776+
const queriesByKey = this.stage.queries.key || {};
777+
if (params.key) {
778+
queriesByKey[params.key] = {
779+
...(queriesByKey[params.key] || {}),
780+
...{ [params.limit]: opts },
781+
};
782+
}
783+
const queriesByType = this.stage.queries.type || {};
784+
if (params.type) {
785+
queriesByType[params.type] = {
786+
...(queriesByType[params.type] || {}),
787+
...{ [params.limit]: opts },
788+
};
789+
}
790+
791+
this.stage = {
792+
...this.stage,
793+
queries: { key: queriesByKey, type: queriesByType },
794+
};
795+
}
796+
797+
getStage() {
798+
return this.stage;
799+
}
800+
743801
private openGroupStage() {
744802
this.knock.log("[Guide] Opening a new group stage");
745803

@@ -755,6 +813,7 @@ export class KnockGuideClient {
755813
this.stage = {
756814
status: "open",
757815
ordered: [],
816+
queries: {},
758817
timeoutId,
759818
};
760819

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import {
99
StoreState,
1010
} from "./types";
1111

12+
type SelectionResultMetadata = {
13+
guideGroup: GuideGroupData;
14+
filters: SelectFilterParams;
15+
};
16+
1217
// Extends the map class to allow having metadata on it, which is used to record
1318
// the guide group context for the selection result (though currently only a
1419
// default global group is supported).
1520
export class SelectionResult<K = number, V = KnockGuide> extends Map<K, V> {
16-
metadata: { guideGroup: GuideGroupData } | undefined;
21+
metadata: SelectionResultMetadata | undefined;
1722

1823
constructor() {
1924
super();
@@ -233,3 +238,18 @@ export const predicateUrlPatterns = (
233238
}
234239
}, predicateDefault);
235240
};
241+
242+
// export type SelectorIdParams = {
243+
// filters: SelectFilterParams;
244+
// limit: "one" | "all";
245+
// };
246+
//
247+
// export const formatSelectorId = ({ filters, limit }: SelectorIdParams) => {
248+
//
249+
// // change to query params format
250+
// // key="asdf"&type
251+
//
252+
//
253+
// // Keep this ordering: limit, key, and type.
254+
// return JSON.stringify({ limit, key: filters.key, type: filters.type });
255+
// };

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {
33
DEBUG_QUERY_PARAMS,
44
checkActivatable,
55
} from "./client";
6+
export { checkStateIfThrottled } from "./helpers";
67
export type {
78
KnockGuide,
89
KnockGuideStep,
@@ -12,4 +13,5 @@ export type {
1213
SelectGuideOpts as KnockSelectGuideOpts,
1314
SelectGuidesOpts as KnockSelectGuidesOpts,
1415
StoreState as KnockGuideClientStoreState,
16+
GroupStage as KnockGuideClientGroupStage,
1517
} from "./types";

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export type SelectFilterParams = {
231231

232232
export type SelectGuideOpts = {
233233
includeThrottled?: boolean;
234+
recordSelectQuery?: boolean;
234235
};
235236

236237
export type SelectGuidesOpts = SelectGuideOpts;
@@ -247,9 +248,24 @@ export type ConstructorOpts = {
247248
throttleCheckInterval?: number;
248249
};
249250

251+
export type SelectQueryParams = SelectFilterParams & {
252+
limit: "one" | "all";
253+
};
254+
255+
type SelectGuideOptsByLimit = {
256+
one?: SelectGuideOpts;
257+
all?: SelectGuideOpts;
258+
};
259+
260+
type RecordedSelectQueries = {
261+
key?: Record<KnockGuide["key"], SelectGuideOptsByLimit>;
262+
type?: Record<KnockGuide["key"], SelectGuideOptsByLimit>;
263+
};
264+
250265
export type GroupStage = {
251266
status: "open" | "closed" | "patch";
252267
ordered: Array<KnockGuide["key"]>;
253268
resolved?: KnockGuide["key"];
269+
queries: RecordedSelectQueries;
254270
timeoutId: ReturnType<typeof setTimeout> | null;
255271
};

packages/react/src/modules/guide/components/Toolbar/V2/GuidesListDisplaySelect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const GuidesListDisplaySelect = ({ value, onChange }: Props) => {
2323
}}
2424
>
2525
<Select.Option size="1" value="current-page">
26-
Qualified guides on current page
26+
Resolved guides on current page
2727
</Select.Option>
2828
<Select.Option size="1" value="all-eligible">
2929
All eligible guides for user

packages/react/src/modules/guide/components/Toolbar/V2/V2.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
} from "./GuidesListDisplaySelect";
1616
import { detectToolbarParam } from "./helpers";
1717
import {
18+
checkActionable,
1819
checkEligible,
19-
checkUsable,
2020
useInspectGuideClientStore,
2121
} from "./useInspectGuideClientStore";
2222

@@ -42,6 +42,12 @@ export const V2 = () => {
4242
return null;
4343
}
4444

45+
console.log(
46+
result.guides.map(
47+
(g) => g.__typename !== "MissingGuide" && g.inspection.selectable,
48+
),
49+
);
50+
4551
return (
4652
<Box
4753
id="knock-guide-toolbar-v2"
@@ -103,7 +109,7 @@ export const V2 = () => {
103109
{result.guides.map((guide, idx) => {
104110
if (
105111
guidesListDisplayed === "current-page" &&
106-
!checkUsable(guide)
112+
!checkActionable(guide)
107113
) {
108114
return null;
109115
}

0 commit comments

Comments
 (0)