2020-08-04 12:59:41 +00:00
|
|
|
import {
|
|
|
|
Button,
|
2020-09-17 11:37:30 +00:00
|
|
|
ContextSelector,
|
|
|
|
ContextSelectorItem,
|
2021-07-21 09:30:18 +00:00
|
|
|
Divider,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownToggle,
|
2021-04-08 19:20:35 +00:00
|
|
|
Label,
|
2021-07-21 09:30:18 +00:00
|
|
|
Split,
|
|
|
|
SplitItem,
|
2020-09-01 14:51:59 +00:00
|
|
|
} from "@patternfly/react-core";
|
2020-09-17 11:37:30 +00:00
|
|
|
import { CheckIcon } from "@patternfly/react-icons";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { Fragment, ReactElement, useMemo, useState } from "react";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-16 13:09:14 +00:00
|
|
|
import { useNavigate } from "react-router-dom-v5-compat";
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2021-01-05 19:49:33 +00:00
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2021-11-11 16:04:04 +00:00
|
|
|
import { useRealms } from "../../context/RealmsContext";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useWhoAmI } from "../../context/whoami/WhoAmI";
|
2021-09-07 11:19:50 +00:00
|
|
|
import { toDashboard } from "../../dashboard/routes/Dashboard";
|
2021-07-22 07:13:35 +00:00
|
|
|
import { toAddRealm } from "../../realm/routes/AddRealm";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { toUpperCase } from "../../util";
|
2021-04-08 19:20:35 +00:00
|
|
|
import { RecentUsed } from "./recent-used";
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2020-09-14 18:10:54 +00:00
|
|
|
import "./realm-selector.css";
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2021-05-03 16:44:47 +00:00
|
|
|
export const RealmSelector = () => {
|
2021-11-11 16:04:04 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
const { realms } = useRealms();
|
2021-07-21 09:30:18 +00:00
|
|
|
const { whoAmI } = useWhoAmI();
|
2020-08-04 12:59:41 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
2020-09-17 11:37:30 +00:00
|
|
|
const [search, setSearch] = useState("");
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2020-10-29 18:56:52 +00:00
|
|
|
const { t } = useTranslation("common");
|
2021-04-08 19:20:35 +00:00
|
|
|
const recentUsed = new RecentUsed();
|
2021-05-19 12:35:11 +00:00
|
|
|
const all = recentUsed.used
|
2021-06-08 13:13:04 +00:00
|
|
|
.filter((r) => r !== realm)
|
2021-05-19 12:35:11 +00:00
|
|
|
.map((name) => {
|
|
|
|
return { name, used: true };
|
|
|
|
})
|
|
|
|
.concat(
|
|
|
|
realms
|
2022-01-20 10:20:35 +00:00
|
|
|
.filter((r) => !recentUsed.used.includes(r.realm!) || r.realm === realm)
|
2021-05-19 12:35:11 +00:00
|
|
|
.map((r) => {
|
|
|
|
return { name: r.realm!, used: false };
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const filteredItems = useMemo(() => {
|
|
|
|
if (search === "") {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return all.filter((r) =>
|
|
|
|
r.name.toLowerCase().includes(search.toLowerCase())
|
|
|
|
);
|
|
|
|
}, [search, all]);
|
2021-04-08 19:20:35 +00:00
|
|
|
|
2020-09-17 11:37:30 +00:00
|
|
|
const RealmText = ({ value }: { value: string }) => (
|
2020-09-23 08:19:46 +00:00
|
|
|
<Split className="keycloak__realm_selector__list-item-split">
|
2020-09-17 11:37:30 +00:00
|
|
|
<SplitItem isFilled>{toUpperCase(value)}</SplitItem>
|
|
|
|
<SplitItem>{value === realm && <CheckIcon />}</SplitItem>
|
|
|
|
</Split>
|
|
|
|
);
|
|
|
|
|
2021-04-08 19:20:35 +00:00
|
|
|
const AddRealm = () => (
|
2020-09-23 08:19:46 +00:00
|
|
|
<Button
|
2021-05-03 16:44:47 +00:00
|
|
|
data-testid="add-realm"
|
2020-09-23 08:19:46 +00:00
|
|
|
component="div"
|
|
|
|
isBlock
|
2021-01-12 12:39:37 +00:00
|
|
|
onClick={() => {
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toAddRealm({ realm }));
|
2021-01-12 12:39:37 +00:00
|
|
|
setOpen(!open);
|
|
|
|
}}
|
2020-09-23 08:19:46 +00:00
|
|
|
>
|
2020-10-29 18:56:52 +00:00
|
|
|
{t("createRealm")}
|
2020-09-17 11:37:30 +00:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
2021-04-08 19:20:35 +00:00
|
|
|
const selectRealm = (realm: string) => {
|
|
|
|
setOpen(!open);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toDashboard({ realm }));
|
2021-04-08 19:20:35 +00:00
|
|
|
};
|
|
|
|
|
2021-05-03 16:44:47 +00:00
|
|
|
const dropdownItems = realms.map((r) => (
|
2020-09-04 18:16:11 +00:00
|
|
|
<DropdownItem
|
2020-10-26 08:00:05 +00:00
|
|
|
key={`realm-dropdown-item-${r.realm}`}
|
2020-09-08 17:20:29 +00:00
|
|
|
onClick={() => {
|
2021-04-08 19:20:35 +00:00
|
|
|
selectRealm(r.realm!);
|
2020-09-08 17:20:29 +00:00
|
|
|
}}
|
2020-09-04 18:16:11 +00:00
|
|
|
>
|
2020-11-12 12:55:52 +00:00
|
|
|
<RealmText value={r.realm!} />
|
2020-08-27 12:09:36 +00:00
|
|
|
</DropdownItem>
|
2020-08-04 12:59:41 +00:00
|
|
|
));
|
2020-09-04 18:16:11 +00:00
|
|
|
|
2020-10-02 06:47:32 +00:00
|
|
|
const addRealmComponent = (
|
2022-08-03 12:12:07 +00:00
|
|
|
<Fragment key="Add Realm">
|
2021-01-12 12:39:37 +00:00
|
|
|
{whoAmI.canCreateRealm() && (
|
2020-10-02 06:47:32 +00:00
|
|
|
<>
|
|
|
|
<Divider key="divider" />
|
|
|
|
<DropdownItem key="add">
|
|
|
|
<AddRealm />
|
|
|
|
</DropdownItem>
|
|
|
|
</>
|
|
|
|
)}
|
2022-08-03 12:12:07 +00:00
|
|
|
</Fragment>
|
2020-10-02 06:47:32 +00:00
|
|
|
);
|
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
return (
|
2020-09-17 11:37:30 +00:00
|
|
|
<>
|
2021-05-03 16:44:47 +00:00
|
|
|
{realms.length > 5 && (
|
2020-09-17 11:37:30 +00:00
|
|
|
<ContextSelector
|
2021-05-03 16:44:47 +00:00
|
|
|
data-testid="realmSelector"
|
2020-09-17 11:37:30 +00:00
|
|
|
toggleText={toUpperCase(realm)}
|
|
|
|
isOpen={open}
|
|
|
|
screenReaderLabel={toUpperCase(realm)}
|
2020-08-04 12:59:41 +00:00
|
|
|
onToggle={() => setOpen(!open)}
|
2020-09-17 11:37:30 +00:00
|
|
|
onSelect={(_, r) => {
|
2021-04-08 19:20:35 +00:00
|
|
|
let element: ReactElement;
|
|
|
|
if (Array.isArray(r)) {
|
|
|
|
element = (r as ReactElement[])[0];
|
|
|
|
} else {
|
|
|
|
element = r as ReactElement;
|
|
|
|
}
|
2021-05-03 16:44:47 +00:00
|
|
|
const value = element.props.value;
|
|
|
|
if (value) {
|
|
|
|
selectRealm(value);
|
|
|
|
}
|
2020-09-17 11:37:30 +00:00
|
|
|
}}
|
|
|
|
searchInputValue={search}
|
|
|
|
onSearchInputChange={(value) => setSearch(value)}
|
|
|
|
className="keycloak__realm_selector__context_selector"
|
2021-06-08 13:13:04 +00:00
|
|
|
footer={
|
2022-01-20 10:20:35 +00:00
|
|
|
whoAmI.canCreateRealm() && (
|
|
|
|
<ContextSelectorItem key="add">
|
|
|
|
<AddRealm />
|
|
|
|
</ContextSelectorItem>
|
|
|
|
)
|
2021-06-08 13:13:04 +00:00
|
|
|
}
|
2020-08-04 12:59:41 +00:00
|
|
|
>
|
2021-05-19 12:35:11 +00:00
|
|
|
{(filteredItems || all).map((item) => (
|
|
|
|
<ContextSelectorItem key={item.name}>
|
|
|
|
<RealmText value={item.name} />{" "}
|
|
|
|
{item.used && <Label>{t("recent")}</Label>}
|
2020-09-17 11:37:30 +00:00
|
|
|
</ContextSelectorItem>
|
|
|
|
))}
|
|
|
|
</ContextSelector>
|
|
|
|
)}
|
2021-05-03 16:44:47 +00:00
|
|
|
{realms.length <= 5 && (
|
2020-09-17 11:37:30 +00:00
|
|
|
<Dropdown
|
|
|
|
id="realm-select"
|
2021-05-03 16:44:47 +00:00
|
|
|
data-testid="realmSelector"
|
2020-09-17 11:37:30 +00:00
|
|
|
className="keycloak__realm_selector__dropdown"
|
|
|
|
isOpen={open}
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
2021-05-03 16:44:47 +00:00
|
|
|
data-testid="realmSelectorToggle"
|
2020-09-17 11:37:30 +00:00
|
|
|
onToggle={() => setOpen(!open)}
|
|
|
|
className="keycloak__realm_selector_dropdown__toggle"
|
|
|
|
>
|
|
|
|
{toUpperCase(realm)}
|
|
|
|
</DropdownToggle>
|
|
|
|
}
|
2020-10-02 06:47:32 +00:00
|
|
|
dropdownItems={[...dropdownItems, addRealmComponent]}
|
2020-09-17 11:37:30 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2020-08-04 12:59:41 +00:00
|
|
|
);
|
|
|
|
};
|