Skip to content

Commit

Permalink
Fix in label displayed for state change in signature decoding section
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekpur committed Dec 9, 2024
1 parent 44696fd commit 5f12d7a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import configureMockStore from 'redux-mock-store';
import {
DecodingData,
DecodingDataChangeType,
DecodingDataStateChanges,
} from '@metamask/signature-controller';
Expand All @@ -11,7 +10,7 @@ import { renderWithConfirmContextProvider } from '../../../../../../../../../tes
import { permitSignatureMsg } from '../../../../../../../../../test/data/confirmations/typed_sign';
import PermitSimulation, { getStateChangeToolip } from './decoded-simulation';

const decodingData: DecodingData = {
const decodingData = {
stateChanges: [
{
assetType: 'ERC20',
Expand Down Expand Up @@ -165,4 +164,26 @@ describe('DecodedSimulation', () => {
);
expect(tooltip).toBe('signature_decoding_bid_nft_tooltip');
});

it('renders label only once if there are multiple state changes of same changeType', async () => {
const state = getMockTypedSignConfirmStateForRequest({
...permitSignatureMsg,
decodingLoading: false,
decodingData: {
stateChanges: [
decodingData.stateChanges[0],
decodingData.stateChanges[0],
decodingData.stateChanges[0],
],
},
});
const mockStore = configureMockStore([])(state);

const { findAllByText } = renderWithConfirmContextProvider(
<PermitSimulation />,
mockStore,
);

expect(await findAllByText('Spending cap')).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,20 @@ const StateChangeRow = ({
stateChangeList,
stateChange,
chainId,
displayLabel,
}: {
stateChangeList: DecodingDataStateChanges | null;
stateChange: DecodingDataStateChange;
chainId: Hex;
displayLabel: boolean;
}) => {
const t = useI18nContext();
const { assetType, changeType, amount, contractAddress, tokenID } =
stateChange;
const tooltip = getStateChangeToolip(stateChangeList, stateChange, t);
return (
<ConfirmInfoRow
label={getStateChangeLabelMap(t, changeType)}
label={displayLabel ? getStateChangeLabelMap(t, changeType) : ''}
tooltip={tooltip}
>
{(assetType === TokenStandard.ERC20 ||
Expand Down Expand Up @@ -104,19 +106,39 @@ const DecodedSimulation: React.FC<object> = () => {
const chainId = currentConfirmation.chainId as Hex;
const { decodingLoading, decodingData } = currentConfirmation;

const stateChangeFragment = useMemo(
() =>
(decodingData?.stateChanges ?? []).map(
(change: DecodingDataStateChange) => (
const stateChangeFragment = useMemo(() => {
const stateChangesGrouped: Record<string, DecodingDataStateChange[]> = (
decodingData?.stateChanges ?? []
).reduce(
(
result: Record<string, DecodingDataStateChange[]>,
stateChange: DecodingDataStateChange,
) => {
result[stateChange.changeType] = [
...(result[stateChange.changeType] ?? []),
stateChange,
];
return result;
},
{},
);

return Object.entries(stateChangesGrouped)
.map(([_, changeList]) =>
changeList.map((change: DecodingDataStateChange, index: number) => (
<StateChangeRow
stateChangeList={decodingData?.stateChanges ?? []}
stateChange={change}
chainId={chainId}
displayLabel={index === 0}
/>
),
),
[decodingData?.stateChanges],
);
)),
)
.reduce(
(result, stateChangeGroup) => [...result, ...stateChangeGroup],
[],
);
}, [decodingData?.stateChanges]);

return (
<StaticSimulation
Expand Down

0 comments on commit 5f12d7a

Please sign in to comment.