Updated table

This commit is contained in:
2025-06-26 23:31:51 +02:00
parent 6a8b2b9e2e
commit c44d93603c
2 changed files with 32 additions and 3 deletions

View File

@@ -28,12 +28,14 @@ import {
import { Input } from "@/components/ui/input" import { Input } from "@/components/ui/input"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { deleteUrl } from "@/lib/actions/url" import { deleteUrl } from "@/lib/actions/url"
import { urls } from "@/lib/drizzle/schema" import { urls, visits } from "@/lib/drizzle/schema"
import Link from "next/link" import Link from "next/link"
import { useRouter } from "next/navigation" import { useRouter } from "next/navigation"
import { toast } from "sonner" import { toast } from "sonner"
type UrlRecord = typeof urls.$inferSelect type UrlRecord = typeof urls.$inferSelect & {
visits?: (typeof visits.$inferSelect)[]
}
export const columns: ColumnDef<UrlRecord>[] = [ export const columns: ColumnDef<UrlRecord>[] = [
{ {
@@ -83,6 +85,30 @@ export const columns: ColumnDef<UrlRecord>[] = [
return <div className="max-w-[200px] truncate">{title || "No title"}</div> return <div className="max-w-[200px] truncate">{title || "No title"}</div>
} }
}, },
{
id: "visits",
header: ({ column }) => {
return (
<Button
variant="ghost"
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
>
Visits
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
)
},
cell: ({ row }) => {
const urlRecord = row.original
const visitCount = urlRecord.visits?.length || 0
return <div className="text-center">{visitCount}</div>
},
sortingFn: (rowA, rowB) => {
const visitsA = rowA.original.visits?.length || 0
const visitsB = rowB.original.visits?.length || 0
return visitsA - visitsB
}
},
{ {
accessorKey: "maxVisits", accessorKey: "maxVisits",
header: ({ column }) => { header: ({ column }) => {

View File

@@ -4,7 +4,10 @@ import { urls, visits } from "../drizzle/schema";
export function getAllUrls() { export function getAllUrls() {
return db.query.urls.findMany({ return db.query.urls.findMany({
orderBy: desc(urls.createdAt) orderBy: desc(urls.createdAt),
with: {
visits: true
}
}) })
} }