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 table improvements + implementation for links

Tim Wilson577d077a73b1
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index a584e15b42..336a9f994e 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -19,23 +19,12 @@ import {
} from "@dub/utils";
import { ColumnDef } from "@tanstack/react-table";
import { useSearchParams } from "next/navigation";
-import { Suspense, useEffect, useMemo, useState } from "react";
-import LinkCard from "./link-card";
-import LinkCardPlaceholder from "./link-card-placeholder";
-import LinkPagination from "./link-pagination";
-import NoLinksPlaceholder from "./no-links-placeholder";
+import { useEffect, useMemo, useState } from "react";
type ResponseLink = LinkWithTagsProps & {
user: UserProps;
};
-type ListProps = {
- AddEditLinkButton: () => JSX.Element;
- links?: ResponseLink[];
- count?: number;
- loading?: boolean;
-};
-
export default function LinksContainer({
AddEditLinkButton,
}: {
@@ -54,55 +43,30 @@ export default function LinksContainer({
>
toggle compact
</button>
- {compact ? (
- <CompactList
- AddEditLinkButton={AddEditLinkButton}
- links={links}
- count={count}
- loading={isValidating}
- />
- ) : (
- <DefaultList
- AddEditLinkButton={AddEditLinkButton}
- links={links}
- count={count}
- loading={isValidating}
- />
- )}
+ <LinksList
+ AddEditLinkButton={AddEditLinkButton}
+ links={links}
+ count={count}
+ loading={isValidating}
+ compact={compact}
+ />
</MaxWidthWrapper>
);
}
-function DefaultList({ AddEditLinkButton, links, count, loading }: ListProps) {
- return (
- <>
- <ul className="grid min-h-[66.5vh] auto-rows-min gap-3">
- {links && !loading ? (
- links.length > 0 ? (
- links.map((props) => (
- <Suspense key={props.id} fallback={<LinkCardPlaceholder />}>
- <LinkCard props={props} />
- </Suspense>
- ))
- ) : (
- <NoLinksPlaceholder AddEditLinkButton={AddEditLinkButton} />
- )
- ) : (
- Array.from({ length: 10 }).map((_, i) => (
- <LinkCardPlaceholder key={i} />
- ))
- )}
- </ul>
- {count && count > 0 ? (
- <Suspense>
- <LinkPagination />
- </Suspense>
- ) : null}
- </>
- );
-}
-
-function CompactList({ AddEditLinkButton, links, count, loading }: ListProps) {
+function LinksList({
+ AddEditLinkButton,
+ links,
+ count,
+ loading,
+ compact,
+}: {
+ AddEditLinkButton: () => JSX.Element;
+ links?: ResponseLink[];
+ count?: number;
+ loading?: boolean;
+ compact: boolean;
+}) {
const { queryParams } = useRouterStuff();
const searchParams = useSearchParams();
const currentPage = parseInt(searchParams?.get("page") || "1") || 1;
@@ -196,6 +160,8 @@ function CompactList({ AddEditLinkButton, links, count, loading }: ListProps) {
}, [pagination]);
const { table, ...tableProps } = useTable({
+ variant: compact ? "compact-list" : "loose-list",
+ showColumnHeadings: false,
data: links ?? [],
loading,
columns,
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 3acd16b8d5..b4047d944a 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -9,6 +9,7 @@ import {
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
+import { VariantProps, cva } from "class-variance-authority";
import { AnimatePresence, motion } from "framer-motion";
import {
Dispatch,
@@ -21,14 +22,96 @@ import {
import { Button } from "../button";
import { LoadingSpinner, SortOrder } from "../icons";
-const tableCellClassName =
- "border-r border-b border-gray-200 px-4 py-2.5 text-left text-sm leading-6 whitespace-nowrap";
+const variantDefaults = { default: "", "compact-list": "", "loose-list": "" };
-type UseTableProps<T> = {
+const tableVariants = cva(
+ [
+ "w-full table-fixed border-separate border-spacing-0 transition-[border-spacing]",
+ ],
+ {
+ variants: {
+ variant: {
+ ...variantDefaults,
+ "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",
+ ],
+ },
+ },
+ compoundVariants: [
+ {
+ variant: ["default", "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
+ ],
+ },
+ ],
+ },
+);
+
+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 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",
+ },
+ },
+ },
+);
+
+const tableRowVariants = cva("", {
+ variants: {
+ variant: {
+ ...variantDefaults,
+ "loose-list":
+ "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",
+ {
+ variants: {
+ variant: {
+ ...variantDefaults,
+ "loose-list": "border rounded-lg",
+ },
+ },
+ compoundVariants: [
+ {
+ variant: ["default", "compact-list"],
+ className: "rounded-b-[inherit] border-t",
+ },
+ ],
+ },
+);
+
+type UseTableProps<T> = VariantProps<typeof tableVariants> & {
columns: ColumnDef<T, any>[];
data: T[];
loading?: boolean;
error?: string;
+ showColumnHeadings?: boolean;
emptyState?: ReactNode;
cellRight?: (cell: Cell<T, any>) => ReactNode;
defaultColumn?: Partial<ColumnDef<T, any>>;
@@ -43,16 +126,17 @@ type UseTableProps<T> = {
resizeColumns?: boolean;
resourceName?: (plural: boolean) => string;
+ className?: string;
thClassName?: string;
tdClassName?: string;
} & (
- | {
- pagination: PaginationState;
- onPaginationChange?: Dispatch<SetStateAction<PaginationState>>;
- rowCount: number;
- }
- | { pagination?: never; onPaginationChange?: never; rowCount?: never }
-);
+ | {
+ pagination: PaginationState;
+ onPaginationChange?: Dispatch<SetStateAction<PaginationState>>;
+ rowCount: number;
+ }
+ | { pagination?: never; onPaginationChange?: never; rowCount?: never }
+ );
type TableProps<T> = UseTableProps<T> & {
table: TableType<T>;
@@ -112,10 +196,12 @@ export function useTable<T extends any>(
}
export function Table<T>({
+ variant = "default",
columns,
data,
loading,
error,
+ showColumnHeadings = true,
emptyState,
cellRight,
sortBy,
@@ -124,6 +210,7 @@ export function Table<T>({
sortableColumns = [],
resizeColumns,
columnVisibility = {},
+ className,
thClassName,
tdClassName,
table,
@@ -148,25 +235,18 @@ export function Table<T>({
]);
return (
- <div className="border border-gray-200 bg-white sm:rounded-xl">
- <div className="relative rounded-[inherit]">
- {(!error && !!data?.length) || loading ? (
- <div className="min-h-[400px] overflow-x-auto rounded-[inherit]">
- <table
- className={cn(
- "w-full table-fixed border-separate border-spacing-0",
-
- // Remove side borders from table to avoid interfering with outer border
- "[&_thead_tr:first-child>*]:border-t-0", // Top row
- "[&_tr>*:first-child]:border-l-0", // Left column
- "[&_tr>*:last-child]:border-r-0", // Right column
- )}
- style={columnSizeVars}
- >
+ <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, columnIdx) => {
+ {headerGroup.headers.map((header) => {
const isSortableColumn = sortableColumns.includes(
header.column.id,
);
@@ -174,7 +254,7 @@ export function Table<T>({
<th
key={header.id}
className={cn(
- tableCellClassName,
+ tableCellVariants({ variant }),
"relative select-none font-medium",
thClassName,
)}
@@ -235,92 +315,92 @@ export function Table<T>({
</tr>
))}
</thead>
- <tbody>
- {table.getRowModel().rows.map((row) => (
- <tr key={row.id}>
- {row.getVisibleCells().map((cell) => (
- <td
- key={cell.id}
- className={cn(
- tableCellClassName,
- "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)}
+ )}
+ <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>
- </td>
- ))}
- </tr>
- ))}
- </tbody>
- </table>
+ {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"}
</div>
- ) : (
- <div className="flex h-96 w-full items-center justify-center text-sm text-gray-500">
- {error || emptyState || "Failed to load data."}
+ <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>
- )}
- {pagination && (
- <div className="sticky bottom-0 flex items-center justify-between rounded-b-[inherit] border-t border-gray-200 bg-white px-4 py-3.5 text-sm leading-6 text-gray-600">
- <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 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>
);
}