Commit cab4c85d authored by tom's avatar tom

refactor the rest

parent 036dcde5
import { useRouter } from 'next/router';
export default function useBasePath() {
const router = useRouter();
return `/${ router.query.network_type }/${ router.query.network_sub_type }`;
}
import isBrowser from 'lib/isBrowser';
import { ROUTES } from './routes'; import { ROUTES } from './routes';
import type { RouteName } from './routes'; import type { RouteName } from './routes';
...@@ -14,7 +16,7 @@ export function link(routeName: RouteName, urlParams?: Record<string, string | u ...@@ -14,7 +16,7 @@ export function link(routeName: RouteName, urlParams?: Record<string, string | u
return paramValue ? `/${ paramValue }` : ''; return paramValue ? `/${ paramValue }` : '';
}); });
const url = new URL(path, window.location.origin); const url = new URL(path, isBrowser() ? window.location.origin : 'https://blockscout.com');
queryParams && Object.entries(queryParams).forEach(([ key, value ]) => { queryParams && Object.entries(queryParams).forEach(([ key, value ]) => {
url.searchParams.append(key, value); url.searchParams.append(key, value);
......
...@@ -76,15 +76,30 @@ export const ROUTES = { ...@@ -76,15 +76,30 @@ export const ROUTES = {
pattern: `${ BASE_PATH }/tokens`, pattern: `${ BASE_PATH }/tokens`,
crossNetworkNavigation: true, crossNetworkNavigation: true,
}, },
token_index: {
pattern: `${ BASE_PATH }/token/[id]`,
crossNetworkNavigation: true,
},
// ADDRESSES
address_index: {
pattern: `${ BASE_PATH }/address/[id]`,
crossNetworkNavigation: true,
},
// APPS // APPS
apps: { apps: {
pattern: `${ BASE_PATH }/apps`, pattern: `${ BASE_PATH }/apps`,
}, },
// SEARCH
search_results: {
pattern: `${ BASE_PATH }/apps`,
},
// ??? what URL will be here // ??? what URL will be here
other: { other: {
pattern: `${ BASE_PATH }/other`, pattern: `${ BASE_PATH }/search-results`,
}, },
}; };
......
...@@ -4,8 +4,8 @@ import React from 'react'; ...@@ -4,8 +4,8 @@ import React from 'react';
import type { FunctionComponent, SVGAttributes } from 'react'; import type { FunctionComponent, SVGAttributes } from 'react';
import blockscoutLogo from 'icons/logo.svg'; import blockscoutLogo from 'icons/logo.svg';
import useBasePath from 'lib/hooks/useBasePath';
import useNetwork from 'lib/hooks/useNetwork'; import useNetwork from 'lib/hooks/useNetwork';
import useLink from 'lib/link/useLink';
import getDefaultTransitionProps from 'theme/utils/getDefaultTransitionProps'; import getDefaultTransitionProps from 'theme/utils/getDefaultTransitionProps';
interface Props { interface Props {
...@@ -15,7 +15,8 @@ interface Props { ...@@ -15,7 +15,8 @@ interface Props {
const NetworkLogo = ({ isCollapsed, onClick }: Props) => { const NetworkLogo = ({ isCollapsed, onClick }: Props) => {
const logoColor = useColorModeValue('blue.600', 'white'); const logoColor = useColorModeValue('blue.600', 'white');
const href = useBasePath(); const link = useLink();
const href = link('network_index');
const network = useNetwork(); const network = useNetwork();
const logo = network?.logo; const logo = network?.logo;
......
import type { ChangeEvent, FormEvent } from 'react'; import type { ChangeEvent, FormEvent } from 'react';
import React from 'react'; import React from 'react';
import useBasePath from 'lib/hooks/useBasePath';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
import useLink from 'lib/link/useLink';
import SearchBarDesktop from './SearchBarDesktop'; import SearchBarDesktop from './SearchBarDesktop';
import SearchBarMobile from './SearchBarMobile'; import SearchBarMobile from './SearchBarMobile';
const SearchBar = () => { const SearchBar = () => {
const [ value, setValue ] = React.useState(''); const [ value, setValue ] = React.useState('');
const basePath = useBasePath(); const link = useLink();
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const handleChange = React.useCallback((event: ChangeEvent<HTMLInputElement>) => { const handleChange = React.useCallback((event: ChangeEvent<HTMLInputElement>) => {
...@@ -18,8 +18,9 @@ const SearchBar = () => { ...@@ -18,8 +18,9 @@ const SearchBar = () => {
const handleSubmit = React.useCallback((event: FormEvent<HTMLFormElement>) => { const handleSubmit = React.useCallback((event: FormEvent<HTMLFormElement>) => {
event.preventDefault(); event.preventDefault();
window.location.assign(`https://blockscout.com${ basePath }/search-results?q=${ value }`); const url = link('search_results', undefined, { q: value });
}, [ value, basePath ]); window.location.assign(url);
}, [ link, value ]);
if (isMobile) { if (isMobile) {
return ( return (
......
...@@ -8,7 +8,7 @@ import { ...@@ -8,7 +8,7 @@ import {
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import useBasePath from 'lib/hooks/useBasePath'; import useLink from 'lib/link/useLink';
import PrivateAddressTags from 'ui/privateTags/PrivateAddressTags'; import PrivateAddressTags from 'ui/privateTags/PrivateAddressTags';
import PrivateTransactionTags from 'ui/privateTags/PrivateTransactionTags'; import PrivateTransactionTags from 'ui/privateTags/PrivateTransactionTags';
import Page from 'ui/shared/Page'; import Page from 'ui/shared/Page';
...@@ -25,13 +25,13 @@ type Props = { ...@@ -25,13 +25,13 @@ type Props = {
const PrivateTags = ({ tab }: Props) => { const PrivateTags = ({ tab }: Props) => {
const [ , setActiveTab ] = useState<TabName>(tab); const [ , setActiveTab ] = useState<TabName>(tab);
const basePath = useBasePath(); const link = useLink();
const onChangeTab = useCallback((index: number) => { const onChangeTab = useCallback((index: number) => {
setActiveTab(TABS[index]); setActiveTab(TABS[index]);
const newUrl = basePath + '/account/' + (TABS[index] === 'address' ? 'tag_address' : 'tag_transaction'); const newUrl = link(TABS[index] === 'address' ? 'private_tags_address' : 'private_tags_tx');
history.replaceState(history.state, '', newUrl); history.replaceState(history.state, '', newUrl);
}, [ setActiveTab, basePath ]); }, [ link ]);
return ( return (
<Page> <Page>
......
...@@ -2,7 +2,7 @@ import { Flex, Link } from '@chakra-ui/react'; ...@@ -2,7 +2,7 @@ import { Flex, Link } from '@chakra-ui/react';
import type { HTMLChakraProps } from '@chakra-ui/system'; import type { HTMLChakraProps } from '@chakra-ui/system';
import React from 'react'; import React from 'react';
import useBasePath from 'lib/hooks/useBasePath'; import useLink from 'lib/link/useLink';
import AddressWithDots from './AddressWithDots'; import AddressWithDots from './AddressWithDots';
import CopyToClipboard from './CopyToClipboard'; import CopyToClipboard from './CopyToClipboard';
...@@ -18,14 +18,15 @@ interface Props extends HTMLChakraProps<'div'> { ...@@ -18,14 +18,15 @@ interface Props extends HTMLChakraProps<'div'> {
} }
const AddressLinkWithTooltip = ({ address, type = 'address', truncated, withCopy = true, ...styles }: Props) => { const AddressLinkWithTooltip = ({ address, type = 'address', truncated, withCopy = true, ...styles }: Props) => {
const basePath = useBasePath(); const link = useLink();
let url; let url;
if (type === 'transaction') { if (type === 'transaction') {
url = basePath + '/tx/' + address; url = link('tx_index', { id: address });
} else if (type === 'token') { } else if (type === 'token') {
url = basePath + '/address/' + address + '/tokens#address-tabs'; url = link('token_index', { id: address });
} else { } else {
url = basePath + '/address/' + address; url = link('address_index', { id: address });
} }
return ( return (
<Flex columnGap={ 2 } alignItems="center" overflow="hidden" maxW="100%" { ...styles }> <Flex columnGap={ 2 } alignItems="center" overflow="hidden" maxW="100%" { ...styles }>
......
...@@ -3,7 +3,7 @@ import React from 'react'; ...@@ -3,7 +3,7 @@ import React from 'react';
import tokeIcon from 'icons/tokens/toke.svg'; import tokeIcon from 'icons/tokens/toke.svg';
import usdtIcon from 'icons/tokens/usdt.svg'; import usdtIcon from 'icons/tokens/usdt.svg';
import useBasePath from 'lib/hooks/useBasePath'; import useLink from 'lib/link/useLink';
// temporary solution // temporary solution
// don't know where to get icons and addresses yet // don't know where to get icons and addresses yet
...@@ -29,13 +29,13 @@ interface Props { ...@@ -29,13 +29,13 @@ interface Props {
const Token = ({ symbol, className }: Props) => { const Token = ({ symbol, className }: Props) => {
const token = TOKENS[symbol as keyof typeof TOKENS]; const token = TOKENS[symbol as keyof typeof TOKENS];
const basePath = useBasePath(); const link = useLink();
if (!token) { if (!token) {
return null; return null;
} }
const url = basePath + '/token/' + token.address; const url = link('token_index', { id: token.address });
return ( return (
<Center className={ className }> <Center className={ className }>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment