2020-11-12 12:55:52 +00:00
|
|
|
import React, { useState } from "react";
|
2020-09-09 09:07:17 +00:00
|
|
|
import { useHistory } 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,
|
|
|
|
} from "@patternfly/react-core";
|
2020-09-01 14:51:59 +00:00
|
|
|
import { RealmSelector } from "./components/realm-selector/RealmSelector";
|
2020-09-08 17:20:29 +00:00
|
|
|
import { DataLoader } from "./components/data-loader/DataLoader";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "./context/auth/AdminClient";
|
2020-10-21 11:31:41 +00:00
|
|
|
import { useAccess } from "./context/access/Access";
|
|
|
|
import { routes } from "./route-config";
|
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();
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-09-08 17:20:29 +00:00
|
|
|
const realmLoader = async () => {
|
2020-11-12 12:55:52 +00:00
|
|
|
return await adminClient.realms.find();
|
2020-09-08 17:20:29 +00:00
|
|
|
};
|
2020-09-09 09:07:17 +00:00
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
let initialItem = history.location.pathname;
|
|
|
|
if (initialItem === "/") initialItem = "/client-list";
|
|
|
|
|
|
|
|
const [activeItem, setActiveItem] = useState(initialItem);
|
|
|
|
|
|
|
|
type SelectedItem = {
|
|
|
|
groupId: number | string;
|
|
|
|
itemId: number | string;
|
|
|
|
to: string;
|
|
|
|
event: React.FormEvent<HTMLInputElement>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSelect = (item: SelectedItem) => {
|
|
|
|
setActiveItem(item.to);
|
|
|
|
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) => {
|
|
|
|
const route = routes(() => {}).find((route) => route.path === path);
|
|
|
|
if (!route || !hasAccess(route.access)) return <></>;
|
|
|
|
|
2020-09-09 09:07:17 +00:00
|
|
|
return (
|
|
|
|
<NavItem
|
2020-10-21 11:31:41 +00:00
|
|
|
id={"nav-item" + path.replace("/", "-")}
|
|
|
|
to={path}
|
|
|
|
isActive={activeItem === 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"
|
|
|
|
);
|
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
return (
|
2020-09-17 11:37:30 +00:00
|
|
|
<DataLoader loader={realmLoader}>
|
|
|
|
{(realmList) => (
|
|
|
|
<PageSidebar
|
|
|
|
nav={
|
|
|
|
<Nav onSelect={onSelect}>
|
|
|
|
<NavList>
|
|
|
|
<NavItem className="keycloak__page_nav__nav_item__realm-selector">
|
2020-10-06 11:56:06 +00:00
|
|
|
<RealmSelector realmList={realmList.data || []} />
|
2020-09-17 11:37:30 +00:00
|
|
|
</NavItem>
|
|
|
|
</NavList>
|
2020-10-21 11:31:41 +00:00
|
|
|
{showManage && (
|
|
|
|
<NavGroup title={t("manage")}>
|
|
|
|
<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
|
|
|
|
2020-10-21 11:31:41 +00:00
|
|
|
{showConfigure && (
|
|
|
|
<NavGroup title={t("configure")}>
|
|
|
|
<LeftNav title="realmSettings" path="/realm-settings" />
|
|
|
|
<LeftNav title="authentication" path="/authentication" />
|
|
|
|
<LeftNav
|
|
|
|
title="identityProviders"
|
|
|
|
path="/identity-providers"
|
|
|
|
/>
|
|
|
|
<LeftNav title="userFederation" path="/user-federation" />
|
|
|
|
</NavGroup>
|
|
|
|
)}
|
2020-09-17 11:37:30 +00:00
|
|
|
</Nav>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</DataLoader>
|
2020-08-04 12:59:41 +00:00
|
|
|
);
|
|
|
|
};
|