mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
store network info in cookies
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
API_HOST=blockscout.com
|
||||
API_BASE_PATH=/xdai/testnet/api
|
||||
@@ -1,2 +0,0 @@
|
||||
API_HOST=blockscout.com
|
||||
API_BASE_PATH=/xdai/testnet/api
|
||||
+1
-1
@@ -9,7 +9,7 @@ export default function fetch(path: string, init?: RequestInit): Promise<Respons
|
||||
authorization: `Bearer ${ process.env.API_AUTHORIZATION_TOKEN }`,
|
||||
'content-type': 'application/json',
|
||||
};
|
||||
const url = `https://${ process.env.API_HOST }${ process.env.API_BASE_PATH }${ path }`;
|
||||
const url = `https://blockscout.com${ path }`;
|
||||
|
||||
return nodeFetch(url, {
|
||||
headers,
|
||||
|
||||
+11
-1
@@ -1,6 +1,7 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
import fetch from 'lib/api/fetch';
|
||||
import * as cookies from 'lib/cookies';
|
||||
|
||||
type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
|
||||
@@ -8,7 +9,16 @@ export default function handler<TRes>(getUrl: (_req: NextApiRequest) => string,
|
||||
return async(_req: NextApiRequest, res: NextApiResponse<TRes>) => {
|
||||
if (_req.method && allowedMethods.includes(_req.method as Methods)) {
|
||||
const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD';
|
||||
const response = await fetch(getUrl(_req), {
|
||||
const networkType = _req.cookies[cookies.NAMES.NETWORK_TYPE];
|
||||
const networkSubType = _req.cookies[cookies.NAMES.NETWORK_SUB_TYPE];
|
||||
|
||||
if (!networkType || !networkSubType) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`Incorrect network: NETWORK_TYPE=${ networkType } NETWORK_SUB_TYPE=${ networkSubType }`);
|
||||
}
|
||||
|
||||
const url = `/${ networkType }/${ networkSubType }/api${ getUrl(_req) }`;
|
||||
const response = await fetch(url, {
|
||||
method: _req.method,
|
||||
body: isBodyDisallowed ? undefined : _req.body,
|
||||
});
|
||||
|
||||
+3
-1
@@ -4,7 +4,9 @@ import { Cookies } from 'typescript-cookie';
|
||||
import isBrowser from './isBrowser';
|
||||
|
||||
export enum NAMES {
|
||||
NAV_BAR_COLLAPSED='nav_bar_collapsed'
|
||||
NAV_BAR_COLLAPSED='nav_bar_collapsed',
|
||||
NETWORK_TYPE='network_type',
|
||||
NETWORK_SUB_TYPE='network_sub_type',
|
||||
}
|
||||
|
||||
export function get(name?: string | undefined | null) {
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ module.exports = withReactSvg({
|
||||
return [
|
||||
{
|
||||
source: '/',
|
||||
destination: '/xdai/mainnet',
|
||||
destination: '/xdai/testnet',
|
||||
permanent: true,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { Popover, PopoverTrigger, Icon, useColorModeValue, Button } from '@chakra-ui/react';
|
||||
import { useRouter } from 'next/router';
|
||||
import React from 'react';
|
||||
|
||||
import networksIcon from 'icons/networks.svg';
|
||||
import * as cookies from 'lib/cookies';
|
||||
import getDefaultTransitionProps from 'theme/utils/getDefaultTransitionProps';
|
||||
|
||||
import NetworkMenuPopup from './NetworkMenuPopup';
|
||||
@@ -11,6 +13,19 @@ interface Props {
|
||||
}
|
||||
|
||||
const NetworkMenu = ({ isCollapsed }: Props) => {
|
||||
const router = useRouter();
|
||||
const networkType = router.query.network_type;
|
||||
const networkSubType = router.query.network_sub_type;
|
||||
|
||||
React.useEffect(() => {
|
||||
if (typeof networkType === 'string') {
|
||||
cookies.set(cookies.NAMES.NETWORK_TYPE, networkType);
|
||||
}
|
||||
if (typeof networkSubType === 'string') {
|
||||
cookies.set(cookies.NAMES.NETWORK_SUB_TYPE, networkSubType);
|
||||
}
|
||||
}, [ networkType, networkSubType ]);
|
||||
|
||||
return (
|
||||
<Popover openDelay={ 300 } placement="right-start" gutter={ 22 } isLazy>
|
||||
<PopoverTrigger>
|
||||
|
||||
@@ -32,7 +32,7 @@ const NetworkMenuLink = ({ name, type, subType, icon, isActive, routeName, isAcc
|
||||
const pathName = `/${ type }/${ subType }${ localPath }`;
|
||||
|
||||
// will fix later after we agree on CI/CD workflow
|
||||
const href = type === 'xdai' ? pathName : 'https://blockscout.com' + pathName;
|
||||
const href = type === 'xdai' && subType === 'testnet' ? pathName : 'https://blockscout.com' + pathName;
|
||||
const hasIcon = Boolean(icon);
|
||||
const colors = useColors({ hasIcon });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user