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.

some ui improvements

Kiran K5b403b07cc04
diff --git a/apps/web/app/api/customers/[id]/activity/route.ts b/apps/web/app/api/customers/[id]/activity/route.ts
index cddddc4950..db8dd940fc 100644
--- a/apps/web/app/api/customers/[id]/activity/route.ts
+++ b/apps/web/app/api/customers/[id]/activity/route.ts
@@ -3,6 +3,7 @@ import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
import { withWorkspace } from "@/lib/auth";
import { CustomerActivity, SaleEvent } from "@/lib/types";
import { customerActivityResponseSchema } from "@/lib/zod/schemas/customers";
+import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
// GET /api/customers/[id]/activities - get a customer's activity
@@ -14,21 +15,35 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
id: customerId,
});
- const events = await getEvents({
- customerId: customer.id,
- event: "sales",
- order: "desc",
- sortBy: "timestamp",
- interval: "1y",
- page: 1,
- limit: 50,
- });
+ const [events, link] = await Promise.all([
+ getEvents({
+ customerId: customer.id,
+ event: "sales",
+ order: "desc",
+ sortBy: "timestamp",
+ interval: "1y",
+ page: 1,
+ limit: 50,
+ }),
+
+ prisma.link.findUnique({
+ where: {
+ id: customer.linkId!,
+ },
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ shortLink: true,
+ },
+ }),
+ ]);
- const activities: CustomerActivity[] = events.map((event: SaleEvent) => {
+ const activity: CustomerActivity[] = events.map((event: SaleEvent) => {
return {
timestamp: new Date(event.timestamp),
event: "sale",
- event_name: "payment made", // event.eventName
+ event_name: event.eventName,
metadata: {
amount: event.sale.amount,
payment_processor: event.sale.paymentProcessor,
@@ -37,7 +52,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
});
// Add lead event to activities
- activities.push({
+ activity.push({
timestamp: customer.leadCreatedAt!,
event: "lead",
event_name: "Account created",
@@ -45,7 +60,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
});
// Add click event to activities
- activities.push({
+ activity.push({
timestamp: customer.clickedAt!,
event: "click",
event_name: "Link clicked",
@@ -53,7 +68,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
});
// Find the LTV of the customer
- const ltv = activities.reduce((acc, { event, metadata }) => {
+ const ltv = activity.reduce((acc, { event, metadata }) => {
if (event === "sale" && metadata) {
acc += Number(metadata.amount);
}
@@ -68,7 +83,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
: 0;
// Find the time to first sale of the customer
- const firstSale = activities.filter(({ event }) => event === "sale").pop();
+ const firstSale = activity.filter(({ event }) => event === "sale").pop();
const timeToSale =
firstSale && customer.leadCreatedAt
@@ -82,7 +97,8 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
timeToLead,
timeToSale,
customer,
- activities,
+ activity,
+ link,
}),
);
});
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index a1f3ed69b4..f81bdeeb2d 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -1,4 +1,5 @@
import z from "@/lib/zod";
+import { LinkSchema } from "./links";
import { getPaginationQuerySchema } from "./misc";
export const createCustomerBodySchema = z.object({
@@ -115,6 +116,12 @@ export const customerActivityResponseSchema = z.object({
ltv: z.number(),
timeToLead: z.number(),
timeToSale: z.number(),
+ activity: z.array(customerActivitySchema),
customer: CustomerSchema,
- activities: z.array(customerActivitySchema),
+ link: LinkSchema.pick({
+ id: true,
+ domain: true,
+ key: true,
+ shortLink: true,
+ }),
});
diff --git a/apps/web/ui/analytics/events/row-menu-button.tsx b/apps/web/ui/analytics/events/row-menu-button.tsx
index 952ed404e2..ebae50c2e5 100644
--- a/apps/web/ui/analytics/events/row-menu-button.tsx
+++ b/apps/web/ui/analytics/events/row-menu-button.tsx
@@ -56,7 +56,7 @@ export function RowMenuButton({ row }: { row: Row<EventDatum> }) {
{"customer" in row.original && (
<MenuItem
icon={Copy}
- label="View activities"
+ label="View activity"
onSelect={() => {
setShowCustomerDetailsSheet(true);
setIsOpen(false);
diff --git a/apps/web/ui/partners/customer-details-sheet.tsx b/apps/web/ui/partners/customer-details-sheet.tsx
index d0ab12155a..37a2ad6d02 100644
--- a/apps/web/ui/partners/customer-details-sheet.tsx
+++ b/apps/web/ui/partners/customer-details-sheet.tsx
@@ -34,13 +34,6 @@ import { Dispatch, SetStateAction, useState } from "react";
import useSWR from "swr";
import { AnimatedEmptyState } from "../shared/animated-empty-state";
-// Fake link for now
-const link = {
- domain: "dub.sh",
- key: "with-webhook",
- shortLink: "https://dub.co/kiran",
-};
-
interface CustomerDetailsSheetProps {
customer: Customer;
setIsOpen: Dispatch<SetStateAction<boolean>>;
@@ -58,7 +51,8 @@ function CustomerDetailsSheetContent({
fetcher,
);
- console.log(customerActivity);
+ const link = customerActivity?.link;
+ const events = customerActivity?.activity;
return (
<>
@@ -89,7 +83,7 @@ function CustomerDetailsSheetContent({
</div>
<div className="flex min-w-[40%] shrink grow basis-1/2 flex-col items-end justify-end gap-2">
- {link && (
+ {link ? (
<Link
href={`/${slug}/events?domain=${link.domain}&key=${link.key}`}
target="_blank"
@@ -101,6 +95,8 @@ function CustomerDetailsSheetContent({
</span>
<FilterBars className="hidden size-3 transition-transform group-hover:block" />
</Link>
+ ) : (
+ <div className="h-4 w-20 animate-pulse rounded-full bg-neutral-200 px-1.5 py-0.5" />
)}
{customer.country && (
@@ -246,11 +242,11 @@ function CustomerDetailsSheetContent({
/>
) : (
<ul className="flex flex-col gap-6">
- {customerActivity?.activities.map((activity, index) => (
+ {(events || []).map((activity, index) => (
<Activity
activity={activity}
key={index}
- isLast={index === customerActivity?.activities.length - 1}
+ isLast={index === (events || []).length - 1}
/>
))}
</ul>
@@ -295,16 +291,18 @@ function Activity({
<Icon className="size-4" />
</div>
<span className="flex-grow text-sm text-neutral-700">
- <span className="font-medium text-neutral-700">
- {activity.metadata?.amount
- ? `${currencyFormatter(activity.metadata.amount / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })} `
- : null}
- </span>
-
{activity.event_name}
+
+ {activity.metadata?.amount && (
+ <span className="ml-1 font-medium text-neutral-700">
+ (
+ {currencyFormatter(activity.metadata.amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ })}
+ )
+ </span>
+ )}
</span>
<div className="text-sm text-neutral-500">
{month} at {time}