import { Button, ContextSelector, ContextSelectorItem, Divider, Dropdown, DropdownItem, DropdownToggle, Label, Spinner, Split, SplitItem, } from "@patternfly/react-core"; import { CheckIcon } from "@patternfly/react-icons"; import { Fragment, ReactElement, useState } from "react"; import { useTranslation } from "react-i18next"; import { Link, useLocation, useNavigate } from "react-router-dom"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useRealms } from "../../context/RealmsContext"; import { useWhoAmI } from "../../context/whoami/WhoAmI"; import { toDashboard } from "../../dashboard/routes/Dashboard"; import { toAddRealm } from "../../realm/routes/AddRealm"; import { useUpdateEffect } from "../../utils/useUpdateEffect"; import { RecentUsed } from "./recent-used"; import "./realm-selector.css"; const AddRealm = () => { const { realm } = useRealm(); const { t } = useTranslation("common"); return ( ); }; type RealmTextProps = { value: string; }; const RealmText = ({ value }: RealmTextProps) => { const { realm } = useRealm(); return ( {value} {value === realm && } ); }; export const RealmSelector = () => { const { realm } = useRealm(); const { realms, refresh } = useRealms(); const { whoAmI } = useWhoAmI(); const [open, setOpen] = useState(false); const [search, setSearch] = useState(""); const navigate = useNavigate(); const location = useLocation(); const { t } = useTranslation("common"); const recentUsed = new RecentUsed(); const all = recentUsed.used .filter((r) => r !== realm) .map((name) => { return { name, used: true }; }) .concat( realms .filter((r) => !recentUsed.used.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())); useUpdateEffect(() => setOpen(false), [location]); const dropdownItems = realms.length !== 0 ? realms.map((r) => ( } /> )) : [ Loading , ]; return realms.length > 5 ? ( 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() && ( ) } > {(filteredItems || all).map((item) => ( {" "} {item.used && } ))} ) : ( { if (realms.length === 0) refresh(); setOpen(!open); }} className="keycloak__realm_selector_dropdown__toggle" > {realm} } dropdownItems={[ ...dropdownItems, {whoAmI.canCreateRealm() && ( <> )} , ]} /> ); };