From 12ae800217daf9cdc5a6b569acb8285ea889b01e Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Tue, 16 Jan 2024 10:59:52 +0100 Subject: [PATCH] fixes navigation when searching for groups (#26053) fixes: #25927 Signed-off-by: Erik Jan de Wit --- .../src/groups/components/GroupTree.tsx | 100 +++++++++--------- js/apps/admin-ui/src/groups/routes/Groups.tsx | 5 +- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/js/apps/admin-ui/src/groups/components/GroupTree.tsx b/js/apps/admin-ui/src/groups/components/GroupTree.tsx index 107bdeaa71..681f524a16 100644 --- a/js/apps/admin-ui/src/groups/components/GroupTree.tsx +++ b/js/apps/admin-ui/src/groups/components/GroupTree.tsx @@ -138,8 +138,7 @@ export const GroupTree = ({ const { hasAccess } = useAccess(); const [data, setData] = useState(); - const [groups, setGroups] = useState([]); - const { subGroups, setSubGroups } = useSubGroups(); + const { subGroups, clear } = useSubGroups(); const [search, setSearch] = useState(""); const [max, setMax] = useState(20); @@ -207,32 +206,31 @@ export const GroupTree = ({ return { groups, subGroups }; }, ({ groups, subGroups }) => { - const found: TreeViewDataItem[] = []; - if (activeItem) findGroup(data || [], activeItem.id!, [], found); - - if (found.length && subGroups.length) { - const foundTreeItem = found.pop()!; - foundTreeItem.children = [ - ...(unionBy(foundTreeItem.children || []).splice(0, SUBGROUP_COUNT), - subGroups.map((g) => mapGroup(g, refresh), "id")), - ...(subGroups.length === SUBGROUP_COUNT - ? [ - { - id: "next", - name: ( - - ), - }, - ] - : []), - ]; + if (activeItem) { + const found = findGroup(data || [], activeItem.id!, []); + if (found.length && subGroups.length) { + const foundTreeItem = found.pop()!; + foundTreeItem.children = [ + ...(unionBy(foundTreeItem.children || []).splice(0, SUBGROUP_COUNT), + subGroups.map((g) => mapGroup(g, refresh), "id")), + ...(subGroups.length === SUBGROUP_COUNT + ? [ + { + id: "next", + name: ( + + ), + }, + ] + : []), + ]; + } } - setGroups(groups); if (search || prefFirst.current !== first || prefMax.current !== max) { setData(groups.map((g) => mapGroup(g, refresh))); } else { @@ -252,26 +250,26 @@ export const GroupTree = ({ ); const findGroup = ( - groups: GroupRepresentation[] | TreeViewDataItem[], + groups: TreeViewDataItem[], id: string, - path: (GroupRepresentation | TreeViewDataItem)[], - found: (GroupRepresentation | TreeViewDataItem)[], + path: TreeViewDataItem[], ) => { - return groups.map((group) => { - if (found.length > 0) return; - - if ("subGroups" in group && group.subGroups?.length) { - findGroup(group.subGroups, id, [...path, group], found); - } - - if ("children" in group && group.children) { - findGroup(group.children, id, [...path, group], found); - } - + for (let index = 0; index < groups.length; index++) { + const group = groups[index]; if (group.id === id) { - found.push(...path, group); + path.push(group); + return path; } - }); + + if (group.children) { + path.push(group); + findGroup(group.children, id, path); + if (path[path.length - 1].id !== id) { + path.pop(); + } + } + } + return path; }; return data ? ( @@ -314,14 +312,18 @@ export const GroupTree = ({ onSelect={(_, item) => { if (item.id === "next") return; setActiveItem(item); - const id = item.id?.substring(item.id.lastIndexOf("/") + 1); - const subGroups: GroupRepresentation[] = []; - findGroup(groups, id!, [], subGroups); - setSubGroups(subGroups); + + const path = findGroup(data, item.id!, []); + if (!subGroups.every(({ id }) => path.find((t) => t.id === id))) + clear(); if (canViewDetails || subGroups.at(-1)?.access?.view) { - navigate(toGroups({ realm, id: item.id })); - refresh(); + navigate( + toGroups({ + realm, + id: path.map((g) => g.id).join("/"), + }), + ); } else { addAlert(t("noViewRights"), AlertVariant.warning); navigate(toGroups({ realm })); diff --git a/js/apps/admin-ui/src/groups/routes/Groups.tsx b/js/apps/admin-ui/src/groups/routes/Groups.tsx index 2d14cc72d8..2513e9dabb 100644 --- a/js/apps/admin-ui/src/groups/routes/Groups.tsx +++ b/js/apps/admin-ui/src/groups/routes/Groups.tsx @@ -1,6 +1,5 @@ import { lazy } from "react"; -import type { Path } from "react-router-dom"; -import { generateEncodedPath } from "../../utils/generateEncodedPath"; +import { generatePath, type Path } from "react-router-dom"; import type { AppRouteObject } from "../../routes"; export type GroupsParams = { realm: string; id?: string; lazy?: string }; @@ -24,6 +23,6 @@ export const toGroups = (params: GroupsParams): Partial => { const path = params.id ? GroupsWithIdRoute.path : GroupsRoute.path; return { - pathname: generateEncodedPath(path, params), + pathname: generatePath(path, params), }; };