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

feat: multi RPC editor #25219

Merged
merged 9 commits into from
Jun 18, 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
6 changes: 6 additions & 0 deletions app/_locales/en/messages.json

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

34 changes: 34 additions & 0 deletions ui/pages/settings/networks-tab/index.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
@use "design-system";

.networks-tab {
&__rpc-header {
cursor: default;
}

&__rpc-dropdown {
cursor: pointer;
}

&__rpc-item {
position: relative;
}

&__rpc-item:hover {
cursor: pointer;
background-color: var(--color-background-default-hover);
}

&__rpc-item--selected,
&__rpc-item--selected:hover {
background-color: var(--color-primary-muted);
}

&__rpc-selected-pill {
width: 4px;
height: calc(100% - 8px);
position: absolute;
top: 4px;
left: 4px;
}

&__rpc-popover {
z-index: 1;
}

&__imageclose {
cursor: pointer;
color: var(--color-icon-default);
Expand Down
27 changes: 16 additions & 11 deletions ui/pages/settings/networks-tab/networks-form/networks-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
getMatchedChain,
getMatchedSymbols,
} from '../../../../helpers/utils/network-helper';
import { RpcUrlEditor } from './rpc-url-editor';

/**
* Attempts to convert the given chainId to a decimal string, for display
Expand Down Expand Up @@ -760,17 +761,21 @@ const NetworksForm = ({
disabled={viewOnly}
dataTestId="network-form-network-name"
/>
<FormField
error={errors.rpcUrl?.msg || ''}
onChange={(value) => {
setIsEditing(true);
setRpcUrl(value);
}}
titleText={t('rpcUrl')}
value={displayRpcUrl}
disabled={viewOnly}
dataTestId="network-form-rpc-url"
/>
{window.metamaskFeatureFlags?.networkMenuRedesign ? (
<RpcUrlEditor currentRpcUrl={displayRpcUrl} />
) : (
<FormField
error={errors.rpcUrl?.msg || ''}
onChange={(value) => {
setIsEditing(true);
setRpcUrl(value);
}}
titleText={t('rpcUrl')}
value={displayRpcUrl}
disabled={viewOnly}
dataTestId="network-form-rpc-url"
/>
)}
<FormField
warning={warnings.chainId?.msg || ''}
error={errors.chainId?.msg || ''}
Expand Down
144 changes: 144 additions & 0 deletions ui/pages/settings/networks-tab/networks-form/rpc-url-editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import React, { useRef, useState } from 'react';
import classnames from 'classnames';
import {
Box,
ButtonIcon,
ButtonIconSize,
Icon,
IconName,
IconSize,
Popover,
PopoverPosition,
Text,
} from '../../../../components/component-library';
import {
AlignItems,
BackgroundColor,
BorderColor,
BorderRadius,
Display,
IconColor,
JustifyContent,
TextColor,
TextVariant,
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';

export const RpcUrlEditor = ({ currentRpcUrl }: { currentRpcUrl: string }) => {
// TODO: real endpoints
const dummyRpcUrls = [
currentRpcUrl,
'https://dummy.mainnet.public.blastapi.io',
'https://dummy.io/v3/blockchain/node/dummy',
];

const t = useI18nContext();
const rpcDropdown = useRef(null);
const [isOpen, setIsOpen] = useState(false);
const [currentRpcEndpoint, setCurrentRpcEndpoint] = useState(currentRpcUrl);

return (
<>
<Text
className="networks-tab__rpc-header"
marginTop={1}
marginBottom={1}
variant={TextVariant.bodySmBold}
>
{t('defaultRpcUrl')}
</Text>
<Box
bergeron marked this conversation as resolved.
Show resolved Hide resolved
onClick={() => setIsOpen(!isOpen)}
className="networks-tab__rpc-dropdown"
display={Display.Flex}
justifyContent={JustifyContent.spaceBetween}
borderRadius={BorderRadius.MD}
borderColor={BorderColor.borderDefault}
borderWidth={1}
padding={2}
ref={rpcDropdown}
>
<Text variant={TextVariant.bodySm}>{currentRpcEndpoint}</Text>
<ButtonIcon
iconName={isOpen ? IconName.ArrowUp : IconName.ArrowDown}
ariaLabel={t('defaultRpcUrl')}
size={ButtonIconSize.Sm}
/>
</Box>
<Popover
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The width of this popover is just based on contents for now. Will need to be constrained once this form becomes a fullscreen/modal.

paddingTop={2}
paddingBottom={2}
paddingLeft={0}
paddingRight={0}
className="networks-tab__rpc-popover"
referenceElement={rpcDropdown.current}
position={PopoverPosition.Bottom}
isOpen={isOpen}
>
{dummyRpcUrls.map((rpcEndpoint) => (
<Box
padding={4}
display={Display.Flex}
justifyContent={JustifyContent.spaceBetween}
key={rpcEndpoint}
onClick={() => setCurrentRpcEndpoint(rpcEndpoint)}
className={classnames('networks-tab__rpc-item', {
'networks-tab__rpc-item--selected':
rpcEndpoint === currentRpcEndpoint,
})}
>
{rpcEndpoint === currentRpcEndpoint && (
<Box
className="networks-tab__rpc-selected-pill"
borderRadius={BorderRadius.pill}
backgroundColor={BackgroundColor.primaryDefault}
/>
)}
<Text
as="button"
color={TextColor.textDefault}
variant={TextVariant.bodySmMedium}
backgroundColor={BackgroundColor.transparent}
>
{rpcEndpoint}
</Text>
<ButtonIcon
marginLeft={5}
ariaLabel={t('delete')}
size={ButtonIconSize.Sm}
iconName={IconName.Trash}
color={IconColor.errorDefault}
// eslint-disable-next-line no-alert
onClick={() => alert('TODO: delete confirmation modal')}
/>
</Box>
))}
<Box
bergeron marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-alert
onClick={() => alert('TODO: add RPC modal')}
padding={4}
display={Display.Flex}
alignItems={AlignItems.center}
className="networks-tab__rpc-item"
>
<Icon
color={IconColor.primaryDefault}
name={IconName.Add}
size={IconSize.Sm}
marginRight={2}
/>
<Text
as="button"
backgroundColor={BackgroundColor.transparent}
color={TextColor.primaryDefault}
variant={TextVariant.bodySmMedium}
>
{t('addRpcUrl')}
</Text>
</Box>
</Popover>
</>
);
};

export default RpcUrlEditor;