Skip to content

Commit bca327e

Browse files
authored
Add better search for network-routes by group name (#336)
1 parent 6c74506 commit bca327e

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/interfaces/Route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface Route {
1212
peer_groups?: string[];
1313
routesGroups?: string[];
1414
groupedRoutes?: GroupedRoute[];
15+
group_names?: string[];
1516
}
1617

1718
export interface GroupedRoute {
@@ -22,5 +23,6 @@ export interface GroupedRoute {
2223
high_availability_count: number;
2324
is_using_route_groups: boolean;
2425
routes?: Route[];
26+
group_names?: string[];
2527
description?: string;
2628
}

src/modules/route-group/NetworkRoutesTable.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export const GroupedRouteTableColumns: ColumnDef<GroupedRoute>[] = [
4343
accessorKey: "enabled",
4444
sortingFn: "basic",
4545
},
46+
{
47+
id: "group_names",
48+
accessorFn: (row) => {
49+
return row.group_names?.map((name) => name).join(", ");
50+
},
51+
},
4652
{
4753
accessorKey: "network",
4854
header: ({ column }) => {
@@ -121,10 +127,11 @@ export default function NetworkRoutesTable({
121127
setSorting={setSorting}
122128
columns={GroupedRouteTableColumns}
123129
data={groupedRoutes}
124-
searchPlaceholder={"Search by network, range or name..."}
130+
searchPlaceholder={"Search by network, range, name or groups..."}
125131
columnVisibility={{
126132
enabled: false,
127133
description: false,
134+
group_names: false,
128135
}}
129136
renderExpandedRow={(row) => {
130137
const data = cloneDeep(row);

src/modules/route-group/useGroupedRoutes.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ export default function useGroupedRoutes({ routes }: Props) {
4141
const countEnabledPeers = peerRoutes.filter((r) => r.enabled).length;
4242
const allPeers = countPeersOfGroup + countEnabledPeers;
4343

44+
// Get the group names for better search results
45+
const peerGroupNames =
46+
groupPeerRoute?.peer_groups?.map((id) => {
47+
return groups?.find((g) => g.id == id)?.name || "";
48+
}) || [];
49+
50+
const routeGroups = routes.map((r) => r.groups).flat();
51+
const distributionGroupNames = routeGroups.map((group) => {
52+
return groups?.find((g) => g.id == group)?.name || "";
53+
});
54+
55+
const allGroupNames = [...peerGroupNames, ...distributionGroupNames];
56+
4457
results.push({
4558
id,
4659
enabled: routes.find((r) => r.enabled) != undefined,
@@ -50,6 +63,7 @@ export default function useGroupedRoutes({ routes }: Props) {
5063
is_using_route_groups: !!groupPeerRoute,
5164
description: groupPeerRoute ? groupPeerRoute?.description : undefined,
5265
routes: routes,
66+
group_names: allGroupNames,
5367
});
5468
});
5569
return results;

src/modules/routes/RouteTable.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { DataTable } from "@components/table/DataTable";
22
import DataTableHeader from "@components/table/DataTableHeader";
33
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";
56
import { GroupedRoute, Route } from "@/interfaces/Route";
67
import RouteActionCell from "@/modules/routes/RouteActionCell";
78
import RouteActiveCell from "@/modules/routes/RouteActiveCell";
@@ -39,7 +40,6 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
3940
),
4041
cell: ({ row }) => <RouteActiveCell route={row.original} />,
4142
},
42-
4343
{
4444
id: "groups",
4545
accessorFn: (r) => r.groups?.length,
@@ -50,6 +50,12 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
5050
},
5151
cell: ({ row }) => <RouteDistributionGroupsCell route={row.original} />,
5252
},
53+
{
54+
id: "group_names",
55+
accessorFn: (row) => {
56+
return row.group_names?.map((name) => name).join(", ");
57+
},
58+
},
5359
{
5460
accessorKey: "id",
5561
header: "",
@@ -58,6 +64,8 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
5864
];
5965

6066
export default function RouteTable({ row }: Props) {
67+
const { groups } = useGroups();
68+
6169
// Default sorting state of the table
6270
const [sorting, setSorting] = useState<SortingState>([
6371
{
@@ -74,6 +82,26 @@ export default function RouteTable({ row }: Props) {
7482
const [currentRow, setCurrentRow] = useState<Route>();
7583
const [currentCellClicked, setCurrentCellClicked] = useState("");
7684

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+
77105
return (
78106
<>
79107
{editModal && currentRow && (
@@ -92,14 +120,17 @@ export default function RouteTable({ row }: Props) {
92120
text={"Network Routes"}
93121
manualPagination={true}
94122
sorting={sorting}
123+
columnVisibility={{
124+
group_names: false,
125+
}}
95126
onRowClick={(row, cell) => {
96127
setCurrentRow(row.original);
97128
setEditModal(true);
98129
setCurrentCellClicked(cell);
99130
}}
100131
setSorting={setSorting}
101132
columns={RouteTableColumns}
102-
data={row.routes}
133+
data={data}
103134
/>
104135
</>
105136
);

0 commit comments

Comments
 (0)