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 filter by linkId

Steven Tey3e76d115b616
diff --git a/apps/web/app/api/customers/count/route.ts b/apps/web/app/api/customers/count/route.ts
index 76653fbf7c..3002733ccb 100644
--- a/apps/web/app/api/customers/count/route.ts
+++ b/apps/web/app/api/customers/count/route.ts
@@ -31,11 +31,9 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => {
// Get customer count by country
if (groupBy === "country") {
- const customers = await prisma.customer.groupBy({
+ const data = await prisma.customer.groupBy({
by: ["country"],
- where: {
- ...commonWhere,
- },
+ where: commonWhere,
_count: true,
orderBy: {
_count: {
@@ -44,7 +42,38 @@ export const GET = withWorkspace(async ({ workspace, searchParams }) => {
},
});
- return NextResponse.json(customers);
+ return NextResponse.json(data);
+ }
+
+ // Get customer count by linkId
+ if (groupBy === "linkId") {
+ const data = await prisma.customer.groupBy({
+ by: ["linkId"],
+ where: { ...commonWhere, linkId: { not: null } },
+ _count: true,
+ orderBy: {
+ _count: {
+ linkId: "desc",
+ },
+ },
+ });
+
+ const links = await prisma.link.findMany({
+ where: {
+ id: { in: data.map(({ linkId }) => linkId!) },
+ },
+ select: {
+ id: true,
+ shortLink: true,
+ },
+ });
+
+ return NextResponse.json(
+ data.map(({ linkId }) => ({
+ ...data.find(({ linkId: id }) => id === linkId)!,
+ shortLink: links.find(({ id }) => id === linkId)?.shortLink,
+ })),
+ );
}
const count = await prisma.customer.count({
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 1134beea89..4e40cab7b3 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -71,7 +71,7 @@ export const getCustomersCountQuerySchema = getCustomersQuerySchema
sortBy: true,
sortOrder: true,
})
- .extend({ groupBy: z.enum(["country"]).optional() });
+ .extend({ groupBy: z.enum(["country", "linkId"]).optional() });
export const createCustomerBodySchema = z.object({
email: z
diff --git a/apps/web/ui/customers/customer-table/customer-table.tsx b/apps/web/ui/customers/customer-table/customer-table.tsx
index 51e414badd..3d96668abe 100644
--- a/apps/web/ui/customers/customer-table/customer-table.tsx
+++ b/apps/web/ui/customers/customer-table/customer-table.tsx
@@ -106,9 +106,26 @@ export function CustomerTable() {
},
},
{
- id: "createdAt",
- header: "Created date",
- accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
+ id: "country",
+ header: "Country",
+ minSize: 150,
+ cell: ({ row }) => {
+ const country = row.original.country;
+ return (
+ <div className="flex items-center gap-2">
+ {country && (
+ <img
+ alt={`${country} flag`}
+ src={`https://hatscripts.github.io/circle-flags/flags/${country.toLowerCase()}.svg`}
+ className="size-4 shrink-0"
+ />
+ )}
+ <span className="min-w-0 truncate">
+ {(country ? COUNTRIES[country] : null) ?? "-"}
+ </span>
+ </div>
+ );
+ },
},
{
id: "saleAmount",
@@ -127,6 +144,11 @@ export function CustomerTable() {
</div>
),
},
+ {
+ id: "createdAt",
+ header: "Created date",
+ accessorFn: (d) => formatDate(d.createdAt, { month: "short" }),
+ },
{
id: "link",
header: "Referral link",
@@ -173,28 +195,6 @@ export function CustomerTable() {
"-"
),
},
- {
- id: "country",
- header: "Country",
- minSize: 150,
- cell: ({ row }) => {
- const country = row.original.country;
- return (
- <div className="flex items-center gap-2">
- {country && (
- <img
- alt={`${country} flag`}
- src={`https://hatscripts.github.io/circle-flags/flags/${country.toLowerCase()}.svg`}
- className="size-4 shrink-0"
- />
- )}
- <span className="min-w-0 truncate">
- {(country ? COUNTRIES[country] : null) ?? "-"}
- </span>
- </div>
- );
- },
- },
// Menu
{
id: "menu",
diff --git a/apps/web/ui/customers/customer-table/use-column-visibility.ts b/apps/web/ui/customers/customer-table/use-column-visibility.ts
index fe70b4ff88..dc5450d563 100644
--- a/apps/web/ui/customers/customer-table/use-column-visibility.ts
+++ b/apps/web/ui/customers/customer-table/use-column-visibility.ts
@@ -2,15 +2,8 @@ import { useLocalStorage } from "@dub/ui";
import { VisibilityState } from "@tanstack/react-table";
export const customersColumns = {
- all: ["customer", "createdAt", "saleAmount", "link", "externalId", "country"],
- defaultVisible: [
- "customer",
- "createdAt",
- "saleAmount",
- "link",
- "externalId",
- "country",
- ],
+ all: ["customer", "country", "saleAmount", "createdAt", "link", "externalId"],
+ defaultVisible: ["customer", "country", "saleAmount", "createdAt", "link"],
};
export function useColumnVisibility() {
diff --git a/apps/web/ui/customers/customer-table/use-customer-filters.tsx b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
index faa320c0a8..ddc331b946 100644
--- a/apps/web/ui/customers/customer-table/use-customer-filters.tsx
+++ b/apps/web/ui/customers/customer-table/use-customer-filters.tsx
@@ -1,8 +1,8 @@
import useCustomersCount from "@/lib/swr/use-customers-count";
import useWorkspace from "@/lib/swr/use-workspace";
import { useRouterStuff } from "@dub/ui";
-import { FlagWavy } from "@dub/ui/icons";
-import { COUNTRIES, nFormatter } from "@dub/utils";
+import { FlagWavy, Hyperlink } from "@dub/ui/icons";
+import { COUNTRIES, getPrettyUrl, nFormatter } from "@dub/utils";
import { useCallback, useMemo } from "react";
export function useCustomerFilters(
@@ -10,7 +10,7 @@ export function useCustomerFilters(
{ enabled = true }: { enabled?: boolean } = {},
) {
const { searchParamsObj, queryParams } = useRouterStuff();
- const { id: workspaceId } = useWorkspace();
+ const { id: workspaceId, slug } = useWorkspace();
const { data: countriesCount } = useCustomersCount<
| {
@@ -25,6 +25,20 @@ export function useCustomerFilters(
enabled,
});
+ const { data: linksCount } = useCustomersCount<
+ | {
+ linkId: string;
+ shortLink: string;
+ _count: number;
+ }[]
+ | undefined
+ >({
+ query: {
+ groupBy: "linkId",
+ },
+ enabled,
+ });
+
const filters = useMemo(
() => [
{
@@ -46,16 +60,32 @@ export function useCustomerFilters(
value: country,
label: COUNTRIES[country],
right: nFormatter(_count, { full: true }),
+ permalink: `/${slug}/analytics?event=leads&country=${country}`,
})) ?? [],
},
+ {
+ key: "linkId",
+ icon: Hyperlink,
+ label: "Link",
+ options:
+ linksCount?.map(({ linkId, shortLink, _count }) => ({
+ value: linkId,
+ label: getPrettyUrl(shortLink),
+ right: nFormatter(_count, { full: true }),
+ permalink: `/${slug}/links/${getPrettyUrl(shortLink)}`,
+ })) ?? [],
+ },
],
- [countriesCount],
+ [countriesCount, linksCount],
);
const activeFilters = useMemo(() => {
- const { country } = searchParamsObj;
+ const { country, linkId } = searchParamsObj;
- return [...(country ? [{ key: "country", value: country }] : [])];
+ return [
+ ...(country ? [{ key: "country", value: country }] : []),
+ ...(linkId ? [{ key: "linkId", value: linkId }] : []),
+ ];
}, [searchParamsObj]);
const onSelect = useCallback(
@@ -70,7 +100,7 @@ export function useCustomerFilters(
);
const onRemove = useCallback(
- (key: string, value: any) =>
+ (key: string) =>
queryParams({
del: [key, "page"],
}),
diff --git a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
index 00f9921302..dab7ce45ec 100644
--- a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
@@ -6,7 +6,6 @@ import {
Books2,
CircleInfo,
ConnectedDots,
- ConnectedDots4,
CubeSettings,
Gear2,
Gift,
@@ -22,6 +21,7 @@ import { useSession } from "next-auth/react";
import { useParams, usePathname } from "next/navigation";
import { ReactNode, useMemo } from "react";
import UserSurveyButton from "../user-survey";
+import { ConnectedDots4 } from "./icons/connected-dots4";
import { CursorRays } from "./icons/cursor-rays";
import { Gear } from "./icons/gear";
import { Hyperlink } from "./icons/hyperlink";
diff --git a/apps/web/ui/layout/sidebar/icons/connected-dots4.tsx b/apps/web/ui/layout/sidebar/icons/connected-dots4.tsx
new file mode 100644
index 0000000000..7100c9a547
--- /dev/null
+++ b/apps/web/ui/layout/sidebar/icons/connected-dots4.tsx
@@ -0,0 +1,119 @@
+import { SVGProps, useEffect, useRef } from "react";
+
+export function ConnectedDots4({
+ "data-hovered": hovered,
+ ...rest
+}: { "data-hovered"?: boolean } & SVGProps<SVGSVGElement>) {
+ const ref = useRef<SVGSVGElement>(null);
+
+ useEffect(() => {
+ if (!ref.current) return;
+
+ if (hovered) {
+ ref.current.animate(
+ [{ transform: "rotate(0)" }, { transform: "rotate(180deg)" }],
+ {
+ duration: 300,
+ },
+ );
+ }
+ }, [hovered]);
+
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ ref={ref}
+ {...rest}
+ >
+ <g fill="currentColor">
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="4.664"
+ x2="7.586"
+ y1="7.586"
+ y2="4.664"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="10.414"
+ x2="13.336"
+ y1="4.664"
+ y2="7.586"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="13.336"
+ x2="10.414"
+ y1="10.414"
+ y2="13.336"
+ />
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="7.586"
+ x2="4.664"
+ y1="13.336"
+ y2="10.414"
+ />
+ <circle
+ cx="9"
+ cy="3.25"
+ fill="none"
+ r="2"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="3.25"
+ cy="9"
+ fill="none"
+ r="2"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="9"
+ cy="14.75"
+ fill="none"
+ r="2"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="14.75"
+ cy="9"
+ fill="none"
+ r="2"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ </g>
+ </svg>
+ );
+}