2021-02-09 12:32:41 +00:00
|
|
|
import React from "react";
|
2021-07-22 07:13:35 +00:00
|
|
|
import { useHistory, useRouteMatch } from "react-router-dom";
|
2020-09-21 12:54:42 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-09-14 18:10:54 +00:00
|
|
|
import {
|
|
|
|
Nav,
|
|
|
|
NavItem,
|
|
|
|
NavGroup,
|
|
|
|
NavList,
|
|
|
|
PageSidebar,
|
2021-08-17 12:50:31 +00:00
|
|
|
Divider,
|
2020-09-14 18:10:54 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-04-08 19:20:35 +00:00
|
|
|
|
2020-09-01 14:51:59 +00:00
|
|
|
import { RealmSelector } from "./components/realm-selector/RealmSelector";
|
2021-01-05 19:49:33 +00:00
|
|
|
import { useRealm } from "./context/realm-context/RealmContext";
|
2020-10-21 11:31:41 +00:00
|
|
|
import { useAccess } from "./context/access/Access";
|
|
|
|
import { routes } from "./route-config";
|
2021-07-22 07:13:35 +00:00
|
|
|
import { AddRealmRoute } from "./realm/routes/AddRealm";
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2020-09-04 18:16:11 +00:00
|
|
|
export const PageNav: React.FunctionComponent = () => {
|
2020-09-21 12:54:42 +00:00
|
|
|
const { t } = useTranslation("common");
|
2020-10-21 11:31:41 +00:00
|
|
|
const { hasAccess, hasSomeAccess } = useAccess();
|
2021-01-12 12:39:37 +00:00
|
|
|
const { realm } = useRealm();
|
2020-09-09 09:07:17 +00:00
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
type SelectedItem = {
|
|
|
|
groupId: number | string;
|
|
|
|
itemId: number | string;
|
|
|
|
to: string;
|
|
|
|
event: React.FormEvent<HTMLInputElement>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSelect = (item: SelectedItem) => {
|
|
|
|
history.push(item.to);
|
|
|
|
item.event.preventDefault();
|
|
|
|
};
|
|
|
|
|
2020-10-21 11:31:41 +00:00
|
|
|
type LeftNavProps = { title: string; path: string };
|
|
|
|
const LeftNav = ({ title, path }: LeftNavProps) => {
|
2021-07-21 09:20:32 +00:00
|
|
|
const route = routes.find(
|
2021-03-05 10:42:06 +00:00
|
|
|
(route) => route.path.replace(/\/:.+?(\?|(?:(?!\/).)*|$)/g, "") === path
|
2021-01-05 19:49:33 +00:00
|
|
|
);
|
2021-08-09 20:20:49 +00:00
|
|
|
|
|
|
|
const accessAllowed =
|
|
|
|
route &&
|
|
|
|
(route.access instanceof Array
|
|
|
|
? hasAccess(...route.access)
|
|
|
|
: hasAccess(route.access));
|
|
|
|
|
|
|
|
if (!accessAllowed) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:12:57 +00:00
|
|
|
//remove "/realm-name" from the start of the path
|
|
|
|
const activeItem = history.location.pathname.substr(realm.length + 1);
|
2020-09-09 09:07:17 +00:00
|
|
|
return (
|
|
|
|
<NavItem
|
2020-10-21 11:31:41 +00:00
|
|
|
id={"nav-item" + path.replace("/", "-")}
|
2021-01-05 19:49:33 +00:00
|
|
|
to={`/${realm}${path}`}
|
2021-03-01 21:12:57 +00:00
|
|
|
isActive={
|
|
|
|
path === activeItem || (path !== "/" && activeItem.startsWith(path))
|
|
|
|
}
|
2020-09-09 09:07:17 +00:00
|
|
|
>
|
2020-09-21 12:54:42 +00:00
|
|
|
{t(title)}
|
2020-09-09 09:07:17 +00:00
|
|
|
</NavItem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-10-21 11:31:41 +00:00
|
|
|
const showManage = hasSomeAccess(
|
|
|
|
"view-realm",
|
|
|
|
"query-groups",
|
|
|
|
"query-users",
|
2020-10-28 18:17:15 +00:00
|
|
|
"query-clients",
|
2020-10-21 11:31:41 +00:00
|
|
|
"view-events"
|
|
|
|
);
|
|
|
|
|
|
|
|
const showConfigure = hasSomeAccess(
|
|
|
|
"view-realm",
|
|
|
|
"query-clients",
|
|
|
|
"view-identity-providers"
|
|
|
|
);
|
|
|
|
|
2021-07-22 07:13:35 +00:00
|
|
|
const isOnAddRealm = !!useRouteMatch(AddRealmRoute.path);
|
2021-02-09 12:32:41 +00:00
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
return (
|
2020-11-12 19:08:09 +00:00
|
|
|
<PageSidebar
|
|
|
|
nav={
|
|
|
|
<Nav onSelect={onSelect}>
|
|
|
|
<NavList>
|
2021-05-03 16:44:47 +00:00
|
|
|
<NavItem className="keycloak__page_nav__nav_item__realm-selector">
|
|
|
|
<RealmSelector />
|
|
|
|
</NavItem>
|
2020-11-12 19:08:09 +00:00
|
|
|
</NavList>
|
2021-08-17 12:50:31 +00:00
|
|
|
<Divider />
|
2021-07-22 07:13:35 +00:00
|
|
|
{showManage && !isOnAddRealm && (
|
2021-08-17 12:50:31 +00:00
|
|
|
<NavGroup aria-label={t("manage")} title={t("manage")}>
|
2020-11-12 19:08:09 +00:00
|
|
|
<LeftNav title="clients" path="/clients" />
|
|
|
|
<LeftNav title="clientScopes" path="/client-scopes" />
|
|
|
|
<LeftNav title="realmRoles" path="/roles" />
|
|
|
|
<LeftNav title="users" path="/users" />
|
|
|
|
<LeftNav title="groups" path="/groups" />
|
|
|
|
<LeftNav title="sessions" path="/sessions" />
|
|
|
|
<LeftNav title="events" path="/events" />
|
|
|
|
</NavGroup>
|
|
|
|
)}
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2021-07-22 07:13:35 +00:00
|
|
|
{showConfigure && !isOnAddRealm && (
|
2021-08-17 12:50:31 +00:00
|
|
|
<NavGroup aria-label={t("configure")} title={t("configure")}>
|
2020-11-12 19:08:09 +00:00
|
|
|
<LeftNav title="realmSettings" path="/realm-settings" />
|
|
|
|
<LeftNav title="authentication" path="/authentication" />
|
|
|
|
<LeftNav title="identityProviders" path="/identity-providers" />
|
|
|
|
<LeftNav title="userFederation" path="/user-federation" />
|
|
|
|
</NavGroup>
|
|
|
|
)}
|
|
|
|
</Nav>
|
|
|
|
}
|
|
|
|
/>
|
2020-08-04 12:59:41 +00:00
|
|
|
);
|
|
|
|
};
|