Add better search for network-routes by group name (#336)

This commit is contained in:
Eduard Gert
2024-02-16 12:15:14 +01:00
committed by GitHub
parent 6c74506316
commit bca327e4cf
4 changed files with 58 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ export interface Route {
peer_groups?: string[]; peer_groups?: string[];
routesGroups?: string[]; routesGroups?: string[];
groupedRoutes?: GroupedRoute[]; groupedRoutes?: GroupedRoute[];
group_names?: string[];
} }
export interface GroupedRoute { export interface GroupedRoute {
@@ -22,5 +23,6 @@ export interface GroupedRoute {
high_availability_count: number; high_availability_count: number;
is_using_route_groups: boolean; is_using_route_groups: boolean;
routes?: Route[]; routes?: Route[];
group_names?: string[];
description?: string; description?: string;
} }

View File

@@ -43,6 +43,12 @@ export const GroupedRouteTableColumns: ColumnDef<GroupedRoute>[] = [
accessorKey: "enabled", accessorKey: "enabled",
sortingFn: "basic", sortingFn: "basic",
}, },
{
id: "group_names",
accessorFn: (row) => {
return row.group_names?.map((name) => name).join(", ");
},
},
{ {
accessorKey: "network", accessorKey: "network",
header: ({ column }) => { header: ({ column }) => {
@@ -121,10 +127,11 @@ export default function NetworkRoutesTable({
setSorting={setSorting} setSorting={setSorting}
columns={GroupedRouteTableColumns} columns={GroupedRouteTableColumns}
data={groupedRoutes} data={groupedRoutes}
searchPlaceholder={"Search by network, range or name..."} searchPlaceholder={"Search by network, range, name or groups..."}
columnVisibility={{ columnVisibility={{
enabled: false, enabled: false,
description: false, description: false,
group_names: false,
}} }}
renderExpandedRow={(row) => { renderExpandedRow={(row) => {
const data = cloneDeep(row); const data = cloneDeep(row);

View File

@@ -41,6 +41,19 @@ export default function useGroupedRoutes({ routes }: Props) {
const countEnabledPeers = peerRoutes.filter((r) => r.enabled).length; const countEnabledPeers = peerRoutes.filter((r) => r.enabled).length;
const allPeers = countPeersOfGroup + countEnabledPeers; const allPeers = countPeersOfGroup + countEnabledPeers;
// Get the group names for better search results
const peerGroupNames =
groupPeerRoute?.peer_groups?.map((id) => {
return groups?.find((g) => g.id == id)?.name || "";
}) || [];
const routeGroups = routes.map((r) => r.groups).flat();
const distributionGroupNames = routeGroups.map((group) => {
return groups?.find((g) => g.id == group)?.name || "";
});
const allGroupNames = [...peerGroupNames, ...distributionGroupNames];
results.push({ results.push({
id, id,
enabled: routes.find((r) => r.enabled) != undefined, enabled: routes.find((r) => r.enabled) != undefined,
@@ -50,6 +63,7 @@ export default function useGroupedRoutes({ routes }: Props) {
is_using_route_groups: !!groupPeerRoute, is_using_route_groups: !!groupPeerRoute,
description: groupPeerRoute ? groupPeerRoute?.description : undefined, description: groupPeerRoute ? groupPeerRoute?.description : undefined,
routes: routes, routes: routes,
group_names: allGroupNames,
}); });
}); });
return results; return results;

View File

@@ -1,7 +1,8 @@
import { DataTable } from "@components/table/DataTable"; import { DataTable } from "@components/table/DataTable";
import DataTableHeader from "@components/table/DataTableHeader"; import DataTableHeader from "@components/table/DataTableHeader";
import { ColumnDef, SortingState } from "@tanstack/react-table"; import { ColumnDef, SortingState } from "@tanstack/react-table";
import React, { useState } from "react"; import React, { useMemo, useState } from "react";
import { useGroups } from "@/contexts/GroupsProvider";
import { GroupedRoute, Route } from "@/interfaces/Route"; import { GroupedRoute, Route } from "@/interfaces/Route";
import RouteActionCell from "@/modules/routes/RouteActionCell"; import RouteActionCell from "@/modules/routes/RouteActionCell";
import RouteActiveCell from "@/modules/routes/RouteActiveCell"; import RouteActiveCell from "@/modules/routes/RouteActiveCell";
@@ -39,7 +40,6 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
), ),
cell: ({ row }) => <RouteActiveCell route={row.original} />, cell: ({ row }) => <RouteActiveCell route={row.original} />,
}, },
{ {
id: "groups", id: "groups",
accessorFn: (r) => r.groups?.length, accessorFn: (r) => r.groups?.length,
@@ -50,6 +50,12 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
}, },
cell: ({ row }) => <RouteDistributionGroupsCell route={row.original} />, cell: ({ row }) => <RouteDistributionGroupsCell route={row.original} />,
}, },
{
id: "group_names",
accessorFn: (row) => {
return row.group_names?.map((name) => name).join(", ");
},
},
{ {
accessorKey: "id", accessorKey: "id",
header: "", header: "",
@@ -58,6 +64,8 @@ export const RouteTableColumns: ColumnDef<Route>[] = [
]; ];
export default function RouteTable({ row }: Props) { export default function RouteTable({ row }: Props) {
const { groups } = useGroups();
// Default sorting state of the table // Default sorting state of the table
const [sorting, setSorting] = useState<SortingState>([ const [sorting, setSorting] = useState<SortingState>([
{ {
@@ -74,6 +82,26 @@ export default function RouteTable({ row }: Props) {
const [currentRow, setCurrentRow] = useState<Route>(); const [currentRow, setCurrentRow] = useState<Route>();
const [currentCellClicked, setCurrentCellClicked] = useState(""); const [currentCellClicked, setCurrentCellClicked] = useState("");
const data = useMemo(() => {
if (!row.routes) return [];
// Get the group names for better search results
return row.routes.map((route) => {
const distributionGroupNames =
route.groups?.map((id) => {
return groups?.find((g) => g.id === id)?.name || "";
}) || [];
const peerGroupNames =
route.peer_groups?.map((id) => {
return groups?.find((g) => g.id === id)?.name || "";
}) || [];
const allGroupNames = [...distributionGroupNames, ...peerGroupNames];
return {
...route,
group_names: allGroupNames,
} as Route;
});
}, [row.routes, groups]);
return ( return (
<> <>
{editModal && currentRow && ( {editModal && currentRow && (
@@ -92,6 +120,9 @@ export default function RouteTable({ row }: Props) {
text={"Network Routes"} text={"Network Routes"}
manualPagination={true} manualPagination={true}
sorting={sorting} sorting={sorting}
columnVisibility={{
group_names: false,
}}
onRowClick={(row, cell) => { onRowClick={(row, cell) => {
setCurrentRow(row.original); setCurrentRow(row.original);
setEditModal(true); setEditModal(true);
@@ -99,7 +130,7 @@ export default function RouteTable({ row }: Props) {
}} }}
setSorting={setSorting} setSorting={setSorting}
columns={RouteTableColumns} columns={RouteTableColumns}
data={row.routes} data={data}
/> />
</> </>
); );