Skip to content

feat: Better handle very long names in the name component #28560

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

Merged
merged 2 commits into from
Nov 21, 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
62 changes: 62 additions & 0 deletions ui/components/app/name/__snapshots__/name.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,68 @@ exports[`Name renders address with image 1`] = `
</div>
`;

exports[`Name renders address with long saved name 1`] = `
<div>
<div
class="mm-box mm-box--display-flex"
>
<div
class="name name__saved"
>
<div
class=""
>
<div
class="identicon"
style="height: 16px; width: 16px; border-radius: 8px;"
>
<div
style="border-radius: 50px; overflow: hidden; padding: 0px; margin: 0px; width: 16px; height: 16px; display: inline-block; background: rgb(245, 196, 0);"
>
<svg
height="16"
width="16"
x="0"
y="0"
>
<rect
fill="#C8144D"
height="16"
transform="translate(-0.8205726313476182 -2.375300755278998) rotate(408.5 8 8)"
width="16"
x="0"
y="0"
/>
<rect
fill="#FB1860"
height="16"
transform="translate(7.733089412585649 -1.3465466417019656) rotate(402.6 8 8)"
width="16"
x="0"
y="0"
/>
<rect
fill="#03505E"
height="16"
transform="translate(1.1000464230208766 -13.147318085284851) rotate(308.0 8 8)"
width="16"
x="0"
y="0"
/>
</svg>
</div>
</div>
</div>
<p
class="mm-box mm-text name__name mm-text--body-md mm-box--color-text-default"
>
Very long and l...
</p>
</div>
</div>
</div>
`;

exports[`Name renders address with no saved name 1`] = `
<div>
<div
Expand Down
18 changes: 18 additions & 0 deletions ui/components/app/name/name.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ describe('Name', () => {
expect(container).toMatchSnapshot();
});

it('renders address with long saved name', () => {
useDisplayNameMock.mockReturnValue({
name: "Very long and length saved name that doesn't seem to end, really.",
hasPetname: true,
});

const { container } = renderWithProvider(
<Name
type={NameType.ETHEREUM_ADDRESS}
value={ADDRESS_SAVED_NAME_MOCK}
variation={VARIATION_MOCK}
/>,
store,
);

expect(container).toMatchSnapshot();
});

it('renders address with image', () => {
useDisplayNameMock.mockReturnValue({
name: SAVED_NAME_MOCK,
Expand Down
10 changes: 8 additions & 2 deletions ui/components/app/name/name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NameType } from '@metamask/name-controller';
import classnames from 'classnames';
import { toChecksumAddress } from 'ethereumjs-util';
import { Box, Icon, IconName, IconSize, Text } from '../../component-library';
import { shortenAddress } from '../../../helpers/utils/util';
import { shortenAddress, shortenString } from '../../../helpers/utils/util';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
MetaMetricsEventCategory,
Expand Down Expand Up @@ -103,6 +103,12 @@ const Name = memo(
}, [setModalOpen]);

const formattedValue = formatValue(value, type);
const formattedName = shortenString(name || undefined, {
truncatedCharLimit: 15,
truncatedStartChars: 15,
truncatedEndChars: 0,
skipCharacterInEnd: true,
});
const hasDisplayName = Boolean(name);

return (
Expand Down Expand Up @@ -135,7 +141,7 @@ const Name = memo(
)}
{hasDisplayName ? (
<Text className="name__name" variant={TextVariant.bodyMd}>
{name}
{formattedName}
</Text>
) : (
<Text className="name__value" variant={TextVariant.bodyMd}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Will be nice to have some unit test coverage also

Expand Down
Loading