Skip to content

Commit

Permalink
fix conditions in useSetPrimaryName
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkas committed Aug 26, 2024
1 parent ddfd3cc commit 5997b7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Button, ButtonSizes, ButtonVariants } from 'apps/web/src/components/But

export default function UsernameProfileSettingsName() {
// Profile username
const { profileUsername, profileAddress, currentWalletIsProfileAddress } = useUsernameProfile();
const { profileUsername, profileAddress, currentWalletIsProfileOwner } = useUsernameProfile();

// Primary username
const { data: primaryUsername } = useBaseEnsName({
Expand All @@ -21,7 +21,7 @@ export default function UsernameProfileSettingsName() {
isLoading: setPrimaryNameIsLoading,
canSetUsernameAsPrimary,
} = useSetPrimaryBasename({
secondaryName: profileUsername,
basename: profileUsername,
});

// Error & Analytics
Expand All @@ -33,8 +33,8 @@ export default function UsernameProfileSettingsName() {
});
}, [logError, setPrimaryName]);

const isPrimaryName = currentWalletIsProfileAddress && profileUsername === primaryUsername;
const isSecondaryName = currentWalletIsProfileAddress && profileUsername !== primaryUsername;
const isPrimaryName = currentWalletIsProfileOwner && profileUsername === primaryUsername;
const isSecondaryName = currentWalletIsProfileOwner && profileUsername !== primaryUsername;

return (
<div className="flex flex-col gap-4">
Expand Down
50 changes: 14 additions & 36 deletions apps/web/src/hooks/useSetPrimaryBasename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
import useBasenameChain from 'apps/web/src/hooks/useBasenameChain';
import { useCallback, useEffect } from 'react';
import { BaseName } from '@coinbase/onchainkit/identity';
import { useAccount, useEnsAddress } from 'wagmi';
import { useAccount } from 'wagmi';
import useBaseEnsName from 'apps/web/src/hooks/useBaseEnsName';
import { useErrors } from 'apps/web/contexts/Errors';

import useWriteContractWithReceipt from 'apps/web/src/hooks/useWriteContractWithReceipt';
import { useUsernameProfile } from 'apps/web/src/components/Basenames/UsernameProfileContext';

/*
A hook to set an name as primary for resolution.
Expand All @@ -22,13 +22,18 @@ import useWriteContractWithReceipt from 'apps/web/src/hooks/useWriteContractWith
*/

type UseSetPrimaryBasenameProps = {
secondaryName: BaseName;
basename: BaseName;
};

export default function useSetPrimaryBasename({ secondaryName }: UseSetPrimaryBasenameProps) {
export default function useSetPrimaryBasename({ basename }: UseSetPrimaryBasenameProps) {
const { address } = useAccount();
const { logError } = useErrors();

const { currentWalletIsProfileOwner } = useUsernameProfile();
const { basenameChain: secondaryBaseChain } = useBasenameChain(basename);

// Get current primary username
// Note: This is sometimes undefined
const {
data: primaryUsername,
refetch: refetchPrimaryUsername,
Expand All @@ -38,23 +43,8 @@ export default function useSetPrimaryBasename({ secondaryName }: UseSetPrimaryBa
address: address,
});

const { basenameChain: secondaryBaseChain } = useBasenameChain(secondaryName);
const { basenameChain: primaryBaseChain } = useBasenameChain(primaryUsername);

const { data: secondaryAddress } = useEnsAddress({
name: secondaryName,
universalResolverAddress: USERNAME_L2_RESOLVER_ADDRESSES[secondaryBaseChain.id],
query: {
refetchOnWindowFocus: false,
},
});

const usernamesHaveSameOwner = secondaryAddress === address;
const usernamesOnSameChains = secondaryBaseChain.id === primaryBaseChain.id;
const usernamesDiffer = secondaryName !== primaryUsername;

const canSetUsernameAsPrimary =
usernamesDiffer && usernamesOnSameChains && usernamesHaveSameOwner;
const usernamesDiffer = basename !== primaryUsername;
const canSetUsernameAsPrimary = usernamesDiffer && currentWalletIsProfileOwner;

const { initiateTransaction, transactionIsLoading, transactionIsSuccess } =
useWriteContractWithReceipt({
Expand All @@ -72,7 +62,7 @@ export default function useSetPrimaryBasename({ secondaryName }: UseSetPrimaryBa

const setPrimaryName = useCallback(async () => {
// Already primary
if (secondaryName === primaryUsername) return;
if (basename === primaryUsername) return;

// No user is connected
if (!address) return;
Expand All @@ -81,27 +71,15 @@ export default function useSetPrimaryBasename({ secondaryName }: UseSetPrimaryBa
await initiateTransaction({
abi: ReverseRegistrarAbi,
address: USERNAME_REVERSE_REGISTRAR_ADDRESSES[secondaryBaseChain.id],
args: [
address,
address,
USERNAME_L2_RESOLVER_ADDRESSES[secondaryBaseChain.id],
secondaryName,
],
args: [address, address, USERNAME_L2_RESOLVER_ADDRESSES[secondaryBaseChain.id], basename],
functionName: 'setNameForAddr',
});
} catch (error) {
logError(error, 'Set primary name transaction canceled');
}

return true;
}, [
secondaryName,
primaryUsername,
address,
initiateTransaction,
secondaryBaseChain.id,
logError,
]);
}, [basename, primaryUsername, address, initiateTransaction, secondaryBaseChain.id, logError]);

const isLoading = transactionIsLoading || primaryUsernameIsLoading || primaryUsernameIsFetching;

Expand Down

0 comments on commit 5997b7a

Please sign in to comment.