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.

Reward activity log with feed, history sheet, actor chip and tooltip options

Kiran K2bf56388b63f
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx
index 01158d836e..0ed5bceeb5 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx
@@ -3,6 +3,7 @@
import useGroup from "@/lib/swr/use-group";
import type { GroupProps, RewardProps } from "@/lib/types";
import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";
+import { useRewardHistorySheet } from "@/ui/activity-logs/reward-history-sheet";
import { REWARD_EVENTS } from "@/ui/partners/constants";
import { ProgramRewardDescription } from "@/ui/partners/program-reward-description";
import {
@@ -11,7 +12,7 @@ import {
} from "@/ui/partners/rewards/add-edit-reward-sheet";
import { EventType } from "@dub/prisma/client";
import { Button, useRouterStuff } from "@dub/ui";
-import { cn } from "@dub/utils";
+import { cn, formatDate } from "@dub/utils";
import Link from "next/link";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";
@@ -139,12 +140,21 @@ const RewardItem = ({
reward: reward || undefined,
});
+ const {
+ hasActivityLogs,
+ rewardHistorySheet,
+ setIsOpen: setHistoryOpen,
+ } = useRewardHistorySheet({
+ reward: reward ?? null,
+ });
+
const Icon = REWARD_EVENTS[event].icon;
const As = reward ? Link : "div";
return (
<>
{RewardSheet}
+ {rewardHistorySheet}
<As
href={
reward
@@ -163,14 +173,45 @@ const RewardItem = ({
<Icon className="size-4 text-neutral-600" />
</div>
<div className="flex flex-1 flex-col justify-between gap-y-4 md:flex-row md:items-center">
- <div className="flex items-center gap-2">
+ <div className="flex w-full items-center gap-2">
{reward ? (
- <span className="text-sm font-normal">
- <ProgramRewardDescription
- reward={reward}
- amountClassName="text-blue-600"
- />
- </span>
+ <div className="flex min-w-0 flex-1 flex-col gap-1">
+ <div className="text-sm font-normal">
+ <ProgramRewardDescription
+ reward={reward}
+ amountClassName="text-blue-600"
+ />
+ </div>
+
+ <div className="flex items-center gap-1 text-xs font-medium text-neutral-500">
+ <span>
+ {`Updated ${formatDate(reward.updatedAt, {
+ month: "short",
+ day: "numeric",
+ year: "numeric",
+ })}`}
+ </span>
+
+ {hasActivityLogs && (
+ <>
+ <span
+ className="ml-1 size-1 shrink-0 rounded-full bg-neutral-400"
+ aria-hidden
+ />
+ <Button
+ variant="outline"
+ text="View history"
+ className="h-4 w-fit px-1 py-0.5 text-xs font-medium text-neutral-500"
+ onClick={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setHistoryOpen(true);
+ }}
+ />
+ </>
+ )}
+ </div>
+ </div>
) : (
<div className="flex flex-col">
<span className="text-sm font-medium text-neutral-900">
diff --git a/apps/web/lib/api/activity-log/track-reward-activity-log.ts b/apps/web/lib/api/activity-log/track-reward-activity-log.ts
index 01882e55b4..cb571feb02 100644
--- a/apps/web/lib/api/activity-log/track-reward-activity-log.ts
+++ b/apps/web/lib/api/activity-log/track-reward-activity-log.ts
@@ -17,6 +17,7 @@ interface TrackRewardActivityLogParams
function toRewardActivitySnapshot(reward: RewardProps) {
return {
+ event: reward.event,
type: reward.type,
amountInCents: reward.amountInCents ?? null,
amountInPercentage: reward.amountInPercentage ?? null,
@@ -27,16 +28,6 @@ function toRewardActivitySnapshot(reward: RewardProps) {
};
}
-function normalizeModifiers(modifiers: unknown): RewardConditionModifier[] {
- if (!Array.isArray(modifiers)) return [];
- return modifiers.filter(
- (m): m is RewardConditionModifier =>
- m != null &&
- typeof m === "object" &&
- Array.isArray((m as RewardConditionModifier).conditions),
- ) as RewardConditionModifier[];
-}
-
function modifierEquals(
a: RewardConditionModifier,
b: RewardConditionModifier,
@@ -45,9 +36,16 @@ function modifierEquals(
}
function buildModifierChangeSetEntries(
- oldModifiers: RewardConditions[],
- newModifiers: RewardConditions[],
+ oldReward: Omit<RewardProps, "id" | "updatedAt">,
+ newReward: Omit<RewardProps, "id" | "updatedAt">,
) {
+ const oldModifiers = Array.isArray(oldReward.modifiers)
+ ? oldReward.modifiers
+ : [];
+ const newModifiers = Array.isArray(newReward.modifiers)
+ ? newReward.modifiers
+ : [];
+
const logs: Pick<TrackActivityLogInput, "action" | "changeSet">[] = [];
const oldById = new Map<string, RewardConditions>();
@@ -73,8 +71,11 @@ function buildModifierChangeSetEntries(
logs.push({
action: "reward.conditionRemoved",
changeSet: {
- condition: {
- old: oldMod,
+ reward: {
+ old: {
+ ...oldReward,
+ modifiers: [oldMod],
+ },
new: null,
},
},
@@ -86,9 +87,15 @@ function buildModifierChangeSetEntries(
logs.push({
action: "reward.conditionUpdated",
changeSet: {
- condition: {
- old: oldMod,
- new: newMod,
+ reward: {
+ old: {
+ ...oldReward,
+ modifiers: [oldMod],
+ },
+ new: {
+ ...newReward,
+ modifiers: [newMod],
+ },
},
},
});
@@ -103,9 +110,12 @@ function buildModifierChangeSetEntries(
logs.push({
action: "reward.conditionAdded",
changeSet: {
- condition: {
+ reward: {
old: null,
- new: newMod,
+ new: {
+ ...newReward,
+ modifiers: [newMod],
+ },
},
},
});
@@ -165,20 +175,17 @@ export function trackRewardActivityLog({
activityLogs.push({
...baseInput,
action: "reward.updated",
- changeSet: diff,
+ changeSet: {
+ reward: {
+ old: oldSnapshot,
+ new: newSnapshot,
+ },
+ },
});
}
- // Find the reward modifiers diff
- const oldModifiers = Array.isArray(oldReward.modifiers)
- ? oldReward.modifiers
- : [];
- const newModifiers = Array.isArray(newReward.modifiers)
- ? newReward.modifiers
- : [];
-
activityLogs.push(
- ...buildModifierChangeSetEntries(oldModifiers, newModifiers),
+ ...buildModifierChangeSetEntries(oldSnapshot, newSnapshot),
);
}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 111364a344..1e8ae6d22c 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -825,8 +825,3 @@ export type FieldDiff = z.infer<typeof fieldDiffSchema>;
export type ChangeSet = Record<string, FieldDiff>;
export type ActivityLog = z.infer<typeof activityLogSchema>;
-
-export type ActivityLogResourceTypeWithFeed = Extract<
- ActivityLogResourceType,
- "partner" | "referral"
->;
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index b72eea5546..17205b9ba1 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -288,6 +288,7 @@ export const RewardSchema = z.object({
amountInPercentage: decimalToNumber,
maxDuration: z.number().nullish(),
modifiers: z.any().nullish(), // TODO: Fix this
+ updatedAt: z.coerce.date(),
});
export const REWARD_DESCRIPTION_MAX_LENGTH = 100;
diff --git a/apps/web/ui/activity-logs/action-renderers/reward-activity-renderer.tsx b/apps/web/ui/activity-logs/action-renderers/reward-activity-renderer.tsx
new file mode 100644
index 0000000000..b225ecd4cc
--- /dev/null
+++ b/apps/web/ui/activity-logs/action-renderers/reward-activity-renderer.tsx
@@ -0,0 +1,118 @@
+"use client";
+
+import { ActivityLog, ActivityLogAction, RewardProps } from "@/lib/types";
+import { ProgramRewardDescription } from "@/ui/partners/program-reward-description";
+import { ProgramRewardModifiersTooltipContent } from "@/ui/partners/program-reward-modifiers-tooltip";
+import { formatDate } from "@dub/utils";
+import { UserAvatar } from "../activity-entry-chips";
+
+interface RewardActivityConfig {
+ title: string;
+ field: "new" | "old";
+ displayModifiers: boolean;
+}
+
+const REWARD_ACTIVITY_CONFIG: Record<
+ Extract<
+ ActivityLogAction,
+ | "reward.created"
+ | "reward.updated"
+ | "reward.deleted"
+ | "reward.conditionAdded"
+ | "reward.conditionUpdated"
+ | "reward.conditionRemoved"
+ >,
+ RewardActivityConfig
+> = {
+ "reward.created": {
+ title: "Created reward",
+ field: "new",
+ displayModifiers: false,
+ },
+ "reward.updated": {
+ title: "Updated reward",
+ field: "new",
+ displayModifiers: false,
+ },
+ "reward.deleted": {
+ title: "Deleted reward",
+ field: "old",
+ displayModifiers: false,
+ },
+ "reward.conditionAdded": {
+ title: "Added reward condition",
+ field: "new",
+ displayModifiers: true,
+ },
+ "reward.conditionRemoved": {
+ title: "Removed reward condition",
+ field: "old",
+ displayModifiers: true,
+ },
+ "reward.conditionUpdated": {
+ title: "Updated reward condition",
+ field: "new",
+ displayModifiers: true,
+ },
+};
+
+interface RewardActivityRendererProps {
+ log: ActivityLog;
+}
+
+export function RewardActivityRenderer({ log }: RewardActivityRendererProps) {
+ if (!log?.action) {
+ return null;
+ }
+
+ const config = REWARD_ACTIVITY_CONFIG[log.action];
+ if (!config) {
+ return null;
+ }
+
+ const reward = log.changeSet?.reward?.[config.field] as
+ | RewardProps
+ | undefined;
+
+ if (!reward) {
+ return null;
+ }
+
+ return (
+ <div className="flex w-full flex-col gap-4">
+ <div className="flex items-center justify-between gap-2">
+ <div className="flex items-center gap-2">
+ <UserAvatar user={log.user} />
+ <span className="text-sm font-medium leading-5 text-neutral-800">
+ {config.title}
+ </span>
+ </div>
+
+ <time className="shrink-0 text-sm font-normal text-neutral-500">
+ {formatDate(log.createdAt, {
+ month: "short",
+ year: "numeric",
+ hour: "numeric",
+ minute: "2-digit",
+ })}
+ </time>
+ </div>
+
+ <div className="rounded-lg border border-neutral-200 bg-white px-3 py-2.5 text-xs font-medium leading-4 text-neutral-800">
+ {config.displayModifiers ? (
+ <ProgramRewardModifiersTooltipContent
+ reward={reward}
+ showBaseReward={false}
+ showBottomGradient={false}
+ className="p-0"
+ />
+ ) : (
+ <ProgramRewardDescription
+ reward={reward}
+ showModifiersTooltip={false}
+ />
+ )}
+ </div>
+ </div>
+ );
+}
diff --git a/apps/web/ui/activity-logs/activity-entry-chips.tsx b/apps/web/ui/activity-logs/activity-entry-chips.tsx
index 20a7c8dbdb..023bff2a15 100644
--- a/apps/web/ui/activity-logs/activity-entry-chips.tsx
+++ b/apps/web/ui/activity-logs/activity-entry-chips.tsx
@@ -2,7 +2,7 @@ import { ActivityLog, GroupProps, ProgramProps } from "@/lib/types";
import { getResourceColorData, RAINBOW_CONIC_GRADIENT } from "@/ui/colors";
import { ReferralStatusBadges } from "@/ui/referrals/referral-status-badges";
import { ReferralStatus } from "@dub/prisma/client";
-import { Bolt } from "@dub/ui";
+import { Bolt, Tooltip } from "@dub/ui";
import { cn, OG_AVATAR_URL } from "@dub/utils";
import { ReactNode } from "react";
import { useActivityLogContext } from "./activity-log-context";
@@ -132,3 +132,38 @@ export function ReferralStatusPill({ status }: { status: ReferralStatus }) {
return <ActivityChip className={badge.className}>{badge.label}</ActivityChip>;
}
+
+export function UserAvatar({ user }: { user: ActivityLog["user"] }) {
+ if (!user) return null;
+
+ const image = user.image || `${OG_AVATAR_URL}${user.id}`;
+ const name = user.name || user.email || "User";
+
+ return (
+ <Tooltip
+ content={
+ <div className="flex flex-col gap-1 p-2.5">
+ <img
+ src={image}
+ alt={name}
+ className="size-6 shrink-0 rounded-full"
+ />
+ <p className="text-sm font-medium text-neutral-900">
+ {user.name || user.email}
+ </p>
+ {user.email && user.name && (
+ <p className="text-xs text-neutral-500">{user.email}</p>
+ )}
+ </div>
+ }
+ >
+ <div>
+ <img
+ src={image}
+ alt={name}
+ className="size-4 shrink-0 rounded-full transition-transform duration-100 hover:scale-110 hover:cursor-pointer"
+ />
+ </div>
+ </Tooltip>
+ );
+}
diff --git a/apps/web/ui/activity-logs/activity-feed.tsx b/apps/web/ui/activity-logs/activity-feed.tsx
index ee7429c44c..ef469d2b18 100644
--- a/apps/web/ui/activity-logs/activity-feed.tsx
+++ b/apps/web/ui/activity-logs/activity-feed.tsx
@@ -1,19 +1,21 @@
-import { ActivityLog, ActivityLogResourceTypeWithFeed } from "@/lib/types";
+import { ActivityLog, ActivityLogResourceType } from "@/lib/types";
import { PartnerGroupActivityItem } from "@/ui/activity-logs/partner-group-activity-item";
import { ReferralActivityItem } from "@/ui/activity-logs/referral-activity-item";
+import { RewardActivityItem } from "@/ui/activity-logs/reward-activity-item";
import { ComponentType } from "react";
const ACTIVITY_ITEM_MAP: Record<
- ActivityLogResourceTypeWithFeed,
+ ActivityLogResourceType,
ComponentType<{ log: ActivityLog; isLast?: boolean }>
> = {
partner: PartnerGroupActivityItem,
referral: ReferralActivityItem,
+ reward: RewardActivityItem,
};
interface ActivityFeedProps {
logs: ActivityLog[];
- resourceType: ActivityLogResourceTypeWithFeed;
+ resourceType: ActivityLogResourceType;
}
export function ActivityFeed({ logs, resourceType }: ActivityFeedProps) {
diff --git a/apps/web/ui/activity-logs/activity-log-registry.tsx b/apps/web/ui/activity-logs/activity-log-registry.tsx
index 98cf80e90a..bda08fd735 100644
--- a/apps/web/ui/activity-logs/activity-log-registry.tsx
+++ b/apps/web/ui/activity-logs/activity-log-registry.tsx
@@ -1,9 +1,19 @@
import { ActivityLog, ActivityLogAction } from "@/lib/types";
-import { CircleInfo, FileSend, UserArrowRight, UserClock } from "@dub/ui";
+import {
+ CircleInfo,
+ FilePen,
+ FileSend,
+ Minus,
+ Plus,
+ SquareCheck,
+ UserArrowRight,
+ UserClock,
+} from "@dub/ui";
import { ComponentType, ReactNode } from "react";
import { PartnerGroupChangedRenderer } from "./action-renderers/partner-group-changed-renderer";
import { ReferralCreatedRenderer } from "./action-renderers/referral-created-renderer";
import { ReferralStatusChangedRenderer } from "./action-renderers/referral-status-changed-renderer";
+import { RewardActivityRenderer } from "./action-renderers/reward-activity-renderer";
export type ActorType = "USER" | "SYSTEM";
@@ -24,6 +34,12 @@ const ACTIVITY_LOG_ICONS: Partial<
"referral.unqualified": UserClock,
"referral.closedWon": UserClock,
"referral.closedLost": UserClock,
+ "reward.created": SquareCheck,
+ "reward.updated": FilePen,
+ "reward.deleted": Minus,
+ "reward.conditionAdded": Plus,
+ "reward.conditionRemoved": Minus,
+ "reward.conditionUpdated": FilePen,
};
const ACTIVITY_LOG_REGISTRY: Array<{
@@ -51,6 +67,19 @@ const ACTIVITY_LOG_REGISTRY: Array<{
action,
renderer: ReferralStatusChangedRenderer,
})),
+ ...(
+ [
+ "reward.created",
+ "reward.updated",
+ "reward.deleted",
+ "reward.conditionAdded",
+ "reward.conditionRemoved",
+ "reward.conditionUpdated",
+ ] as const
+ ).map((action) => ({
+ action,
+ renderer: RewardActivityRenderer,
+ })),
];
const renderers = new Map(
diff --git a/apps/web/ui/activity-logs/reward-activity-item.tsx b/apps/web/ui/activity-logs/reward-activity-item.tsx
new file mode 100644
index 0000000000..4340bc483e
--- /dev/null
+++ b/apps/web/ui/activity-logs/reward-activity-item.tsx
@@ -0,0 +1,44 @@
+import { ActivityLog } from "@/lib/types";
+import {
+ getActivityLogIcon,
+ getActivityLogRenderer,
+} from "./activity-log-registry";
+
+interface RewardActivityItemProps {
+ log: ActivityLog;
+ isLast?: boolean;
+}
+
+export function RewardActivityItem({
+ log,
+ isLast = false,
+}: RewardActivityItemProps) {
+ const icon = getActivityLogIcon(log);
+ const Renderer = getActivityLogRenderer(log.action);
+
+ if (!Renderer) {
+ return null;
+ }
+
+ return (
+ <li className="relative flex min-w-0 gap-3">
+ {!isLast && (
+ <div
+ className="absolute left-[11px] top-6 h-[calc(100%-8px)] w-px bg-neutral-200"
+ aria-hidden="true"
+ />
+ )}
+
+ <div
+ className="flex size-6 shrink-0 items-center justify-center text-neutral-500"
+ aria-hidden="true"
+ >
+ {icon}
+ </div>
+
+ <div className="flex min-w-0 flex-1 flex-col gap-2 overflow-hidden pb-4">
+ <Renderer log={log} />
+ </div>
+ </li>
+ );
+}
diff --git a/apps/web/ui/activity-logs/reward-activity-section.tsx b/apps/web/ui/activity-logs/reward-activity-section.tsx
new file mode 100644
index 0000000000..dde57ae794
--- /dev/null
+++ b/apps/web/ui/activity-logs/reward-activity-section.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import { useActivityLogs } from "@/lib/swr/use-activity-logs";
+import {
+ ActivityFeed,
+ ActivityFeedSkeleton,
+} from "@/ui/activity-logs/activity-feed";
+
+export function RewardActivitySection({ rewardId }: { rewardId: string }) {
+ const { activityLogs, loading, error } = useActivityLogs({
+ query: {
+ resourceType: "reward",
+ resourceId: rewardId,
+ },
+ enabled: !!rewardId,
+ });
+
+ if (loading) {
+ return <ActivityFeedSkeleton count={3} />;
+ }
+
+ if (error) {
+ return (
+ <div className="flex flex-col items-center justify-center py-8 text-center">
+ <p className="text-sm text-neutral-500">
+ Failed to load history. Please try again.
+ </p>
+ </div>
+ );
+ }
+
+ if (!activityLogs || activityLogs.length === 0) {
+ return (
+ <div className="flex flex-col items-center justify-center py-8 text-center">
+ <p className="text-sm text-neutral-500">No reward history yet</p>
+ </div>
+ );
+ }
+
+ return <ActivityFeed logs={activityLogs} resourceType="reward" />;
+}
diff --git a/apps/web/ui/activity-logs/reward-history-sheet.tsx b/apps/web/ui/activity-logs/reward-history-sheet.tsx
new file mode 100644
index 0000000000..75ac639b6f
--- /dev/null
+++ b/apps/web/ui/activity-logs/reward-history-sheet.tsx
@@ -0,0 +1,90 @@
+"use client";
+
+import { useActivityLogs } from "@/lib/swr/use-activity-logs";
+import { RewardProps } from "@/lib/types";
+import { X } from "@/ui/shared/icons";
+import { Button, Sheet } from "@dub/ui";
+import { Dispatch, SetStateAction, useState } from "react";
+import { RewardActivitySection } from "./reward-activity-section";
+
+const REWARD_EVENT_TITLES: Record<RewardProps["event"], string> = {
+ sale: "Sale reward history",
+ lead: "Lead reward history",
+ click: "Click reward history",
+};
+
+interface RewardHistorySheetProps {
+ reward: Pick<RewardProps, "id" | "event"> | null;
+ isOpen: boolean;
+ setIsOpen: Dispatch<SetStateAction<boolean>>;
+}
+
+function RewardHistorySheetContent({
+ reward,
+}: Omit<RewardHistorySheetProps, "isOpen" | "setIsOpen">) {
+ if (!reward) {
+ return null;
+ }
+
+ return (
+ <div className="flex size-full flex-col">
+ <div className="flex h-16 shrink-0 items-center justify-between border-b border-neutral-200 px-6 py-4">
+ <Sheet.Title className="text-lg font-semibold">
+ {REWARD_EVENT_TITLES[reward.event]}
+ </Sheet.Title>
+ <Sheet.Close asChild>
+ <Button
+ variant="outline"
+ icon={<X className="size-5" />}
+ className="h-auto w-fit p-1"
+ />
+ </Sheet.Close>
+ </div>
+
+ <div className="scrollbar-hide flex min-h-0 flex-1 flex-col overflow-y-auto p-4 sm:p-6">
+ <RewardActivitySection rewardId={reward.id} />
+ </div>
+ </div>
+ );
+}
+
+export function RewardHistorySheet({
+ isOpen,
+ ...rest
+}: RewardHistorySheetProps) {
+ return (
+ <Sheet open={isOpen} onOpenChange={rest.setIsOpen}>
+ <RewardHistorySheetContent {...rest} />
+ </Sheet>
+ );
+}
+
+export function useRewardHistorySheet({
+ reward,
+}: {
+ reward: Pick<RewardProps, "id" | "event"> | null;
+}) {
+ const [isOpen, setIsOpen] = useState(false);
+
+ const { activityLogs } = useActivityLogs({
+ query: reward
+ ? {
+ resourceType: "reward",
+ resourceId: reward.id,
+ }
+ : undefined,
+ enabled: !!reward?.id,
+ });
+
+ return {
+ hasActivityLogs: (activityLogs?.length ?? 0) > 0,
+ rewardHistorySheet: reward ? (
+ <RewardHistorySheet
+ reward={reward}
+ isOpen={isOpen}
+ setIsOpen={setIsOpen}
+ />
+ ) : null,
+ setIsOpen,
+ };
+}
diff --git a/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx b/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
index 7dc126c0d1..0d772b4602 100644
--- a/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
+++ b/apps/web/ui/partners/program-reward-modifiers-tooltip.tsx
@@ -2,27 +2,34 @@
import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";
import { getRewardAmount } from "@/lib/partners/get-reward-amount";
-import { RewardProps } from "@/lib/types";
+import { RewardConditions, RewardProps } from "@/lib/types";
import {
CONDITION_OPERATOR_LABELS,
REWARD_CONDITIONS,
rewardConditionSchema,
- rewardConditionsArraySchema,
rewardConditionsSchema,
} from "@/lib/zod/schemas/rewards";
import { InfoTooltip, useScrollProgress } from "@dub/ui";
import {
COUNTRIES,
capitalize,
+ cn,
currencyFormatter,
pluralize,
} from "@dub/utils";
import { useRef } from "react";
import * as z from "zod/v4";
-type ProgramRewardModifiersTooltipProps = {
- reward?: Omit<RewardProps, "id"> | null;
-};
+interface ProgramRewardModifiersTooltipProps {
+ reward?: Omit<RewardProps, "id" | "updatedAt"> | null;
+}
+
+interface ProgramRewardModifiersTooltipContentProps {
+ reward?: Omit<RewardProps, "id" | "updatedAt"> | null;
+ showBottomGradient?: boolean;
+ showBaseReward?: boolean;
+ className?: string;
+}
export function ProgramRewardModifiersTooltip({
reward,
@@ -34,7 +41,11 @@ export function ProgramRewardModifiersTooltip({
<InfoTooltip
content={
reward.tooltipDescription || (
- <ProgramRewardModifiersTooltipContent reward={reward} />
+ <ProgramRewardModifiersTooltipContent
+ reward={reward}
+ showBottomGradient={true}
+ showBaseReward={true}
+ />
)
}
contentClassName={reward.tooltipDescription ? "text-left" : undefined}
@@ -45,54 +56,64 @@ export function ProgramRewardModifiersTooltip({
export function ProgramRewardModifiersTooltipContent({
reward,
-}: ProgramRewardModifiersTooltipProps) {
+ showBottomGradient = true,
+ showBaseReward = true,
+ className,
+}: ProgramRewardModifiersTooltipContentProps & {
+ showBottomGradient?: boolean;
+ showBaseReward?: boolean;
+}) {
const scrollRef = useRef<HTMLDivElement>(null);
const { scrollProgress, updateScrollProgress } = useScrollProgress(scrollRef);
if (!reward?.modifiers?.length) return null;
- const showBaseReward = getRewardAmount(reward) !== 0;
+ const nonZeroBaseAmount = getRewardAmount(reward) !== 0;
+ const displayBaseReward = showBaseReward && nonZeroBaseAmount;
return (
<div className="relative">
<div
ref={scrollRef}
onScroll={updateScrollProgress}
- className="scrollbar-hide max-h-[calc(var(--radix-popper-available-height,100dvh)-12px)] max-w-sm space-y-2 overflow-y-auto p-3"
- >
- {showBaseReward && <RewardItem reward={reward} />}
- {(reward.modifiers as z.infer<typeof rewardConditionsArraySchema>).map(
- (modifier, idx) => (
- <div key={idx} className="space-y-2">
- {(showBaseReward || idx > 0) && (
- <span className="flex w-full items-center justify-center rounded bg-neutral-100 px-2 py-1 text-xs font-semibold text-neutral-600">
- OR
- </span>
- )}
-
- <RewardItem
- reward={{
- event: reward.event,
- type:
- modifier.type === undefined ? reward.type : modifier.type, // fallback to primary
- amountInCents: modifier.amountInCents,
- amountInPercentage: modifier.amountInPercentage,
- maxDuration:
- modifier.maxDuration === undefined
- ? reward.maxDuration
- : modifier.maxDuration, // fallback to primary
- }}
- conditions={modifier.conditions}
- operator={modifier.operator}
- />
- </div>
- ),
+ className={cn(
+ "scrollbar-hide max-h-[calc(var(--radix-popper-available-height,100dvh)-12px)] max-w-sm space-y-2 overflow-y-auto p-3",
+ className,
)}
+ >
+ {displayBaseReward && <RewardItem reward={reward} />}
+ {(reward.modifiers as RewardConditions[]).map((modifier, idx) => (
+ <div key={idx} className="space-y-2">
+ {(displayBaseReward || idx > 0) && (
+ <span className="flex w-full items-center justify-center rounded bg-neutral-100 px-2 py-1 text-xs font-semibold text-neutral-600">
+ OR
+ </span>
+ )}
+
+ <RewardItem
+ reward={{
+ event: reward.event,
+ type: modifier.type === undefined ? reward.type : modifier.type, // fallback to primary
+ amountInCents: modifier.amountInCents,
+ amountInPercentage: modifier.amountInPercentage,
+ maxDuration:
+ modifier.maxDuration === undefined
+ ? reward.maxDuration
+ : modifier.maxDuration, // fallback to primary
+ }}
+ conditions={modifier.conditions}
+ operator={modifier.operator}
+ />
+ </div>
+ ))}
</div>
- <div
- className="pointer-events-none absolute bottom-0 left-0 hidden h-16 w-full rounded-b-lg bg-gradient-to-t from-white sm:block"
- style={{ opacity: 1 - Math.pow(scrollProgress, 2) }}
- />
+
+ {showBottomGradient && (
+ <div
+ className="pointer-events-none absolute bottom-0 left-0 hidden h-16 w-full rounded-b-lg bg-gradient-to-t from-white sm:block"
+ style={{ opacity: 1 - Math.pow(scrollProgress, 2) }}
+ />
+ )}
</div>
);
}
@@ -102,7 +123,7 @@ const RewardItem = ({
conditions,
operator = "AND",
}: {
- reward: Omit<RewardProps, "id">;
+ reward: Omit<RewardProps, "id" | "updatedAt">;
conditions?: z.infer<typeof rewardConditionSchema>[];
operator?: z.infer<typeof rewardConditionsSchema>["operator"];
}) => {