Skip to content

Commit ff75a9d

Browse files
committed
Store sorting state for download/popular/search tables
1 parent ce62a4f commit ff75a9d

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/tribler/ui/src/components/ui/simple-table.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SetStateAction, useEffect, useRef, useState } from 'react';
22
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
33
import { getCoreRowModel, useReactTable, flexRender, getFilteredRowModel, getPaginationRowModel, getExpandedRowModel, getSortedRowModel } from '@tanstack/react-table';
4-
import type { ColumnDef, Row, PaginationState, RowSelectionState, ColumnFiltersState, ExpandedState, ColumnDefTemplate, HeaderContext } from '@tanstack/react-table';
4+
import type { ColumnDef, Row, PaginationState, RowSelectionState, ColumnFiltersState, ExpandedState, ColumnDefTemplate, HeaderContext, SortingState } from '@tanstack/react-table';
55
import { cn } from '@/lib/utils';
66
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel } from './select';
77
import { Button } from './button';
@@ -41,6 +41,15 @@ export function getHeader<T>(name: string, translate: boolean = true, addSorting
4141
}
4242
}
4343

44+
function getStoredSortingState(key?: string) {
45+
if (key) {
46+
let sortingString = localStorage.getItem(key);
47+
if (sortingString) {
48+
return JSON.parse(sortingString);
49+
}
50+
}
51+
}
52+
4453
interface ReactTableProps<T extends object> {
4554
data: T[];
4655
columns: ColumnDef<T>[];
@@ -58,6 +67,7 @@ interface ReactTableProps<T extends object> {
5867
filters?: { id: string, value: string }[];
5968
maxHeight?: string | number;
6069
expandable?: boolean;
70+
storeSortingState?: string;
6171
}
6272

6373
function SimpleTable<T extends object>({
@@ -75,7 +85,8 @@ function SimpleTable<T extends object>({
7585
allowMultiSelect,
7686
filters,
7787
maxHeight,
78-
expandable
88+
expandable,
89+
storeSortingState
7990
}: ReactTableProps<T>) {
8091
const [pagination, setPagination] = useState<PaginationState>({
8192
pageIndex: pageIndex ?? 0,
@@ -84,6 +95,7 @@ function SimpleTable<T extends object>({
8495
const [rowSelection, setRowSelection] = useState<RowSelectionState>(initialRowSelection || {});
8596
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(filters || [])
8697
const [expanded, setExpanded] = useState<ExpandedState>({});
98+
const [sorting, setSorting] = useState<SortingState>(getStoredSortingState(storeSortingState) || []);
8799

88100
const table = useReactTable({
89101
data,
@@ -98,7 +110,8 @@ function SimpleTable<T extends object>({
98110
pagination,
99111
rowSelection,
100112
columnFilters,
101-
expanded
113+
expanded,
114+
sorting
102115
},
103116
getFilteredRowModel: getFilteredRowModel(),
104117
onColumnFiltersChange: setColumnFilters,
@@ -107,6 +120,7 @@ function SimpleTable<T extends object>({
107120
if (allowSelect || allowSelectCheckbox || allowMultiSelect) setRowSelection(arg);
108121
},
109122
onExpandedChange: setExpanded,
123+
onSortingChange: setSorting,
110124
getSubRows: (row: any) => row?.subRows,
111125
});
112126

@@ -133,6 +147,12 @@ function SimpleTable<T extends object>({
133147
}
134148
}, [filters])
135149

150+
useEffect(() => {
151+
if (storeSortingState) {
152+
localStorage.setItem(storeSortingState, JSON.stringify(sorting));
153+
}
154+
}, [sorting]);
155+
136156
// For some reason the ScrollArea scrollbar is only shown when it's set to a specific height.
137157
// So, we wrap it in a parent div, monitor its size, and set the height of the table accordingly.
138158
const parentRef = useRef<HTMLTableElement>(null);

src/tribler/ui/src/pages/Downloads/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export default function Downloads({ statusFilter }: { statusFilter: number[] })
194194
allowMultiSelect={true}
195195
onSelectedRowsChange={setSelectedDownloads}
196196
maxHeight={Math.max((parentRect?.height ?? 50)-50, 50)}
197+
storeSortingState="download-sorting"
197198
/>
198199
</Card>
199200
</div>

src/tribler/ui/src/pages/Popular/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export default function Popular() {
111111
<SimpleTable
112112
data={torrents}
113113
columns={torrentColumns}
114+
storeSortingState="popular-sorting"
114115
/>
115116
</>
116117
)

src/tribler/ui/src/pages/Search/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export default function Search() {
150150
<SimpleTable
151151
data={torrents}
152152
columns={torrentColumns}
153+
storeSortingState="search-sorting"
153154
/>
154155
</>
155156
)

0 commit comments

Comments
 (0)