Skip to content

Commit

Permalink
Resolve BENS names from URL directly to address page (#2004)
Browse files Browse the repository at this point in the history
* Resolve BENS names from URL directly to address page

Fixes #1965

* fix tests
  • Loading branch information
tom2drum authored Jun 13, 2024
1 parent d392e5c commit aeb16bf
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 36 deletions.
4 changes: 2 additions & 2 deletions lib/address/useAddressMetadataInfoQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useApiQuery from 'lib/api/useApiQuery';

import parseMetaPayload from './parseMetaPayload';

export default function useAddressMetadataInfoQuery(addresses: Array<string>) {
export default function useAddressMetadataInfoQuery(addresses: Array<string>, isEnabled = true) {

const resource = 'address_metadata_info';

Expand All @@ -16,7 +16,7 @@ export default function useAddressMetadataInfoQuery(addresses: Array<string>) {
tagsLimit: '20',
},
queryOptions: {
enabled: addresses.length > 0 && config.features.addressMetadata.isEnabled,
enabled: isEnabled && addresses.length > 0 && config.features.addressMetadata.isEnabled,
select: (data) => {
const addresses = Object.entries(data.addresses)
.map(([ address, { tags, reputation } ]) => {
Expand Down
3 changes: 3 additions & 0 deletions playwright/mocks/lib/growthbook/useFeatureValue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const useFeatureValue = (name, fallback) => {
try {
const value = JSON.parse(localStorage.getItem(`pw_feature:${ name }`));
if (value === null) {
throw new Error();
}
return { isLoading: false, value };
} catch (error) {
return { isLoading: false, value: fallback };
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressAccountHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ const getFilterValue = (getFilterValueFromQuery<NovesHistoryFilterValue>).bind(n
type Props = {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
}

const AddressAccountHistory = ({ scrollRef, shouldRender = true }: Props) => {
const AddressAccountHistory = ({ scrollRef, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const isMounted = useIsMounted();

Expand All @@ -42,6 +43,7 @@ const AddressAccountHistory = ({ scrollRef, shouldRender = true }: Props) => {
pathParams: { address: currentAddress },
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'noves_address_history'>(NOVES_TRANSLATE, 10, { hasNextPage: false, pageNumber: 1, pageSize: 10 }),
},
});
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressBlocksValidated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import AddressBlocksValidatedTableItem from './blocksValidated/AddressBlocksVali
interface Props {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
}

const AddressBlocksValidated = ({ scrollRef, shouldRender = true }: Props) => {
const AddressBlocksValidated = ({ scrollRef, shouldRender = true, isQueryEnabled = true }: Props) => {
const [ socketAlert, setSocketAlert ] = React.useState(false);
const queryClient = useQueryClient();
const router = useRouter();
Expand All @@ -41,6 +42,7 @@ const AddressBlocksValidated = ({ scrollRef, shouldRender = true }: Props) => {
pathParams: { hash: addressHash },
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'address_blocks_validated'>(
BLOCK,
50,
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressCoinBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import AddressCoinBalanceHistory from './coinBalance/AddressCoinBalanceHistory';

type Props = {
shouldRender?: boolean;
isQueryEnabled?: boolean;
}

const AddressCoinBalance = ({ shouldRender = true }: Props) => {
const AddressCoinBalance = ({ shouldRender = true, isQueryEnabled = true }: Props) => {
const [ socketAlert, setSocketAlert ] = React.useState(false);
const queryClient = useQueryClient();
const router = useRouter();
Expand All @@ -36,6 +37,7 @@ const AddressCoinBalance = ({ shouldRender = true }: Props) => {
pathParams: { hash: addressHash },
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'address_coin_balance'>(
ADDRESS_COIN_BALANCE,
50,
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressInternalTxs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const getFilterValue = (getFilterValueFromQuery<AddressFromToFilter>).bind(null,
type Props = {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
}
const AddressInternalTxs = ({ scrollRef, shouldRender = true }: Props) => {
const AddressInternalTxs = ({ scrollRef, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const isMounted = useIsMounted();

Expand All @@ -41,6 +42,7 @@ const AddressInternalTxs = ({ scrollRef, shouldRender = true }: Props) => {
filters: { filter: filterValue },
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'address_internal_txs'>(
INTERNAL_TX,
50,
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import AddressCsvExportLink from './AddressCsvExportLink';
type Props ={
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
}

const AddressLogs = ({ scrollRef, shouldRender = true }: Props) => {
const AddressLogs = ({ scrollRef, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const isMounted = useIsMounted();

Expand All @@ -28,6 +29,7 @@ const AddressLogs = ({ scrollRef, shouldRender = true }: Props) => {
pathParams: { hash },
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'address_logs'>(LOG, 3, { next_page_params: {
block_number: 9005750,
index: 42,
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressTokenTransfers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ const matchFilters = (filters: Filters, tokenTransfer: TokenTransfer, address?:
type Props = {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
// for tests only
overloadCount?: number;
}

const AddressTokenTransfers = ({ scrollRef, overloadCount = OVERLOAD_COUNT, shouldRender = true }: Props) => {
const AddressTokenTransfers = ({ scrollRef, overloadCount = OVERLOAD_COUNT, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const queryClient = useQueryClient();
const isMobile = useIsMobile();
Expand All @@ -95,6 +96,7 @@ const AddressTokenTransfers = ({ scrollRef, overloadCount = OVERLOAD_COUNT, shou
filters: tokenFilter ? { token: tokenFilter } : filters,
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: getTokenTransfersStub(undefined, {
block_number: 7793535,
index: 46,
Expand Down
9 changes: 5 additions & 4 deletions ui/address/AddressTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ const getTokenFilterValue = (getFilterValuesFromQuery<NFTTokenType>).bind(null,

type Props = {
shouldRender?: boolean;
isQueryEnabled?: boolean;
}

const AddressTokens = ({ shouldRender = true }: Props) => {
const AddressTokens = ({ shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const isMobile = useIsMobile();
const isMounted = useIsMounted();
Expand All @@ -67,7 +68,7 @@ const AddressTokens = ({ shouldRender = true }: Props) => {
filters: { type: 'ERC-20' },
scrollRef,
options: {
enabled: !tab || tab === 'tokens_erc20',
enabled: isQueryEnabled && (!tab || tab === 'tokens_erc20'),
refetchOnMount: false,
placeholderData: generateListStub<'address_tokens'>(ADDRESS_TOKEN_BALANCE_ERC_20, 10, { next_page_params: null }),
},
Expand All @@ -78,7 +79,7 @@ const AddressTokens = ({ shouldRender = true }: Props) => {
pathParams: { hash },
scrollRef,
options: {
enabled: tab === 'tokens_nfts' && nftDisplayType === 'collection',
enabled: isQueryEnabled && tab === 'tokens_nfts' && nftDisplayType === 'collection',
placeholderData: generateListStub<'address_collections'>(ADDRESS_COLLECTION, 10, { next_page_params: null }),
},
filters: { type: tokenTypes },
Expand All @@ -89,7 +90,7 @@ const AddressTokens = ({ shouldRender = true }: Props) => {
pathParams: { hash },
scrollRef,
options: {
enabled: tab === 'tokens_nfts' && nftDisplayType === 'list',
enabled: isQueryEnabled && tab === 'tokens_nfts' && nftDisplayType === 'list',
placeholderData: generateListStub<'address_nfts'>(ADDRESS_NFT_1155, 10, { next_page_params: null }),
},
filters: { type: tokenTypes },
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressTxs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ const matchFilter = (filterValue: AddressFromToFilter, transaction: Transaction,
type Props = {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
// for tests only
overloadCount?: number;
}

const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT, shouldRender = true }: Props) => {
const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const queryClient = useQueryClient();
const isMounted = useIsMounted();
Expand All @@ -74,6 +75,7 @@ const AddressTxs = ({ scrollRef, overloadCount = OVERLOAD_COUNT, shouldRender =
sorting: getSortParamsFromValue<TransactionsSortingValue, TransactionsSortingField, TransactionsSorting['order']>(sort),
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'address_txs'>(TX, 50, { next_page_params: {
block_number: 9005713,
index: 5,
Expand Down
5 changes: 3 additions & 2 deletions ui/address/AddressUserOps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import UserOpsContent from 'ui/userOps/UserOpsContent';
type Props = {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
}

const AddressUserOps = ({ scrollRef, shouldRender = true }: Props) => {
const AddressUserOps = ({ scrollRef, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const isMounted = useIsMounted();

Expand All @@ -23,7 +24,7 @@ const AddressUserOps = ({ scrollRef, shouldRender = true }: Props) => {
resourceName: 'user_ops',
scrollRef,
options: {
enabled: Boolean(hash),
enabled: isQueryEnabled && Boolean(hash),
placeholderData: generateListStub<'user_ops'>(USER_OPS_ITEM, 50, { next_page_params: {
page_token: '10355938,0x5956a847d8089e254e02e5111cad6992b99ceb9e5c2dc4343fd53002834c4dc6',
page_size: 50,
Expand Down
4 changes: 3 additions & 1 deletion ui/address/AddressWithdrawals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import BeaconChainWithdrawalsTable from 'ui/withdrawals/beaconChain/BeaconChainW
type Props = {
scrollRef?: React.RefObject<HTMLDivElement>;
shouldRender?: boolean;
isQueryEnabled?: boolean;
}
const AddressWithdrawals = ({ scrollRef, shouldRender = true }: Props) => {
const AddressWithdrawals = ({ scrollRef, shouldRender = true, isQueryEnabled = true }: Props) => {
const router = useRouter();
const isMounted = useIsMounted();

Expand All @@ -28,6 +29,7 @@ const AddressWithdrawals = ({ scrollRef, shouldRender = true }: Props) => {
pathParams: { hash },
scrollRef,
options: {
enabled: isQueryEnabled,
placeholderData: generateListStub<'address_withdrawals'>(WITHDRAWAL, 50, { next_page_params: {
index: 5,
items_count: 50,
Expand Down
5 changes: 3 additions & 2 deletions ui/address/utils/useAddressQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ export type AddressQuery = UseQueryResult<Address, ResourceError<{ status: numbe

interface Params {
hash: string;
isEnabled?: boolean;
}

export default function useAddressQuery({ hash }: Params): AddressQuery {
export default function useAddressQuery({ hash, isEnabled = true }: Params): AddressQuery {
const [ isRefetchEnabled, setRefetchEnabled ] = React.useState(false);

const apiQuery = useApiQuery<'address', { status: number }>('address', {
pathParams: { hash },
queryOptions: {
enabled: Boolean(hash),
enabled: isEnabled && Boolean(hash),
placeholderData: ADDRESS_INFO,
refetchOnMount: false,
retry: (failureCount, error) => {
Expand Down
46 changes: 46 additions & 0 deletions ui/address/utils/useCheckDomainNameParam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useRouter } from 'next/router';
import React from 'react';

import config from 'configs/app';
import useApiQuery from 'lib/api/useApiQuery';

const DOMAIN_NAME_REGEXP = /.\../;

export default function useCheckDomainNameParam(hashOrDomainName: string) {
const router = useRouter();
const maybeDomainName = DOMAIN_NAME_REGEXP.test(hashOrDomainName);
const isQueryEnabled = config.features.nameService.isEnabled && maybeDomainName;
const [ isLoading, setIsLoading ] = React.useState(isQueryEnabled);

const domainLookupQuery = useApiQuery('domains_lookup', {
pathParams: { chainId: config.chain.id },
queryParams: {
name: hashOrDomainName,
only_active: false,
},
queryOptions: {
enabled: isQueryEnabled,
},
});

React.useEffect(() => {
if (domainLookupQuery.isPending) {
return;
}

const firstDomainAddress = domainLookupQuery.data?.items[0]?.resolved_address?.hash;
if (firstDomainAddress) {
router.replace({ pathname: '/address/[hash]', query: { hash: firstDomainAddress } });
} else {
setIsLoading(false);
}
}, [ domainLookupQuery.isPending, domainLookupQuery.data, router ]);

React.useEffect(() => {
if (!maybeDomainName) {
setIsLoading(false);
}
}, [ maybeDomainName ]);

return isLoading;
}
Loading

0 comments on commit aeb16bf

Please sign in to comment.