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

TX details: support switching between hex and base64 data display #904

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .changelog/904.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TX details: support switching between hex and base64 data display
88 changes: 88 additions & 0 deletions src/app/components/DataFormatSwitch/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Switch, { switchClasses } from '@mui/material/Switch'
import { styled } from '@mui/material/styles'
import { COLORS } from '../../../styles/theme/colors'
import Typography from '@mui/material/Typography'
import Box from '@mui/material/Box'
import { ChangeEvent, FC } from 'react'
import { useTranslation } from 'react-i18next'
import HelpIcon from '@mui/icons-material/Help'
import Tooltip from '@mui/material/Tooltip'

const StyledAddressSwitch = styled(Box)(({ theme }) => ({
display: 'flex',
alignItems: 'center',
gap: theme.spacing(3),
}))

const StyledSwitch = styled(Switch)(() => ({
zoom: 2,
margin: '-8px', // Reduce padding
[`.${switchClasses.switchBase} .${switchClasses.thumb}`]: {
// backgroundColor: 'black',
backgroundColor: COLORS.brandMedium,
// backgroundImage: `url("${oasisLogo}")`,
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
backgroundSize: '15px',
},
[`.${switchClasses.switchBase}.${switchClasses.checked} .${switchClasses.thumb}`]: {
// backgroundColor: COLORS.lightSilver,
// backgroundImage: `url("${ethLogo}")`,
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
backgroundSize: '10px',
},
[`&&&& .${switchClasses.track}`]: {
opacity: 1,
// backgroundColor: COLORS.grayMedium2,
},
}))

export enum DataFormatSwitchOption {
Hex = 'hex',
Base64 = 'base64',
}

interface DataFormatSwitchProps {
selected?: DataFormatSwitchOption.Hex | DataFormatSwitchOption.Base64
onSelectionChange: (selection: DataFormatSwitchOption.Hex | DataFormatSwitchOption.Base64) => void
}

export const DataFormatSwitch: FC<DataFormatSwitchProps> = ({
selected = DataFormatSwitchOption.Hex,
onSelectionChange,
}) => {
const { t } = useTranslation()

const checked = selected === DataFormatSwitchOption.Base64

const onChange = (_event: ChangeEvent<HTMLInputElement>, checked: boolean) => {
onSelectionChange(checked ? DataFormatSwitchOption.Base64 : DataFormatSwitchOption.Hex)
}

return (
<StyledAddressSwitch>
<span>{t('dataFormatSwitch.label')}</span>
<Tooltip title={t('dataFormatSwitch.explanation')}>
<HelpIcon />
</Tooltip>
<Typography
sx={{
color: COLORS.brandExtraDark,
fontSize: '10px',
}}
>
{t('common.hex')}
</Typography>
<StyledSwitch checked={checked} onChange={onChange} title={'whatever'} />
<Typography
sx={{
color: COLORS.brandExtraDark,
fontSize: '10px',
}}
>
{t('common.base64')}
</Typography>
</StyledAddressSwitch>
)
}
41 changes: 31 additions & 10 deletions src/app/pages/TransactionDetailPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { LongDataDisplay } from '../../components/LongDataDisplay'
import { getPreciseNumberFormat } from '../../../locales/getPreciseNumberFormat'
import { base64ToHex } from '../../utils/helpers'
import { DappBanner } from '../../components/DappBanner'
import { DataFormatSwitch, DataFormatSwitchOption } from '../../components/DataFormatSwitch'

type TransactionSelectionResult = {
wantedTransaction?: RuntimeTransaction
Expand Down Expand Up @@ -92,6 +93,10 @@ export const TransactionDetailPage: FC = () => {
AddressSwitchOption.Oasis | AddressSwitchOption.ETH
>(AddressSwitchOption.ETH)

const [dataFormatSwitchOption, setDataFormatSwitchOption] = useState<
DataFormatSwitchOption.Hex | DataFormatSwitchOption.Base64
>(DataFormatSwitchOption.Hex)

const { isLoading, data } = useGetRuntimeTransactionsTxHash(
scope.network,
scope.layer, // This is OK since consensus has been handled separately
Expand All @@ -116,17 +121,25 @@ export const TransactionDetailPage: FC = () => {
featured
title={t('transaction.header')}
action={
<AddressSwitch
selected={addressSwitchOption}
onSelectionChange={addressSwitch => setAddressSwitchOption(addressSwitch)}
/>
<span style={{ display: 'inline-flex' }}>
<DataFormatSwitch
selected={dataFormatSwitchOption}
onSelectionChange={dataFormat => setDataFormatSwitchOption(dataFormat)}
/>
&nbsp; &nbsp; &nbsp; &nbsp;
<AddressSwitch
selected={addressSwitchOption}
onSelectionChange={addressSwitch => setAddressSwitchOption(addressSwitch)}
/>
</span>
}
>
<TransactionDetailView
isLoading={isLoading}
transaction={transaction}
tokenPriceInfo={tokenPriceInfo}
addressSwitchOption={addressSwitchOption}
dataFormatSwitchOption={dataFormatSwitchOption}
/>
</SubPageCard>
<DappBanner scope={scope} ethAddress={transaction?.sender_0_eth} />
Expand Down Expand Up @@ -168,13 +181,15 @@ export const TransactionDetailView: FC<{
standalone?: boolean
tokenPriceInfo: TokenPriceInfo
addressSwitchOption?: AddressSwitchOption
dataFormatSwitchOption?: DataFormatSwitchOption
}> = ({
isLoading,
transaction,
showLayer,
standalone = false,
tokenPriceInfo,
addressSwitchOption = AddressSwitchOption.ETH,
dataFormatSwitchOption = DataFormatSwitchOption.Hex,
}) => {
const { t } = useTranslation()
const { isMobile } = useScreenSize()
Expand All @@ -189,6 +204,9 @@ export const TransactionDetailView: FC<{
const ticker = transaction?.ticker || Ticker.ROSE
const tickerName = getNameForTicker(t, ticker)

const formatBase64Data = (base64Data: string): string =>
dataFormatSwitchOption === DataFormatSwitchOption.Hex ? base64ToHex(base64Data) : base64Data

return (
<>
{isLoading && <TextSkeleton numberOfRows={10} />}
Expand Down Expand Up @@ -356,7 +374,7 @@ export const TransactionDetailView: FC<{
<>
<dt>{t('transaction.rawData')}</dt>
<dd>
<LongDataDisplay data={base64ToHex(transaction.body.data)} threshold={300} />
<LongDataDisplay data={formatBase64Data(transaction.body.data)} threshold={300} />
</dd>
</>
)}
Expand All @@ -368,7 +386,7 @@ export const TransactionDetailView: FC<{
<dt>{t('transactions.encryption.publicKey')}</dt>
<dd>
<Typography variant="mono" sx={{ overflowWrap: 'anywhere' }}>
{transaction.encryption_envelope.public_key}
{formatBase64Data(transaction.encryption_envelope.public_key)}
</Typography>
</dd>
</>
Expand All @@ -379,7 +397,7 @@ export const TransactionDetailView: FC<{
<dt>{t('transactions.encryption.dataNonce')}</dt>
<dd>
<Typography variant="mono" sx={{ overflowWrap: 'anywhere' }}>
{transaction.encryption_envelope.data_nonce}
{formatBase64Data(transaction.encryption_envelope.data_nonce)}
</Typography>
</dd>
</>
Expand All @@ -389,7 +407,10 @@ export const TransactionDetailView: FC<{
<>
<dt>{t('transactions.encryption.encryptedData')}</dt>
<dd>
<LongDataDisplay data={transaction.encryption_envelope.data} threshold={300} />
<LongDataDisplay
data={formatBase64Data(transaction.encryption_envelope.data)}
threshold={300}
/>
</dd>
</>
)}
Expand All @@ -399,7 +420,7 @@ export const TransactionDetailView: FC<{
<dt>{t('transactions.encryption.resultNonce')}</dt>
<dd>
<Typography variant="mono" sx={{ fontWeight: 400 }}>
{transaction.encryption_envelope.result_nonce}
{formatBase64Data(transaction.encryption_envelope.result_nonce)}
</Typography>
</dd>
</>
Expand All @@ -410,7 +431,7 @@ export const TransactionDetailView: FC<{
<dt>{t('transactions.encryption.encryptedResult')}</dt>
<dd>
<LongDataDisplay
data={transaction.encryption_envelope.result}
data={formatBase64Data(transaction.encryption_envelope.result)}
fontWeight={400}
threshold={300}
/>
Expand Down
6 changes: 6 additions & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"address": "Address",
"age": "Age",
"balance": "Balance",
"base64": "Base64",
"block": "Block",
"bytes": "{{value, number}}",
"cancel": "Cancel",
Expand All @@ -61,6 +62,7 @@
"gasLimit": "Gas Limit",
"hash": "Hash",
"height": "Height",
"hex": "Hex",
"hide": "Hide",
"loadMore": "Load more",
"lessThanAmount": "&lt; {{value, number}} <TickerLink />",
Expand Down Expand Up @@ -509,5 +511,9 @@
"title": "Validator",
"uptime": "Uptime",
"voting": "Voting %"
},
"dataFormatSwitch": {
"label": "Data format:",
"explanation": "Which data format to use when displaying raw data?"
}
}
Loading