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.

Improve sorting/sizing

Tim Wilson43f9f5cae7b3
diff --git a/apps/web/lib/analytics/get-events.ts b/apps/web/lib/analytics/get-events.ts
index 187c91f3fb..3952cdff73 100644
--- a/apps/web/lib/analytics/get-events.ts
+++ b/apps/web/lib/analytics/get-events.ts
@@ -7,8 +7,7 @@ import { EventsFilters } from "./types";
// Fetch data for /api/analytics/events
export const getEvents = async (params: EventsFilters) => {
- let { event, workspaceId, interval, start, end, isDemo, offset, order } =
- params;
+ let { event, workspaceId, interval, start, end, isDemo } = params;
if (start) {
start = new Date(start);
@@ -31,8 +30,6 @@ export const getEvents = async (params: EventsFilters) => {
data: clickEventEnrichedSchema,
});
- console.log({ offset, order });
-
const response = await pipe({
...params,
eventType: event,
diff --git a/apps/web/lib/zod/schemas/analytics.ts b/apps/web/lib/zod/schemas/analytics.ts
index 459e470da4..bef732129b 100644
--- a/apps/web/lib/zod/schemas/analytics.ts
+++ b/apps/web/lib/zod/schemas/analytics.ts
@@ -178,6 +178,7 @@ export const eventsFilterTB = analyticsFilterTB
.and(
z.object({
offset: z.coerce.number().default(0),
+ limit: z.coerce.number().default(50),
order: z.enum(["asc", "desc"]).default("desc"),
}),
);
@@ -187,6 +188,7 @@ export const eventsQuerySchema = analyticsQuerySchema
.and(
z.object({
offset: z.coerce.number().default(0),
+ limit: z.coerce.number().default(50),
order: z.enum(["asc", "desc"]).default("desc"),
}),
);
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index fc8e5dc2e5..ef114fdbee 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -1,24 +1,29 @@
"use client";
import { editQueryString } from "@/lib/analytics/utils";
-import { Button, LinkLogo, LoadingSpinner } from "@dub/ui";
-import { COUNTRIES, capitalize, cn, fetcher } from "@dub/utils";
+import {
+ Button,
+ LinkLogo,
+ LoadingSpinner,
+ Tooltip,
+ useRouterStuff,
+} from "@dub/ui";
+import { SortOrder } from "@dub/ui/src/icons";
+import { COUNTRIES, capitalize, cn, fetcher, getApexDomain } from "@dub/utils";
import {
ColumnDef,
PaginationState,
- SortingState,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
import { AnimatePresence, motion } from "framer-motion";
-import { ChevronUp } from "lucide-react";
import { useContext, useMemo, useState } from "react";
import useSWR from "swr";
import { AnalyticsContext } from "../analytics-provider";
import DeviceIcon from "../device-icon";
-const PAGE_SIZE = 500;
+const PAGE_SIZE = 20;
const tableCellClassName =
"border-r border-b border-gray-200 px-4 py-2.5 text-left text-sm leading-6";
@@ -30,25 +35,24 @@ type FakeDatum = {
};
export default function EventsTable() {
+ const { searchParams, queryParams } = useRouterStuff();
+
+ const order = searchParams.get("order") === "asc" ? "asc" : "desc";
+
const [pagination, setPagination] = useState<PaginationState>({
pageIndex: 0,
pageSize: PAGE_SIZE,
});
- const [sorting, setSorting] = useState<SortingState>([
- { id: "timestamp", desc: true },
- ]);
-
const columns = useMemo<ColumnDef<FakeDatum, any>[]>(
() => [
{
header: "Link",
- enableSorting: false,
accessorKey: "link",
cell: ({ getValue }) => (
- <div className="flex items-center gap-3">
+ <div className="flex items-center gap-3 sm:min-w-[125px]">
<LinkLogo
- // apexDomain={getApexDomain(getValue().url)}
+ apexDomain={getApexDomain(getValue().url)}
className="h-4 w-4 sm:h-4 sm:w-4"
/>
<span>
@@ -62,7 +66,6 @@ export default function EventsTable() {
},
{
header: "Country",
- enableSorting: false,
accessorKey: "country",
cell: ({ getValue }) => (
<div className="flex items-center gap-3">
@@ -77,7 +80,6 @@ export default function EventsTable() {
},
{
header: "Device",
- enableSorting: false,
accessorKey: "device",
cell: ({ getValue }) => (
<div className="flex items-center gap-3">
@@ -93,16 +95,30 @@ export default function EventsTable() {
{
id: "timestamp",
header: "Date",
- enableSorting: true,
- accessorFn: (d) =>
- new Date(d.timestamp).toLocaleTimeString("en-US", {
- month: "short",
- day: "numeric",
- hour: "numeric",
- minute: "numeric",
- second: "numeric",
- hour12: true,
- }),
+ accessorFn: (d) => new Date(d.timestamp),
+ cell: ({ getValue }) => (
+ <Tooltip
+ content={getValue().toLocaleTimeString("en-US", {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ hour: "numeric",
+ minute: "numeric",
+ second: "numeric",
+ hour12: true,
+ })}
+ >
+ <span>
+ {getValue().toLocaleTimeString("en-US", {
+ month: "short",
+ day: "numeric",
+ hour: "numeric",
+ minute: "numeric",
+ hour12: true,
+ })}
+ </span>
+ </Tooltip>
+ ),
},
],
[],
@@ -115,7 +131,8 @@ export default function EventsTable() {
const { data, isLoading } = useSWR<FakeDatum[]>(
`/api/analytics/events?${editQueryString(queryString, {
offset: (pagination.pageIndex * pagination.pageSize).toString(),
- order: sorting?.[0]?.desc ?? true ? "desc" : "asc",
+ limit: pagination.pageSize.toString(),
+ order,
}).toString()}`,
fetcher,
{
@@ -129,10 +146,8 @@ export default function EventsTable() {
getCoreRowModel: getCoreRowModel(),
rowCount: totalEvents?.clicks ?? 0,
onPaginationChange: setPagination,
- onSortingChange: setSorting,
state: {
pagination,
- sorting,
},
manualPagination: true,
autoResetPageIndex: false,
@@ -158,39 +173,39 @@ export default function EventsTable() {
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
- {headerGroup.headers.map((header, idx) => (
- <th
- key={header.id}
- className={cn(tableCellClassName, "font-medium")}
- >
- <button
- className="flex items-center gap-1"
- disabled={!header.column.getCanSort()}
- onClick={() => header.column.toggleSorting()}
+ {headerGroup.headers.map((header) => {
+ const isDateColumn = header.column.id === "timestamp";
+ return (
+ <th
+ key={header.id}
+ className={cn(tableCellClassName, "font-medium")}
+ style={{ width: `${header.getSize()}px` }}
>
- <span>
- {header.isPlaceholder
- ? null
- : flexRender(
- header.column.columnDef.header,
- header.getContext(),
- )}
- </span>
- {header.column.getIsSorted() && (
+ <button
+ className="flex items-center gap-2"
+ disabled={!isDateColumn}
+ onClick={() =>
+ queryParams({
+ set: {
+ order: order === "asc" ? "desc" : "asc",
+ },
+ })
+ }
+ aria-label="Sort by column"
+ >
<span>
- <ChevronUp
- className={cn(
- "h-3.5 w-3.5 transition-transform",
- header.column.getIsSorted() === "desc" &&
- "rotate-180",
- )}
- transform=""
- />
+ {header.isPlaceholder
+ ? null
+ : flexRender(
+ header.column.columnDef.header,
+ header.getContext(),
+ )}
</span>
- )}
- </button>
- </th>
- ))}
+ {isDateColumn && <SortOrder order={order} />}
+ </button>
+ </th>
+ );
+ })}
</tr>
))}
</thead>
diff --git a/packages/ui/src/icons/index.tsx b/packages/ui/src/icons/index.tsx
index ecdc52186f..20514c75bf 100644
--- a/packages/ui/src/icons/index.tsx
+++ b/packages/ui/src/icons/index.tsx
@@ -3,6 +3,7 @@ export { default as Copy } from "./copy";
export { default as ExpandingArrow } from "./expanding-arrow";
export { default as Magic } from "./magic";
export { default as Photo } from "./photo";
+export { default as SortOrder } from "./sort-order";
export { default as Success } from "./success";
export { default as Tick } from "./tick";
diff --git a/packages/ui/src/icons/sort-order.tsx b/packages/ui/src/icons/sort-order.tsx
new file mode 100644
index 0000000000..6c7c21b6f2
--- /dev/null
+++ b/packages/ui/src/icons/sort-order.tsx
@@ -0,0 +1,38 @@
+import { cn } from "@dub/utils";
+import { motion } from "framer-motion";
+
+const upPath = "M6.75 8.25L4 11L1.25 8.25";
+const downPath = "M6.75 3.75L4 1L1.25 3.75";
+
+export default function SortOrder({
+ order,
+ className,
+}: {
+ order: "asc" | "desc";
+ className?: string;
+}) {
+ return (
+ <svg
+ className={cn("w-2 text-gray-950", className)}
+ viewBox="0 0 8 12"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <motion.path
+ animate={{ d: order === "asc" ? upPath : downPath }}
+ stroke="currentColor"
+ strokeWidth="1.5"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ <motion.path
+ className="opacity-40"
+ animate={{ d: order === "asc" ? downPath : upPath }}
+ stroke="currentColor"
+ strokeWidth="1.5"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ </svg>
+ );
+}