Skip to content

Commit

Permalink
client as props + domainName id dataFromObject
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed May 16, 2024
1 parent d657423 commit 75516e5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
13 changes: 5 additions & 8 deletions web-frontend/src/custom/useMNS.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import { useCallback, useState } from 'react';

import { Args, ICallData } from '@massalabs/massa-web3';
import { Args, Client, ICallData } from '@massalabs/massa-web3';
import { bytesToStr } from '@massalabs/web3-utils';

import { usePrepareScCall } from './usePrepareScCall';
import { contracts } from '@/utils/const';

export function useMNS() {
export function useMNS(client: Client | undefined) {
const [targetMnsAddress, setTargetMnsAddress] = useState<string>('');
const [domainName, setDomainName] = useState<string>('');

const { client, account, isMainnet } = usePrepareScCall();
const { isMainnet } = usePrepareScCall();

const targetContractAddress = isMainnet
? contracts.mainnet.mnsContract
: contracts.buildnet.mnsContract;

const reverseResolveDns = useCallback(
async (address = '') => {
const targetAddress = address || account?.address();
if (!targetAddress) return;

async (targetAddress = '') => {
const reverseResolveCallData = {
targetAddress: targetContractAddress,
targetFunction: 'dnsReverseResolve',
Expand All @@ -38,7 +35,7 @@ export function useMNS() {
console.error('Reverse DNS resolution failed:', e);
}
},
[client, account?.address, targetContractAddress],
[client, targetContractAddress],
);

const resolveDns = useCallback(
Expand Down
12 changes: 7 additions & 5 deletions web-frontend/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Loading } from './Loading';
import { TAB_SEND, TAB_RECEIVE } from '@/const/tabs/tabs';
import { useResource } from '@/custom/api';
import { useMNS } from '@/custom/useMNS';
import { usePrepareScCall } from '@/custom/usePrepareScCall';
import Intl from '@/i18n/i18n';
import { WalletLayout, MenuItem } from '@/layouts/WalletLayout/WalletLayout';
import { AccountObject } from '@/models/AccountModel';
Expand All @@ -27,10 +28,12 @@ export default function Home() {
data: account,
isLoading,
} = useResource<AccountObject>(`accounts/${nickname}`);
const { reverseResolveDns, domainName } = useMNS();
const { client } = usePrepareScCall();
const { reverseResolveDns, domainName } = useMNS(client);
const accountAddress = account?.address ?? '';

useEffect(() => {
reverseResolveDns();
reverseResolveDns(accountAddress);
}, [reverseResolveDns, nickname]);

Check warning on line 37 in web-frontend/src/pages/Home/Home.tsx

View workflow job for this annotation

GitHub Actions / lint-web-frontend

React Hook useEffect has a missing dependency: 'accountAddress'. Either include it or remove the dependency array

useEffect(() => {
Expand All @@ -46,7 +49,6 @@ export default function Home() {
const formattedBalance = formatAmount(
balance.toString(),
).amountFormattedPreview;
const address = account?.address ?? '';

return (
<WalletLayout menuItem={MenuItem.Home}>
Expand Down Expand Up @@ -93,8 +95,8 @@ export default function Home() {
<div className="flex w-full justify-between items-center">
{domainName && <Mns mns={domainName} />}
<Clipboard
displayedContent={maskAddress(address)}
rawContent={address}
displayedContent={maskAddress(accountAddress)}
rawContent={accountAddress}
error={Intl.t('errors.no-content-to-copy')}
className="flex flex-row items-center mas-body2 justify-between
w-fit h-12 px-3 rounded bg-primary cursor-pointer"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useEffect } from 'react';

import {
Balance,
Button,
Expand All @@ -13,7 +11,6 @@ import {
import { FiChevronLeft } from 'react-icons/fi';

import { PRESET_HIGH, PRESET_LOW, PRESET_STANDARD } from './Advanced';
import { useMNS } from '@/custom/useMNS';
import { usePrepareScCall } from '@/custom/usePrepareScCall';
import Intl from '@/i18n/i18n';
import { Asset } from '@/models/AssetModel';
Expand All @@ -25,6 +22,7 @@ export interface SendConfirmationData {
asset: Asset;
fees: string;
recipientAddress: string;
recipientDomainName?: string;
}

interface SendConfirmationProps {
Expand Down Expand Up @@ -55,12 +53,6 @@ export function SendConfirmation(props: SendConfirmationProps) {
decimals,
).amountFormattedFull;

const { reverseResolveDns, domainName } = useMNS();

useEffect(() => {
reverseResolveDns(recipientAddress);
}, [reverseResolveDns, recipientAddress]);

let selectedFees;

switch (fees) {
Expand Down Expand Up @@ -137,7 +129,7 @@ export function SendConfirmation(props: SendConfirmationProps) {
w-fit h-fit px-3 py-1 rounded bg-primary cursor-pointer"
/>
</div>
{domainName && <Mns mns={domainName} />}
{data.recipientDomainName && <Mns mns={data.recipientDomainName} />}
</div>
<Button disabled={isLoading} onClick={() => handleConfirm(true)}>
{Intl.t('send-coins.confirm-sign')}
Expand Down
10 changes: 8 additions & 2 deletions web-frontend/src/pages/TransferCoins/SendCoins/SendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FiArrowUpRight, FiPlus } from 'react-icons/fi';

import { MAS } from '@/const/assets/assets';
import { useMNS } from '@/custom/useMNS';
import { usePrepareScCall } from '@/custom/usePrepareScCall';
import Intl from '@/i18n/i18n';
import { AccountObject } from '@/models/AccountModel';
import { Asset } from '@/models/AssetModel';
Expand Down Expand Up @@ -77,7 +78,10 @@ export function SendForm(props: SendFormProps) {
const balance = BigInt(selectedAsset?.balance || '0');
const mnsExtension = /\.massa$/;

const { resolveDns, targetMnsAddress, resetTargetMnsAddress } = useMNS();
const { client } = usePrepareScCall();

const { resolveDns, targetMnsAddress, resetTargetMnsAddress } =
useMNS(client);

useEffect(() => {
if (!sendOpData) {
Expand Down Expand Up @@ -139,9 +143,11 @@ export function SendForm(props: SendFormProps) {
let formObject = parseForm(e);

if (targetMnsAddress && mnsExtension.test(recipient)) {
// recipient is a domain name
formObject = {
...formObject,
recipientAddress: targetMnsAddress,
recipientAddress: targetMnsAddress, // set the resolved address
recipientDomainName: recipient.replace(mnsExtension, ''), // set the domain name
};
} else {
setMnsAddressCorrelation(false);
Expand Down

0 comments on commit 75516e5

Please sign in to comment.