/* eslint-disable */ // @ts-nocheck import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation"; import { KeycloakSelect } from "../../../shared/keycloak-ui-shared"; import { Button, ButtonVariant, Dropdown, DropdownItem, DropdownList, MenuToggle, Modal, ModalVariant, SelectOption, } from "../../../shared/@patternfly/react-core"; import { CaretDownIcon, CaretUpIcon, FilterIcon, } from "../../../shared/@patternfly/react-icons"; import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { ClientScopeType, clientScopeTypesDropdown, ParameterizedScopeLabel, isParameterizedScope, } from "../../components/client-scope/ClientScopeTypes"; import { ListEmptyState } from "../../../shared/keycloak-ui-shared"; import { KeycloakDataTable } from "../../../shared/keycloak-ui-shared"; import useToggle from "../../utils/useToggle"; import { getProtocolName } from "../utils"; import useIsFeatureEnabled, { Feature } from "../../utils/useIsFeatureEnabled"; import "./client-scopes.css"; export type AddScopeDialogProps = { clientScopes: ClientScopeRepresentation[]; clientName?: string; open: boolean; toggleDialog: () => void; onAdd: ( scopes: { scope: ClientScopeRepresentation; type?: ClientScopeType }[], ) => void; isClientScopesConditionType?: boolean; }; enum FilterType { Name = "Name", Protocol = "Protocol", } enum ProtocolType { All = "All", SAML = "SAML", OpenIDConnect = "OpenID Connect", OID4VC = "OpenID4VC", } export const AddScopeDialog = ({ clientScopes: scopes, clientName, open, toggleDialog, onAdd, isClientScopesConditionType, }: AddScopeDialogProps) => { const { t } = useTranslation(); const isFeatureEnabled = useIsFeatureEnabled(); const [addToggle, setAddToggle] = useState(false); const [rows, setRows] = useState([]); const [filterType, setFilterType] = useState(FilterType.Name); const [protocolType, setProtocolType] = useState(ProtocolType.All); const isOid4vcEnabled = isFeatureEnabled(Feature.OpenId4VCI); const [isFilterTypeDropdownOpen, toggleIsFilterTypeDropdownOpen] = useToggle(); const [isProtocolTypeDropdownOpen, toggleIsProtocolTypeDropdownOpen] = useToggle(false); const clientScopes = useMemo(() => { if (protocolType === ProtocolType.OpenIDConnect) { return scopes.filter((item) => item.protocol === "openid-connect"); } else if (protocolType === ProtocolType.SAML) { return scopes.filter((item) => item.protocol === "saml"); } else if (protocolType === ProtocolType.OID4VC) { return scopes.filter((item) => item.protocol === "oid4vc"); } return scopes; }, [scopes, filterType, protocolType]); const action = (scope: ClientScopeType) => { const scopes = rows.map((row) => { return { scope: row, type: scope }; }); onAdd(scopes); setAddToggle(false); toggleDialog(); }; const onFilterTypeDropdownSelect = (filterType: string) => { if (filterType === FilterType.Name) { setFilterType(FilterType.Protocol); } else if (filterType === FilterType.Protocol) { setFilterType(FilterType.Name); setProtocolType(ProtocolType.All); } toggleIsFilterTypeDropdownOpen(); }; const onProtocolTypeDropdownSelect = (protocolType: string) => { if (protocolType === ProtocolType.SAML) { setProtocolType(ProtocolType.SAML); } else if (protocolType === ProtocolType.OpenIDConnect) { setProtocolType(ProtocolType.OpenIDConnect); } else if (protocolType === ProtocolType.OID4VC) { setProtocolType(ProtocolType.OID4VC); } else if (protocolType === ProtocolType.All) { setProtocolType(ProtocolType.All); } toggleIsProtocolTypeDropdownOpen(); }; const protocolTypeOptions = useMemo(() => { const options = [ {t("protocolTypes.saml")} , {t("protocolTypes.openid-connect")} , ]; if (isOid4vcEnabled) { options.push( {t("protocolTypes.oid4vc")} , ); } options.push( {t("protocolTypes.all")} , ); return options; }, [t, isOid4vcEnabled]); return ( { const scopes = rows.map((scope) => ({ scope })); onAdd(scopes); toggleDialog(); }} isDisabled={rows.length === 0} > {t("add")} , , ] : [ setAddToggle(isOpen)} className="keycloak__client-scopes-add__add-dropdown" key="add-dropdown" isOpen={addToggle} toggle={(ref) => ( setAddToggle(!addToggle)} variant="primary" id="add-dropdown" data-testid="add-dropdown" statusIcon={} > {t("add")} )} > {clientScopeTypesDropdown(t, action, rows)} , , ] } > { onFilterTypeDropdownSelect(filterType); }} onOpenChange={toggleIsFilterTypeDropdownOpen} toggle={(ref) => ( } statusIcon={} > {filterType} )} isOpen={isFilterTypeDropdownOpen} > {filterType === FilterType.Name ? t("protocol") : t("name")} } toolbarItem={ filterType === FilterType.Protocol && ( <> { onFilterTypeDropdownSelect(filterType); }} onOpenChange={toggleIsFilterTypeDropdownOpen} data-testid="filter-type-dropdown" toggle={(ref) => ( } icon={} > {filterType} )} isOpen={isFilterTypeDropdownOpen} > {t("name")} onProtocolTypeDropdownSelect(value.toString()) } selections={protocolType} isOpen={isProtocolTypeDropdownOpen} > {protocolTypeOptions} ) } canSelectAll onSelect={(rows) => setRows(rows)} columns={[ { name: "name", cellRenderer: (row) => ( <> {row.name}{" "} {isParameterizedScope(row) && } ), }, { name: "protocol", displayKey: "protocol", cellRenderer: (client) => getProtocolName(t, client.protocol ?? "openid-connect"), }, { name: "description", }, ]} emptyState={ } /> ); };