Files
explore/ui/address/utils/useCheckDomainNameParam.ts
tom goriunovandzeekay aa349ab54f Name service: use protocol list instead of chain ID (#3283)
* Name service: use protocol list instead of chain ID

Resolves #3281

* fix envs for demo
2026-02-24 01:12:57 -08:00

49 lines
1.4 KiB
TypeScript

import { useRouter } from 'next/router';
import React from 'react';
import config from 'configs/app';
import useApiQuery from 'lib/api/useApiQuery';
const DOMAIN_NAME_REGEXP = /.\../;
const feature = config.features.nameServices;
export default function useCheckDomainNameParam(hashOrDomainName: string) {
const router = useRouter();
const maybeDomainName = DOMAIN_NAME_REGEXP.test(hashOrDomainName);
const isQueryEnabled = feature.isEnabled && feature.ens.isEnabled && maybeDomainName;
const [ isLoading, setIsLoading ] = React.useState(isQueryEnabled);
const domainLookupQuery = useApiQuery('bens:domains_lookup', {
queryParams: {
name: hashOrDomainName,
protocols: feature.isEnabled && feature.ens.isEnabled ? feature.ens.protocols : undefined,
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;
}