From f388833c6aea3a8735295fb5742d2616e29ca61b Mon Sep 17 00:00:00 2001 From: Jean-Marc Millet Date: Mon, 5 Aug 2024 11:25:11 +0200 Subject: [PATCH] Move components declaration out of main components to avoid the rerendering of list (causing to lose focus when selecting an account) --- .../AccountRoleSelectButtonAndModal.tsx | 270 ++++++++++-------- 1 file changed, 146 insertions(+), 124 deletions(-) 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 ( - -

- -