Skip to content

Commit

Permalink
fix: replace the ledger username with ledger hsm (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl authored Feb 5, 2025
1 parent 4dc4edd commit 45ae9ba
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
3 changes: 2 additions & 1 deletion components/react/views/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChevronLeftIcon } from '@heroicons/react/20/solid';
import { useState } from 'react';
import ProfileAvatar from '@/utils/identicon';
import { useBalance } from '@/hooks/useQueries';
import { Username } from '@/components/username';
import { CopyIcon } from '@/components/icons';
import { getRealLogo, shiftDigits, truncateAddress } from '@/utils';
import Image from 'next/image';
Expand Down Expand Up @@ -80,7 +81,7 @@ export const Connected = ({
<div className="flex items-center ">
<ProfileAvatar walletAddress={address ?? ''} size={60} />
<div className="ml-4">
<p className="text-lg font-semibold">{username || 'Anonymous'}</p>
<Username className="text-lg font-semibold" walletName={name} username={username} />
<div className="flex items-center">
<p className="text-sm text-gray-500 dark:text-gray-400">
{truncateAddress(address || '')}
Expand Down
58 changes: 58 additions & 0 deletions components/username.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { afterEach, test, expect, describe } from 'bun:test';
import { cleanup, render, screen } from '@testing-library/react';
import matchers from '@testing-library/jest-dom/matchers';
import React from 'react';
import { Username } from './username';

expect.extend(matchers);

// Test the happy path of the username component
describe('Username', () => {
afterEach(() => {
cleanup();
});
test('renders Ledger correctly', () => {
render(<Username walletName="Ledger" username="NOT LEDGER" />);
expect(screen.getByText('Ledger HSM')).toBeInTheDocument();
// This will be a title attribute.
expect(screen.queryByText('NOT LEDGER')).not.toBeInTheDocument();
expect(screen.queryByTitle('NOT LEDGER')).toBeInTheDocument();
});

test('renders correctly', () => {
render(<Username walletName="AnyOther" username="Some Username" />);
expect(screen.getByText('Some Username')).toBeInTheDocument();
expect(screen.getByTitle('Some Username')).toBeInTheDocument();
});

test('renders correctly with truncated', () => {
render(<Username walletName="AnyOther" username="Some Username" truncated />);
expect(screen.getByText('Some U...ername')).toBeInTheDocument();
expect(screen.getByTitle('Some Username')).toBeInTheDocument();
});

test('renders correctly with no username', () => {
render(<Username />);
expect(screen.getByText('Anonymous')).toBeInTheDocument();
expect(screen.getByTitle('Anonymous')).toBeInTheDocument();
});

test('renders correctly with no walletName', () => {
render(<Username username="Some Username" />);
expect(screen.getByText('Some Username')).toBeInTheDocument();
expect(screen.getByTitle('Some Username')).toBeInTheDocument();
});

test('renders correctly with no username and truncated', () => {
render(<Username truncated />);
expect(screen.getByText('Anonymous')).toBeInTheDocument();
expect(screen.getByTitle('Anonymous')).toBeInTheDocument();
});

test('renders correctly with no walletName and truncated', () => {
render(<Username username="Some Very Very Long Username" truncated />);
expect(screen.getByText('Some V...ername')).toBeInTheDocument();
expect(screen.queryByText('Some Very Very Long Username')).not.toBeInTheDocument();
expect(screen.getByTitle('Some Very Very Long Username')).toBeInTheDocument();
});
});
35 changes: 35 additions & 0 deletions components/username.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { truncateString } from '@/utils';

/**
* Special list of names for accounts based on the name of the service.
*/
enum WalletDisplayNames {
LEDGER = 'Ledger HSM',
}

export const Username = ({
className,
truncated,
username,
walletName,
}: {
className?: string;
truncated?: boolean;
username?: string;
walletName?: string;
}) => {
let name = username || 'Anonymous';
if (walletName !== undefined) {
const walletDisplayName = walletName.toUpperCase() as keyof typeof WalletDisplayNames;
name = WalletDisplayNames[walletDisplayName] ?? (username || 'Anonymous');
}
if (truncated) {
name = truncateString(name);
}

return (
<p className={className} title={username || 'Anonymous'}>
{name}
</p>
);
};
13 changes: 7 additions & 6 deletions components/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useChain } from '@cosmos-kit/react';
import { WalletStatus } from 'cosmos-kit';
import { MdWallet } from 'react-icons/md';
import env from '@/config/env';
import { Username } from './username';
import { truncateAddress, truncateString } from '@/utils';

const buttons = {
Expand Down Expand Up @@ -36,7 +37,7 @@ interface WalletSectionProps {
}

export const WalletSection: React.FC<WalletSectionProps> = ({ chainName }) => {
const { connect, openView, status, username, address } = useChain(chainName);
const { connect, openView, status, username, address, wallet } = useChain(chainName);

const [localStatus, setLocalStatus] = useState(status);
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
Expand Down Expand Up @@ -127,12 +128,12 @@ export const WalletSection: React.FC<WalletSectionProps> = ({ chainName }) => {
}}
>
<div className="relative z-10 h-full flex flex-col justify-between">
<p
<Username
className="font-medium text-xl text-center mb-2 truncate"
title={username || 'Connected user'}
>
{username ? truncateString(username, 20) : 'Connected User'}
</p>
username={username}
walletName={wallet?.prettyName}
truncated
/>
<div className="bg-base-100 dark:bg-base-200 rounded-full py-2 px-4 text-center mb-4 flex items-center flex-row justify-between w-full ">
<p className="text-xs truncate flex-grow">
{address ? truncateAddress(address) : 'Address not available'}
Expand Down

0 comments on commit 45ae9ba

Please sign in to comment.