Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkas committed Aug 26, 2024
1 parent 5997b7a commit e0af666
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function UsernameProfileSettingsName() {
isLoading: setPrimaryNameIsLoading,
canSetUsernameAsPrimary,
} = useSetPrimaryBasename({
basename: profileUsername,
secondaryUsername: profileUsername,
});

// Error & Analytics
Expand Down
30 changes: 21 additions & 9 deletions apps/web/src/hooks/useSetPrimaryBasename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import { useUsernameProfile } from 'apps/web/src/components/Basenames/UsernamePr
*/

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

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

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

// Get current primary username
// Note: This is sometimes undefined
Expand All @@ -43,12 +43,12 @@ export default function useSetPrimaryBasename({ basename }: UseSetPrimaryBasenam
address: address,
});

const usernamesDiffer = basename !== primaryUsername;
const usernamesDiffer = secondaryUsername !== primaryUsername;
const canSetUsernameAsPrimary = usernamesDiffer && currentWalletIsProfileOwner;

const { initiateTransaction, transactionIsLoading, transactionIsSuccess } =
useWriteContractWithReceipt({
chain: secondaryBaseChain,
chain: secondaryUsernameChain,
eventName: 'update_primary_name',
});

Expand All @@ -62,24 +62,36 @@ export default function useSetPrimaryBasename({ basename }: UseSetPrimaryBasenam

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

// No user is connected
if (!address) return;

try {
await initiateTransaction({
abi: ReverseRegistrarAbi,
address: USERNAME_REVERSE_REGISTRAR_ADDRESSES[secondaryBaseChain.id],
args: [address, address, USERNAME_L2_RESOLVER_ADDRESSES[secondaryBaseChain.id], basename],
address: USERNAME_REVERSE_REGISTRAR_ADDRESSES[secondaryUsernameChain.id],
args: [
address,
address,
USERNAME_L2_RESOLVER_ADDRESSES[secondaryUsernameChain.id],
secondaryUsername,
],
functionName: 'setNameForAddr',
});
} catch (error) {
logError(error, 'Set primary name transaction canceled');
}

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

const isLoading = transactionIsLoading || primaryUsernameIsLoading || primaryUsernameIsFetching;

Expand Down

0 comments on commit e0af666

Please sign in to comment.