Skip to content

Commit

Permalink
feat: truncate ENS names (#250)
Browse files Browse the repository at this point in the history
* feat: truncate ENS names

* chore: adds changeset

* Update packages/rainbowkit/src/components/ConnectButton/formatENS.test.ts

Co-authored-by: Mark Dalgleish <mark.john.dalgleish@gmail.com>

* WIP

* fix: adds subdomain functionality

Co-authored-by: Mark Dalgleish <mark.john.dalgleish@gmail.com>
  • Loading branch information
nickbytes and markdalgleish committed Apr 27, 2022
1 parent a864107 commit df6c310
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-otters-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rainbow-me/rainbowkit': patch
---

Truncates long ENS names
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../RainbowKitProvider/RainbowKitChainContext';
import { ShowRecentTransactionsContext } from '../RainbowKitProvider/ShowRecentTransactionsContext';
import { formatAddress } from './formatAddress';
import { formatENS } from './formatENS';

const useBooleanState = (initialValue: boolean) => {
const [value, setValue] = useState(initialValue);
Expand Down Expand Up @@ -153,8 +154,9 @@ export function ConnectButtonRenderer({
balanceFormatted: balanceData?.formatted,
balanceSymbol: balanceData?.symbol,
displayBalance,
displayName:
accountData.ens?.name ?? formatAddress(accountData.address),
displayName: accountData.ens?.name
? formatENS(accountData.ens?.name)
: formatAddress(accountData.address),
ensAvatar: accountData.ens?.avatar ?? undefined,
ensName: accountData.ens?.name,
hasPendingTransactions,
Expand Down
32 changes: 32 additions & 0 deletions packages/rainbowkit/src/components/ConnectButton/formatENS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';
import { formatENS } from './formatENS';

describe('formatENS', () => {
it('trucates ENS name over 24 characters', () => {
expect(formatENS('reallylongensnameheretotestlongnames.eth')).toEqual(
'reallylongensnameheretot...'
);
});

it(`doesn't do anything to ENS names 24 characters or less`, () => {
expect(formatENS('rainbow.eth')).toEqual('rainbow.eth');
});

it(`if 24 characters, do not truncate .eth`, () => {
expect(formatENS('qwertyuiopasdfghjklzxcvb.eth')).toEqual(
'qwertyuiopasdfghjklzxcvb.eth'
);
});

it(`Subdomains are taken into account`, () => {
expect(formatENS('rainbowrainbowrainbow.rainbowrainbow.eth')).toEqual(
'rainbowrainbowrainbow.ra...'
);
});

it(`Non .eth names work`, () => {
expect(formatENS('qwertyuiopasdfghjklzxcvb.xyz')).toEqual(
'qwertyuiopasdfghjklzxcvb.xyz'
);
});
});
8 changes: 8 additions & 0 deletions packages/rainbowkit/src/components/ConnectButton/formatENS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function formatENS(name: string): string {
const parts = name.split('.');
const last = parts.pop();
if (parts.join('.').length > 24) {
return `${parts.join('.').substring(0, 24)}...`;
}
return `${parts.join('.')}.${last}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Avatar } from '../Avatar/Avatar';
import { Box } from '../Box/Box';
import { CloseButton } from '../CloseButton/CloseButton';
import { formatAddress } from '../ConnectButton/formatAddress';
import { formatENS } from '../ConnectButton/formatENS';
import { CopiedIcon } from '../Icons/Copied';
import { CopyIcon } from '../Icons/Copy';
import { DisconnectIcon } from '../Icons/Disconnect';
Expand Down Expand Up @@ -49,8 +50,9 @@ export function ProfileDetails({
return null;
}

const accountName =
accountData.ens?.name ?? formatAddress(accountData.address);
const accountName = accountData.ens?.name
? formatENS(accountData.ens?.name)
: formatAddress(accountData.address);
const ethBalance = balanceData?.formatted;
const balance = Number(ethBalance).toPrecision(3);
const titleId = 'rk_profile_title';
Expand Down

2 comments on commit df6c310

@vercel
Copy link

@vercel vercel bot commented on df6c310 Apr 27, 2022

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on df6c310 Apr 27, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

rainbowkit-site – ./

rainbowkit.vercel.app
rainbowkit-site-rainbowdotme.vercel.app
rainbowkit-site-git-main-rainbowdotme.vercel.app

Please sign in to comment.