Skip to content
Draft
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
7 changes: 7 additions & 0 deletions packages/client/src/clients/guide/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ const predicate = (
return false;
}

return checkActivatable(guide, location);
};

export const checkActivatable = (
guide: KnockGuide,
location: string | undefined,
) => {
const url = location ? newUrl(location) : undefined;

const urlRules = guide.activation_url_rules || [];
Expand Down
6 changes: 5 additions & 1 deletion packages/client/src/clients/guide/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { KnockGuideClient, DEBUG_QUERY_PARAMS } from "./client";
export {
KnockGuideClient,
DEBUG_QUERY_PARAMS,
checkActivatable,
} from "./client";
export type {
KnockGuide,
KnockGuideStep,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Button } from "@telegraph/button";
import { Stack } from "@telegraph/layout";
import { Box, Stack } from "@telegraph/layout";
import { Tag } from "@telegraph/tag";
import { Tooltip } from "@telegraph/tooltip";
import { Text } from "@telegraph/typography";
import { CheckCircle2, CircleDashed, Eye, UserCircle2 } from "lucide-react";
import {
CheckCircle2,
CircleDashed,
Eye,
LocateFixed,
UserCircle2,
} from "lucide-react";
import * as React from "react";

import { GuideHoverCard } from "./GuideHoverCard";
Expand Down Expand Up @@ -39,6 +45,27 @@ export const GuideRow = ({ guide, orderIndex }: Props) => {
</Stack>

<Stack justify="flex-end">
{guide.__typename === "Guide" && (
<Stack gap="1">
<Tooltip
label="Current location does not match the activation conditions"
enabled={!guide.inspection.activatable.status}
>
<Button
px="1"
size="1"
variant="soft"
color={guide.inspection.activatable.status ? "green" : "red"}
leadingIcon={{ icon: LocateFixed, alt: "Target" }}
/>
</Tooltip>
</Stack>
)}
{guide.__typename === "Guide" && (
<Stack px="1" align="center">
<Box h="3" borderLeft="px" borderColor="gray-6" />
</Stack>
)}
<Stack gap="1">
{guide.__typename === "Guide" && (
<>
Expand All @@ -49,6 +76,7 @@ export const GuideRow = ({ guide, orderIndex }: Props) => {
: guide.inspection.targetable.message
}
// enabled={!guide.inspection.targetable.status}
enabled={!guide.inspection.targetable.status}
>
<Button
px="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ export const GuidesListDisplaySelect = ({ value, onChange }: Props) => {
style: { zIndex: MAX_Z_INDEX },
}}
>
{/*
<Select.Option size="1" value="current-page">
Displayable on current page
</Select.Option>
*/}
<Select.Option size="1" value="current-page">
Displayable on current page
</Select.Option>
<Select.Option size="1" value="all-eligible">
All eligible guides for user
</Select.Option>
Expand Down
10 changes: 9 additions & 1 deletion packages/react/src/modules/guide/components/Toolbar/V2/V2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import {
import { detectToolbarParam } from "./helpers";
import {
checkEligible,
checkUsable,
useInspectGuideClientStore,
} from "./useInspectGuideClientStore";

export const V2 = () => {
const { client } = useGuideContext();

const [guidesListDisplayed, setGuidesListDisplayed] =
React.useState<DisplayOption>("all-eligible");
React.useState<DisplayOption>("current-page");

const [isVisible, setIsVisible] = React.useState(detectToolbarParam());
const [isCollapsed, setIsCollapsed] = React.useState(true);
Expand Down Expand Up @@ -104,6 +105,13 @@ export const V2 = () => {
<Box w="full">
{data.error && <Box>{data.error}</Box>}
{data.guides.map((guide, idx) => {
if (
guidesListDisplayed === "current-page" &&
!checkUsable(guide)
) {
return null;
}

if (
guidesListDisplayed === "all-eligible" &&
!checkEligible(guide)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
KnockGuide,
KnockGuideClientStoreState,
KnockGuideIneligibilityMarker,
checkActivatable,
} from "@knocklabs/client";
import { useGuideContext, useStore } from "@knocklabs/react-core";

Expand All @@ -19,6 +20,10 @@ type TargetableStatusFalse = {
};
type TargetableStatus = TargetableStatusTrue | TargetableStatusFalse;

type ActivatableStatus = {
status: boolean;
};

type ArchivedStatus = {
status: boolean;
};
Expand All @@ -28,6 +33,7 @@ export type InspectedGuide = KnockGuide & {
// true status = good
active: ActiveStatus;
targetable: TargetableStatus;
activatable: ActivatableStatus;

// false status = good
archived: ArchivedStatus;
Expand All @@ -43,6 +49,14 @@ export const checkEligible = (guide: InspectedGuide | MissingGuide) => {
return true;
};

export const checkUsable = (guide: InspectedGuide | MissingGuide) => {
if (guide.__typename === "MissingGuide") return false;
if (!checkEligible(guide)) return false;
if (!guide.inspection.activatable.status) return false;

return true;
};

// Exists and ordered in control but absent in switchboard (therefore not
// included in the api response), which implies a newly created guide that has
// never been published to switchboard.
Expand Down Expand Up @@ -87,13 +101,15 @@ const toArchivedStatus = (

const inspectGuide = (
guide: KnockGuide,
ineligibleGuides: KnockGuideClientStoreState["ineligibleGuides"],
{ ineligibleGuides, location }: StoreStateSnapshot,
// ineligibleGuides: KnockGuideClientStoreState["ineligibleGuides"],
): InspectedGuide => {
const marker = ineligibleGuides[guide.key];

const inspection: InspectedGuide["inspection"] = {
active: { status: guide.active },
targetable: marker ? toTargetableStatus(marker) : { status: true },
activatable: { status: checkActivatable(guide, location) },
archived: marker ? toArchivedStatus(marker) : { status: false },
};

Expand All @@ -111,12 +127,18 @@ const newMissingGuide = (key: KnockGuide["key"]) =>
bypass_global_group_limit: false,
}) as MissingGuide;

type StoreStateSnapshot = Pick<
KnockGuideClientStoreState,
"location" | "guides" | "guideGroups" | "ineligibleGuides" | "debug"
>;

export const useInspectGuideClientStore = (): InspectionResult | undefined => {
const { client } = useGuideContext();

// Extract a snapshot of the client store state for debugging.
const snapshot = useStore(client.store, (state) => {
const snapshot: StoreStateSnapshot = useStore(client.store, (state) => {
return {
location: state.location,
guides: state.guides,
guideGroups: state.guideGroups,
ineligibleGuides: state.ineligibleGuides,
Expand Down Expand Up @@ -146,7 +168,7 @@ export const useInspectGuideClientStore = (): InspectionResult | undefined => {
return newMissingGuide(guideKey);
}

return inspectGuide(guide, snapshot.ineligibleGuides);
return inspectGuide(guide, snapshot);
});

return {
Expand Down
Loading