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

Feat/update route network #1061

Merged
merged 8 commits into from
Aug 1, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

- [#1061](https://github.com/alleslabs/celatone-frontend/pull/1061) Refactor custom network routes and add support chain ids to hook
- [#1053](https://github.com/alleslabs/celatone-frontend/pull/1053) Move move decoder and verifier links to env

### Bug fixes
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const AppContext = createContext<AppContextInterface>(DEFAULT_STATES);

export const AppProvider = observer(({ children }: AppProviderProps) => {
const { setModalTheme } = useModalTheme();
const { chainConfigs } = useChainConfigs();
const { chainConfigs, supportedChainIds } = useChainConfigs();

const [states, setStates] = useState<AppContextInterface>(DEFAULT_STATES);

Expand All @@ -73,7 +73,7 @@ export const AppProvider = observer(({ children }: AppProviderProps) => {

setStates({
isHydrated: true,
availableChainIds: SUPPORTED_CHAIN_IDS,
availableChainIds: supportedChainIds,
currentChainId: newChainId,
chainConfig,
indexerGraphClient: new GraphQLClient(chainConfig.indexer, {
Expand All @@ -87,7 +87,7 @@ export const AppProvider = observer(({ children }: AppProviderProps) => {
setStates((prev) => ({ ...prev, theme: newTheme })),
});
},
[chainConfigs]
[chainConfigs, supportedChainIds]
);

// Disable "Leave page" alert
Expand Down
6 changes: 3 additions & 3 deletions src/lib/app-provider/hooks/useAllowCustomNetworks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SUPPORTED_CHAIN_IDS } from "env";

import { useChainConfigs } from "./useChainConfigs";
import { useInternalNavigate } from "./useInternalNavigate";

export const useAllowCustomNetworks = ({
Expand All @@ -8,8 +7,9 @@ export const useAllowCustomNetworks = ({
shouldRedirect: boolean;
}) => {
const navigate = useInternalNavigate();
const { supportedChainIds } = useChainConfigs();

const isAllow = SUPPORTED_CHAIN_IDS.some(
const isAllow = supportedChainIds.some(
(chainId) => chainId === "initiation-1"
);

Expand Down
7 changes: 6 additions & 1 deletion src/lib/app-provider/hooks/useChainConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useMemo } from "react";

import type { ChainConfig, ChainConfigs } from "config/chain";
import { CHAIN_CONFIGS } from "config/chain";
import { SUPPORTED_CHAIN_IDS } from "env";
import {
initiatestnet,
initiatestnetAssets,
Expand Down Expand Up @@ -48,6 +49,7 @@ export const useChainConfigs = (): {
chainConfigs: ChainConfigs;
registryChains: Chain[];
registryAssets: AssetList[];
supportedChainIds: string[];
} => {
const { chainConfigs } = useChainConfigStore();

Expand Down Expand Up @@ -88,7 +90,7 @@ export const useChainConfigs = (): {
pretty_name: each.prettyName,
chain_id: each.chainId,
bech32_prefix: each.registry?.bech32_prefix ?? "",
slip44: 118,
slip44: each.registry?.slip44 ?? 118,
fees: each.fees,
staking: each.registry?.staking,
logo_URIs: each.logo_URIs,
Expand All @@ -107,12 +109,14 @@ export const useChainConfigs = (): {
},
registryChains: [...acc.registryChains, localRegistryChain],
registryAssets: [...acc.registryAssets, localRegistryAssets],
supportedChainIds: [...acc.supportedChainIds, each.chainId],
};
},
{
chainConfigs: {} as ChainConfigs,
registryChains: [] as Chain[],
registryAssets: [] as AssetList[],
supportedChainIds: [] as string[],
}
),
[chainConfigs]
Expand All @@ -139,5 +143,6 @@ export const useChainConfigs = (): {
...initiatestnetAssets,
...local.registryAssets,
],
supportedChainIds: [...SUPPORTED_CHAIN_IDS, ...local.supportedChainIds],
};
};
9 changes: 6 additions & 3 deletions src/lib/app-provider/hooks/useInternalNavigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import type { Router } from "next/router";
import type { ParsedUrlQueryInput } from "node:querystring";
import { useCallback } from "react";

import { FALLBACK_SUPPORTED_CHAIN_ID, SUPPORTED_CHAIN_IDS } from "env";
import { FALLBACK_SUPPORTED_CHAIN_ID } from "env";
import { getFirstQueryParam } from "lib/utils";

import { useChainConfigs } from "./useChainConfigs";

export interface NavigationArgs {
pathname: string;
query?: ParsedUrlQueryInput;
Expand All @@ -15,6 +17,7 @@ export interface NavigationArgs {

export const useInternalNavigate = () => {
const router = useRouter();
const { supportedChainIds } = useChainConfigs();

return useCallback(
({
Expand All @@ -28,7 +31,7 @@ export const useInternalNavigate = () => {
{
pathname: `/[network]${pathname}`,
query: {
network: SUPPORTED_CHAIN_IDS.includes(
network: supportedChainIds.includes(
getFirstQueryParam(router.query.network)
)
? router.query.network
Expand All @@ -40,6 +43,6 @@ export const useInternalNavigate = () => {
options
);
},
[router]
[router.push, router.query.network, router.replace, supportedChainIds]
);
};
9 changes: 6 additions & 3 deletions src/lib/app-provider/hooks/useNetworkChange.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useRouter } from "next/router";
import { useEffect, useRef } from "react";

import { FALLBACK_SUPPORTED_CHAIN_ID, SUPPORTED_CHAIN_IDS } from "env";
import { FALLBACK_SUPPORTED_CHAIN_ID } from "env";
import { useChainConfigStore } from "lib/providers/store";
import { getFirstQueryParam } from "lib/utils";

import { useChainConfigs } from "./useChainConfigs";
import { useInternalNavigate } from "./useInternalNavigate";

export const useNetworkChange = (
Expand All @@ -13,6 +14,7 @@ export const useNetworkChange = (
const router = useRouter();
const networkRef = useRef<string>();
const navigate = useInternalNavigate();
const { supportedChainIds } = useChainConfigs();
const { isHydrated: isChainConfigStoreHydrated } = useChainConfigStore();

useEffect(() => {
Expand All @@ -27,7 +29,7 @@ export const useNetworkChange = (
});
} else if (
router.pathname === "/[network]" &&
!SUPPORTED_CHAIN_IDS.includes(networkRoute)
!supportedChainIds.includes(networkRoute)
) {
// Redirect to default network 404 if `/invalid_network`
navigate({
Expand All @@ -47,7 +49,7 @@ export const useNetworkChange = (
navigate({
pathname: "/404",
query: {
network: SUPPORTED_CHAIN_IDS.includes(networkRoute)
network: supportedChainIds.includes(networkRoute)
? networkRoute
: FALLBACK_SUPPORTED_CHAIN_ID,
},
Expand All @@ -61,5 +63,6 @@ export const useNetworkChange = (
router.isReady,
router.pathname,
router.query,
supportedChainIds,
]);
};
6 changes: 4 additions & 2 deletions src/lib/components/AppLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { Text } from "@chakra-ui/react";
import Link from "next/link";
import { useRouter } from "next/router";

import { FALLBACK_SUPPORTED_CHAIN_ID, SUPPORTED_CHAIN_IDS } from "env";
import { FALLBACK_SUPPORTED_CHAIN_ID } from "env";
import { useChainConfigs } from "lib/app-provider/hooks/useChainConfigs";
import { getFirstQueryParam } from "lib/utils";

export const AppLink = ({
children,
...linkProps
}: React.ComponentProps<typeof Link>) => {
const router = useRouter();
const { supportedChainIds } = useChainConfigs();
const componentHref = linkProps.href.toString();

const network = SUPPORTED_CHAIN_IDS.includes(
const network = supportedChainIds.includes(
getFirstQueryParam(router.query.network)
)
? router.query.network
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/MobileGuard.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useRouter } from "next/router";
import type { ReactNode } from "react";

import { SUPPORTED_CHAIN_IDS } from "env";
import { useMobile } from "lib/app-provider";
import { useChainConfigs, useMobile } from "lib/app-provider";

import { NoMobile } from "./modal";

Expand All @@ -11,11 +10,12 @@ interface MobileGuardProps {
}
export const MobileGuard = ({ children }: MobileGuardProps) => {
const router = useRouter();
const { supportedChainIds } = useChainConfigs();
const isMobile = useMobile();

const pathName = router.asPath;
const isResponsive =
SUPPORTED_CHAIN_IDS.includes(pathName.slice(1)) ||
supportedChainIds.includes(pathName.slice(1)) ||
pathName.includes(`/accounts`) ||
pathName.includes(`/txs`) ||
pathName.includes(`/blocks`) ||
Expand Down
17 changes: 13 additions & 4 deletions src/lib/components/modal/RemoveChainConfigModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Text, useToast } from "@chakra-ui/react";
import { useRouter } from "next/router";
import type { ReactNode } from "react";

import { useInternalNavigate } from "lib/app-provider";
import { useCelatoneApp } from "lib/app-provider";
import { CustomIcon } from "lib/components/icon";
import { useChainConfigStore } from "lib/providers/store";
import { useChainConfigStore, useNetworkStore } from "lib/providers/store";

import { ActionModal } from "./ActionModal";

Expand All @@ -17,14 +18,22 @@ export function RemoveChainConfigModal({
trigger,
}: RemoveChainConfigModalProps) {
const { removeChainConfig, getChainConfig } = useChainConfigStore();
const navigate = useInternalNavigate();
const { removeNetwork } = useNetworkStore();
const router = useRouter();
const { currentChainId } = useCelatoneApp();

const chainConfig = getChainConfig(chainId);

const toast = useToast();
const handleRemove = () => {
// remove chain id from chain config store
removeChainConfig(chainId);
navigate({ pathname: "/", replace: true });

// remove chain id from pinned network
removeNetwork(chainId);

// redirect to home page
router.push(currentChainId === chainId ? "/" : `/${currentChainId}`);

setTimeout(() => {
toast({
Expand Down
2 changes: 1 addition & 1 deletion src/lib/layout/network-menu/NetworkMenuBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const NetworkMenuBody = observer(
}
onClose={onClose}
/>
<AppLink href="/add-network">
<AppLink href="/custom-network/add">
<Button
variant="outline-gray"
justifyContent="flex-start"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/layout/network-menu/NetworkMenuTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const NetworkMenuTop = ({
<Text as="span" variant="body3" color="text.dark">
Want to add your network?
</Text>{" "}
<AppLink href="/add-network" onClick={onClose}>
<AppLink href="/custom-network/add" onClick={onClose}>
<Text
as="span"
variant="body3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const NetworkCardCta = observer(
{...pinIconStyles}
onClick={() =>
navigate({
pathname: "/network-config",
pathname: "/custom-network/edit/[chainId]",
query: {
chainId,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export const AddNetwork = () => {
<ButtonCard
title="Fill in Network Details Manually"
description="Add new Minitia through fill in each configuration manually"
onClick={() => navigate({ pathname: "/add-network/manual" })}
onClick={() => navigate({ pathname: "/custom-network/add/manual" })}
/>
{/* // TODO: Open this code to enable import JSON option entry point */}
{/* <ButtonCard
title="Import JSON"
description="Import available JSON that contains all the configuration"
onClick={() => navigate({ pathname: "/add-network/json" })}
onClick={() => navigate({ pathname: "/custom-network/add/json" })}
/> */}
</Flex>
</Flex>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const WalletRegistry = ({ control, errors }: WalletRegistryProps) => {
label="Bech32 Prefix"
variant="fixed-floating"
w="full"
placeholder="ex. Init"
placeholder="ex. init"
rules={{
required: "",
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const AddNetworkManual = () => {
highGasPrice: "",
gasForCosmosSend: "",
gasForIbc: "",
bech32Prefix: "",
bech32Prefix: "init",
slip44: 118,
assets: [],
},
Expand Down
3 changes: 0 additions & 3 deletions src/pages/[network]/add-network/index.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/pages/[network]/add-network/manual.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions src/pages/[network]/custom-network/add/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AddNetwork } from "lib/pages/custom-network";

export default AddNetwork;
3 changes: 3 additions & 0 deletions src/pages/[network]/custom-network/add/manual.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AddNetworkManual } from "lib/pages/custom-network/manual";

export default AddNetworkManual;
3 changes: 3 additions & 0 deletions src/pages/[network]/custom-network/edit/[chainId].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { NetworkConfig } from "lib/pages/custom-network/edit";

export default NetworkConfig;
3 changes: 0 additions & 3 deletions src/pages/[network]/network-config/[chainId].tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/pages/add-network/index.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/pages/add-network/manual.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions src/pages/custom-network/add/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AddNetwork } from "lib/pages/custom-network";

export default AddNetwork;
3 changes: 3 additions & 0 deletions src/pages/custom-network/add/manual.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { AddNetworkManual } from "lib/pages/custom-network/manual";

export default AddNetworkManual;
3 changes: 3 additions & 0 deletions src/pages/custom-network/edit/[chainId].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { NetworkConfig } from "lib/pages/custom-network/edit";

export default NetworkConfig;
3 changes: 0 additions & 3 deletions src/pages/network-config/[chainId].tsx

This file was deleted.