Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions packages/client/src/clients/guide/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
const guide = state.previewGuides[guideKey] || state.guides[guideKey];
if (!guide) continue;

const affirmed = predicate(guide, {
const affirmed = predicate(guide, filters, {
location,
filters,
ineligibleGuides: state.ineligibleGuides,
debug: state.debug,
});

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

type PredicateOpts = {
location?: string | undefined;
filters?: SelectFilterParams | undefined;
debug: DebugState;
};
type PredicateOpts = Pick<
StoreState,
"location" | "ineligibleGuides" | "debug"
>;

const predicate = (
guide: KnockGuide,
{ location, filters = {}, debug = {} }: PredicateOpts,
filters: SelectFilterParams,
{ location, ineligibleGuides = {}, debug = {} }: PredicateOpts,
) => {
if (filters.type && filters.type !== guide.type) {
return false;
Expand All @@ -204,6 +204,11 @@ const predicate = (
return debug.forcedGuideKey === guide.key;
}

const ineligible = ineligibleGuides[guide.key];
if (ineligible) {
return false;
}

if (!guide.active) {
return false;
}
Expand Down Expand Up @@ -277,6 +282,7 @@ export class KnockGuideClient {
guideGroups: [],
guideGroupDisplayLogs: {},
guides: {},
ineligibleGuides: {},
previewGuides: {},
queries: {},
location,
Expand Down Expand Up @@ -358,14 +364,20 @@ export class KnockGuideClient {
>(this.channelId, queryParams);
queryStatus = { status: "ok" };

const { entries, guide_groups: groups, guide_group_display_logs } = data;
const {
entries,
guide_groups: groups,
guide_group_display_logs,
ineligible_guides,
} = data;

this.knock.log("[Guide] Loading fetched guides");
this.store.setState((state) => ({
...state,
guideGroups: groups?.length > 0 ? groups : [mockDefaultGroup(entries)],
guideGroupDisplayLogs: guide_group_display_logs || {},
guides: byKey(entries.map((g) => this.localCopy(g))),
ineligibleGuides: byKey(ineligible_guides || []),
queries: { ...state.queries, [queryKey]: queryStatus },
}));
} catch (e) {
Expand Down Expand Up @@ -466,6 +478,8 @@ export class KnockGuideClient {
private handleSocketEvent(payload: GuideSocketEvent) {
const { event, data } = payload;

// TODO(KNO-11489): Include an ineligible guide in the socket payload too
// and process it when handling socket events in real time.
switch (event) {
case "guide.added":
return this.addOrReplaceGuide(payload);
Expand Down
12 changes: 12 additions & 0 deletions packages/client/src/clients/guide/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export interface GuideGroupData {
updated_at: string;
}

export type GuideIneligibilityMarker = {
__typename: "GuideIneligibilityMarker";
key: KnockGuide["key"];
reason: string;
message: string;
};

export type GetGuidesQueryParams = {
data?: string;
tenant?: string;
Expand All @@ -76,6 +83,7 @@ export type GetGuidesResponse = {
entries: GuideData[];
guide_groups: GuideGroupData[];
guide_group_display_logs: Record<GuideGroupData["key"], string>;
ineligible_guides: GuideIneligibilityMarker[];
};

//
Expand Down Expand Up @@ -202,6 +210,10 @@ export type StoreState = {
guideGroups: GuideGroupData[];
guideGroupDisplayLogs: Record<GuideGroupData["key"], string>;
guides: Record<KnockGuide["key"], KnockGuide>;
ineligibleGuides: Record<
GuideIneligibilityMarker["key"],
GuideIneligibilityMarker
>;
previewGuides: Record<KnockGuide["key"], KnockGuide>;
queries: Record<QueryKey, QueryStatus>;
location: string | undefined;
Expand Down
Loading
Loading