import { Button, ContextSelector, ContextSelectorItem, ContextSelectorItemProps, Divider, Dropdown, DropdownItem, DropdownToggle, Label, Spinner, Split, SplitItem, } from "@patternfly/react-core"; import { CheckIcon } from "@patternfly/react-icons"; import { Fragment, useState } from "react"; import { useTranslation } from "react-i18next"; import { Link, To, useHref } from "react-router-dom"; import { useRealm } from "../../context/realm-context/RealmContext"; 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"; import "./realm-selector.css"; type AddRealmProps = { onClick: () => void; }; const AddRealm = ({ onClick }: AddRealmProps) => { const { realm } = useRealm(); const { t } = useTranslation("common"); return ( ); }; type RealmTextProps = { value: string; }; const RealmText = ({ value }: RealmTextProps) => { const { realm } = useRealm(); return ( {value} {value === realm && } ); }; // We need to make all these props partial because of a bug in PatternFly. // See: https://github.com/patternfly/patternfly-react/pull/8670 // TODO: Remove this partial when a fix has been released. type ContextSelectorItemLinkProps = Partial< Omit > & { to: To; }; const ContextSelectorItemLink = ({ to, ...props }: ContextSelectorItemLinkProps) => { const href = useHref(to); return ; }; export const RealmSelector = () => { const { realm } = useRealm(); const { realms, refresh } = useRealms(); const { whoAmI } = useWhoAmI(); const [open, setOpen] = useState(false); const [search, setSearch] = useState(""); const { t } = useTranslation("common"); const recentRealms = useRecentRealms(); 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 }; }) ); const filteredItems = search === "" ? undefined : all.filter((r) => r.name.toLowerCase().includes(search.toLowerCase())); const dropdownItems = realms.length !== 0 ? realms.map((r) => ( setOpen(false)} > } /> )) : [ Loading , ]; return realms.length > 5 ? ( setOpen(!open)} searchInputValue={search} onSearchInputChange={(value) => setSearch(value)} className="keycloak__realm_selector__context_selector" footer={ whoAmI.canCreateRealm() && ( setOpen(false)} /> ) } > {(filteredItems || all).map((item) => ( setOpen(false)} > {" "} {item.used && } ))} ) : ( { if (realms.length === 0) refresh(); setOpen(!open); }} className="keycloak__realm_selector_dropdown__toggle" > {realm} } dropdownItems={[ ...dropdownItems, {whoAmI.canCreateRealm() && ( <> setOpen(false)} /> )} , ]} /> ); };