Skip to content

Commit

Permalink
feat(dashboard): use table component for rendering sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
klevente committed Jul 29, 2024
1 parent dfb110e commit 4ef3812
Show file tree
Hide file tree
Showing 7 changed files with 847 additions and 64 deletions.
78 changes: 78 additions & 0 deletions dashboard/app/components/ui/data-table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";

import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "./table";

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
}

export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
});

return (
<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
);
}
106 changes: 106 additions & 0 deletions dashboard/app/components/ui/simple-pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Button } from "./button";
import {
ChevronLeft,
ChevronRight,
ChevronsLeft,
ChevronsRight,
} from "lucide-react";
import { Link } from "@remix-run/react";

interface DataTablePaginationProps {
numOfItems: number;
page: number;
pageCount: number;
onFirstPage: boolean;
onLastPage: boolean;
url: string;
}

export function SimplePagination({
numOfItems,
page,
pageCount,
onFirstPage,
onLastPage,
url,
}: DataTablePaginationProps) {
return (
<div className="flex items-center justify-center px-2 mt-2">
<div className="flex items-center space-x-6 lg:space-x-8">
<div className="flex items-center space-x-2">
{onFirstPage ? (
<Button
variant="outline"
className="hidden h-8 w-8 p-0 lg:flex"
disabled
>
<span className="sr-only">Go to first page</span>
<ChevronsLeft className="h-4 w-4" />
</Button>
) : (
<Button
asChild
variant="outline"
className="hidden h-8 w-8 p-0 lg:flex"
>
<Link to={`${url}?page=1`}>
<span className="sr-only">Go to first page</span>
<ChevronsLeft className="h-4 w-4" />
</Link>
</Button>
)}
{onFirstPage ? (
<Button variant="outline" className="h-8 w-8 p-0" disabled>
<span className="sr-only">Go to previous page</span>
<ChevronLeft className="h-4 w-4" />
</Button>
) : (
<Button asChild variant="outline" className="h-8 w-8 p-0">
<Link to={`${url}?page=${page - 1}`}>
<span className="sr-only">Go to previous page</span>
<ChevronLeft className="h-4 w-4" />
</Link>
</Button>
)}
<div className="flex items-center justify-center text-sm font-medium">
Page {page} of {pageCount} ({numOfItems} rows)
</div>
{onLastPage ? (
<Button variant="outline" className="h-8 w-8 p-0" disabled>
<span className="sr-only">Go to next page</span>
<ChevronRight className="h-4 w-4" />
</Button>
) : (
<Button asChild variant="outline" className="h-8 w-8 p-0">
<Link to={`${url}?page=${page + 1}`}>
<span className="sr-only">Go to next page</span>
<ChevronRight className="h-4 w-4" />
</Link>
</Button>
)}
{onLastPage ? (
<Button
variant="outline"
className="hidden h-8 w-8 p-0 lg:flex"
disabled
>
<span className="sr-only">Go to last page</span>
<ChevronsRight className="h-4 w-4" />
</Button>
) : (
<Button
asChild
variant="outline"
className="hidden h-8 w-8 p-0 lg:flex"
>
<Link to={`${url}?page=${pageCount}`}>
<span className="sr-only">Go to last page</span>
<ChevronsRight className="h-4 w-4" />
</Link>
</Button>
)}
</div>
</div>
</div>
);
}
119 changes: 119 additions & 0 deletions dashboard/app/components/ui/table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import * as React from "react";

import { cn } from "~/lib/utils";

const Table = React.forwardRef<
HTMLTableElement,
React.HTMLAttributes<HTMLTableElement>
>(({ className, ...props }, ref) => (
<div className="relative w-full overflow-auto">
<table
ref={ref}
className={cn("w-full caption-bottom text-sm", className)}
{...props}
/>
</div>
));
Table.displayName = "Table";

const TableHeader = React.forwardRef<
HTMLTableSectionElement,
React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
<thead ref={ref} className={cn("[&_tr]:border-b", className)} {...props} />
));
TableHeader.displayName = "TableHeader";

const TableBody = React.forwardRef<
HTMLTableSectionElement,
React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
<tbody
ref={ref}
className={cn("[&_tr:last-child]:border-0", className)}
{...props}
/>
));
TableBody.displayName = "TableBody";

const TableFooter = React.forwardRef<
HTMLTableSectionElement,
React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
<tfoot
ref={ref}
className={cn(
"border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
className,
)}
{...props}
/>
));
TableFooter.displayName = "TableFooter";

const TableRow = React.forwardRef<
HTMLTableRowElement,
React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => (
<tr
ref={ref}
className={cn(
"border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
className,
)}
{...props}
/>
));
TableRow.displayName = "TableRow";

const TableHead = React.forwardRef<
HTMLTableCellElement,
React.ThHTMLAttributes<HTMLTableCellElement>
// eslint-disable-next-line react/prop-types
>(({ className, ...props }, ref) => (
<th
ref={ref}
className={cn(
"h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0",
className,
)}
{...props}
/>
));
TableHead.displayName = "TableHead";

const TableCell = React.forwardRef<
HTMLTableCellElement,
React.TdHTMLAttributes<HTMLTableCellElement>
// eslint-disable-next-line react/prop-types
>(({ className, ...props }, ref) => (
<td
ref={ref}
className={cn("p-4 align-middle [&:has([role=checkbox])]:pr-0", className)}
{...props}
/>
));
TableCell.displayName = "TableCell";

const TableCaption = React.forwardRef<
HTMLTableCaptionElement,
React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, ref) => (
<caption
ref={ref}
className={cn("mt-4 text-sm text-muted-foreground", className)}
{...props}
/>
));
TableCaption.displayName = "TableCaption";

export {
Table,
TableHeader,
TableBody,
TableFooter,
TableHead,
TableRow,
TableCell,
TableCaption,
};
Loading

0 comments on commit 4ef3812

Please sign in to comment.