-
Notifications
You must be signed in to change notification settings - Fork 201
fix: Name, Metric and Domain column headers do nothing when clicked #681
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
Open
Silex
wants to merge
2
commits into
netbirdio:main
Choose a base branch
from
Silex:fix/table-header-sort-noop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| "use client"; | ||
|
|
||
| import type { Table as TanStackTable } from "@tanstack/table-core"; | ||
| import React, { createContext, useContext } from "react"; | ||
|
|
||
| const DataTableInstanceContext = createContext<TanStackTable<any> | null>(null); | ||
|
|
||
| type ProviderProps = { | ||
| table: TanStackTable<any>; | ||
| children: React.ReactNode; | ||
| }; | ||
|
|
||
| export function DataTableInstanceProvider({ table, children }: ProviderProps) { | ||
| return ( | ||
| <DataTableInstanceContext.Provider value={table}> | ||
| {children} | ||
| </DataTableInstanceContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the tanstack table instance for the surrounding DataTable, or null | ||
| * when used outside of one. | ||
| */ | ||
| export function useOptionalDataTable() { | ||
| return useContext(DataTableInstanceContext); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import type { ColumnDef, SortingState } from "@tanstack/react-table"; | ||
| import { | ||
| getCoreRowModel, | ||
| getSortedRowModel, | ||
| useReactTable, | ||
| } from "@tanstack/react-table"; | ||
| import { cleanup, fireEvent, render, screen } from "@testing-library/react"; | ||
| import React, { useState } from "react"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { DataTableInstanceProvider } from "./DataTableContext"; | ||
| import DataTableHeader from "./DataTableHeader"; | ||
|
|
||
| // A header click must always replace the sort with the clicked column alone. | ||
| // The peers tables default to a multi-sort ([connected, last_seen, name]), and | ||
| // column.toggleSorting() only toggles in place when the clicked column is that | ||
| // sort's last entry — so the click used to leave the visible order untouched. | ||
|
|
||
| const { setSort } = vi.hoisted(() => ({ setSort: vi.fn() })); | ||
|
|
||
| vi.mock("@/contexts/ServerPaginationProvider", () => ({ | ||
| useOptionalServerPagination: () => ({ setSort }), | ||
| })); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| setSort.mockClear(); | ||
| }); | ||
|
|
||
| type Peer = { name: string; connected: boolean; last_seen: string }; | ||
|
|
||
| const peers: Peer[] = [ | ||
| { name: "alpha", connected: false, last_seen: "2026-01-01" }, | ||
| { name: "zulu", connected: true, last_seen: "2026-03-01" }, | ||
| { name: "mike", connected: true, last_seen: "2026-02-01" }, | ||
| ]; | ||
|
|
||
| const columns: ColumnDef<Peer>[] = [ | ||
| { id: "name", accessorKey: "name", sortingFn: "text" }, | ||
| { id: "connected", accessorKey: "connected" }, | ||
| { id: "last_seen", accessorKey: "last_seen", sortingFn: "text" }, | ||
| ]; | ||
|
|
||
| // DataTable augments TanStack's FilterFns and SortingFns interfaces, which makes | ||
| // both options required on every table in the app. Nothing here uses them. | ||
| const filterFns = { | ||
| fuzzy: () => true, | ||
| dateRange: () => true, | ||
| exactMatch: () => true, | ||
| arrIncludesSomeExact: () => true, | ||
| }; | ||
| const sortingFns = { checkbox: () => 0, datetime: () => 0 }; | ||
|
|
||
| // Mirrors DataTable: sorting state lives in the parent and is fed back through | ||
| // state.sorting / onSortingChange. | ||
| function Harness({ | ||
| initialSorting, | ||
| columnId, | ||
| name, | ||
| }: { | ||
| initialSorting: SortingState; | ||
| columnId: string; | ||
| name?: string; | ||
| }) { | ||
| const [sorting, setSorting] = useState<SortingState>(initialSorting); | ||
| const table = useReactTable({ | ||
| data: peers, | ||
| columns, | ||
| filterFns, | ||
| sortingFns, | ||
| state: { sorting }, | ||
| onSortingChange: setSorting, | ||
| getCoreRowModel: getCoreRowModel(), | ||
| getSortedRowModel: getSortedRowModel(), | ||
| }); | ||
|
|
||
| return ( | ||
| <DataTableInstanceProvider table={table}> | ||
| <DataTableHeader column={table.getColumn(columnId)!} name={name}> | ||
| Header | ||
| </DataTableHeader> | ||
| <output data-testid={"sorting"}>{JSON.stringify(sorting)}</output> | ||
| <ul> | ||
| {table.getRowModel().rows.map((row) => ( | ||
| <li key={row.id}>{row.original.name}</li> | ||
| ))} | ||
| </ul> | ||
| </DataTableInstanceProvider> | ||
| ); | ||
| } | ||
|
|
||
| const defaultSorting: SortingState = [ | ||
| { id: "connected", desc: true }, | ||
| { id: "last_seen", desc: true }, | ||
| { id: "name", desc: false }, | ||
| ]; | ||
|
|
||
| const rowOrder = () => | ||
| screen.getAllByRole("listitem").map((row) => row.textContent); | ||
|
|
||
| const sortingState = () => | ||
| JSON.parse(screen.getByTestId("sorting").textContent ?? ""); | ||
|
|
||
| const clickHeader = () => fireEvent.click(screen.getByText("Header")); | ||
|
|
||
| describe("DataTableHeader sorting", () => { | ||
| it("replaces a default multi-sort when its lowest-priority column is clicked", () => { | ||
| render(<Harness initialSorting={defaultSorting} columnId={"name"} />); | ||
| expect(rowOrder()).toEqual(["zulu", "mike", "alpha"]); | ||
|
|
||
| clickHeader(); | ||
|
|
||
| expect(rowOrder()).toEqual(["alpha", "mike", "zulu"]); | ||
| expect(sortingState()).toEqual([{ id: "name", desc: false }]); | ||
| }); | ||
|
|
||
| it("sorts ascending on the first click of a column that does not lead the sort", () => { | ||
| render(<Harness initialSorting={[]} columnId={"name"} />); | ||
|
|
||
| clickHeader(); | ||
|
|
||
| expect(sortingState()).toEqual([{ id: "name", desc: false }]); | ||
| expect(rowOrder()).toEqual(["alpha", "mike", "zulu"]); | ||
| }); | ||
|
|
||
| it("toggles the direction while the column leads the sort", () => { | ||
| render( | ||
| <Harness | ||
| initialSorting={[{ id: "name", desc: false }]} | ||
| columnId={"name"} | ||
| />, | ||
| ); | ||
|
|
||
| clickHeader(); | ||
| expect(sortingState()).toEqual([{ id: "name", desc: true }]); | ||
| expect(rowOrder()).toEqual(["zulu", "mike", "alpha"]); | ||
|
|
||
| clickHeader(); | ||
| expect(sortingState()).toEqual([{ id: "name", desc: false }]); | ||
| expect(rowOrder()).toEqual(["alpha", "mike", "zulu"]); | ||
| }); | ||
|
|
||
| it("reports the direction it applied to server-side pagination", () => { | ||
| render( | ||
| <Harness | ||
| initialSorting={defaultSorting} | ||
| columnId={"name"} | ||
| name={"name"} | ||
| />, | ||
| ); | ||
|
|
||
| clickHeader(); | ||
|
|
||
| expect(sortingState()).toEqual([{ id: "name", desc: false }]); | ||
| expect(setSort).toHaveBeenCalledWith("name", "asc"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: netbirdio/dashboard
Length of output: 999
🏁 Script executed:
Repository: netbirdio/dashboard
Length of output: 21321
🏁 Script executed:
Repository: netbirdio/dashboard
Length of output: 7967
Preserve fallback direction toggling.
When
tableis unavailable,handleSortpassesfalsetocolumn.toggleSorting, which forces ascending order instead of toggling an existing descending sort. The same value causes server pagination to report"asc". Use the fallback toggle result for both sorting and server pagination, and add an unwrapped-header test for descending input.🤖 Prompt for AI Agents