2020-09-23 08:19:46 +00:00
|
|
|
import React, { useState, useContext, useEffect } from "react";
|
2021-01-12 12:39:37 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
2020-10-29 18:56:52 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-09-04 18:16:11 +00:00
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
import {
|
|
|
|
Dropdown,
|
|
|
|
DropdownToggle,
|
|
|
|
DropdownItem,
|
|
|
|
Button,
|
2020-09-04 18:16:11 +00:00
|
|
|
Divider,
|
2020-09-17 11:37:30 +00:00
|
|
|
SplitItem,
|
|
|
|
Split,
|
|
|
|
ContextSelector,
|
|
|
|
ContextSelectorItem,
|
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";
|
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
import RealmRepresentation from "keycloak-admin/lib/defs/realmRepresentation";
|
2021-01-05 19:49:33 +00:00
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2020-10-06 08:25:38 +00:00
|
|
|
import { WhoAmIContext } from "../../context/whoami/WhoAmI";
|
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
|
|
|
|
2020-09-08 17:20:29 +00:00
|
|
|
type RealmSelectorProps = {
|
2020-09-15 19:44:28 +00:00
|
|
|
realmList: RealmRepresentation[];
|
2020-09-08 17:20:29 +00:00
|
|
|
};
|
|
|
|
|
2020-09-17 11:37:30 +00:00
|
|
|
export const RealmSelector = ({ realmList }: RealmSelectorProps) => {
|
2021-01-05 19:49:33 +00:00
|
|
|
const { realm, setRealm } = useRealm();
|
2021-01-12 12:39:37 +00:00
|
|
|
const { whoAmI } = useContext(WhoAmIContext);
|
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("");
|
|
|
|
const [filteredItems, setFilteredItems] = useState(realmList);
|
2020-08-27 12:09:36 +00:00
|
|
|
const history = useHistory();
|
2020-10-29 18:56:52 +00:00
|
|
|
const { t } = useTranslation("common");
|
2020-09-17 11:37:30 +00:00
|
|
|
|
|
|
|
const toUpperCase = (realmName: string) =>
|
|
|
|
realmName.charAt(0).toUpperCase() + realmName.slice(1);
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
|
2020-09-23 08:19:46 +00:00
|
|
|
const AddRealm = ({ className }: { className?: string }) => (
|
|
|
|
<Button
|
|
|
|
component="div"
|
|
|
|
isBlock
|
2021-01-12 12:39:37 +00:00
|
|
|
onClick={() => {
|
|
|
|
history.push(`/${realm}/add-realm`);
|
|
|
|
setOpen(!open);
|
|
|
|
}}
|
2020-09-23 08:19:46 +00:00
|
|
|
className={className}
|
|
|
|
>
|
2020-10-29 18:56:52 +00:00
|
|
|
{t("createRealm")}
|
2020-09-17 11:37:30 +00:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
|
|
|
const onFilter = () => {
|
|
|
|
const filtered =
|
|
|
|
search === ""
|
|
|
|
? realmList
|
|
|
|
: realmList.filter(
|
2020-11-12 12:55:52 +00:00
|
|
|
(r) => r.realm!.toLowerCase().indexOf(search.toLowerCase()) !== -1
|
2020-09-17 11:37:30 +00:00
|
|
|
);
|
|
|
|
setFilteredItems(filtered || []);
|
|
|
|
};
|
2020-09-04 18:16:11 +00:00
|
|
|
|
2020-09-23 08:19:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
onFilter();
|
|
|
|
}, [search]);
|
|
|
|
|
2020-09-08 17:20:29 +00:00
|
|
|
const dropdownItems = realmList.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={() => {
|
2020-11-12 12:55:52 +00:00
|
|
|
setRealm(r.realm!);
|
2021-01-05 19:49:33 +00:00
|
|
|
history.push(`/${r.realm}`);
|
2020-09-08 17:20:29 +00:00
|
|
|
setOpen(!open);
|
|
|
|
}}
|
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 = (
|
|
|
|
<React.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>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
2020-08-04 12:59:41 +00:00
|
|
|
return (
|
2020-09-17 11:37:30 +00:00
|
|
|
<>
|
|
|
|
{realmList.length > 5 && (
|
|
|
|
<ContextSelector
|
|
|
|
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) => {
|
|
|
|
const value = ((r as unknown) as any).props.value;
|
|
|
|
setRealm(value || "master");
|
|
|
|
setOpen(!open);
|
|
|
|
}}
|
|
|
|
searchInputValue={search}
|
|
|
|
onSearchInputChange={(value) => setSearch(value)}
|
|
|
|
onSearchButtonClick={() => onFilter()}
|
|
|
|
className="keycloak__realm_selector__context_selector"
|
2020-08-04 12:59:41 +00:00
|
|
|
>
|
2020-09-17 11:37:30 +00:00
|
|
|
{filteredItems.map((item) => (
|
|
|
|
<ContextSelectorItem key={item.id}>
|
2020-11-12 12:55:52 +00:00
|
|
|
<RealmText value={item.realm!} />
|
2020-09-17 11:37:30 +00:00
|
|
|
</ContextSelectorItem>
|
|
|
|
))}
|
|
|
|
<ContextSelectorItem key="add">
|
|
|
|
<AddRealm />
|
|
|
|
</ContextSelectorItem>
|
|
|
|
</ContextSelector>
|
|
|
|
)}
|
|
|
|
{realmList.length <= 5 && (
|
|
|
|
<Dropdown
|
|
|
|
id="realm-select"
|
|
|
|
className="keycloak__realm_selector__dropdown"
|
|
|
|
isOpen={open}
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
|
|
|
id="realm-select-toggle"
|
|
|
|
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
|
|
|
);
|
|
|
|
};
|