From bca327e4cf050c50ed99acdda9079444b648cb51 Mon Sep 17 00:00:00 2001 From: Eduard Gert Date: Fri, 16 Feb 2024 12:15:14 +0100 Subject: [PATCH] Add better search for network-routes by group name (#336) --- src/interfaces/Route.ts | 2 + .../route-group/NetworkRoutesTable.tsx | 9 ++++- src/modules/route-group/useGroupedRoutes.tsx | 14 +++++++ src/modules/routes/RouteTable.tsx | 37 +++++++++++++++++-- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/interfaces/Route.ts b/src/interfaces/Route.ts index a577d0e..3da4e6f 100644 --- a/src/interfaces/Route.ts +++ b/src/interfaces/Route.ts @@ -12,6 +12,7 @@ export interface Route { peer_groups?: string[]; routesGroups?: string[]; groupedRoutes?: GroupedRoute[]; + group_names?: string[]; } export interface GroupedRoute { @@ -22,5 +23,6 @@ export interface GroupedRoute { high_availability_count: number; is_using_route_groups: boolean; routes?: Route[]; + group_names?: string[]; description?: string; } diff --git a/src/modules/route-group/NetworkRoutesTable.tsx b/src/modules/route-group/NetworkRoutesTable.tsx index cde964c..29e5cac 100644 --- a/src/modules/route-group/NetworkRoutesTable.tsx +++ b/src/modules/route-group/NetworkRoutesTable.tsx @@ -43,6 +43,12 @@ export const GroupedRouteTableColumns: ColumnDef[] = [ accessorKey: "enabled", sortingFn: "basic", }, + { + id: "group_names", + accessorFn: (row) => { + return row.group_names?.map((name) => name).join(", "); + }, + }, { accessorKey: "network", header: ({ column }) => { @@ -121,10 +127,11 @@ export default function NetworkRoutesTable({ setSorting={setSorting} columns={GroupedRouteTableColumns} data={groupedRoutes} - searchPlaceholder={"Search by network, range or name..."} + searchPlaceholder={"Search by network, range, name or groups..."} columnVisibility={{ enabled: false, description: false, + group_names: false, }} renderExpandedRow={(row) => { const data = cloneDeep(row); diff --git a/src/modules/route-group/useGroupedRoutes.tsx b/src/modules/route-group/useGroupedRoutes.tsx index b20d888..3b49a11 100644 --- a/src/modules/route-group/useGroupedRoutes.tsx +++ b/src/modules/route-group/useGroupedRoutes.tsx @@ -41,6 +41,19 @@ export default function useGroupedRoutes({ routes }: Props) { const countEnabledPeers = peerRoutes.filter((r) => r.enabled).length; 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({ id, enabled: routes.find((r) => r.enabled) != undefined, @@ -50,6 +63,7 @@ export default function useGroupedRoutes({ routes }: Props) { is_using_route_groups: !!groupPeerRoute, description: groupPeerRoute ? groupPeerRoute?.description : undefined, routes: routes, + group_names: allGroupNames, }); }); return results; diff --git a/src/modules/routes/RouteTable.tsx b/src/modules/routes/RouteTable.tsx index 9846bda..3764731 100644 --- a/src/modules/routes/RouteTable.tsx +++ b/src/modules/routes/RouteTable.tsx @@ -1,7 +1,8 @@ import { DataTable } from "@components/table/DataTable"; import DataTableHeader from "@components/table/DataTableHeader"; 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 RouteActionCell from "@/modules/routes/RouteActionCell"; import RouteActiveCell from "@/modules/routes/RouteActiveCell"; @@ -39,7 +40,6 @@ export const RouteTableColumns: ColumnDef[] = [ ), cell: ({ row }) => , }, - { id: "groups", accessorFn: (r) => r.groups?.length, @@ -50,6 +50,12 @@ export const RouteTableColumns: ColumnDef[] = [ }, cell: ({ row }) => , }, + { + id: "group_names", + accessorFn: (row) => { + return row.group_names?.map((name) => name).join(", "); + }, + }, { accessorKey: "id", header: "", @@ -58,6 +64,8 @@ export const RouteTableColumns: ColumnDef[] = [ ]; export default function RouteTable({ row }: Props) { + const { groups } = useGroups(); + // Default sorting state of the table const [sorting, setSorting] = useState([ { @@ -74,6 +82,26 @@ export default function RouteTable({ row }: Props) { const [currentRow, setCurrentRow] = 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 ( <> {editModal && currentRow && ( @@ -92,6 +120,9 @@ export default function RouteTable({ row }: Props) { text={"Network Routes"} manualPagination={true} sorting={sorting} + columnVisibility={{ + group_names: false, + }} onRowClick={(row, cell) => { setCurrentRow(row.original); setEditModal(true); @@ -99,7 +130,7 @@ export default function RouteTable({ row }: Props) { }} setSorting={setSorting} columns={RouteTableColumns} - data={row.routes} + data={data} /> );