1
1
import { DataTable } from "@components/table/DataTable" ;
2
2
import DataTableHeader from "@components/table/DataTableHeader" ;
3
3
import { ColumnDef , SortingState } from "@tanstack/react-table" ;
4
- import React , { useState } from "react" ;
4
+ import React , { useMemo , useState } from "react" ;
5
+ import { useGroups } from "@/contexts/GroupsProvider" ;
5
6
import { GroupedRoute , Route } from "@/interfaces/Route" ;
6
7
import RouteActionCell from "@/modules/routes/RouteActionCell" ;
7
8
import RouteActiveCell from "@/modules/routes/RouteActiveCell" ;
@@ -39,7 +40,6 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
39
40
) ,
40
41
cell : ( { row } ) => < RouteActiveCell route = { row . original } /> ,
41
42
} ,
42
-
43
43
{
44
44
id : "groups" ,
45
45
accessorFn : ( r ) => r . groups ?. length ,
@@ -50,6 +50,12 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
50
50
} ,
51
51
cell : ( { row } ) => < RouteDistributionGroupsCell route = { row . original } /> ,
52
52
} ,
53
+ {
54
+ id : "group_names" ,
55
+ accessorFn : ( row ) => {
56
+ return row . group_names ?. map ( ( name ) => name ) . join ( ", " ) ;
57
+ } ,
58
+ } ,
53
59
{
54
60
accessorKey : "id" ,
55
61
header : "" ,
@@ -58,6 +64,8 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
58
64
] ;
59
65
60
66
export default function RouteTable ( { row } : Props ) {
67
+ const { groups } = useGroups ( ) ;
68
+
61
69
// Default sorting state of the table
62
70
const [ sorting , setSorting ] = useState < SortingState > ( [
63
71
{
@@ -74,6 +82,26 @@ export default function RouteTable({ row }: Props) {
74
82
const [ currentRow , setCurrentRow ] = useState < Route > ( ) ;
75
83
const [ currentCellClicked , setCurrentCellClicked ] = useState ( "" ) ;
76
84
85
+ const data = useMemo ( ( ) => {
86
+ if ( ! row . routes ) return [ ] ;
87
+ // Get the group names for better search results
88
+ return row . routes . map ( ( route ) => {
89
+ const distributionGroupNames =
90
+ route . groups ?. map ( ( id ) => {
91
+ return groups ?. find ( ( g ) => g . id === id ) ?. name || "" ;
92
+ } ) || [ ] ;
93
+ const peerGroupNames =
94
+ route . peer_groups ?. map ( ( id ) => {
95
+ return groups ?. find ( ( g ) => g . id === id ) ?. name || "" ;
96
+ } ) || [ ] ;
97
+ const allGroupNames = [ ...distributionGroupNames , ...peerGroupNames ] ;
98
+ return {
99
+ ...route ,
100
+ group_names : allGroupNames ,
101
+ } as Route ;
102
+ } ) ;
103
+ } , [ row . routes , groups ] ) ;
104
+
77
105
return (
78
106
< >
79
107
{ editModal && currentRow && (
@@ -92,14 +120,17 @@ export default function RouteTable({ row }: Props) {
92
120
text = { "Network Routes" }
93
121
manualPagination = { true }
94
122
sorting = { sorting }
123
+ columnVisibility = { {
124
+ group_names : false ,
125
+ } }
95
126
onRowClick = { ( row , cell ) => {
96
127
setCurrentRow ( row . original ) ;
97
128
setEditModal ( true ) ;
98
129
setCurrentCellClicked ( cell ) ;
99
130
} }
100
131
setSorting = { setSorting }
101
132
columns = { RouteTableColumns }
102
- data = { row . routes }
133
+ data = { data }
103
134
/>
104
135
</ >
105
136
) ;
0 commit comments