import { Trash2, Link as LinkIcon } from 'lucide-react'; import { useRouter } from '@uirouter/react'; import { Row } from '@tanstack/react-table'; import clsx from 'clsx'; import { useMemo } from 'react'; import { useEnvironmentId } from '@/react/hooks/useEnvironmentId'; import { useAuthorizations, Authorized } from '@/react/hooks/useUser'; import { notifyError, notifySuccess } from '@/portainer/services/notifications'; import { SystemResourceDescription } from '@/react/kubernetes/datatables/SystemResourceDescription'; import { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store'; import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton'; import { confirmDelete } from '@@/modals/confirm'; import { Datatable, Table, TableSettingsMenu } from '@@/datatables'; import { LoadingButton } from '@@/buttons'; import { useTableState } from '@@/datatables/useTableState'; import { DefaultDatatableSettings } from '../../../datatables/DefaultDatatableSettings'; import { ClusterRoleBinding } from './types'; import { columns } from './columns'; import { useGetClusterRoleBindingsQuery } from './queries/useGetClusterRoleBindingsQuery'; import { useDeleteClusterRoleBindingsMutation } from './queries/useDeleteClusterRoleBindingsMutation'; const storageKey = 'clusterRoleBindings'; const settingsStore = createStore(storageKey); export function ClusterRoleBindingsDatatable() { const environmentId = useEnvironmentId(); const tableState = useTableState(settingsStore, storageKey); const clusterRoleBindingsQuery = useGetClusterRoleBindingsQuery( environmentId, { autoRefreshRate: tableState.autoRefreshRate * 1000, } ); const filteredClusterRoleBindings = useMemo( () => clusterRoleBindingsQuery.data?.filter( (crb) => tableState.showSystemResources || !crb.isSystem ), [clusterRoleBindingsQuery.data, tableState.showSystemResources] ); const { authorized: isAuthorizedToAddOrEdit } = useAuthorizations([ 'K8sClusterRoleBindingsW', ]); return ( row.uid} isRowSelectable={(row) => !row.original.isSystem} renderTableActions={(selectedRows) => ( )} renderTableSettings={() => ( )} description={ } disableSelect={!isAuthorizedToAddOrEdit} renderRow={renderRow} data-cy="k8s-cluster-role-bindings-datatable" /> ); } // needed to apply custom styling to the row and not globally required in the AC's for this ticket. function renderRow(row: Row, highlightedItemId?: string) { return ( cells={row.getVisibleCells()} className={clsx('[&>td]:!py-4 [&>td]:!align-top', { active: highlightedItemId === row.id, })} /> ); } interface SelectedRole { name: string; } type TableActionsProps = { selectedItems: ClusterRoleBinding[]; }; function TableActions({ selectedItems }: TableActionsProps) { const environmentId = useEnvironmentId(); const deleteClusterRoleBindingsMutation = useDeleteClusterRoleBindingsMutation(environmentId); const router = useRouter(); async function handleRemoveClick(roles: SelectedRole[]) { const confirmed = await confirmDelete( <>

Are you sure you want to delete the selected cluster role binding(s)?

    {roles.map((s, index) => (
  • {s.name}
  • ))}
); if (!confirmed) { return null; } const payload: string[] = []; roles.forEach((r) => { payload.push(r.name); }); deleteClusterRoleBindingsMutation.mutate( { environmentId, data: payload }, { onSuccess: () => { notifySuccess( 'Roles successfully removed', roles.map((r) => `${r.name}`).join(', ') ); router.stateService.reload(); }, onError: (error) => { notifyError( 'Unable to delete cluster role bindings', error as Error, roles.map((r) => `${r.name}`).join(', ') ); }, } ); return roles; } return ( handleRemoveClick(selectedItems)} icon={Trash2} isLoading={deleteClusterRoleBindingsMutation.isLoading} loadingText="Removing cluster role bindings..." data-cy="k8s-cluster-role-bindings-remove-button" > Remove ); }