Skip to content

Commit

Permalink
fixes navigation when searching for groups (keycloak#26053)
Browse files Browse the repository at this point in the history
fixes: keycloak#25927

Signed-off-by: Erik Jan de Wit <[email protected]>
  • Loading branch information
edewit authored Jan 16, 2024
1 parent bd62c73 commit 12ae800
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
98 changes: 50 additions & 48 deletions js/apps/admin-ui/src/groups/components/GroupTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ export const GroupTree = ({
const { hasAccess } = useAccess();

const [data, setData] = useState<TreeViewDataItem[]>();
const [groups, setGroups] = useState<GroupRepresentation[]>([]);
const { subGroups, setSubGroups } = useSubGroups();
const { subGroups, clear } = useSubGroups();

const [search, setSearch] = useState("");
const [max, setMax] = useState(20);
Expand Down Expand Up @@ -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: (
<Button
variant="plain"
onClick={() => setFirstSub(firstSub + SUBGROUP_COUNT)}
>
<AngleRightIcon />
</Button>
),
},
]
: []),
];
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: (
<Button
variant="plain"
onClick={() => setFirstSub(firstSub + SUBGROUP_COUNT)}
>
<AngleRightIcon />
</Button>
),
},
]
: []),
];
}
}
setGroups(groups);
if (search || prefFirst.current !== first || prefMax.current !== max) {
setData(groups.map((g) => mapGroup(g, refresh)));
} else {
Expand All @@ -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) {
path.push(group);
return path;
}

if (group.id === id) {
found.push(...path, group);
if (group.children) {
path.push(group);
findGroup(group.children, id, path);
if (path[path.length - 1].id !== id) {
path.pop();
}
}
});
}
return path;
};

return data ? (
Expand Down Expand Up @@ -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 }));
Expand Down
5 changes: 2 additions & 3 deletions js/apps/admin-ui/src/groups/routes/Groups.tsx
Original file line number Diff line number Diff line change
@@ -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 };
Expand All @@ -24,6 +23,6 @@ export const toGroups = (params: GroupsParams): Partial<Path> => {
const path = params.id ? GroupsWithIdRoute.path : GroupsRoute.path;

return {
pathname: generateEncodedPath(path, params),
pathname: generatePath(path, params),
};
};

0 comments on commit 12ae800

Please sign in to comment.