Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECO-237: Basename: redirect to profile without domain if mainnet #730

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apps/web/src/components/Basenames/RegistrationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import {
} from 'apps/web/src/hooks/useAggregatedDiscountValidators';
import useBaseEnsName from 'apps/web/src/hooks/useBaseEnsName';
import useBasenameChain from 'apps/web/src/hooks/useBasenameChain';
import { Discount, isValidDiscount } from 'apps/web/src/utils/usernames';
import { Discount, formatBaseEthDomain, isValidDiscount } from 'apps/web/src/utils/usernames';
import { ActionType } from 'libs/base-ui/utils/logEvent';
import { useRouter } from 'next/navigation';
import {
Dispatch,
ReactNode,
SetStateAction,
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import { Address, TransactionReceipt } from 'viem';
import { base } from 'viem/chains';
import { useAccount, useWaitForTransactionReceipt } from 'wagmi';
import { useCallsStatus } from 'wagmi/experimental';

Expand All @@ -44,6 +47,7 @@ export type RegistrationContextProps = {
setRegisterNameTransactionHash: Dispatch<SetStateAction<`0x${string}` | undefined>>;
registerNameCallsBatchId: string;
setRegisterNameCallsBatchId: Dispatch<SetStateAction<string>>;
redirectToProfile: () => void;
loadingDiscounts: boolean;
discount: DiscountData | undefined;
allActiveDiscounts: Set<Discount>;
Expand Down Expand Up @@ -76,6 +80,9 @@ export const RegistrationContext = createContext<RegistrationContextProps>({
setRegisterNameCallsBatchId: function () {
return undefined;
},
redirectToProfile: function () {
return undefined;
},
loadingDiscounts: true,
discount: undefined,
allActiveDiscounts: new Set(),
Expand All @@ -101,6 +108,8 @@ export default function RegistrationProvider({ children }: RegistrationProviderP

const { basenameChain } = useBasenameChain();

const router = useRouter();

// Analytics
const { logEventWithContext } = useAnalytics();

Expand Down Expand Up @@ -156,6 +165,14 @@ export default function RegistrationProvider({ children }: RegistrationProviderP
},
});

const redirectToProfile = useCallback(() => {
if (basenameChain.id === base.id) {
router.push(`name/${selectedName}`);
} else {
router.push(`name/${formatBaseEthDomain(selectedName, basenameChain.id)}`);
}
}, [basenameChain.id, router, selectedName]);

useEffect(() => {
if (transactionIsFetching || callsIsFetching) {
logEventWithContext('register_name_transaction_processing', ActionType.change);
Expand Down Expand Up @@ -235,6 +252,7 @@ export default function RegistrationProvider({ children }: RegistrationProviderP
setRegisterNameTransactionHash,
registerNameCallsBatchId,
setRegisterNameCallsBatchId,
redirectToProfile,
loadingDiscounts,
discount,
allActiveDiscounts,
Expand All @@ -246,6 +264,7 @@ export default function RegistrationProvider({ children }: RegistrationProviderP
callsError,
discount,
loadingDiscounts,
redirectToProfile,
registerNameCallsBatchId,
registerNameTransactionHash,
registrationStep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import {
UsernameTextRecords,
UsernameTextRecordKeys,
textRecordsSocialFieldsEnabled,
formatBaseEthDomain,
} from 'apps/web/src/utils/usernames';
import classNames from 'classnames';
import { ActionType } from 'libs/base-ui/utils/logEvent';
import { useRouter } from 'next/navigation';
import { useCallback, useEffect, useState } from 'react';
import { useAccount, useWaitForTransactionReceipt } from 'wagmi';

Expand All @@ -37,9 +35,8 @@ export enum FormSteps {
export default function RegistrationProfileForm() {
const [currentFormStep, setCurrentFormStep] = useState<FormSteps>(FormSteps.Description);
const [transitionStep, setTransitionStep] = useState<boolean>(false);
const { selectedName } = useRegistration();
const { selectedName, redirectToProfile } = useRegistration();
const { address } = useAccount();
const router = useRouter();
const { logEventWithContext } = useAnalytics();
const { basenameChain } = useBasenameChain();
const { data: baseEnsName } = useBaseEnsName({
Expand Down Expand Up @@ -91,7 +88,7 @@ export default function RegistrationProfileForm() {

refetchExistingTextRecords()
.then(() => {
router.push(`name/${formatBaseEthDomain(selectedName, basenameChain.id)}`);
redirectToProfile();
})
.catch(() => {});
}
Expand All @@ -104,11 +101,11 @@ export default function RegistrationProfileForm() {
}, [
logEventWithContext,
refetchExistingTextRecords,
router,
baseEnsName,
transactionData,
selectedName,
basenameChain.id,
redirectToProfile,
]);

useEffect(() => {
Expand Down Expand Up @@ -165,7 +162,7 @@ export default function RegistrationProfileForm() {
logEventWithContext('update_text_records_transaction_approved', ActionType.change);
} else {
// no text records had to be updated, simply go to profile
router.push(`name/${formatBaseEthDomain(selectedName, basenameChain.id)}`);
redirectToProfile();
}
})
.catch(console.error);
Expand All @@ -174,11 +171,9 @@ export default function RegistrationProfileForm() {
event.preventDefault();
},
[
basenameChain.id,
currentFormStep,
logEventWithContext,
router,
selectedName,
redirectToProfile,
textRecords,
transitionFormOpacity,
writeTextRecords,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ import {
} from 'apps/web/src/components/Basenames/RegistrationContext';
import ShareUsernameModal from 'apps/web/src/components/Basenames/ShareUsernameModal';
import { Button, ButtonVariants } from 'apps/web/src/components/Button/Button';
import useBasenameChain from 'apps/web/src/hooks/useBasenameChain';
import { formatBaseEthDomain } from 'apps/web/src/utils/usernames';
import { ActionType } from 'libs/base-ui/utils/logEvent';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import React, { useCallback, useState } from 'react';

export default function RegistrationSuccessMessage() {
const { setRegistrationStep, selectedName } = useRegistration();
const router = useRouter();
const { setRegistrationStep, selectedName, redirectToProfile } = useRegistration();
const [isOpen, setIsOpen] = useState<boolean>(false);
const { logEventWithContext } = useAnalytics();
const { basenameChain } = useBasenameChain();
const openModal = useCallback(
(event: React.MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();
Expand All @@ -38,8 +33,8 @@ export default function RegistrationSuccessMessage() {

const goToProfileOnClick = useCallback(() => {
logEventWithContext('go_to_profile', ActionType.click);
router.push(`name/${formatBaseEthDomain(selectedName, basenameChain.id)}`);
}, [basenameChain.id, logEventWithContext, router, selectedName]);
redirectToProfile();
}, [logEventWithContext, redirectToProfile]);

return (
<>
Expand Down
Loading