Skip to content

Add virtual scrolling #8533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/tribler/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/tribler/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-tooltip": "^1.0.7",
"@tanstack/react-table": "^8.10.7",
"@tanstack/react-virtual": "^3.13.5",
"@types/video.js": "^7.3.58",
"axios": "^1.6.8",
"class-variance-authority": "^0.7.0",
Expand Down
3 changes: 1 addition & 2 deletions src/tribler/ui/src/components/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ const ScrollArea = React.forwardRef<
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
<ScrollAreaPrimitive.Root
ref={ref}
className={cn("relative overflow-hidden", className)}
{...props}
>
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
<ScrollAreaPrimitive.Viewport ref={ref} className="h-full w-full rounded-[inherit]">
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
Expand Down
182 changes: 108 additions & 74 deletions src/tribler/ui/src/components/ui/simple-table.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
import { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { getCoreRowModel, useReactTable, flexRender, getFilteredRowModel, getPaginationRowModel, getExpandedRowModel, getSortedRowModel } from '@tanstack/react-table';
import type { ColumnDef, Row, PaginationState, RowSelectionState, ColumnFiltersState, ExpandedState, ColumnDefTemplate, HeaderContext, SortingState, VisibilityState, Header, Column, InitialTableState } from '@tanstack/react-table';
import type { ColumnDef, Row, PaginationState, RowSelectionState, ColumnFiltersState, ColumnDefTemplate, HeaderContext, SortingState, VisibilityState, Header, Column, InitialTableState } from '@tanstack/react-table';
import { cn, isMac } from '@/lib/utils';
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel } from './select';
import { Button } from './button';
import { ArrowDownIcon, ArrowUpIcon, ChevronLeftIcon, ChevronRightIcon, DotsHorizontalIcon, DoubleArrowLeftIcon, DoubleArrowRightIcon } from '@radix-ui/react-icons';
import * as SelectPrimitive from "@radix-ui/react-select"
import type { Table as ReactTable } from '@tanstack/react-table';
import { useTranslation } from 'react-i18next';
import { useResizeObserver } from '@/hooks/useResizeObserver';
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from './dropdown-menu';
import { triblerService } from '@/services/tribler.service';
import { useHotkeys } from 'react-hotkeys-hook';
import { notUndefined, useVirtualizer } from '@tanstack/react-virtual';
import { ScrollArea } from './scroll-area';


declare module '@tanstack/table-core/build/lib/types' {
Expand Down Expand Up @@ -136,13 +137,13 @@ interface ReactTableProps<T extends object> {
allowMultiSelect?: boolean;
allowColumnToggle?: string;
filters?: { id: string, value: string }[];
maxHeight?: string | number;
scrollClassName?: string;
expandable?: boolean;
storeSortingState?: string;
rowId?: (originalRow: T, index: number, parent?: Row<T>) => string,
selectOnRightClick?: boolean,
initialState?: InitialTableState
initialState?: InitialTableState,
className?: string,
style?: React.CSSProperties
}

function SimpleTable<T extends object>({
Expand All @@ -160,13 +161,13 @@ function SimpleTable<T extends object>({
allowMultiSelect,
allowColumnToggle,
filters,
maxHeight,
scrollClassName,
expandable,
storeSortingState,
rowId,
selectOnRightClick,
initialState
initialState,
className,
style
}: ReactTableProps<T>) {
const [pagination, setPagination] = useState<PaginationState>({
pageIndex: pageIndex ?? 0,
Expand Down Expand Up @@ -293,25 +294,46 @@ function SimpleTable<T extends object>({
}
}, [columnVisibility]);

// For some reason the ScrollArea scrollbar is only shown when it's set to a specific height.
// So, we wrap it in a parent div, monitor its size, and set the height of the table accordingly.
const parentRef = useRef<HTMLTableElement>(null);
const parentRect = (!maxHeight) ? useResizeObserver({ ref: parentRef }) : undefined;
const columnCount = table.getAllColumns().length;
const { rows } = table.getRowModel();
const virtualizer = useVirtualizer({
count: rows.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 40, // if set too low the last row will start flickering
overscan: 20,
})

const items = virtualizer.getVirtualItems();
const [before, after] =
items.length > 0
? [
notUndefined(items[0]).start - virtualizer.options.scrollMargin,
virtualizer.getTotalSize() - notUndefined(items[items.length - 1]).end
]
: [0, 0];

return (
<>
<div ref={parentRef} className='flex-grow flex'>
<Table maxHeight={maxHeight ?? (parentRect?.height ?? 200)} scrollClassName={scrollClassName}>
<ScrollArea
className={cn("relative w-full overflow-auto", className)}
ref={parentRef}
style={style}
>
<Table>
<TableHeader className='z-10'>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id} className="bg-neutral-100 hover:bg-neutral-100 dark:bg-neutral-900 dark:hover:bg-neutral-900">
{headerGroup.headers.map((header, index) => {
return (
<TableHead key={header.id} className={cn({
'pl-4': index === 0,
'pr-4': !allowColumnToggle && index + 1 === headerGroup.headers.length,
'pr-0': !!allowColumnToggle
})}>
<TableHead
key={header.id}
className={cn({
'pl-4': index === 0,
'pr-4': !allowColumnToggle && index + 1 === headerGroup.headers.length,
'pr-0': !!allowColumnToggle
})}
>
{header.isPlaceholder
? null
: flexRender(
Expand Down Expand Up @@ -360,71 +382,83 @@ function SimpleTable<T extends object>({
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getPaginationRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
className={`select-none ${allowSelect || allowMultiSelect ? "cursor-pointer" : ""}`}
onContextMenu={(event) => {
if (selectOnRightClick && !row.getIsSelected()) {
event.target.dispatchEvent(new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
}));
}
}}
onClick={(event) => {
if (!allowSelect && !allowMultiSelect)
return;

if (allowMultiSelect && (isMac() ? event.metaKey : event.ctrlKey)) {
row.toggleSelected(!row.getIsSelected());
if (!row.getIsSelected()) setStartId(row.id)
return;
}

let rows = table.getSortedRowModel().rows;
let startRow = rows.find((row) => row.id === startId);

if (startRow && allowMultiSelect && event.shiftKey) {
let selection: any = {};
let startIndex = rows.findIndex((r) => r.id == startRow.id);
let stopIndex = rows.findIndex((r) => r.id == row.id);
for (let i = Math.min(startIndex, stopIndex); i <= Math.max(startIndex, stopIndex); i++) {
selection[rows[i].id] = true;
{before > 0 && <TableRow><td colSpan={columnCount} style={{ height: before }} /></TableRow>}

{rows.length ? (
items.map((item, rowIndex) => {
const row = rows[item.index];

return (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
className={`select-none ${allowSelect || allowMultiSelect ? "cursor-pointer" : ""}`}
onContextMenu={(event) => {
if (selectOnRightClick && !row.getIsSelected()) {
event.target.dispatchEvent(new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
}));
}
}}
onClick={(event) => {
if (!allowSelect && !allowMultiSelect)
return;

if (allowMultiSelect && (isMac() ? event.metaKey : event.ctrlKey)) {
row.toggleSelected(!row.getIsSelected());
if (!row.getIsSelected()) setStartId(row.id)
return;
}

let rows = table.getSortedRowModel().rows;
let startRow = rows.find((row) => row.id === startId);

if (startRow && allowMultiSelect && event.shiftKey) {
let selection: any = {};
let startIndex = rows.findIndex((r) => r.id == startRow.id);
let stopIndex = rows.findIndex((r) => r.id == row.id);
for (let i = Math.min(startIndex, stopIndex); i <= Math.max(startIndex, stopIndex); i++) {
selection[rows[i].id] = true;
}
setRowSelection(selection);
} else {
const selected = row.getIsSelected()
table.resetRowSelection();
row.toggleSelected(!selected);
if (!selected) setStartId(row.id)
}
setRowSelection(selection);
} else {
const selected = row.getIsSelected()
table.resetRowSelection();
row.toggleSelected(!selected);
if (!selected) setStartId(row.id)
}
}}
onDoubleClick={() => {
if (onRowDoubleClick) {
onRowDoubleClick(row.original)
}
}}>
{row.getVisibleCells().map((cell, index) => (
<TableCell key={cell.id} className={cn({ 'pl-4': index === 0, 'pr-4': index + 1 === row.getVisibleCells().length, })}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
}}
onDoubleClick={() => {
if (onRowDoubleClick) {
onRowDoubleClick(row.original)
}
}}>

{row.getVisibleCells().map((cell, colIndex) => (
<TableCell
key={cell.id}
className={cn({ 'pl-4': colIndex === 0, 'pr-4': colIndex + 1 === row.getVisibleCells().length, })}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
)
})
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center text-muted-foreground">
{t('NoResults')}
</TableCell>
</TableRow>
)}

{after > 0 && <TableRow><td colSpan={columnCount} style={{ height: after }} /></TableRow>}
</TableBody>
</Table>
</div>
</ScrollArea>

{!!pageSize && table.getPageCount() > 1 && <Pagination table={table} />}
</>
Expand Down
22 changes: 7 additions & 15 deletions src/tribler/ui/src/components/ui/table.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import * as React from "react"

import { cn } from "@/lib/utils"
import { ScrollArea } from "./scroll-area"

interface ExtendedTableProps extends React.HTMLAttributes<HTMLTableElement> {
maxHeight?: string | number;
scrollClassName?: string;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/tribler/ui/src/dialogs/CreateTorrent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default function CreateTorrent(props: JSX.IntrinsicAttributes & DialogPro
data={files}
columns={filenameColumns}
allowSelect={false}
maxHeight={200} />
style={{maxHeight: 200}} />

<div>
<SelectRemotePath
Expand Down
Loading