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.

More link list progress

Tim Wilson544f405986fd
diff --git a/apps/web/ui/links/link-title-column.tsx b/apps/web/ui/links/link-title-column.tsx
new file mode 100644
index 0000000000..c708e2da59
--- /dev/null
+++ b/apps/web/ui/links/link-title-column.tsx
@@ -0,0 +1,131 @@
+"use client";
+
+import useWorkspace from "@/lib/swr/use-workspace";
+import { UserProps } from "@/lib/types";
+import {
+ ArrowTurnRight2,
+ Avatar,
+ CopyButton,
+ LinkLogo,
+ TableContext,
+ Tooltip,
+} from "@dub/ui";
+import { ArrowRight } from "@dub/ui/src/icons";
+import { cn, formatDateTime, getApexDomain } from "@dub/utils";
+import { formatDate } from "date-fns";
+import { Mail } from "lucide-react";
+import { useContext, useId } from "react";
+import { ResponseLink } from "./links-container";
+
+export function LinkTitleColumn({ link }: { link: ResponseLink }) {
+ const { url, domain, user, createdAt } = link;
+ const path = link.key === "_root" ? "" : `/${link.key}`;
+
+ const { variant } = useContext(TableContext);
+
+ const id = useId();
+
+ return (
+ <div className="flex items-center gap-3 transition-[padding] group-data-[variant=loose-list]/table:sm:py-2">
+ <div className="relative flex items-center justify-center">
+ {/* Link logo background circle */}
+ <div className="absolute inset-0 shrink-0 rounded-full border border-gray-200 opacity-0 transition-opacity group-data-[variant=loose-list]/table:sm:opacity-100">
+ <div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-gray-100" />
+ </div>
+ <div className="relative transition-[padding] group-data-[variant=loose-list]/table:p-1 group-data-[variant=loose-list]/table:md:p-2">
+ <LinkLogo
+ apexDomain={getApexDomain(url)}
+ className="h-4 w-4 shrink-0 transition-[width,height] sm:h-6 sm:w-6 group-data-[variant=loose-list]/table:sm:h-5 group-data-[variant=loose-list]/table:sm:w-5"
+ />
+ </div>
+ </div>
+ <div className="h-[24px] min-w-0 overflow-hidden transition-[height] group-data-[variant=loose-list]/table:h-[44px]">
+ <div className="flex items-center gap-2">
+ <span
+ className="block truncate font-medium text-gray-950"
+ title={`${domain}${path}`}
+ >
+ {domain + path}
+ </span>
+ <Details link={link} compact />
+ </div>
+
+ <div className="mt-1">
+ <Details link={link} />
+ </div>
+ </div>
+ </div>
+ );
+}
+
+function Details({ link, compact }: { link: ResponseLink; compact?: boolean }) {
+ const { url, user, createdAt } = link;
+ return (
+ <div
+ className={cn(
+ "flex items-center gap-1.5 text-xs transition-opacity md:gap-3",
+ compact
+ ? "opacity-0 group-data-[variant=compact-list]/table:opacity-100"
+ : "opacity-0 group-data-[variant=loose-list]/table:opacity-100",
+ )}
+ >
+ <div className="flex min-w-0 items-center gap-1">
+ {compact ? (
+ <ArrowRight className="mr-1 h-3 w-3 text-gray-400" />
+ ) : (
+ <ArrowTurnRight2 className="h-3 w-3 text-gray-400" />
+ )}
+ <span
+ className={cn("truncate", url ? "text-gray-500" : "text-gray-400")}
+ title={url}
+ >
+ {url || "No URL configured"}
+ </span>
+ </div>
+ <div className="hidden shrink-0 sm:block">
+ <UserAvatar user={user} />
+ </div>
+ <div className="hidden sm:block">
+ <Tooltip content={formatDateTime(createdAt)}>
+ <span className="text-gray-400">
+ {formatDate(createdAt, "MMM d")}
+ </span>
+ </Tooltip>
+ </div>
+ </div>
+ );
+}
+
+function UserAvatar({ user }: { user: UserProps }) {
+ const { slug } = useWorkspace();
+
+ return (
+ <Tooltip
+ content={
+ <div className="w-full p-3">
+ <Avatar user={user} className="h-8 w-8" />
+ <div className="mt-2 flex items-center gap-1.5">
+ <p className="text-sm font-semibold text-gray-700">
+ {user?.name || user?.email || "Anonymous User"}
+ </p>
+ {!slug && // this is only shown in admin mode (where there's no slug)
+ user?.email && (
+ <CopyButton
+ value={user.email}
+ icon={Mail}
+ className="[&>*]:h-3 [&>*]:w-3"
+ />
+ )}
+ </div>
+ {user?.name && user.email && (
+ <p className="mt-1 text-xs text-gray-500">{user.email}</p>
+ )}
+ </div>
+ }
+ >
+ <div>
+ <Avatar user={user} className="h-4 w-4" />
+ </div>
+ </Tooltip>
+ );
+}
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 336a9f994e..85884ba5d8 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -3,25 +3,14 @@
import useLinks from "@/lib/swr/use-links";
import useLinksCount from "@/lib/swr/use-links-count";
import { LinkWithTagsProps, UserProps } from "@/lib/types";
-import {
- Avatar,
- LinkLogo,
- MaxWidthWrapper,
- Table,
- useRouterStuff,
- useTable,
-} from "@dub/ui";
-import {
- PAGINATION_LIMIT,
- formatDate,
- formatDateTime,
- getApexDomain,
-} from "@dub/utils";
+import { MaxWidthWrapper, Table, useRouterStuff, useTable } from "@dub/ui";
+import { PAGINATION_LIMIT } from "@dub/utils";
import { ColumnDef } from "@tanstack/react-table";
import { useSearchParams } from "next/navigation";
import { useEffect, useMemo, useState } from "react";
+import { LinkTitleColumn } from "./link-title-column";
-type ResponseLink = LinkWithTagsProps & {
+export type ResponseLink = LinkWithTagsProps & {
user: UserProps;
};
@@ -77,65 +66,12 @@ function LinksList({
id: "link",
header: "Link",
accessorFn: (d) => d,
- cell: ({ getValue }) => {
- const path = getValue().key === "_root" ? "" : `/${getValue().key}`;
-
- return (
- <div className="flex items-center gap-3">
- <LinkLogo
- apexDomain={getApexDomain(getValue().url)}
- className="h-4 w-4 sm:h-4 sm:w-4"
- />
- <span className="truncate" title={`${getValue().domain}${path}`}>
- <span className="font-medium text-gray-950">
- {getValue().domain}
- </span>
- {path}
- </span>
- </div>
- );
- },
+ cell: ({ getValue }) => (
+ <LinkTitleColumn link={getValue() as ResponseLink} />
+ ),
enableHiding: false,
size: 130,
},
- {
- id: "url",
- header: "URL",
- accessorKey: "url",
- size: 130,
- },
- {
- id: "creator",
- header: "Creator",
- accessorFn: (d) => d.user,
- cell: ({ getValue }) => {
- const user = getValue() as ResponseLink["user"];
- return (
- <div className="flex items-center gap-3">
- <Avatar user={user} className="h-4 w-4" />
- <span className="truncate" title={user.name}>
- {user.name}
- </span>
- </div>
- );
- },
- size: 50,
- },
- {
- id: "created",
- header: "Created",
- accessorKey: "createdAt",
- cell: ({ getValue }) => {
- const createdAt = getValue() as Date;
-
- return (
- <span title={formatDateTime(createdAt)}>
- {formatDate(createdAt)}
- </span>
- );
- },
- size: 50,
- },
{
id: "clicks",
header: "Clicks",
@@ -152,17 +88,21 @@ function LinksList({
});
useEffect(() => {
- queryParams({
- set: {
- page: (pagination.pageIndex + 1).toString(),
- },
- });
+ queryParams(
+ pagination.pageIndex === 0
+ ? { del: "page" }
+ : {
+ set: {
+ page: (pagination.pageIndex + 1).toString(),
+ },
+ },
+ );
}, [pagination]);
const { table, ...tableProps } = useTable({
variant: compact ? "compact-list" : "loose-list",
showColumnHeadings: false,
- data: links ?? [],
+ data: links?.slice(0, 10) ?? [],
loading,
columns,
pagination: pagination,
diff --git a/packages/ui/src/icons/nucleo/arrow-right.tsx b/packages/ui/src/icons/nucleo/arrow-right.tsx
new file mode 100644
index 0000000000..bda4e9ea5e
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/arrow-right.tsx
@@ -0,0 +1,35 @@
+import { SVGProps } from "react";
+
+export function ArrowRight(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ {...props}
+ >
+ <g fill="currentColor">
+ <line
+ fill="none"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ x1="15.25"
+ x2="2.75"
+ y1="9"
+ y2="9"
+ />
+ <polyline
+ fill="none"
+ points="11 4.75 15.25 9 11 13.25"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index f7039aad91..3d0f89d88f 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -1,3 +1,4 @@
+export * from "./arrow-right";
export * from "./arrow-turn-right2";
export * from "./blog";
export * from "./book2";
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index b4047d944a..20298d10b0 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -15,6 +15,7 @@ import {
Dispatch,
ReactNode,
SetStateAction,
+ createContext,
useEffect,
useMemo,
useState,
@@ -26,53 +27,61 @@ const variantDefaults = { default: "", "compact-list": "", "loose-list": "" };
const tableVariants = cva(
[
- "w-full table-fixed border-separate border-spacing-0 transition-[border-spacing]",
+ "group/table w-full table-fixed border-separate transition-[border-spacing] border-spacing-0",
],
{
variants: {
variant: {
...variantDefaults,
+ default: [
+ // Remove side borders from table to avoid interfering with outer border
+ "[&_tr>*:first-child]:border-l-transparent", // Left column
+ "[&_tr>*:last-child]:border-r-transparent", // Right column
+ ],
"loose-list": [
"border-spacing-y-4",
- "[&_tr>*:first-child]:border-l [&_tr>*:first-child]:rounded-l-lg",
- "[&_tr>*:last-child]:border-r [&_tr>*:last-child]:rounded-r-lg",
+ // Round left and right sides of each row
+ "[&_tr>*:first-child]:rounded-l-xl",
+ "[&_tr>*:last-child]:rounded-r-xl",
+ ],
+ "compact-list": [
+ // Round top corners
+ "[&_*:first-child>tr:first-child>*:first-child]:rounded-tl-xl",
+ "[&_*:first-child>tr:first-child>*:last-child]:rounded-tr-xl",
+
+ // Top border
+ "[&_*:first-child>tr:first-child>*]:border-t",
],
},
},
compoundVariants: [
{
- variant: ["default", "compact-list"],
+ variant: ["loose-list", "compact-list"],
className: [
- // Remove side borders from table to avoid interfering with outer border
- "[&_tr>*:first-child]:border-l-0", // Left column
- "[&_tr>*:last-child]:border-r-0", // Right column
+ "[&_tr>*:first-child]:border-l [&_tr>*:last-child]:border-r",
],
},
],
},
);
-const tableWrapperVariants = cva(
- "relative transition-[background-color,border-color]",
- {
- variants: {
- variant: {
- ...variantDefaults,
- default: "border border-gray-200 bg-white sm:rounded-xl",
- "compact-list": "border border-gray-200 bg-white sm:rounded-xl",
- },
+const tableWrapperVariants = cva("relative sm:rounded-t-xl", {
+ variants: {
+ variant: {
+ ...variantDefaults,
+ default: "border border-gray-200 bg-white",
},
},
-);
+});
const tableCellVariants = cva(
"px-4 py-2.5 text-left text-sm leading-6 whitespace-nowrap border-gray-200 transition-[background-color]",
{
variants: {
variant: {
- default: "border-r border-b",
- "compact-list": "border-b",
- "loose-list": "border-y bg-white",
+ default: "border-r border-b", // Right and left borders, removed from outer columns above
+ "compact-list": "border-b bg-white", // Bottom border, with other sides added above
+ "loose-list": "border-y bg-white", // Top and bottom borders, with other sides added above
},
},
},
@@ -80,32 +89,46 @@ const tableCellVariants = cva(
const tableRowVariants = cva("", {
variants: {
- variant: {
- ...variantDefaults,
- "loose-list":
+ variant: variantDefaults,
+ },
+ compoundVariants: [
+ {
+ // Loose/compact lists have hover effects
+ variant: ["loose-list", "compact-list"],
+ className:
"transition-[filter] hover:[filter:drop-shadow(0_8px_12px_#222A350d)_drop-shadow(0_32px_80px_#2f30370f)]",
},
- },
+ ],
});
const tablePaginationVariants = cva(
- "sticky bottom-0 flex items-center border-gray-200 justify-between bg-white px-4 py-3.5 text-sm leading-6 text-gray-600",
+ "-mt-px sticky flex mx-auto items-center border-gray-200 justify-between bg-white px-4 py-3.5 text-sm leading-6 text-gray-600 transition-[width,bottom,filter]",
{
variants: {
variant: {
...variantDefaults,
- "loose-list": "border rounded-lg",
+ "loose-list":
+ "rounded-xl [filter:drop-shadow(0_5px_8px_#222A351d)] bottom-4 w-[768px] max-w-full",
},
},
compoundVariants: [
{
variant: ["default", "compact-list"],
- className: "rounded-b-[inherit] border-t",
+ className: "rounded-b-[inherit] bottom-0 w-full border-t",
+ },
+ {
+ // Needs its own border because the table wrapper doesn't have one
+ variant: ["loose-list", "compact-list"],
+ className: "border",
},
],
},
);
+export const TableContext = createContext<{
+ variant: "default" | "compact-list" | "loose-list";
+}>({ variant: "default" });
+
type UseTableProps<T> = VariantProps<typeof tableVariants> & {
columns: ColumnDef<T, any>[];
data: T[];
@@ -235,172 +258,176 @@ export function Table<T>({
]);
return (
- <div className={tableWrapperVariants({ variant })}>
- {(!error && !!data?.length) || loading ? (
- <div className="min-h-[400px] rounded-[inherit]">
- <table
- className={cn(tableVariants({ variant }), className)}
- style={columnSizeVars}
- >
- {showColumnHeadings && (
- <thead>
- {table.getHeaderGroups().map((headerGroup) => (
- <tr key={headerGroup.id}>
- {headerGroup.headers.map((header) => {
- const isSortableColumn = sortableColumns.includes(
- header.column.id,
- );
- return (
- <th
- key={header.id}
- className={cn(
- tableCellVariants({ variant }),
- "relative select-none font-medium",
- thClassName,
- )}
- style={{
- width: `calc(var(--header-${header?.id}-size) * 1px)`,
- }}
- >
- <div className="flex items-center justify-between gap-6 !pr-0">
- <button
- type="button"
- className="flex items-center gap-2"
- disabled={!isSortableColumn}
- onClick={() =>
- onSortChange?.({
- sortBy: header.column.id,
- sortOrder:
- sortBy !== header.column.id
- ? "desc"
- : sortOrder === "asc"
+ <TableContext.Provider value={{ variant: variant ?? "default" }}>
+ <div className={tableWrapperVariants({ variant })}>
+ {(!error && !!data?.length) || loading ? (
+ <div className="min-h-[400px] rounded-[inherit]">
+ <table
+ className={cn(tableVariants({ variant }), className)}
+ style={columnSizeVars}
+ data-variant={variant}
+ >
+ {showColumnHeadings && (
+ <thead>
+ {table.getHeaderGroups().map((headerGroup) => (
+ <tr key={headerGroup.id}>
+ {headerGroup.headers.map((header) => {
+ const isSortableColumn = sortableColumns.includes(
+ header.column.id,
+ );
+ return (
+ <th
+ key={header.id}
+ className={cn(
+ tableCellVariants({ variant }),
+ "relative select-none font-medium",
+ thClassName,
+ )}
+ style={{
+ width: `calc(var(--header-${header?.id}-size) * 1px)`,
+ }}
+ >
+ <div className="flex items-center justify-between gap-6 !pr-0">
+ <button
+ type="button"
+ className="flex items-center gap-2"
+ disabled={!isSortableColumn}
+ onClick={() =>
+ onSortChange?.({
+ sortBy: header.column.id,
+ sortOrder:
+ sortBy !== header.column.id
? "desc"
- : "asc",
- })
- }
- aria-label="Sort by column"
- >
- <span>
- {header.isPlaceholder
- ? null
- : flexRender(
- header.column.columnDef.header,
- header.getContext(),
- )}
- </span>
- {isSortableColumn && (
- <SortOrder
- order={
- sortBy === header.column.id
- ? sortOrder || "desc"
- : null
- }
- />
- )}
- </button>
+ : sortOrder === "asc"
+ ? "desc"
+ : "asc",
+ })
+ }
+ aria-label="Sort by column"
+ >
+ <span>
+ {header.isPlaceholder
+ ? null
+ : flexRender(
+ header.column.columnDef.header,
+ header.getContext(),
+ )}
+ </span>
+ {isSortableColumn && (
+ <SortOrder
+ order={
+ sortBy === header.column.id
+ ? sortOrder || "desc"
+ : null
+ }
+ />
+ )}
+ </button>
+ </div>
+ {resizeColumns && (
+ <div
+ className="absolute -right-[4px] top-0 z-[1] h-full w-[7px] cursor-col-resize"
+ {...{
+ onDoubleClick: () =>
+ header.column.resetSize(),
+ onMouseDown: header.getResizeHandler(),
+ onTouchStart: header.getResizeHandler(),
+ }}
+ />
+ )}
+ </th>
+ );
+ })}
+ </tr>
+ ))}
+ </thead>
+ )}
+ <tbody>
+ {table.getRowModel().rows.map((row) => (
+ <tr key={row.id} className={tableRowVariants({ variant })}>
+ {row.getVisibleCells().map((cell) => (
+ <td
+ key={cell.id}
+ className={cn(
+ tableCellVariants({ variant }),
+ "group relative text-gray-600",
+ tdClassName,
+ )}
+ style={{
+ width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
+ }}
+ >
+ <div className="flex w-full items-center justify-between overflow-hidden truncate">
+ <div className="min-w-0 shrink">
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
</div>
- {resizeColumns && (
- <div
- className="absolute -right-[4px] top-0 z-[1] h-full w-[7px] cursor-col-resize"
- {...{
- onDoubleClick: () => header.column.resetSize(),
- onMouseDown: header.getResizeHandler(),
- onTouchStart: header.getResizeHandler(),
- }}
- />
- )}
- </th>
- );
- })}
+ {cellRight?.(cell)}
+ </div>
+ </td>
+ ))}
</tr>
))}
- </thead>
- )}
- <tbody>
- {table.getRowModel().rows.map((row) => (
- <tr key={row.id} className={tableRowVariants({ variant })}>
- {row.getVisibleCells().map((cell) => (
- <td
- key={cell.id}
- className={cn(
- tableCellVariants({ variant }),
- "group relative text-gray-600",
- tdClassName,
- )}
- style={{
- width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
- }}
- >
- <div className="flex w-full items-center justify-between overflow-hidden truncate">
- <div className="min-w-0 shrink">
- {flexRender(
- cell.column.columnDef.cell,
- cell.getContext(),
- )}
- </div>
- {cellRight?.(cell)}
- </div>
- </td>
- ))}
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- ) : (
- <div className="flex h-96 w-full items-center justify-center text-sm text-gray-500">
- {error || emptyState || "Failed to load data."}
- </div>
- )}
- {pagination && (
- <div className={tablePaginationVariants({ variant })}>
- <div>
- <span className="hidden sm:inline-block">Viewing</span>{" "}
- <span className="font-medium">
- {pagination.pageIndex * pagination.pageSize + 1}-
- {Math.min(
- pagination.pageIndex * pagination.pageSize +
- pagination.pageSize,
- table.getRowCount(),
- )}
- </span>{" "}
- of{" "}
- <span className="font-medium">
- {table.getRowCount().toLocaleString()}
- </span>{" "}
- {resourceName?.(table.getRowCount() !== 1) || "items"}
+ </tbody>
+ </table>
</div>
- <div className="flex items-center gap-2">
- <Button
- variant="secondary"
- text="Previous"
- className="h-7 px-2"
- onClick={() => table.previousPage()}
- disabled={!table.getCanPreviousPage()}
- />
- <Button
- variant="secondary"
- text="Next"
- className="h-7 px-2"
- onClick={() => table.nextPage()}
- disabled={!table.getCanNextPage()}
- />
+ ) : (
+ <div className="flex h-96 w-full items-center justify-center text-sm text-gray-500">
+ {error || emptyState || "Failed to load data."}
+ </div>
+ )}
+ {pagination && !error && (!loading || !!data?.length) && (
+ <div className={tablePaginationVariants({ variant })}>
+ <div>
+ <span className="hidden sm:inline-block">Viewing</span>{" "}
+ <span className="font-medium">
+ {pagination.pageIndex * pagination.pageSize + 1}-
+ {Math.min(
+ pagination.pageIndex * pagination.pageSize +
+ pagination.pageSize,
+ table.getRowCount(),
+ )}
+ </span>{" "}
+ of{" "}
+ <span className="font-medium">
+ {table.getRowCount().toLocaleString()}
+ </span>{" "}
+ {resourceName?.(table.getRowCount() !== 1) || "items"}
+ </div>
+ <div className="flex items-center gap-2">
+ <Button
+ variant="secondary"
+ text="Previous"
+ className="h-7 px-2"
+ onClick={() => table.previousPage()}
+ disabled={!table.getCanPreviousPage()}
+ />
+ <Button
+ variant="secondary"
+ text="Next"
+ className="h-7 px-2"
+ onClick={() => table.nextPage()}
+ disabled={!table.getCanNextPage()}
+ />
+ </div>
</div>
- </div>
- )}
-
- {/* Loading overlay */}
- <AnimatePresence>
- {loading && (
- <motion.div
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- className="absolute inset-0 flex items-center justify-center rounded-xl bg-white/50"
- >
- <LoadingSpinner />
- </motion.div>
)}
- </AnimatePresence>
- </div>
+
+ {/* Loading overlay */}
+ <AnimatePresence>
+ {loading && (
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ className="absolute inset-0 flex items-center justify-center rounded-xl bg-white/50"
+ >
+ <LoadingSpinner />
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </div>
+ </TableContext.Provider>
);
}