Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

code improvements

Pedro Ladeirac51a02c388b1
diff --git a/apps/web/lib/ai/review-reward-tooltip-schema.ts b/apps/web/lib/ai/review-reward-tooltip-schema.ts
index a2031878e8..ba1c34f9b2 100644
--- a/apps/web/lib/ai/review-reward-tooltip-schema.ts
+++ b/apps/web/lib/ai/review-reward-tooltip-schema.ts
@@ -145,7 +145,7 @@ export const reviewRewardTooltipInputSchema = z.object({
tooltip: z.string().min(1).max(4000),
description: z.string().nullish(),
basePayout: rewardPayoutSchema,
- modifiers: z.array(reviewRewardTooltipModifierSchema).min(1),
+ modifiers: z.array(reviewRewardTooltipModifierSchema),
});
export type TooltipSuggestion = z.infer<typeof tooltipSuggestionSchema>;
diff --git a/apps/web/lib/ai/review-reward-tooltip.ts b/apps/web/lib/ai/review-reward-tooltip.ts
index bf3b5effcf..59f4e23679 100644
--- a/apps/web/lib/ai/review-reward-tooltip.ts
+++ b/apps/web/lib/ai/review-reward-tooltip.ts
@@ -92,11 +92,6 @@ export async function screenRewardTooltipContradiction(
const answer = result.answers.contradicts;
if (answer?.type !== "boolean") {
- console.log("[screenRewardTooltipContradiction] jev result", {
- reward,
- answer,
- flagged: null,
- });
return { flagged: null };
}
@@ -104,12 +99,6 @@ export async function screenRewardTooltipContradiction(
Number.isFinite(answer.probability) &&
answer.probability > TOOLTIP_SUGGESTION_CONFIDENCE_FLOOR;
- console.log("[screenRewardTooltipContradiction] jev result", {
- reward,
- probability: answer.probability,
- flagged,
- });
-
return { flagged };
} catch (error) {
console.error("[screenRewardTooltipContradiction]", error);
diff --git a/apps/web/lib/rewards/validate-tooltip-suggestion.ts b/apps/web/lib/rewards/validate-tooltip-suggestion.ts
index 13eb7a3691..e68b90665f 100644
--- a/apps/web/lib/rewards/validate-tooltip-suggestion.ts
+++ b/apps/web/lib/rewards/validate-tooltip-suggestion.ts
@@ -15,6 +15,7 @@ import {
STRING_CONDITION_OPERATORS,
type RewardConditionEntityAttribute,
} from "@/lib/zod/schemas/rewards";
+import { COUNTRIES } from "@dub/utils";
import { EventType } from "@prisma/client";
type ConditionOperator = (typeof CONDITION_OPERATORS)[number];
@@ -306,16 +307,27 @@ function isValueValidForOperator({
return false;
}
+ const attributeEntry = getRewardConditionAttribute({
+ event,
+ entity,
+ attribute,
+ });
+ const allowedValues =
+ attributeEntry?.options?.map(({ id }) => id) ??
+ (attributeEntry?.id === "country" ? Object.keys(COUNTRIES) : undefined);
+ const isAllowedValue = (item: unknown) =>
+ typeof item === "string" &&
+ (!allowedValues || allowedValues.includes(item));
+
if (operator === "in" || operator === "not_in") {
- return Array.isArray(value);
+ return Array.isArray(value) && value.every(isAllowedValue);
}
if (Array.isArray(value)) {
return false;
}
- const attributeType =
- getRewardConditionAttribute({ event, entity, attribute })?.type ?? "string";
+ const attributeType = attributeEntry?.type ?? "string";
const numeric =
attributeType === "number" ||
@@ -328,7 +340,7 @@ function isValueValidForOperator({
return typeof value === "number" && !Number.isNaN(value);
}
- return typeof value === "string";
+ return isAllowedValue(value);
}
function valuesEqual(left: unknown, right: unknown): boolean {
diff --git a/apps/web/ui/partners/rewards/suggested-fix-popover.tsx b/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
index 12a2294553..59a1dd0315 100644
--- a/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
+++ b/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
@@ -46,6 +46,8 @@ export function SuggestedFixPopoverHost() {
acceptAll,
dismiss,
dismissAll,
+ popoverContentRef,
+ handlePopoverCloseAutoFocus,
} = consistency;
const activePage = pages[activeIndex];
@@ -61,10 +63,15 @@ export function SuggestedFixPopoverHost() {
sideOffset={8}
virtualAnchorRef={activeAnchorRef}
onOpenAutoFocus={(event) => event.preventDefault()}
+ onCloseAutoFocus={handlePopoverCloseAutoFocus}
popoverContentClassName={POPOVER_CONTENT_CLASS_NAME}
content={
activePage ? (
- <div onMouseEnter={cancelHide} onMouseLeave={scheduleHide}>
+ <div
+ ref={popoverContentRef}
+ onMouseEnter={cancelHide}
+ onMouseLeave={scheduleHide}
+ >
<SuggestedFixContent
suggestion={activePage.suggestion}
field={activePage.field}
@@ -141,16 +148,21 @@ export function SuggestedFixBadge({
onMouseLeave={() => {
if (!isMobile) consistency.scheduleHide();
}}
- onClick={() => {
- if (!isMobile) return;
+ onClick={(event) => {
+ const fromKeyboard = event.detail === 0;
+
+ if (!isMobile && !fromKeyboard) return;
- if (isOpen) {
+ if (isOpen && !fromKeyboard) {
consistency.hide();
return;
}
consistency.showPage(thisIndex);
+ if (fromKeyboard) consistency.focusPopoverContent();
}}
+ aria-haspopup="dialog"
+ aria-expanded={isOpen}
className={SUGGESTED_FIX_BADGE_CLASS_NAME}
>
<Sparkle3 variant="fill" className="size-3 shrink-0 text-[#E17100]" />
diff --git a/apps/web/ui/partners/rewards/use-reward-tooltip-consistency.ts b/apps/web/ui/partners/rewards/use-reward-tooltip-consistency.ts
index e58cb37a24..bd7e9f9d1f 100644
--- a/apps/web/ui/partners/rewards/use-reward-tooltip-consistency.ts
+++ b/apps/web/ui/partners/rewards/use-reward-tooltip-consistency.ts
@@ -70,7 +70,10 @@ type RewardTooltipConsistencyValue = {
open: boolean;
activeAnchorRef: MutableRefObject<HTMLElement | null>;
registerBadge: (key: string, element: HTMLElement | null) => void;
+ popoverContentRef: MutableRefObject<HTMLDivElement | null>;
showPage: (index: number) => void;
+ focusPopoverContent: () => void;
+ handlePopoverCloseAutoFocus: (event: Event) => void;
scheduleHide: () => void;
cancelHide: () => void;
hide: () => void;
@@ -148,6 +151,8 @@ export function useRewardTooltipConsistency({
const closeTimerRef = useRef<number | undefined>(undefined);
const badgeRefs = useRef(new Map<string, HTMLElement>());
const activeAnchorRef = useRef<HTMLElement | null>(null);
+ const popoverContentRef = useRef<HTMLDivElement | null>(null);
+ const returnFocusOnCloseRef = useRef(false);
const tooltip = stripRewardTooltipMarkdown(tooltipDescription ?? "");
const serializedReward = useMemo(
@@ -198,15 +203,9 @@ export function useRewardTooltipConsistency({
const cached = reviewCache.get(cacheKey);
if (cached?.v === REVIEW_CACHE_VERSION) {
- const cachedNote = noteForFlaggedReward({
- flagged: cached.flagged,
- suggestions: cached.suggestions,
- payoutFixes: cached.payoutFixes,
- note: cached.note,
- });
setSuggestions(cached.suggestions);
setPayoutFixes(cached.payoutFixes);
- setNote(cachedNote);
+ setNote(cached.note);
setStatus("idle");
setActiveIndex(0);
return;
@@ -287,7 +286,7 @@ export function useRewardTooltipConsistency({
});
reviewCache.set(cacheKey, {
v: REVIEW_CACHE_VERSION,
- flagged: true,
+ flagged: flagged === true,
suggestions: next,
payoutFixes: nextPayoutFixes,
note: nextNote,
@@ -311,7 +310,7 @@ export function useRewardTooltipConsistency({
if (nextNote) {
reviewCache.set(cacheKey, {
v: REVIEW_CACHE_VERSION,
- flagged: true,
+ flagged: flagged === true,
suggestions: [],
payoutFixes: [],
note: nextNote,
@@ -408,9 +407,29 @@ export function useRewardTooltipConsistency({
[cancelHide, pages],
);
+ const focusPopoverContent = useCallback(() => {
+ returnFocusOnCloseRef.current = true;
+ window.requestAnimationFrame(() => {
+ window.requestAnimationFrame(() => {
+ popoverContentRef.current
+ ?.querySelector<HTMLElement>("button")
+ ?.focus();
+ });
+ });
+ }, []);
+
+ const handlePopoverCloseAutoFocus = useCallback((event: Event) => {
+ if (!returnFocusOnCloseRef.current) return;
+
+ event.preventDefault();
+ returnFocusOnCloseRef.current = false;
+ activeAnchorRef.current?.focus();
+ }, []);
+
const scheduleHide = useCallback(() => {
cancelHide();
closeTimerRef.current = window.setTimeout(() => {
+ if (popoverContentRef.current?.contains(document.activeElement)) return;
setOpen(false);
}, HIDE_MS);
}, [cancelHide]);
@@ -538,7 +557,10 @@ export function useRewardTooltipConsistency({
open,
activeAnchorRef,
registerBadge,
+ popoverContentRef,
showPage,
+ focusPopoverContent,
+ handlePopoverCloseAutoFocus,
scheduleHide,
cancelHide,
hide,
@@ -629,9 +651,10 @@ function serializeRewardForReview({
if (baseReward?.type !== "flat" && baseReward?.type !== "percentage") {
return null;
}
- if (!isAiRewardEvent(event) || !modifiers?.length) return null;
+ if (!isAiRewardEvent(event)) return null;
+ const reviewModifiers = modifiers ?? [];
if (
- !modifiers.every(
+ !reviewModifiers.every(
(modifier) =>
!!modifier?.conditions?.length &&
modifier.conditions.every((condition) =>
@@ -652,15 +675,17 @@ function serializeRewardForReview({
return {
description: shownAs || null,
basePayout,
- modifiers: modifiers.map((modifier) => {
+ modifiers: reviewModifiers.map((modifier) => {
const type =
modifier?.type === "flat" || modifier?.type === "percentage"
? modifier.type
: basePayout.type;
const amount =
type === "percentage"
- ? modifier?.amountInPercentage ?? basePayout.amount
- : modifier?.amountInCents ?? basePayout.amount;
+ ? modifier?.amountInPercentage ??
+ (basePayout.type === "percentage" ? basePayout.amount : null)
+ : modifier?.amountInCents ??
+ (basePayout.type === "flat" ? basePayout.amount : null);
return {
operator: modifier?.operator ?? "AND",