"use client" import * as React from "react" import { type ColumnDef, type ColumnFiltersState, type SortingState, flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { CodeHostType, getCodeHostBrowseAtBranchUrl } from "@/lib/utils" import Link from "next/link" type RepoBranchesTableProps = { indexRevisions: string[]; repoWebUrl: string | null; repoCodeHostType: string; } export const RepoBranchesTable = ({ indexRevisions, repoWebUrl, repoCodeHostType }: RepoBranchesTableProps) => { const [sorting, setSorting] = React.useState([]) const [columnFilters, setColumnFilters] = React.useState([]) const columns = React.useMemo[]>(() => { return [ { accessorKey: "refName", header: "Revision", cell: ({ row }) => { const refName = row.original; const shortRefName = refName.replace(/^refs\/(heads|tags)\//, ""); const branchUrl = getCodeHostBrowseAtBranchUrl({ webUrl: repoWebUrl, codeHostType: repoCodeHostType as CodeHostType, branchName: refName, }); return branchUrl ? ( {shortRefName} ) : ( {shortRefName} ) }, } ] }, [repoCodeHostType, repoWebUrl]); const table = useReactTable({ data: indexRevisions, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, state: { sorting, columnFilters, }, initialState: { pagination: { pageSize: 5, }, }, }) return (
table.getColumn("refName")?.setFilterValue(event.target.value)} className="max-w-sm" />
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No branches found. )}
) }