/* eslint-disable */ // @ts-nocheck import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation"; import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation"; import { ListEmptyState, PaginatingTableToolbar, useAlerts, useFetch, } from "../../../shared/keycloak-ui-shared"; import { Alert, AlertVariant, Button, PageSection, ToolbarItem, } from "../../../shared/@patternfly/react-core"; import { ExpandableRowContent, Table, TableText, Tbody, Td, Th, Thead, Tr, } from "../../../shared/@patternfly/react-table"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { Link, useNavigate } from "react-router-dom"; import { useAdminClient } from "../../admin-client"; import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog"; import { KeycloakSpinner } from "../../../shared/keycloak-ui-shared"; import { useRealm } from "../../context/realm-context/RealmContext"; import { toNewPermission } from "../routes/NewPermission"; import { toCreateResource } from "../routes/NewResource"; import { toResourceDetails } from "../routes/Resource"; import { DetailCell } from "./DetailCell"; import { MoreLabel } from "./MoreLabel"; import { SearchDropdown, SearchForm } from "./SearchDropdown"; type ResourcesProps = { clientId: string; isDisabled?: boolean; }; type ExpandableResourceRepresentation = ResourceRepresentation & { isExpanded: boolean; }; const UriRenderer = ({ row }: { row: ResourceRepresentation }) => ( {row.uris?.[0]} ); export const AuthorizationResources = ({ clientId, isDisabled = false, }: ResourcesProps) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const navigate = useNavigate(); const { addAlert, addError } = useAlerts(); const { realm } = useRealm(); const [resources, setResources] = useState(); const [selectedResource, setSelectedResource] = useState(); const [permissions, setPermission] = useState(); const [key, setKey] = useState(0); const refresh = () => setKey(key + 1); const [max, setMax] = useState(10); const [first, setFirst] = useState(0); const [search, setSearch] = useState({}); useFetch( () => { const params = { first, max: max + 1, deep: false, ...search, }; return adminClient.clients.listResources({ ...params, id: clientId, }); }, (resources) => setResources( resources.map((resource) => ({ ...resource, isExpanded: false })), ), [key, search, first, max], ); const fetchPermissions = async (id: string) => { return adminClient.clients.listPermissionsByResource({ id: clientId, resourceId: id, }); }; const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: "deleteResource", children: ( <> {t("deleteResourceConfirm")} {permissions?.length && (

{permissions.map((permission) => ( {permission.name} ))}

)} ), continueButtonLabel: "confirm", onConfirm: async () => { try { await adminClient.clients.delResource({ id: clientId, resourceId: selectedResource?._id!, }); addAlert(t("resourceDeletedSuccess"), AlertVariant.success); if (resources?.length === 1 && first > 0) { // Go back one page. Changing 'first' will automatically re-trigger // the useFetch hook, so we don't need to call refresh() here. setFirst(first - max); } else { refresh(); } } catch (error) { addError("resourceDeletedError", error); } }, }); if (!resources) { return ; } const noData = resources.length === 0; const searching = Object.keys(search).length !== 0; return ( {(!noData || searching) && ( { setFirst(first); setMax(max); }} toolbarItem={ <> } > {!noData && ( {!isDisabled && ( <> {resources.slice(0, max).map((resource, rowIndex) => ( {!isDisabled && ( <> ))}
{t("name")} {t("displayName")} {t("type")} {t("owner")} {t("uris")}
{ const rows = resources.map((resource, index) => index === rowIndex ? { ...resource, isExpanded: !resource.isExpanded, } : resource, ); setResources(rows); }, }} /> {resource.name} {resource.displayName} {resource.type} {resource.owner?.name} { setSelectedResource(resource); setPermission( await fetchPermissions(resource._id!), ); toggleDeleteDialog(); }, }, ], }} /> )}
{resource.isExpanded && ( )}
)}
)} {noData && searching && ( )} {noData && !searching && ( navigate(toCreateResource({ realm, id: clientId })) } /> )}
); };