/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./details.scss"; import { reaction } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import React from "react"; import type { ClusterRoleBinding, ClusterRoleBindingSubject } from "../../../../common/k8s-api/endpoints"; import { autoBind, ObservableHashSet, prevDefault } from "../../../utils"; import { AddRemoveButtons } from "../../add-remove-buttons"; import { DrawerTitle } from "../../drawer"; import type { KubeObjectDetailsProps } from "../../kube-object-details"; import { KubeObjectMeta } from "../../kube-object-meta"; import { Table, TableCell, TableHead, TableRow } from "../../table"; import { ClusterRoleBindingDialog } from "./dialog"; import { clusterRoleBindingsStore } from "./store"; import { hashClusterRoleBindingSubject } from "./hashers"; import type { OpenConfirmDialog } from "../../confirm-dialog/open.injectable"; import { withInjectables } from "@ogre-tools/injectable-react"; import openConfirmDialogInjectable from "../../confirm-dialog/open.injectable"; export interface ClusterRoleBindingDetailsProps extends KubeObjectDetailsProps { } interface Dependencies { openConfirmDialog: OpenConfirmDialog; } @observer class NonInjectedClusterRoleBindingDetails extends React.Component { selectedSubjects = new ObservableHashSet([], hashClusterRoleBindingSubject); constructor(props: ClusterRoleBindingDetailsProps & Dependencies) { super(props); autoBind(this); } async componentDidMount() { disposeOnUnmount(this, [ reaction(() => this.props.object, () => { this.selectedSubjects.clear(); }), ]); } removeSelectedSubjects() { const { object: clusterRoleBinding, openConfirmDialog } = this.props; const { selectedSubjects } = this; openConfirmDialog({ ok: () => clusterRoleBindingsStore.removeSubjects(clusterRoleBinding, selectedSubjects), labelOk: `Remove`, message: (

Remove selected bindings for {clusterRoleBinding.getName()}?

), }); } render() { const { selectedSubjects } = this; const { object: clusterRoleBinding } = this.props; if (!clusterRoleBinding) { return null; } const { roleRef } = clusterRoleBinding; const subjects = clusterRoleBinding.getSubjects(); return (
Reference Kind Name API Group {roleRef.kind} {roleRef.name} {roleRef.apiGroup}
Bindings {subjects.length > 0 && ( Type Name Namespace { subjects.map((subject, i) => { const { kind, name, namespace } = subject; const isSelected = selectedSubjects.has(subject); return ( this.selectedSubjects.toggle(subject))} > {kind} {name} {namespace || "-"} ); }) }
)} ClusterRoleBindingDialog.open(clusterRoleBinding)} onRemove={selectedSubjects.size ? this.removeSelectedSubjects : null} addTooltip={`Add bindings to ${roleRef.name}`} removeTooltip={`Remove selected bindings from ${roleRef.name}`} />
); } } export const ClusterRoleBindingDetails = withInjectables(NonInjectedClusterRoleBindingDetails, { getProps: (di, props) => ({ ...props, openConfirmDialog: di.inject(openConfirmDialogInjectable), }), });