Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: updated edit modals #27623

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const render = (
<EditAccountsModal
accounts={accounts}
defaultSelectedAccountAddresses={[accounts[0].address]}
activeTabOrigin={'https://test.dapp'}
{...props}
/>,
store,
Expand Down
281 changes: 137 additions & 144 deletions ui/components/multichain/edit-accounts-modal/edit-accounts-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
IconColor,
FlexDirection,
AlignItems,
BlockSize,
} from '../../../helpers/constants/design-system';
import { getURLHost } from '../../../helpers/utils/util';
import { MergedInternalAccount } from '../../../selectors/selectors.types';
import {
MetaMetricsEventCategory,
Expand All @@ -38,15 +38,13 @@ import { MetaMetricsContext } from '../../../contexts/metametrics';
import { isEqualCaseInsensitive } from '../../../../shared/modules/string-utils';

type EditAccountsModalProps = {
activeTabOrigin: string;
accounts: MergedInternalAccount[];
defaultSelectedAccountAddresses: string[];
onClose: () => void;
onSubmit: (addresses: string[]) => void;
};

export const EditAccountsModal: React.FC<EditAccountsModalProps> = ({
activeTabOrigin,
accounts,
defaultSelectedAccountAddresses,
onClose,
Expand All @@ -56,7 +54,6 @@ export const EditAccountsModal: React.FC<EditAccountsModalProps> = ({
const trackEvent = useContext(MetaMetricsContext);

const [showAddNewAccounts, setShowAddNewAccounts] = useState(false);

const [selectedAccountAddresses, setSelectedAccountAddresses] = useState(
defaultSelectedAccountAddresses,
);
Expand Down Expand Up @@ -84,155 +81,151 @@ export const EditAccountsModal: React.FC<EditAccountsModalProps> = ({
}
};

const allAreSelected = () => {
return accounts.length === selectedAccountAddresses.length;
};

const allAreSelected = () =>
accounts.length === selectedAccountAddresses.length;
const checked = allAreSelected();
const isIndeterminate = !checked && selectedAccountAddresses.length > 0;

const hostName = getURLHost(activeTabOrigin);

const defaultSet = new Set(defaultSelectedAccountAddresses);
const selectedSet = new Set(selectedAccountAddresses);

return (
<>
<Modal
isOpen
onClose={onClose}
data-testid="edit-accounts-modal"
className="edit-accounts-modal"
>
<ModalOverlay />
<ModalContent>
<ModalHeader onClose={onClose}>{t('editAccounts')}</ModalHeader>
<ModalBody paddingLeft={0} paddingRight={0}>
{showAddNewAccounts ? (
<Box paddingLeft={4} paddingRight={4} paddingBottom={4}>
<CreateEthAccount
onActionComplete={() => setShowAddNewAccounts(false)}
<Modal
isOpen
onClose={onClose}
data-testid="edit-accounts-modal"
className="edit-accounts-modal"
>
<ModalOverlay />
<ModalContent>
<ModalHeader onClose={onClose}>{t('editAccounts')}</ModalHeader>
<ModalBody
paddingLeft={0}
paddingRight={0}
className="edit-accounts-modal__body"
>
{showAddNewAccounts ? (
<Box paddingLeft={4} paddingRight={4} paddingBottom={4}>
<CreateEthAccount
onActionComplete={() => setShowAddNewAccounts(false)}
/>
</Box>
) : (
<>
<Box
padding={4}
display={Display.Flex}
justifyContent={JustifyContent.spaceBetween}
>
<Checkbox
label={t('selectAll')}
andreahaku marked this conversation as resolved.
Show resolved Hide resolved
isChecked={checked}
gap={4}
onClick={() =>
allAreSelected() ? deselectAll() : selectAll()
}
isIndeterminate={isIndeterminate}
/>
<ButtonLink onClick={() => setShowAddNewAccounts(true)}>
{t('newAccount')}
</ButtonLink>
</Box>
) : (
<>
<Box
padding={4}
display={Display.Flex}
justifyContent={JustifyContent.spaceBetween}
>
<Checkbox
label={t('selectAll')}
isChecked={checked}
gap={4}
onClick={() =>
allAreSelected() ? deselectAll() : selectAll()
}
isIndeterminate={isIndeterminate}
/>
<ButtonLink onClick={() => setShowAddNewAccounts(true)}>
{t('newAccount')}
</ButtonLink>
</Box>
{accounts.map((account) => (
<AccountListItem
onClick={() => handleAccountClick(account.address)}
account={account}
key={account.address}
isPinned={Boolean(account.pinned)}
startAccessory={
<Checkbox
isChecked={selectedAccountAddresses.some(
(selectedAccountAddress) =>
isEqualCaseInsensitive(
selectedAccountAddress,
account.address,
),
)}
/>
}
selected={false}
/>
))}

<ModalFooter>
{selectedAccountAddresses.length === 0 ? (
<Box
display={Display.Flex}
flexDirection={FlexDirection.Column}
gap={4}
>
<Box
display={Display.Flex}
gap={1}
alignItems={AlignItems.center}
>
<Icon
name={IconName.Danger}
size={IconSize.Xs}
color={IconColor.errorDefault}
/>
<Text
variant={TextVariant.bodySm}
color={TextColor.errorDefault}
>
{t('disconnectMessage', [hostName])}
</Text>
</Box>
<ButtonPrimary
data-testid="disconnect-accounts-button"
onClick={() => {
onSubmit([]);
onClose();
}}
size={ButtonPrimarySize.Lg}
block
danger
>
{t('disconnect')}
</ButtonPrimary>
</Box>
) : (
<ButtonPrimary
data-testid="connect-more-accounts-button"
onClick={() => {
// Get accounts that are in `selectedAccountAddresses` but not in `defaultSelectedAccountAddresses`
const addedAccounts = selectedAccountAddresses.filter(
(address) => !defaultSet.has(address),
);

// Get accounts that are in `defaultSelectedAccountAddresses` but not in `selectedAccountAddresses`
const removedAccounts =
defaultSelectedAccountAddresses.filter(
(address) => !selectedSet.has(address),
);

onSubmit(selectedAccountAddresses);
trackEvent({
category: MetaMetricsEventCategory.Permissions,
event:
MetaMetricsEventName.UpdatePermissionedAccounts,
properties: {
addedAccounts: addedAccounts.length,
removedAccounts: removedAccounts.length,
location: 'Edit Accounts Modal',
},
});

onClose();
}}
size={ButtonPrimarySize.Lg}
block
>
{t('update')}
</ButtonPrimary>
)}
</ModalFooter>
</>
)}
</ModalBody>
</ModalContent>
</Modal>
</>
{accounts.map((account) => (
<AccountListItem
onClick={() => handleAccountClick(account.address)}
account={account}
key={account.address}
isPinned={Boolean(account.pinned)}
startAccessory={
<Checkbox
isChecked={selectedAccountAddresses.some(
(selectedAccountAddress) =>
isEqualCaseInsensitive(
selectedAccountAddress,
account.address,
),
)}
/>
}
selected={false}
/>
))}
</>
)}
</ModalBody>

<ModalFooter>
{selectedAccountAddresses.length === 0 ? (
<Box
display={Display.Flex}
flexDirection={FlexDirection.Column}
gap={4}
width={BlockSize.Full}
alignItems={AlignItems.center}
>
<Box
display={Display.Flex}
gap={1}
alignItems={AlignItems.center}
>
<Icon
name={IconName.Danger}
size={IconSize.Xs}
color={IconColor.errorDefault}
/>
<Text
variant={TextVariant.bodySm}
color={TextColor.errorDefault}
>
{t('disconnectMessage')}
</Text>
</Box>
<ButtonPrimary
data-testid="disconnect-accounts-button"
onClick={() => {
onSubmit([]);
onClose();
}}
size={ButtonPrimarySize.Lg}
block
danger
>
{t('disconnect')}
</ButtonPrimary>
</Box>
) : (
<ButtonPrimary
data-testid="connect-more-accounts-button"
onClick={() => {
const addedAccounts = selectedAccountAddresses.filter(
(address) => !defaultSet.has(address),
);
const removedAccounts = defaultSelectedAccountAddresses.filter(
(address) => !selectedSet.has(address),
);

onSubmit(selectedAccountAddresses);
trackEvent({
category: MetaMetricsEventCategory.Permissions,
event: MetaMetricsEventName.UpdatePermissionedAccounts,
properties: {
addedAccounts: addedAccounts.length,
removedAccounts: removedAccounts.length,
location: 'Edit Accounts Modal',
},
});

onClose();
}}
size={ButtonPrimarySize.Lg}
block
>
{t('update')}
</ButtonPrimary>
)}
</ModalFooter>
</ModalContent>
</Modal>
);
};
5 changes: 5 additions & 0 deletions ui/components/multichain/edit-accounts-modal/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.edit-accounts-modal {
&__body {
overflow: auto;
}
}
Loading