Skip to content

Commit b49c1ad

Browse files
committed
refactor and more clean ups
1 parent d7660d3 commit b49c1ad

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import { detectToolbarParam } from "./helpers";
1717
import {
1818
InspectionResult,
19-
checkEligible,
2019
useInspectGuideClientStore,
2120
} from "./useInspectGuideClientStore";
2221

@@ -28,7 +27,7 @@ const GuidesList = ({
2827
displayOption: DisplayOption;
2928
}) => {
3029
return guides.map((guide, idx) => {
31-
if (displayOption === "all-eligible" && !checkEligible(guide)) {
30+
if (displayOption === "all-eligible" && !guide.annotation.isEligible) {
3231
return null;
3332
}
3433

packages/react/src/modules/guide/components/Toolbar/V2/useInspectGuideClientStore.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ import {
55
} from "@knocklabs/client";
66
import { useGuideContext, useStore } from "@knocklabs/react-core";
77

8-
export const checkEligible = (guide: AnnotatedGuide | MissingGuide) => {
9-
if (guide.__typename === "MissingGuide") return false;
10-
if (!guide.annotation.active.status) return false;
11-
if (!guide.annotation.targetable.status) return false;
12-
if (guide.annotation.archived.status) return false;
13-
8+
const resolveIsEligible = ({
9+
active,
10+
targetable,
11+
archived,
12+
}: Pick<GuideAnnotation, "active" | "targetable" | "archived">) => {
13+
if (!active.status) return false;
14+
if (!targetable.status) return false;
15+
if (archived.status) return false;
1416
return true;
1517
};
1618

19+
// Active: `true` status = good
1720
type ActiveStatus = {
1821
status: boolean;
1922
};
2023

24+
// Targetable: `true` status = good
2125
type TargetableStatusTrue = {
2226
status: true;
2327
};
@@ -28,18 +32,26 @@ type TargetableStatusFalse = {
2832
};
2933
type TargetableStatus = TargetableStatusTrue | TargetableStatusFalse;
3034

35+
// Archived: `false` status = good
3136
type ArchivedStatus = {
3237
status: boolean;
3338
};
3439

40+
type AnnotatedStatuses = {
41+
// Individual eligibility statuses:
42+
active: ActiveStatus;
43+
targetable: TargetableStatus;
44+
archived: ArchivedStatus;
45+
};
46+
47+
export type GuideAnnotation = AnnotatedStatuses & {
48+
// Resolved eligibility based on active, targetable and archived statuses,
49+
// which are backend driven evaluation results that are exposed for debugging.
50+
isEligible: boolean;
51+
};
52+
3553
export type AnnotatedGuide = KnockGuide & {
36-
annotation: {
37-
// true status = good
38-
active: ActiveStatus;
39-
targetable: TargetableStatus;
40-
// false status = good
41-
archived: ArchivedStatus;
42-
};
54+
annotation: GuideAnnotation;
4355
};
4456

4557
// Exists and ordered in control but absent in switchboard (therefore not
@@ -50,6 +62,9 @@ export type MissingGuide = {
5062
key: KnockGuide["key"];
5163
active: false;
5264
bypass_global_group_limit: false;
65+
annotation: {
66+
isEligible: false;
67+
};
5368
};
5469

5570
export type InspectionResult = {
@@ -96,12 +111,17 @@ const annotateGuide = (
96111
const { ineligibleGuides } = snapshot;
97112
const marker = ineligibleGuides[guide.key];
98113

99-
const annotation: AnnotatedGuide["annotation"] = {
114+
const statuses: AnnotatedStatuses = {
100115
active: { status: guide.active },
101116
targetable: marker ? toTargetableStatus(marker) : { status: true },
102117
archived: marker ? toArchivedStatus(marker) : { status: false },
103118
};
104119

120+
const annotation: GuideAnnotation = {
121+
...statuses,
122+
isEligible: resolveIsEligible(statuses),
123+
};
124+
105125
return {
106126
...guide,
107127
annotation,
@@ -114,6 +134,9 @@ const newMissingGuide = (key: KnockGuide["key"]) =>
114134
key,
115135
active: false,
116136
bypass_global_group_limit: false,
137+
annotation: {
138+
isEligible: false,
139+
},
117140
}) as MissingGuide;
118141

119142
export const useInspectGuideClientStore = (): InspectionResult | undefined => {

0 commit comments

Comments
 (0)