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.

Add ActivityRing component and update to program owner activity (#3342)

Marcus Farrellac98e0311fa6
diff --git a/apps/web/ui/partners/fraud-risks/partner-cross-program-summary.tsx b/apps/web/ui/partners/fraud-risks/partner-cross-program-summary.tsx
index ffb67e77f3..2007a29900 100644
--- a/apps/web/ui/partners/fraud-risks/partner-cross-program-summary.tsx
+++ b/apps/web/ui/partners/fraud-risks/partner-cross-program-summary.tsx
@@ -1,7 +1,7 @@
"use client";
import { usePartnerCrossProgramSummary } from "@/lib/swr/use-partner-cross-program-summary";
-import { UserCheck, UserXmark } from "@dub/ui";
+import { ActivityRing, User, UserCheck, UserXmark } from "@dub/ui";
export function PartnerCrossProgramSummary({
partnerId,
@@ -12,49 +12,78 @@ export function PartnerCrossProgramSummary({
partnerId,
});
- const crossProgramMetrics = [
- {
- icon: UserCheck,
- text: "Marked as trustworthy",
- total: crossProgramSummary?.totalPrograms,
- value: crossProgramSummary?.trustedPrograms,
- },
- {
- icon: UserXmark,
- text: "Removed from programs",
- total: crossProgramSummary?.totalPrograms,
- value: crossProgramSummary?.removedPrograms,
- },
- ];
+ if (isLoading || !crossProgramSummary) {
+ return <LoadingSkeleton />;
+ }
+
+ const { totalPrograms, trustedPrograms, removedPrograms } =
+ crossProgramSummary;
return (
- <>
- {crossProgramMetrics.map((item) => (
- <div
- key={item.text}
- className="flex items-center justify-between gap-2"
- >
- <div className="flex flex-grow items-center gap-2 sm:w-64">
- <item.icon className="size-4 shrink-0" />
- <span className="text-xs font-medium text-neutral-700">
- {item.text}
- </span>
- </div>
+ <div className="flex items-center gap-3">
+ <ActivityRing
+ positiveValue={trustedPrograms}
+ negativeValue={removedPrograms}
+ positiveIcon={UserCheck}
+ negativeIcon={UserXmark}
+ neutralIcon={User}
+ />
+ <div className="flex min-w-0 grow flex-col gap-[5px]">
+ <StatRow
+ label="Marked as trustworthy"
+ value={trustedPrograms}
+ total={totalPrograms}
+ />
+ <StatRow
+ label="Removed from programs"
+ value={removedPrograms}
+ total={totalPrograms}
+ />
+ </div>
+ </div>
+ );
+}
- <div className="w-10">
- {isLoading || !crossProgramSummary ? (
- <div className="h-4 w-full animate-pulse justify-end rounded bg-neutral-200" />
- ) : (
- <div className="flex items-center justify-end gap-1 text-xs font-medium">
- <span className="text-neutral-700">{item.value || 0}</span>
- <span className="whitespace-nowrap text-neutral-400">
- of {item.total || 0}
- </span>
- </div>
- )}
+function StatRow({
+ label,
+ value,
+ total,
+}: {
+ label: string;
+ value: number;
+ total: number;
+}) {
+ return (
+ <div className="flex items-center justify-between gap-6">
+ <span className="text-xs font-medium text-neutral-700">{label}</span>
+ <div className="flex items-center gap-1 text-xs">
+ <span className="font-semibold text-neutral-800">{value}</span>
+ <span className="font-medium text-neutral-500">of {total}</span>
+ </div>
+ </div>
+ );
+}
+
+function LoadingSkeleton() {
+ return (
+ <div className="flex items-center gap-3">
+ <div className="size-10 shrink-0 animate-pulse rounded-full bg-neutral-200" />
+ <div className="flex min-w-0 grow flex-col gap-[5px]">
+ <div className="flex items-center justify-between gap-6">
+ <div className="h-4 w-28 animate-pulse rounded bg-neutral-200" />
+ <div className="flex items-center gap-1">
+ <div className="h-4 w-4 animate-pulse rounded bg-neutral-200" />
+ <div className="h-4 w-7 animate-pulse rounded bg-neutral-200" />
+ </div>
+ </div>
+ <div className="flex items-center justify-between gap-6">
+ <div className="h-4 w-32 animate-pulse rounded bg-neutral-200" />
+ <div className="flex items-center gap-1">
+ <div className="h-4 w-4 animate-pulse rounded bg-neutral-200" />
+ <div className="h-4 w-7 animate-pulse rounded bg-neutral-200" />
</div>
</div>
- ))}
- </>
+ </div>
+ </div>
);
}
diff --git a/packages/ui/src/activity-ring.tsx b/packages/ui/src/activity-ring.tsx
new file mode 100644
index 0000000000..384b900e48
--- /dev/null
+++ b/packages/ui/src/activity-ring.tsx
@@ -0,0 +1,270 @@
+import { cn } from "@dub/utils";
+import { ComponentType, SVGProps, useMemo } from "react";
+
+type IconComponent = ComponentType<SVGProps<SVGSVGElement>>;
+
+interface ActivityRingProps {
+ /** Value for the positive/trustworthy side */
+ positiveValue: number;
+ /** Value for the negative/removed side */
+ negativeValue: number;
+ /** Size of the ring in pixels (default: 40) */
+ size?: number;
+ /** Icon to show when positive leads */
+ positiveIcon?: IconComponent;
+ /** Icon to show when negative leads */
+ negativeIcon?: IconComponent;
+ /** Icon to show when neutral (tie) */
+ neutralIcon?: IconComponent;
+ className?: string;
+}
+
+const COLORS = {
+ positive: "#00C951", // green ring
+ negative: "#FB2C36", // red ring
+ neutral: "#e5e5e5", // neutral-200 for rings
+ positiveIcon: "#166534", // green-800
+ negativeIcon: "#991b1b", // red-800
+ neutralIcon: "#262626", // neutral-800
+};
+
+// Gap angle in degrees at each connection point
+const GAP_ANGLE = 15;
+
+// Minimum arc sweep to ensure visibility when value > 0
+const MIN_ARC_DEGREES = 30;
+
+function polarToCartesian(
+ cx: number,
+ cy: number,
+ r: number,
+ angleDeg: number,
+): { x: number; y: number } {
+ const angleRad = ((angleDeg - 90) * Math.PI) / 180;
+ return {
+ x: cx + r * Math.cos(angleRad),
+ y: cy + r * Math.sin(angleRad),
+ };
+}
+
+function describeArc(
+ cx: number,
+ cy: number,
+ r: number,
+ startAngle: number,
+ endAngle: number,
+): string {
+ const start = polarToCartesian(cx, cy, r, startAngle);
+ const end = polarToCartesian(cx, cy, r, endAngle);
+ const sweep = endAngle - startAngle;
+ const largeArcFlag = sweep > 180 ? 1 : 0;
+
+ return `M ${start.x} ${start.y} A ${r} ${r} 0 ${largeArcFlag} 1 ${end.x} ${end.y}`;
+}
+
+// Creates a filled arc segment (donut slice) between outer and inner radius
+function describeFilledArc(
+ cx: number,
+ cy: number,
+ outerRadius: number,
+ innerRadius: number,
+ startAngle: number,
+ endAngle: number,
+): string {
+ const outerStart = polarToCartesian(cx, cy, outerRadius, startAngle);
+ const outerEnd = polarToCartesian(cx, cy, outerRadius, endAngle);
+ const innerStart = polarToCartesian(cx, cy, innerRadius, startAngle);
+ const innerEnd = polarToCartesian(cx, cy, innerRadius, endAngle);
+ const sweep = endAngle - startAngle;
+ const largeArcFlag = sweep > 180 ? 1 : 0;
+
+ return [
+ `M ${outerStart.x} ${outerStart.y}`, // Start at outer arc
+ `A ${outerRadius} ${outerRadius} 0 ${largeArcFlag} 1 ${outerEnd.x} ${outerEnd.y}`, // Outer arc
+ `L ${innerEnd.x} ${innerEnd.y}`, // Line to inner arc
+ `A ${innerRadius} ${innerRadius} 0 ${largeArcFlag} 0 ${innerStart.x} ${innerStart.y}`, // Inner arc (reverse)
+ "Z", // Close path
+ ].join(" ");
+}
+
+type RingState = "positive" | "negative" | "neutral";
+
+export function ActivityRing({
+ positiveValue,
+ negativeValue,
+ size = 40,
+ positiveIcon: PositiveIcon,
+ negativeIcon: NegativeIcon,
+ neutralIcon: NeutralIcon,
+ className,
+}: ActivityRingProps) {
+ const state: RingState = useMemo(() => {
+ if (positiveValue > negativeValue) return "positive";
+ if (negativeValue > positiveValue) return "negative";
+ return "neutral";
+ }, [positiveValue, negativeValue]);
+
+ // Calculate arc angles based on ratio
+ const { positiveArc, negativeArc } = useMemo(() => {
+ const total = positiveValue + negativeValue;
+
+ // Available sweep angle (360 - 2 gaps)
+ const availableSweep = 360 - GAP_ANGLE * 2;
+
+ if (total === 0) {
+ // Neutral: equal arcs at 50% each
+ const halfSweep = availableSweep / 2;
+ return {
+ // Right side: from top going clockwise to bottom
+ positiveArc: { start: GAP_ANGLE / 2, end: GAP_ANGLE / 2 + halfSweep },
+ // Left side: from bottom going clockwise to top
+ negativeArc: {
+ start: 180 + GAP_ANGLE / 2,
+ end: 180 + GAP_ANGLE / 2 + halfSweep,
+ },
+ };
+ }
+
+ const positiveRatio = positiveValue / total;
+
+ // Calculate sweeps with minimum visibility
+ let positiveSweep = positiveRatio * availableSweep;
+ let negativeSweep = (1 - positiveRatio) * availableSweep;
+
+ // Ensure minimum visibility for non-zero values
+ if (positiveValue > 0 && positiveSweep < MIN_ARC_DEGREES) {
+ positiveSweep = MIN_ARC_DEGREES;
+ negativeSweep = availableSweep - MIN_ARC_DEGREES;
+ }
+ if (negativeValue > 0 && negativeSweep < MIN_ARC_DEGREES) {
+ negativeSweep = MIN_ARC_DEGREES;
+ positiveSweep = availableSweep - MIN_ARC_DEGREES;
+ }
+
+ return {
+ // Right side: from top going clockwise
+ positiveArc: {
+ start: GAP_ANGLE / 2,
+ end: GAP_ANGLE / 2 + positiveSweep,
+ },
+ // Left side: from where positive ends + gap, going clockwise
+ negativeArc: {
+ start: GAP_ANGLE / 2 + positiveSweep + GAP_ANGLE,
+ end: GAP_ANGLE / 2 + positiveSweep + GAP_ANGLE + negativeSweep,
+ },
+ };
+ }, [positiveValue, negativeValue]);
+
+ const center = size / 2;
+ const strokeWidth = 3;
+ const outerRadius = (size - strokeWidth) / 2; // Stroke centered on this radius
+ const fillOuterRadius = outerRadius - strokeWidth / 2; // Inner edge of stroke
+ const fillInnerRadius = size * 0.28; // Inner radius for filled area
+
+ const IconComponent = useMemo(() => {
+ switch (state) {
+ case "positive":
+ return PositiveIcon;
+ case "negative":
+ return NegativeIcon;
+ default:
+ return NeutralIcon;
+ }
+ }, [state, PositiveIcon, NegativeIcon, NeutralIcon]);
+
+ const iconColor = useMemo(() => {
+ switch (state) {
+ case "positive":
+ return COLORS.positiveIcon;
+ case "negative":
+ return COLORS.negativeIcon;
+ default:
+ return COLORS.neutralIcon;
+ }
+ }, [state]);
+
+ // Always use green and red colors
+ const positiveColor = COLORS.positive;
+ const negativeColor = COLORS.negative;
+
+ return (
+ <div
+ className={cn("relative shrink-0", className)}
+ style={{ width: size, height: size }}
+ >
+ <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`}>
+ {/* Filled arc for positive (behind stroke) */}
+ {positiveValue > 0 && (
+ <path
+ d={describeFilledArc(
+ center,
+ center,
+ fillOuterRadius,
+ fillInnerRadius,
+ positiveArc.start,
+ positiveArc.end,
+ )}
+ fill={COLORS.positive}
+ opacity={0.1}
+ />
+ )}
+
+ {/* Filled arc for negative (behind stroke) */}
+ {negativeValue > 0 && (
+ <path
+ d={describeFilledArc(
+ center,
+ center,
+ fillOuterRadius,
+ fillInnerRadius,
+ negativeArc.start,
+ negativeArc.end,
+ )}
+ fill={COLORS.negative}
+ opacity={0.1}
+ />
+ )}
+
+ {/* Positive arc stroke (right side) */}
+ <path
+ d={describeArc(
+ center,
+ center,
+ outerRadius,
+ positiveArc.start,
+ positiveArc.end,
+ )}
+ stroke={positiveColor}
+ strokeWidth={strokeWidth}
+ fill="none"
+ strokeLinecap="round"
+ />
+
+ {/* Negative arc stroke (left side) */}
+ <path
+ d={describeArc(
+ center,
+ center,
+ outerRadius,
+ negativeArc.start,
+ negativeArc.end,
+ )}
+ stroke={negativeColor}
+ strokeWidth={strokeWidth}
+ fill="none"
+ strokeLinecap="round"
+ />
+ </svg>
+
+ {/* Centered icon */}
+ {IconComponent && (
+ <div
+ className="absolute inset-0 flex items-center justify-center"
+ style={{ color: iconColor }}
+ >
+ <IconComponent className="size-3.5" />
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index 489f4be03a..8a8ddba900 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -3,6 +3,7 @@ import "./styles.css";
// components
export * from "./accordion";
+export * from "./activity-ring";
export * from "./alert";
export * from "./avatar";
export * from "./badge";