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.

Make sales amount sortable

Tim Wilsonf1a8659286dc
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index bef732129b..22bd39455b 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -180,6 +180,7 @@ export const eventsFilterTB = analyticsFilterTB
offset: z.coerce.number().default(0),
limit: z.coerce.number().default(50),
order: z.enum(["asc", "desc"]).default("desc"),
+ sortBy: z.enum(["timestamp", "amount"]).default("timestamp"),
}),
);
@@ -190,5 +191,6 @@ export const eventsQuerySchema = analyticsQuerySchema
offset: z.coerce.number().default(0),
limit: z.coerce.number().default(50),
order: z.enum(["asc", "desc"]).default("desc"),
+ sortBy: z.enum(["timestamp", "amount"]).default("timestamp"),
}),
);
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index 05d9382453..c3d3ac065f 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -77,6 +77,7 @@ export default function EventsTable() {
const { searchParams, queryParams } = useRouterStuff();
const tab = searchParams.get("tab") || "clicks";
+ const sortBy = searchParams.get("sort") || "date";
const order = searchParams.get("order") === "asc" ? "asc" : "desc";
const { pagination, setPagination } = usePagination(PAGE_SIZE);
@@ -179,18 +180,7 @@ export default function EventsTable() {
</div>
),
},
- // Sales amount
- {
- id: "amount",
- header: "Sales Amount",
- accessorKey: "amount",
- cell: ({ getValue }) => (
- <div className="flex items-center gap-2">
- <span>${nFormatter(getValue() / 100)}</span>
- <span className="text-gray-400">USD</span>
- </div>
- ),
- },
+ // Date
{
id: "timestamp",
header: "Date",
@@ -219,6 +209,18 @@ export default function EventsTable() {
</Tooltip>
),
},
+ // Sales amount
+ {
+ id: "amount",
+ header: "Sales Amount",
+ accessorKey: "amount",
+ cell: ({ getValue }) => (
+ <div className="flex items-center gap-2">
+ <span>${nFormatter(getValue() / 100)}</span>
+ <span className="text-gray-400">USD</span>
+ </div>
+ ),
+ },
].filter((c) => eventColumns[tab].all.includes(c.id)),
[tab],
);
@@ -232,6 +234,7 @@ export default function EventsTable() {
event: tab,
offset: (pagination.pageIndex * pagination.pageSize).toString(),
limit: pagination.pageSize.toString(),
+ sortBy,
order,
}).toString()}`,
fetcher,
@@ -278,7 +281,9 @@ export default function EventsTable() {
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => {
- const isDateColumn = header.column.id === "timestamp";
+ const isSortableColumn = ["timestamp", "amount"].includes(
+ header.column.id,
+ );
return (
<th
key={header.id}
@@ -287,11 +292,17 @@ export default function EventsTable() {
>
<button
className="flex items-center gap-2"
- disabled={!isDateColumn}
+ disabled={!isSortableColumn}
onClick={() =>
queryParams({
set: {
- order: order === "asc" ? "desc" : "asc",
+ sort: header.column.id,
+ order:
+ sortBy !== header.column.id
+ ? "desc"
+ : order === "asc"
+ ? "desc"
+ : "asc",
},
})
}
@@ -305,7 +316,13 @@ export default function EventsTable() {
header.getContext(),
)}
</span>
- {isDateColumn && <SortOrder order={order} />}
+ {isSortableColumn && (
+ <SortOrder
+ order={
+ sortBy === header.column.id ? order : null
+ }
+ />
+ )}
</button>
</th>
);
diff --git a/apps/web/ui/analytics/events/events-tabs.tsx b/apps/web/ui/analytics/events/events-tabs.tsx
index deb2faaa6e..a85cae4c76 100644
--- a/apps/web/ui/analytics/events/events-tabs.tsx
+++ b/apps/web/ui/analytics/events/events-tabs.tsx
@@ -9,7 +9,7 @@ import { ParentSize } from "@visx/responsive";
import { scaleLinear, scaleUtc } from "@visx/scale";
import { Area, AreaClosed } from "@visx/shape";
import { motion } from "framer-motion";
-import { useContext, useMemo } from "react";
+import { useCallback, useContext, useEffect, useMemo } from "react";
import useSWR from "swr";
import { AnalyticsContext } from "../analytics-provider";
@@ -43,6 +43,27 @@ export default function EventsTabs() {
},
);
+ const onEventTabClick = useCallback(
+ (event: string) => {
+ const sortOptions = event === "sales" ? ["date", "amount"] : ["date"];
+ const currentSort = searchParams.get("sort");
+ queryParams({
+ set: { tab: event },
+ // Reset sort when tab changes (only sales have `amount`)
+ del:
+ currentSort && !sortOptions.includes(currentSort)
+ ? "sort"
+ : undefined,
+ });
+ },
+ [queryParams, searchParams.get("sort")],
+ );
+
+ useEffect(() => {
+ const sortBy = searchParams.get("sort");
+ if (tab !== "sales" && sortBy !== "date") queryParams({ del: "sort" });
+ }, [tab, searchParams.get("sort")]);
+
return (
<div className="grid w-full grid-cols-3 gap-2 overflow-x-auto sm:gap-4">
{["clicks", ...(demo || betaTester ? ["leads", "sales"] : [])].map(
@@ -53,11 +74,7 @@ export default function EventsTabs() {
"flex justify-between gap-4 rounded-xl border border-gray-200 bg-white px-5 py-4 text-left transition-all",
tab === event && "border-black shadow-[0_0_0_1px_black_inset]",
)}
- onClick={() =>
- queryParams({
- set: { tab: event },
- })
- }
+ onClick={() => onEventTabClick(event)}
>
<div>
<p className="text-sm text-gray-600">{capitalize(event)}</p>
diff --git a/packages/tinybird/pipes/v1_events.pipe b/packages/tinybird/pipes/v1_events.pipe
index e8b2ce79d8..5e2918a191 100644
--- a/packages/tinybird/pipes/v1_events.pipe
+++ b/packages/tinybird/pipes/v1_events.pipe
@@ -146,7 +146,7 @@ SQL >
{% if defined(os) %} AND os = {{ os }} {% end %}
{% if defined(referer) %} AND referer = {{ referer }} {% end %}
{% if defined(url) %} AND url = {{ url }} {% end %}
- ORDER BY dse.timestamp {% if order == 'asc' %} ASC {% else %} DESC {% end %}
+ ORDER BY {% if sortBy == 'amount' %} dse.amount {% else %} dse.timestamp {% end %} {% if order == 'asc' %} ASC {% else %} DESC {% end %}
LIMIT {{ Int32(limit, 500) }}
{% if defined(offset) %} OFFSET {{ Int32(offset, 0) }} {% end %}
diff --git a/packages/ui/src/icons/sort-order.tsx b/packages/ui/src/icons/sort-order.tsx
index ca03d6d4d0..a02ced655c 100644
--- a/packages/ui/src/icons/sort-order.tsx
+++ b/packages/ui/src/icons/sort-order.tsx
@@ -8,7 +8,7 @@ export default function SortOrder({
order,
className,
}: {
- order: "asc" | "desc";
+ order: "asc" | "desc" | null;
className?: string;
}) {
return (
@@ -19,6 +19,7 @@ export default function SortOrder({
xmlns="http://www.w3.org/2000/svg"
>
<motion.path
+ className={cn(!order && "opacity-40")}
animate={{ d: order === "asc" ? downPath : upPath }}
stroke="currentColor"
strokeWidth="1.5"