Skip to content

Commit 9c0e9d0

Browse files
authored
chore: handle ineligible_guides in guides fetch response (#844)
1 parent c2b343f commit 9c0e9d0

3 files changed

Lines changed: 222 additions & 18 deletions

File tree

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
164164
const guide = state.previewGuides[guideKey] || state.guides[guideKey];
165165
if (!guide) continue;
166166

167-
const affirmed = predicate(guide, {
167+
const affirmed = predicate(guide, filters, {
168168
location,
169-
filters,
169+
ineligibleGuides: state.ineligibleGuides,
170170
debug: state.debug,
171171
});
172172

@@ -179,15 +179,15 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
179179
return result;
180180
};
181181

182-
type PredicateOpts = {
183-
location?: string | undefined;
184-
filters?: SelectFilterParams | undefined;
185-
debug: DebugState;
186-
};
182+
type PredicateOpts = Pick<
183+
StoreState,
184+
"location" | "ineligibleGuides" | "debug"
185+
>;
187186

188187
const predicate = (
189188
guide: KnockGuide,
190-
{ location, filters = {}, debug = {} }: PredicateOpts,
189+
filters: SelectFilterParams,
190+
{ location, ineligibleGuides = {}, debug = {} }: PredicateOpts,
191191
) => {
192192
if (filters.type && filters.type !== guide.type) {
193193
return false;
@@ -204,6 +204,11 @@ const predicate = (
204204
return debug.forcedGuideKey === guide.key;
205205
}
206206

207+
const ineligible = ineligibleGuides[guide.key];
208+
if (ineligible) {
209+
return false;
210+
}
211+
207212
if (!guide.active) {
208213
return false;
209214
}
@@ -277,6 +282,7 @@ export class KnockGuideClient {
277282
guideGroups: [],
278283
guideGroupDisplayLogs: {},
279284
guides: {},
285+
ineligibleGuides: {},
280286
previewGuides: {},
281287
queries: {},
282288
location,
@@ -358,14 +364,20 @@ export class KnockGuideClient {
358364
>(this.channelId, queryParams);
359365
queryStatus = { status: "ok" };
360366

361-
const { entries, guide_groups: groups, guide_group_display_logs } = data;
367+
const {
368+
entries,
369+
guide_groups: groups,
370+
guide_group_display_logs,
371+
ineligible_guides,
372+
} = data;
362373

363374
this.knock.log("[Guide] Loading fetched guides");
364375
this.store.setState((state) => ({
365376
...state,
366377
guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],
367378
guideGroupDisplayLogs: guide_group_display_logs || {},
368379
guides: byKey(entries.map((g) => this.localCopy(g))),
380+
ineligibleGuides: byKey(ineligible_guides || []),
369381
queries: { ...state.queries, [queryKey]: queryStatus },
370382
}));
371383
} catch (e) {
@@ -466,6 +478,8 @@ export class KnockGuideClient {
466478
private handleSocketEvent(payload: GuideSocketEvent) {
467479
const { event, data } = payload;
468480

481+
// TODO(KNO-11489): Include an ineligible guide in the socket payload too
482+
// and process it when handling socket events in real time.
469483
switch (event) {
470484
case "guide.added":
471485
return this.addOrReplaceGuide(payload);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export interface GuideGroupData {
6565
updated_at: string;
6666
}
6767

68+
export type GuideIneligibilityMarker = {
69+
__typename: "GuideIneligibilityMarker";
70+
key: KnockGuide["key"];
71+
reason: string;
72+
message: string;
73+
};
74+
6875
export type GetGuidesQueryParams = {
6976
data?: string;
7077
tenant?: string;
@@ -76,6 +83,7 @@ export type GetGuidesResponse = {
7683
entries: GuideData[];
7784
guide_groups: GuideGroupData[];
7885
guide_group_display_logs: Record<GuideGroupData["key"], string>;
86+
ineligible_guides: GuideIneligibilityMarker[];
7987
};
8088

8189
//
@@ -202,6 +210,10 @@ export type StoreState = {
202210
guideGroups: GuideGroupData[];
203211
guideGroupDisplayLogs: Record<GuideGroupData["key"], string>;
204212
guides: Record<KnockGuide["key"], KnockGuide>;
213+
ineligibleGuides: Record<
214+
GuideIneligibilityMarker["key"],
215+
GuideIneligibilityMarker
216+
>;
205217
previewGuides: Record<KnockGuide["key"], KnockGuide>;
206218
queries: Record<QueryKey, QueryStatus>;
207219
location: string | undefined;

0 commit comments

Comments
 (0)