diff --git a/src/react/account/AccountRoleSelectButtonAndModal.tsx b/src/react/account/AccountRoleSelectButtonAndModal.tsx
index b990828e7..64d38c844 100644
--- a/src/react/account/AccountRoleSelectButtonAndModal.tsx
+++ b/src/react/account/AccountRoleSelectButtonAndModal.tsx
@@ -1,7 +1,7 @@
-import { useState } from 'react';
+import { useMemo, useState } from 'react';
import { generatePath, useHistory, useRouteMatch } from 'react-router-dom';
import { Box, Button, Table } from '@scality/core-ui/dist/next';
-import { Stack, Tooltip, Wrap, spacing, Icon } from '@scality/core-ui';
+import { Stack, Tooltip, Wrap, Icon } from '@scality/core-ui';
import { CustomModal as Modal, ModalBody } from '../ui-elements/Modal';
import { regexArn, SCALITY_INTERNAL_ROLES, useAccounts } from '../utils/hooks';
import {
@@ -11,6 +11,78 @@ import {
} from '../DataServiceRoleProvider';
import { AccountSelectorButton } from '../ui-elements/Table';
+function AccountRoleList({ accountsWithRoles, onRowSelected }) {
+ const { roleArn } = useDataServiceRole();
+ const [assumedRoleArn, setAssumedRoleArn] = useState(roleArn);
+ const columns = [
+ {
+ Header: 'Account Name',
+ accessor: 'accountName',
+ cellStyle: {
+ minWidth: '10rem',
+ paddingLeft: '1rem',
+ },
+ },
+ {
+ Header: 'Role Name',
+ accessor: 'roleName',
+ cellStyle: {
+ minWidth: '12rem',
+ marginRight: '10rem',
+ },
+ Cell({ value: roleName }: { value: string }) {
+ if (SCALITY_INTERNAL_ROLES.includes(roleName)) {
+ return (
+
+ {roleName}
+
+
+
+
+ );
+ } else {
+ return <>{roleName}>;
+ }
+ },
+ },
+ {
+ Header: 'Role Path',
+ accessor: 'rolePath',
+ cellStyle: {
+ minWidth: '10rem',
+ },
+ },
+ ];
+
+ return (
+
+
row.roleArn}
+ >
+ {
+ //@ts-expect-error fix this when you are working on it
+ setAssumedRoleArn(rowData.original.roleArn);
+ onRowSelected(rowData);
+ }}
+ selectedId={assumedRoleArn}
+ >
+
+
+ );
+}
+
export function AccountRoleSelectButtonAndModal({
bigButton,
buttonLabel,
@@ -19,7 +91,6 @@ export function AccountRoleSelectButtonAndModal({
buttonLabel?: string;
}) {
const { accounts } = useAccounts();
- const { path } = useRouteMatch();
const { account } = useCurrentAccount();
const { roleArn } = useDataServiceRole();
const [assumedRoleArn, setAssumedRoleArn] = useState(roleArn);
@@ -27,135 +98,38 @@ export function AccountRoleSelectButtonAndModal({
const accountName = account?.Name;
const [assumedAccount, setAssumedAccount] = useState(accountName);
const setRole = useSetAssumedRole();
- const history = useHistory();
+
+ const accountRolesHash = accounts
+ ?.map((acc) => acc.Name + acc.Roles.map((role) => role.Arn).join(''))
+ ?.join('');
const accountsWithRoles: {
accountName: string;
roleName: string;
rolePath: string;
roleArn: string;
- }[] = [];
-
- accounts.forEach((account) => {
- const accountName = account.Name;
- account.Roles.forEach((role) => {
- const parsedArn = regexArn.exec(role.Arn);
- const rolePath = parsedArn?.groups['path'] || '';
- const roleName = parsedArn?.groups['name'] || '';
- accountsWithRoles.push({
- accountName,
- roleName,
- rolePath,
- roleArn: role.Arn,
- });
- });
- });
+ }[] = useMemo(() => {
+ return (
+ accounts?.flatMap((account) => {
+ const accountName = account.Name;
+ return account.Roles.map((role) => {
+ const parsedArn = regexArn.exec(role.Arn);
+ const rolePath = parsedArn?.groups['path'] || '';
+ const roleName = parsedArn?.groups['name'] || '';
+ return {
+ accountName,
+ roleName,
+ rolePath,
+ roleArn: role.Arn,
+ };
+ });
+ }) || []
+ );
+ }, [accountRolesHash]);
const handleClose = () => {
setIsModalOpen(false);
};
- const modalFooter = () => {
- return (
-
-
-
-
- }
- variant="primary"
- onClick={() => {
- setRole({ roleArn: assumedRoleArn });
- history.push(
- generatePath(path, {
- accountName: assumedAccount,
- }),
- );
- handleClose();
- }}
- label="Continue"
- disabled={assumedRoleArn === roleArn}
- />
-
-
- );
- };
-
- function AccountRoleList() {
- const columns = [
- {
- Header: 'Account Name',
- accessor: 'accountName',
- cellStyle: {
- minWidth: '10rem',
- paddingLeft: '1rem',
- },
- },
- {
- Header: 'Role Name',
- accessor: 'roleName',
- cellStyle: {
- minWidth: '12rem',
- marginRight: '10rem',
- },
- Cell({ value: roleName }: { value: string }) {
- if (SCALITY_INTERNAL_ROLES.includes(roleName)) {
- return (
-
- {roleName}
-
-
-
-
- );
- } else {
- return <>{roleName}>;
- }
- },
- },
- {
- Header: 'Role Path',
- accessor: 'rolePath',
- cellStyle: {
- minWidth: '10rem',
- },
- },
- ];
-
- return (
-
-
row.roleArn}
- >
- {
- //@ts-expect-error fix this when you are working on it
- setAssumedRoleArn(rowData.original.roleArn);
- //@ts-expect-error fix this when you are working on it
- setAssumedAccount(rowData.original.accountName);
- }}
- selectedId={assumedRoleArn}
- >
-
-
- );
- }
return (
<>
@@ -181,16 +155,64 @@ export function AccountRoleSelectButtonAndModal({
/>
+ }
isOpen={isModalOpen}
title="Select Account and Role to assume"
>
-
+ {
+ setAssumedRoleArn(rowData.original.roleArn);
+ setAssumedAccount(rowData.original.accountName);
+ }}
+ />
>
);
}
+const ModalFooter = ({
+ handleClose,
+ setRole,
+ assumedRoleArn,
+ roleArn,
+ assumedAccount,
+}) => {
+ const history = useHistory();
+ const { path } = useRouteMatch();
+ return (
+
+
+
+
+ }
+ variant="primary"
+ onClick={() => {
+ setRole({ roleArn: assumedRoleArn });
+ history.push(
+ generatePath(path, {
+ accountName: assumedAccount,
+ }),
+ );
+ handleClose();
+ }}
+ label="Continue"
+ disabled={assumedRoleArn === roleArn}
+ />
+
+
+ );
+};
+
export default AccountRoleSelectButtonAndModal;