-
-
Notifications
You must be signed in to change notification settings - Fork 282
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(suite): refactor advanced coin settings modal #17434
Open
seibei-iguchi
wants to merge
1
commit into
develop
Choose a base branch
from
feat/refactor-advanced-settings-modal
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
236 changes: 178 additions & 58 deletions
236
...odals/ReduxModal/UserContextModal/AdvancedCoinSettingsModal/AdvancedCoinSettingsModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,197 @@ | ||
import styled from 'styled-components'; | ||
import { useState } from 'react'; | ||
|
||
import { type NetworkSymbol, getNetwork } from '@suite-common/wallet-config'; | ||
import { variables } from '@trezor/components'; | ||
import { CoinLogo } from '@trezor/product-components'; | ||
import { | ||
Button, | ||
Card, | ||
CollapsibleBox, | ||
Column, | ||
DotIndicator, | ||
Input, | ||
List, | ||
NewModal, | ||
Row, | ||
Text, | ||
} from '@trezor/components'; | ||
import { spacings } from '@trezor/theme'; | ||
|
||
import { Modal, Translation } from 'src/components/suite'; | ||
import { useSelector } from 'src/hooks/suite'; | ||
import { getCoinLabel } from 'src/utils/suite/getCoinLabel'; | ||
import { toggleTor } from 'src/actions/suite/suiteActions'; | ||
import { Translation } from 'src/components/suite'; | ||
import { useBackendsForm, useDefaultUrls } from 'src/hooks/settings/backends'; | ||
import { useDispatch, useSelector } from 'src/hooks/suite'; | ||
import { selectTorState } from 'src/reducers/suite/suiteReducer'; | ||
|
||
import { CustomBackends } from './CustomBackends/CustomBackends'; | ||
import { BackendTypeSelect } from './CustomBackends/BackendTypeSelect'; | ||
import ConnectionInfo from './CustomBackends/ConnectionInfo'; | ||
import { TorModal, TorResult } from './CustomBackends/TorModal'; | ||
|
||
const Section = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
`; | ||
type AdvancedCoinSettingsModalProps = { | ||
symbol: NetworkSymbol; | ||
onCancel: () => void; | ||
}; | ||
|
||
const Heading = styled.div` | ||
display: flex; | ||
align-items: center; | ||
line-height: initial; | ||
export const AdvancedCoinSettingsModal = ({ symbol, onCancel }: AdvancedCoinSettingsModalProps) => { | ||
const network = getNetwork(symbol); | ||
const { isTorEnabled } = useSelector(selectTorState); | ||
const blockchain = useSelector(state => state.wallet.blockchain); | ||
const dispatch = useDispatch(); | ||
const [torModalOpen, setTorModalOpen] = useState(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: maybe |
||
|
||
> * + * { | ||
margin-left: 16px; | ||
} | ||
`; | ||
const { | ||
type, | ||
urls, | ||
input: { error, name, placeholder, register, reset, validate, value }, | ||
changeType, | ||
addUrl, | ||
removeUrl, | ||
save, | ||
hasOnlyOnions, | ||
} = useBackendsForm(symbol); | ||
|
||
const Header = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
`; | ||
const onSaveClick = () => { | ||
if (!isTorEnabled && hasOnlyOnions()) { | ||
setTorModalOpen(true); | ||
} else { | ||
save(); | ||
onCancel(); | ||
} | ||
}; | ||
|
||
const Subheader = styled.span` | ||
font-size: ${variables.FONT_SIZE.NORMAL}; | ||
font-weight: ${variables.FONT_WEIGHT.MEDIUM}; | ||
color: ${({ theme }) => theme.legacy.TYPE_LIGHT_GREY}; | ||
`; | ||
const onTorResult = async (result: TorResult) => { | ||
switch (result) { | ||
case 'enable-tor': | ||
await dispatch(toggleTor(true)); | ||
|
||
interface AdvancedCoinSettingsModalProps { | ||
symbol: NetworkSymbol; | ||
onCancel: () => void; | ||
} | ||
setTorModalOpen(false); | ||
save(); | ||
onCancel(); | ||
|
||
export const AdvancedCoinSettingsModal = ({ symbol, onCancel }: AdvancedCoinSettingsModalProps) => { | ||
const blockchain = useSelector(state => state.wallet.blockchain); | ||
const network = getNetwork(symbol); | ||
break; | ||
case 'use-defaults': | ||
changeType('default'); | ||
setTorModalOpen(false); | ||
|
||
// no default | ||
} | ||
}; | ||
|
||
const { name, features, testnet: isTestnet } = network; | ||
const hasCustomBackend = !!blockchain[symbol].backends.selected; | ||
const label = getCoinLabel(features, isTestnet, hasCustomBackend); | ||
const { defaultUrls } = useDefaultUrls(symbol); | ||
const { ref: inputRef, ...inputField } = register(name, { validate }); | ||
const isEditable = type !== 'default'; | ||
const isSubmitButtonDisabled = isEditable && !!error; | ||
|
||
return ( | ||
<Modal | ||
isCancelable | ||
return torModalOpen ? ( | ||
<TorModal onResult={onTorResult} /> | ||
) : ( | ||
<NewModal | ||
onCancel={onCancel} | ||
heading={ | ||
<Heading> | ||
<CoinLogo symbol={symbol} /> | ||
|
||
<Header> | ||
<span>{name}</span> | ||
|
||
{label && ( | ||
<Subheader> | ||
<Translation id={label} /> | ||
</Subheader> | ||
)} | ||
</Header> | ||
</Heading> | ||
<Text case="titlecase" as="p"> | ||
{network.name} <Translation id="TR_BACKENDS" /> | ||
</Text> | ||
} | ||
description={ | ||
<Translation | ||
id={ | ||
network?.networkType === 'cardano' | ||
? 'SETTINGS_ADV_COIN_BLOCKFROST_DESCRIPTION' | ||
: 'SETTINGS_ADV_COIN_BLOCKBOOK_DESCRIPTION' | ||
} | ||
/> | ||
} | ||
size="small" | ||
bottomContent={ | ||
<> | ||
<NewModal.Button | ||
onClick={onSaveClick} | ||
isDisabled={isSubmitButtonDisabled} | ||
data-testid="@settings/advance/button/save" | ||
> | ||
<Translation id="TR_CONFIRM" /> | ||
</NewModal.Button> | ||
<NewModal.Button onClick={onCancel} variant="tertiary"> | ||
<Translation id="TR_CANCEL" /> | ||
</NewModal.Button> | ||
</> | ||
} | ||
> | ||
<Section> | ||
<CustomBackends network={network} onCancel={onCancel} /> | ||
</Section> | ||
</Modal> | ||
<Column gap={spacings.lg}> | ||
<Card | ||
heading={ | ||
<BackendTypeSelect network={network} value={type} onChange={changeType} /> | ||
} | ||
> | ||
<Column gap={spacings.xxl}> | ||
{(urls.length || (!isEditable && defaultUrls.length)) && ( | ||
<List bulletComponent={<DotIndicator />} gap={spacings.sm}> | ||
{(isEditable ? urls : defaultUrls).map(url => ( | ||
<List.Item | ||
data-testid="@settings/advance/url" | ||
key={url} | ||
bulletComponent={ | ||
url === blockchain[symbol]?.url ? ( | ||
<DotIndicator isActive /> | ||
) : undefined | ||
} | ||
> | ||
<Row gap={spacings.sm}> | ||
<Text | ||
variant={ | ||
url === blockchain[symbol]?.url | ||
? 'default' | ||
: 'tertiary' | ||
} | ||
> | ||
{url} | ||
</Text> | ||
{isEditable && ( | ||
<Button | ||
variant="tertiary" | ||
size="tiny" | ||
icon="trash" | ||
onClick={() => removeUrl(url)} | ||
> | ||
<Translation id="TR_REMOVE" /> | ||
</Button> | ||
)} | ||
</Row> | ||
</List.Item> | ||
))} | ||
</List> | ||
)} | ||
{isEditable && ( | ||
<Column gap={spacings.sm}> | ||
<Input | ||
data-testid={`@settings/advance/${name}`} | ||
placeholder={placeholder} | ||
inputState={error ? 'error' : undefined} | ||
bottomText={error?.message || null} | ||
innerRef={inputRef} | ||
innerAddon={ | ||
<Button | ||
variant="primary" | ||
size="tiny" | ||
icon="plus" | ||
data-testid="@settings/advance/button/add" | ||
onClick={() => { | ||
addUrl(value); | ||
reset(); | ||
}} | ||
isDisabled={!!error || value === ''} | ||
> | ||
<Translation id="TR_ADD_NEW_BLOCKBOOK_BACKEND" /> | ||
</Button> | ||
} | ||
{...inputField} | ||
/> | ||
</Column> | ||
)} | ||
</Column> | ||
</Card> | ||
<CollapsibleBox heading={<Translation id="SETTINGS_ADV_COIN_CONN_INFO_TITLE" />}> | ||
<ConnectionInfo symbol={symbol} /> | ||
</CollapsibleBox> | ||
</Column> | ||
</NewModal> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prop name seems to me very confusing. Why not keep
textTransform
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I know why you did it. It's because titlecase is non-standard. OK then...