/* eslint-disable */ // @ts-nocheck import RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation"; import { KeycloakDataTable, ListEmptyState, } from "../../../shared/keycloak-ui-shared"; import { Button, Dropdown, DropdownItem, DropdownList, DropdownProps, MenuToggle, Modal, ModalVariant, } from "../../../shared/@patternfly/react-core"; import { cellWidth, TableText } from "../../../shared/@patternfly/react-table"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { useAccess } from "../../context/access/Access"; import { translationFormatter } from "../../utils/translationFormatter"; import useLocaleSort from "../../utils/useLocaleSort"; import useToggle from "../../utils/useToggle"; import { ResourcesKey, Row } from "./RoleMapping"; import { getAvailableRoles } from "./queries"; import { getAvailableClientRoles } from "./resource"; type AddRoleMappingModalProps = { id: string; type: ResourcesKey; filterType: FilterType; name?: string; isRadio?: boolean; onAssign: (rows: Row[]) => void; onClose: () => void; title?: string; actionLabel?: string; groupsResource?: any; }; export type FilterType = "roles" | "clients"; const RoleDescription = ({ role }: { role: RoleRepresentation }) => { const { t } = useTranslation(); return ( {translationFormatter(t)(role.description) as string} ); }; type AddRoleButtonProps = Omit< DropdownProps, "children" | "toggle" | "isOpen" | "onOpenChange" > & { label?: string; variant?: "default" | "plain" | "primary" | "plainText" | "secondary"; isDisabled?: boolean; onFilerTypeChange: (type: FilterType) => void; }; export const AddRoleButton = ({ label, variant, isDisabled, onFilerTypeChange, ...rest }: AddRoleButtonProps) => { const { t } = useTranslation(); const [open, toggle] = useToggle(); const { hasAccess } = useAccess(); const canViewRealmRoles = hasAccess("view-realm") || hasAccess("query-users"); return ( ( {t(label || "assignRole")} )} isOpen={open} {...rest} > { onFilerTypeChange("clients"); }} > {t("clientRoles")} {canViewRealmRoles && ( { onFilerTypeChange("roles"); }} > {t("realmRoles")} )} ); }; export const AddRoleMappingModal = ({ id, name, type, isRadio, filterType, onAssign, onClose, title, actionLabel, groupsResource, }: AddRoleMappingModalProps) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const [selectedRows, setSelectedRows] = useState([]); const localeSort = useLocaleSort(); const compareRow = ({ role: { name } }: Row) => name?.toUpperCase(); const loader = async ( first?: number, max?: number, search?: string, ): Promise => { const params: Record = { first: first!, max: max!, }; if (search) { params.search = search; } const roles = await getAvailableRoles( adminClient, type, { ...params, id }, groupsResource, ); const sorted = localeSort(roles, compareRow); return sorted.map((row) => { return { role: row.role, id: row.role.id, }; }); }; const clientRolesLoader = async ( first?: number, max?: number, search?: string, ): Promise => { const roles = await getAvailableClientRoles(adminClient, { id, type, first: first || 0, max: max || 10, search, }); return localeSort( roles.map((e) => ({ client: { clientId: e.client, id: e.clientId }, role: { id: e.id, name: e.role, description: e.description }, id: e.id, })), ({ client: { clientId }, role: { name } }) => `${clientId}${name}`, ); }; const columns = [ { name: "role.name", displayKey: "name", transforms: [cellWidth(30)], }, { name: "client.clientId", displayKey: "clientId", }, { name: "role.description", displayKey: "description", cellRenderer: RoleDescription, }, ]; if (filterType === "roles") { columns.splice(1, 1); } return ( { onAssign(selectedRows); onClose(); }} > {actionLabel || t("assign")} , , ]} > setSelectedRows([...rows])} searchPlaceholderKey={ filterType === "roles" ? "searchByRoleName" : "search" } isPaginated={!(filterType === "roles" && type !== "roles")} canSelectAll isRadio={isRadio} loader={filterType === "roles" ? loader : clientRolesLoader} ariaLabelKey="associatedRolesText" columns={columns} emptyState={ } /> ); };