Skip to content
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
12 changes: 10 additions & 2 deletions apps/web/src/components/SortableHeader/SortableHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type SortableHeaderProps<TRow> = {
* mounts with no sorting state of its own, so the column reads as unsorted however it is ordered.
*/
sortOrder?: 'asc' | 'desc';
/**
* Passed by a table in server mode, which does not return its page index to the first page when
* the sort changes, so the page controls would keep counting from the page the user left.
*/
table?: TanstackTable.Table<TRow>;
};

const SORT_ICONS = { asc: ChevronUpIcon, desc: ChevronDownIcon };
Expand All @@ -18,14 +23,17 @@ const SORT_ICONS = { asc: ChevronUpIcon, desc: ChevronDownIcon };
* A clickable column label. `DataTableHead` renders whatever the column supplies, so the sort
* affordance lives here rather than coming from the table.
*/
export const SortableHeader = <TRow,>({ column, label, sortOrder }: SortableHeaderProps<TRow>) => {
export const SortableHeader = <TRow,>({ column, label, sortOrder, table }: SortableHeaderProps<TRow>) => {
const sorted = sortOrder ?? column.getIsSorted();
const Icon = sorted ? SORT_ICONS[sorted] : ChevronsUpDownIcon;
return (
<button
className="hover:text-foreground flex min-w-0 items-center gap-1 transition-colors"
type="button"
onClick={() => column.toggleSorting()}
onClick={() => {
column.toggleSorting();
table?.firstPage();
}}
>
<span className="truncate" title={label}>
{label}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ describe('Route', () => {
expect(timeHeaderButton().querySelector('.lucide-chevron-down')).toBeTruthy();
});

it('should return the pagination to the first page when the sort changes, so the next page is page two', () => {
renderPage();
goToPage(2);
fireEvent.click(timeHeaderButton());
fireEvent.click(document.querySelector('.lucide-chevron-right')!.closest('button')!);
expect(lastQueryParams()).toEqual({ page: 2, sortBy: 'timestamp', sortOrder: 'asc' });
});

it.each([
['User', 'user'],
['Active Group', 'group'],
Expand Down
45 changes: 32 additions & 13 deletions apps/web/src/routes/_app/admin/audit/logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ const AuditLogsTable: React.FC<{ search: AuditLogsSearchParams }> = ({ search })
return formatTimestamp(value);
},
enableResizing: false,
header: ({ column }) => (
header: ({ column, table }) => (
<div className="flex items-center gap-2">
<SortableHeader column={column} label={t('common.time')} sortOrder={sortOrderOf('timestamp')} />
<SortableHeader
column={column}
label={t('common.time')}
sortOrder={sortOrderOf('timestamp')}
table={table}
/>
<DateFormatMenu value={dateFormat} onChange={setDateFormat} />
</div>
),
Expand All @@ -219,8 +224,8 @@ const AuditLogsTable: React.FC<{ search: AuditLogsSearchParams }> = ({ search })
const value = ctx.getValue() as string;
return <span className="overflow-hidden text-ellipsis whitespace-nowrap">{value}</span>;
},
header: ({ column }) => (
<SortableHeader column={column} label={t('common.user')} sortOrder={sortOrderOf('user')} />
header: ({ column, table }) => (
<SortableHeader column={column} label={t('common.user')} sortOrder={sortOrderOf('user')} table={table} />
),
id: 'user'
},
Expand All @@ -230,8 +235,13 @@ const AuditLogsTable: React.FC<{ search: AuditLogsSearchParams }> = ({ search })
const value = ctx.getValue() as string;
return <span className="overflow-hidden text-ellipsis whitespace-nowrap">{value}</span>;
},
header: ({ column }) => (
<SortableHeader column={column} label={t('common.activeGroup')} sortOrder={sortOrderOf('group')} />
header: ({ column, table }) => (
<SortableHeader
column={column}
label={t('common.activeGroup')}
sortOrder={sortOrderOf('group')}
table={table}
/>
),
id: 'group'
},
Expand All @@ -247,8 +257,13 @@ const AuditLogsTable: React.FC<{ search: AuditLogsSearchParams }> = ({ search })
/>
);
},
header: ({ column }) => (
<SortableHeader column={column} label={t('common.action')} sortOrder={sortOrderOf('action')} />
header: ({ column, table }) => (
<SortableHeader
column={column}
label={t('common.action')}
sortOrder={sortOrderOf('action')}
table={table}
/>
)
},
{
Expand All @@ -261,8 +276,13 @@ const AuditLogsTable: React.FC<{ search: AuditLogsSearchParams }> = ({ search })
</span>
);
},
header: ({ column }) => (
<SortableHeader column={column} label={t('common.entity')} sortOrder={sortOrderOf('entity')} />
header: ({ column, table }) => (
<SortableHeader
column={column}
label={t('common.entity')}
sortOrder={sortOrderOf('entity')}
table={table}
/>
)
}
]}
Expand All @@ -272,7 +292,6 @@ const AuditLogsTable: React.FC<{ search: AuditLogsSearchParams }> = ({ search })
onPaginationChange={({ pageIndex }) => setPage(pageIndex + 1)}
onSortingChange={(state) => {
setSort(readServerSort(state, $AuditLogsSortBy.options));
setPage(INITIAL_PAGE);
}}
/>
);
Expand All @@ -290,8 +309,8 @@ const RouteComponent = () => {
</Heading>
</PageHeader>
<AuditLogsToolbar />
{/* Remounted when the filters change: in server mode the table owns its page index and offers no
way to set it, so a fresh store is the only way to return it to the first page. */}
{/* Remounted when the filters change: in server mode the table owns its page index, and the
toolbar has no handle on the table to return it to the first page. */}
<AuditLogsTable key={JSON.stringify(search)} search={search} />
</Fragment>
);
Expand Down
Loading