@@ -3,11 +3,18 @@ import {
33 getCoreRowModel ,
44 getSortedRowModel ,
55 useReactTable ,
6+ type SortingState ,
67} from '@tanstack/react-table'
78import { addWeeks , format , parseISO , subWeeks } from 'date-fns'
89import Link from 'next/link'
9- import { useMemo } from 'react'
10- import { FaArrowAltCircleDown , FaArrowAltCircleUp } from 'react-icons/fa'
10+ import { useMemo , useState } from 'react'
11+ import {
12+ FaArrowAltCircleDown ,
13+ FaArrowAltCircleUp ,
14+ FaSort ,
15+ FaSortDown ,
16+ FaSortUp ,
17+ } from 'react-icons/fa'
1118
1219export interface Changepoint {
1320 id : string
@@ -24,11 +31,14 @@ export interface Changepoint {
2431const AlertList : React . FC < { changepoints : Changepoint [ ] } > = ( {
2532 changepoints,
2633} ) => {
34+ const [ sorting , setSorting ] = useState < SortingState > ( [ ] )
35+
2736 const columns = useMemo (
2837 ( ) => [
2938 {
3039 header : 'Direction' ,
3140 accessorKey : 'change_dir' ,
41+ enableSorting : true ,
3242 cell : ( { getValue } : { getValue : ( ) => 'up' | 'down' } ) => {
3343 const direction = getValue ( )
3444 return (
@@ -45,6 +55,7 @@ const AlertList: React.FC<{ changepoints: Changepoint[] }> = ({
4555 {
4656 header : 'Time' ,
4757 accessorKey : 'start_time' ,
58+ enableSorting : true ,
4859 cell : ( { getValue } : { getValue : ( ) => string } ) => {
4960 const time = getValue ( )
5061 return time
@@ -53,10 +64,12 @@ const AlertList: React.FC<{ changepoints: Changepoint[] }> = ({
5364 {
5465 header : 'Country' ,
5566 accessorKey : 'probe_cc' ,
67+ enableSorting : true ,
5668 } ,
5769 {
5870 header : 'Network' ,
5971 accessorKey : 'probe_asn' ,
72+ enableSorting : true ,
6073 cell : ( { getValue } : { getValue : ( ) => string } ) => {
6174 const asn = getValue ( )
6275 return `AS${ asn } `
@@ -65,10 +78,12 @@ const AlertList: React.FC<{ changepoints: Changepoint[] }> = ({
6578 {
6679 header : 'Domain' ,
6780 accessorKey : 'domain' ,
81+ enableSorting : true ,
6882 } ,
6983 {
7084 header : 'See more' ,
7185 id : 'see_more' ,
86+ enableSorting : false ,
7287 cell : ( { row } : { row : { original : Changepoint } } ) => {
7388 const { probe_asn, probe_cc, domain, start_time } = row . original
7489 const startDate = parseISO ( start_time )
@@ -97,9 +112,13 @@ const AlertList: React.FC<{ changepoints: Changepoint[] }> = ({
97112 [ ] ,
98113 )
99114
100- const { getHeaderGroups , getRowModel } = useReactTable ( {
115+ const table = useReactTable ( {
101116 columns,
102117 data : changepoints ,
118+ state : {
119+ sorting,
120+ } ,
121+ onSortingChange : setSorting ,
103122 getCoreRowModel : getCoreRowModel ( ) ,
104123 getSortedRowModel : getSortedRowModel ( ) ,
105124 } )
@@ -108,25 +127,47 @@ const AlertList: React.FC<{ changepoints: Changepoint[] }> = ({
108127 < div className = "flow-root overflow-x-auto" >
109128 < table className = "min-w-full divide-y divide-gray-400" >
110129 < thead >
111- { getHeaderGroups ( ) . map ( ( headerGroup ) => (
130+ { table . getHeaderGroups ( ) . map ( ( headerGroup ) => (
112131 < tr key = { headerGroup . id } >
113132 { headerGroup . headers . map ( ( header ) => (
114133 < th
115134 key = { header . id }
116135 className = "px-3 py-3.5 text-sm text-start font-semibold text-gray-900"
117136 scope = "col"
118137 >
119- { flexRender (
120- header . column . columnDef . header ,
121- header . getContext ( ) ,
138+ { header . isPlaceholder ? null : (
139+ < div
140+ { ...( header . column . getCanSort ( )
141+ ? {
142+ className :
143+ 'flex items-center gap-2 cursor-pointer select-none hover:text-gray-700' ,
144+ onClick : header . column . getToggleSortingHandler ( ) ,
145+ }
146+ : { className : 'flex items-center' } ) }
147+ >
148+ { flexRender (
149+ header . column . columnDef . header ,
150+ header . getContext ( ) ,
151+ ) }
152+ { header . column . getCanSort ( ) && (
153+ < span className = "text-gray-400" >
154+ { {
155+ asc : < FaSortUp /> ,
156+ desc : < FaSortDown /> ,
157+ } [ header . column . getIsSorted ( ) as string ] ?? (
158+ < FaSort />
159+ ) }
160+ </ span >
161+ ) }
162+ </ div >
122163 ) }
123164 </ th >
124165 ) ) }
125166 </ tr >
126167 ) ) }
127168 </ thead >
128169 < tbody >
129- { getRowModel ( ) . rows . map ( ( row ) => {
170+ { table . getRowModel ( ) . rows . map ( ( row ) => {
130171 return (
131172 < tr key = { row . id } className = "even:bg-gray-50" >
132173 { row . getVisibleCells ( ) . map ( ( cell ) => {
0 commit comments