import React, { useState } from "react"; import { useHistory, useLocation } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { DropdownItem, PageSection, PageSectionVariants, AlertVariant, Tab, TabTitleText, Tabs, } from "@patternfly/react-core"; import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { useFetch, useAdminClient } from "../context/auth/AdminClient"; import { useAlerts } from "../components/alert/Alerts"; import { useRealm } from "../context/realm-context/RealmContext"; import { useSubGroups } from "./SubGroupsContext"; import { GroupTable } from "./GroupTable"; import { getId, getLastId } from "./groupIdUtils"; import { Members } from "./Members"; import { GroupAttributes } from "./GroupAttributes"; import { GroupsModal } from "./GroupsModal"; import "./GroupsSection.css"; export const GroupsSection = () => { const { t } = useTranslation("groups"); const [activeTab, setActiveTab] = useState(0); const adminClient = useAdminClient(); const { subGroups, setSubGroups, currentGroup } = useSubGroups(); const { addAlert, addError } = useAlerts(); const { realm } = useRealm(); const [rename, setRename] = useState(); const history = useHistory(); const location = useLocation(); const id = getLastId(location.pathname); const deleteGroup = async (group: GroupRepresentation) => { try { await adminClient.groups.del({ id: group.id!, }); addAlert(t("groupDelete"), AlertVariant.success); } catch (error) { addError("groups:groupDeleteError", error); } return true; }; useFetch( async () => { const ids = getId(location.pathname); const isNavigationStateInValid = ids && ids.length > subGroups.length; if (isNavigationStateInValid) { const groups: GroupRepresentation[] = []; for (const i of ids!) { const group = await adminClient.groups.findOne({ id: i }); if (group) groups.push(group); } return groups; } return []; }, (groups: GroupRepresentation[]) => { if (groups.length) setSubGroups(groups); }, [id] ); const SearchDropdown = ( history.push(`/${realm}/groups/search`)} > {t("searchGroup")} ); return ( <> {rename && ( setSubGroups([...subGroups.slice(0, subGroups.length - 1), group!]) } handleModalToggle={() => setRename(undefined)} /> )} setRename(currentGroup().name)} > {t("renameGroup")} , { deleteGroup({ id }); history.push( location.pathname.substr( 0, location.pathname.lastIndexOf("/") ) ); }} > {t("deleteGroup")} , ] : [SearchDropdown] } /> {subGroups.length > 0 && ( setActiveTab(key as number)} isBox > {t("childGroups")}} > {t("members")}} > {t("common:attributes")}} > )} {subGroups.length === 0 && } ); };