Skip to content

Commit

Permalink
add identi icon (#133)
Browse files Browse the repository at this point in the history
* add identi icon

* refactor address input
  • Loading branch information
cuteolaf authored Jun 4, 2024
1 parent a3bf349 commit 6467cec
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 83 deletions.
41 changes: 41 additions & 0 deletions src/components/Elements/Address/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { IconButton, Stack } from '@mui/material';
import Identicon from '@polkadot/react-identicon';

import { truncateAddres, writeToClipboard } from '@/utils/functions';

import { useToast } from '@/contexts/toast';

interface AddressProps {
value: string;
isShort?: boolean;
isCopy?: boolean;
size?: number;
}

export const Address = ({
value,
isShort,
isCopy,
size = 32,
}: AddressProps) => {
const { toastInfo } = useToast();

const onCopy = () => {
if (!isCopy) return;

const asyncCopy = async () => {
await writeToClipboard(value);
toastInfo('Address copied');
};
asyncCopy();
};

return (
<Stack direction='row' gap='0.5rem' alignItems='center'>
<IconButton onClick={onCopy}>
<Identicon value={value} theme='polkadot' size={size} />
</IconButton>
<p>{isShort ? truncateAddres(value) : value}</p>
</Stack>
);
};
71 changes: 71 additions & 0 deletions src/components/Elements/Inputs/AddressInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Box,
Button,
IconButton,
Input,
InputAdornment,
Typography,
} from '@mui/material';
import Identicon from '@polkadot/react-identicon';

import { isValidAddress, writeToClipboard } from '@/utils/functions';

import { useAccounts } from '@/contexts/account';
import { useToast } from '@/contexts/toast';

export interface AddressInputProps {
onChange: (_: string) => void;
address: string;
}

export const AddressInput = ({ onChange, address }: AddressInputProps) => {
const {
state: { activeAccount },
} = useAccounts();
const { toastInfo } = useToast();

const isValid = isValidAddress(address);

const onCopy = () => {
const asyncCopy = async () => {
await writeToClipboard(address);
toastInfo('Address copied');
};
asyncCopy();
};

return (
<Box>
<Typography>Recipient</Typography>
<Input
value={address}
onChange={(e) => onChange(e.target.value)}
fullWidth
type='text'
placeholder='Address of the recipient'
startAdornment={
isValid ? (
<IconButton onClick={onCopy}>
<Identicon value={address} theme='polkadot' size={24} />
</IconButton>
) : (
<></>
)
}
endAdornment={
<InputAdornment position='end'>
<Button
variant='text'
color='info'
onClick={() => activeAccount && onChange(activeAccount.address)}
>
Me
</Button>
</InputAdornment>
}
sx={{ height: '3rem' }}
error={address.length > 0 && !isValid}
/>
</Box>
);
};
47 changes: 0 additions & 47 deletions src/components/Elements/Inputs/RecipientInput/index.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/Elements/Inputs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './AddressInput';
export * from './AmountInput';
export * from './FileInput';
export * from './RecipientInput';
1 change: 1 addition & 0 deletions src/components/Elements/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Address';
export * from './Balance';
export * from './Banner';
export * from './Buttons';
Expand Down
30 changes: 2 additions & 28 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { ExpandMore } from '@mui/icons-material';
import {
Box,
Button,
Collapse,
Divider,
List,
ListItemButton,
useTheme,
} from '@mui/material';
import Identicon from '@polkadot/react-identicon';
import React, { useState } from 'react';

import { KeyringState, useAccounts } from '@/contexts/account';
import { useToast } from '@/contexts/toast';

import styles from './index.module.scss';
import { ProgressButton } from '../Elements';
import { Address, ProgressButton } from '../Elements';
import NetworkSelector from '../Elements/Selectors/NetworkSelector';

export const Header = () => {
Expand All @@ -28,26 +25,12 @@ export const Header = () => {
} = useAccounts();

const [accountsOpen, openAccounts] = useState(false);
const { toastInfo } = useToast();

const onDisconnect = () => {
openAccounts(false);
disconnectWallet();
};

const truncateAddres = (address: string) => {
return (
address.substring(0, 6) + '...' + address.substring(address.length - 6)
);
};

const copyAddress = async () => {
if (!activeAccount) return;

await navigator.clipboard.writeText(activeAccount.address);
toastInfo('Address copied');
};

return (
<>
<Box
Expand All @@ -60,16 +43,7 @@ export const Header = () => {
>
<Box className={styles.menu}>
{activeAccount && (
<Box display='flex' alignItems='center' sx={{ margin: '1rem' }}>
<Button onClick={copyAddress}>
<Identicon
value={activeAccount.address}
theme='polkadot'
size={32}
/>
</Button>
<p>{truncateAddres(activeAccount.address)}</p>
</Box>
<Address value={activeAccount.address} isCopy isShort />
)}
<div>
<NetworkSelector />
Expand Down
7 changes: 2 additions & 5 deletions src/components/Modals/Regions/Sell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { Region } from 'coretime-utils';
import { useEffect, useState } from 'react';

import { AmountInput, RecipientInput } from '@/components/Elements';
import { AddressInput, AmountInput } from '@/components/Elements';
import { RegionMetaCard } from '@/components/Regions';

import { useAccounts } from '@/contexts/account';
Expand Down Expand Up @@ -123,10 +123,7 @@ export const SellModal = ({
/>
</Stack>
<Stack direction='column' gap={2}>
<RecipientInput
setRecipient={setSaleRecipient}
recipient={saleRecipient}
/>
<AddressInput onChange={setSaleRecipient} address={saleRecipient} />
</Stack>
</Stack>
</DialogContent>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
} from '@/utils/transfers/native';

import {
AddressInput,
AmountInput,
Balance,
ChainSelector,
ProgressButton,
RecipientInput,
RegionMetaCard,
RegionSelector,
} from '@/components';
Expand Down Expand Up @@ -435,7 +435,7 @@ const TransferPage = () => {
>
Transfer to:
</Typography>
<RecipientInput recipient={newOwner} setRecipient={setNewOwner} />
<AddressInput address={newOwner} onChange={setNewOwner} />
</Stack>
{asset === AssetType.TOKEN &&
originChain !== ChainType.NONE &&
Expand Down
10 changes: 10 additions & 0 deletions src/utils/functions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ export const isValidAddress = (chainAddress: string, ss58Prefix = 42) => {
return false;
}
};

export const truncateAddres = (address: string) => {
return (
address.substring(0, 6) + '...' + address.substring(address.length - 6)
);
};

export const writeToClipboard = async (value: string) => {
await navigator.clipboard.writeText(value);
};

0 comments on commit 6467cec

Please sign in to comment.