keycloak-scim/apps/admin-ui/src/components/realm-selector/RealmSelector.tsx

179 lines
4.7 KiB
TypeScript
Raw Normal View History

import {
Button,
ContextSelector,
ContextSelectorItem,
Divider,
Dropdown,
DropdownItem,
DropdownToggle,
Label,
Spinner,
Split,
SplitItem,
} from "@patternfly/react-core";
import { CheckIcon } from "@patternfly/react-icons";
2023-02-07 14:21:58 +00:00
import { Fragment, ReactElement, useState } from "react";
import { useTranslation } from "react-i18next";
2023-02-07 14:21:58 +00:00
import { Link, useLocation, useNavigate } from "react-router-dom";
import { useRealm } from "../../context/realm-context/RealmContext";
2021-11-11 16:04:04 +00:00
import { useRealms } from "../../context/RealmsContext";
import { useRecentRealms } from "../../context/RecentRealms";
import { useWhoAmI } from "../../context/whoami/WhoAmI";
import { toDashboard } from "../../dashboard/routes/Dashboard";
import { toAddRealm } from "../../realm/routes/AddRealm";
2023-02-07 14:21:58 +00:00
import { useUpdateEffect } from "../../utils/useUpdateEffect";
import "./realm-selector.css";
2023-02-07 14:21:58 +00:00
const AddRealm = () => {
const { realm } = useRealm();
const { t } = useTranslation("common");
return (
<Button
data-testid="add-realm"
component={(props) => <Link {...props} to={toAddRealm({ realm })} />}
isBlock
>
{t("createRealm")}
</Button>
);
};
type RealmTextProps = {
value: string;
};
const RealmText = ({ value }: RealmTextProps) => {
const { realm } = useRealm();
return (
<Split className="keycloak__realm_selector__list-item-split">
<SplitItem isFilled>{value}</SplitItem>
<SplitItem>{value === realm && <CheckIcon />}</SplitItem>
</Split>
);
};
export const RealmSelector = () => {
2021-11-11 16:04:04 +00:00
const { realm } = useRealm();
const { realms, refresh } = useRealms();
const { whoAmI } = useWhoAmI();
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
const navigate = useNavigate();
2023-02-07 14:21:58 +00:00
const location = useLocation();
2020-10-29 18:56:52 +00:00
const { t } = useTranslation("common");
const recentRealms = useRecentRealms();
2023-02-07 14:21:58 +00:00
const all = recentRealms
.filter((r) => r !== realm)
.map((name) => {
return { name, used: true };
})
.concat(
realms
.filter((r) => !recentRealms.includes(r.realm!) || r.realm === realm)
.map((r) => {
return { name: r.realm!, used: false };
})
);
2023-02-07 14:21:58 +00:00
const filteredItems =
search === ""
? undefined
: all.filter((r) => r.name.toLowerCase().includes(search.toLowerCase()));
2023-02-07 14:21:58 +00:00
useUpdateEffect(() => setOpen(false), [location]);
const dropdownItems =
realms.length !== 0
? realms.map((r) => (
<DropdownItem
key={`realm-dropdown-item-${r.realm}`}
2023-02-07 14:21:58 +00:00
component={
<Link to={toDashboard({ realm: r.realm! })}>
<RealmText value={r.realm!} />
</Link>
}
/>
))
: [
<DropdownItem key="load">
Loading <Spinner size="sm" />
</DropdownItem>,
];
2023-02-07 14:21:58 +00:00
return realms.length > 5 ? (
<ContextSelector
data-testid="realmSelector"
toggleText={realm}
isOpen={open}
screenReaderLabel={realm}
onToggle={() => setOpen(!open)}
onSelect={(_, r) => {
let element: ReactElement;
if (Array.isArray(r)) {
element = (r as ReactElement[])[0];
} else {
element = r as ReactElement;
}
const value = element.props.value;
if (value) {
navigate(toDashboard({ realm: value }));
}
}}
searchInputValue={search}
onSearchInputChange={(value) => setSearch(value)}
className="keycloak__realm_selector__context_selector"
footer={
whoAmI.canCreateRealm() && (
<ContextSelectorItem key="add">
<AddRealm />
2023-02-07 14:21:58 +00:00
</ContextSelectorItem>
)
}
>
{(filteredItems || all).map((item) => (
<ContextSelectorItem key={item.name}>
<RealmText value={item.name} />{" "}
{item.used && <Label>{t("recent")}</Label>}
</ContextSelectorItem>
))}
</ContextSelector>
) : (
<Dropdown
id="realm-select"
data-testid="realmSelector"
className="keycloak__realm_selector__dropdown"
isOpen={open}
toggle={
<DropdownToggle
data-testid="realmSelectorToggle"
onToggle={() => {
if (realms.length === 0) refresh();
setOpen(!open);
}}
2023-02-07 14:21:58 +00:00
className="keycloak__realm_selector_dropdown__toggle"
>
2023-02-07 14:21:58 +00:00
{realm}
</DropdownToggle>
}
dropdownItems={[
...dropdownItems,
<Fragment key="add-realm">
{whoAmI.canCreateRealm() && (
<>
<Divider key="divider" />
<DropdownItem key="add">
<AddRealm />
</DropdownItem>
</>
)}
</Fragment>,
]}
/>
);
};