forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageNav.tsx
141 lines (127 loc) · 4.25 KB
/
PageNav.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
Divider,
Nav,
NavGroup,
NavItem,
NavList,
PageSidebar,
PageSidebarBody,
} from "@patternfly/react-core";
import { FormEvent } from "react";
import { useTranslation } from "react-i18next";
import { NavLink, useMatch, useNavigate } from "react-router-dom";
import { RealmSelector } from "./components/realm-selector/RealmSelector";
import { useAccess } from "./context/access/Access";
import { useRealm } from "./context/realm-context/RealmContext";
import { useServerInfo } from "./context/server-info/ServerInfoProvider";
import { toPage } from "./page/routes";
import { AddRealmRoute } from "./realm/routes/AddRealm";
import { routes } from "./routes";
import "./page-nav.css";
import useIsFeatureEnabled, { Feature } from "./utils/useIsFeatureEnabled";
type LeftNavProps = { title: string; path: string; id?: string };
const LeftNav = ({ title, path, id }: LeftNavProps) => {
const { t } = useTranslation();
const { hasAccess } = useAccess();
const { realm } = useRealm();
const encodedRealm = encodeURIComponent(realm);
const route = routes.find(
(route) =>
route.path.replace(/\/:.+?(\?|(?:(?!\/).)*|$)/g, "") === (id || path),
);
const accessAllowed =
route &&
(route.handle.access instanceof Array
? hasAccess(...route.handle.access)
: hasAccess(route.handle.access));
if (!accessAllowed) {
return null;
}
return (
<li>
<NavLink
id={"nav-item" + path.replace("/", "-")}
to={`/${encodedRealm}${path}`}
className={({ isActive }) =>
`pf-v5-c-nav__link${isActive ? " pf-m-current" : ""}`
}
>
{t(title)}
</NavLink>
</li>
);
};
export const PageNav = () => {
const { t } = useTranslation();
const { hasSomeAccess } = useAccess();
const { componentTypes } = useServerInfo();
const isFeatureEnabled = useIsFeatureEnabled();
const pages =
componentTypes?.["org.keycloak.services.ui.extend.UiPageProvider"];
const navigate = useNavigate();
type SelectedItem = {
groupId: number | string;
itemId: number | string;
to: string;
event: FormEvent<HTMLInputElement>;
};
const onSelect = (item: SelectedItem) => {
navigate(item.to);
item.event.preventDefault();
};
const showManage = hasSomeAccess(
"view-realm",
"query-groups",
"query-users",
"query-clients",
"view-events",
);
const showConfigure = hasSomeAccess(
"view-realm",
"query-clients",
"view-identity-providers",
);
const isOnAddRealm = !!useMatch(AddRealmRoute.path);
return (
<PageSidebar className="keycloak__page_nav__nav">
<PageSidebarBody>
<Nav onSelect={(_event, item) => onSelect(item as SelectedItem)}>
<NavList>
<NavItem className="keycloak__page_nav__nav_item__realm-selector">
<RealmSelector />
</NavItem>
</NavList>
<Divider />
{showManage && !isOnAddRealm && (
<NavGroup aria-label={t("manage")} 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>
)}
{showConfigure && !isOnAddRealm && (
<NavGroup aria-label={t("configure")} 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" />
{isFeatureEnabled(Feature.DeclarativeUI) &&
pages?.map((p) => (
<LeftNav
key={p.id}
title={p.id}
path={toPage({ providerId: p.id }).pathname!}
id="/page-section"
/>
))}
</NavGroup>
)}
</Nav>
</PageSidebarBody>
</PageSidebar>
);
};