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: add network to url path #95

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#95](https://github.com/alleslabs/celatone-frontend/pull/95) Add network to url path
- [#90](https://github.com/alleslabs/celatone-frontend/pull/90) Add update admin (`/admin`) and migrate (`/migrate`) page routes
- [#91](https://github.com/alleslabs/celatone-frontend/pull/91) Add migrate shortcut to the sidebar
- [#75](https://github.com/alleslabs/celatone-frontend/pull/75) Add code-related contracts table to the code detail page
Expand Down
16 changes: 12 additions & 4 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useWallet } from "@cosmos-kit/react";
import big from "big.js";
import { GraphQLClient } from "graphql-request";
import { observer } from "mobx-react-lite";
import { useRouter } from "next/router";
import type { ReactNode } from "react";
import { useEffect, useContext, useMemo, createContext } from "react";

Expand All @@ -12,7 +13,7 @@ import {
getExplorerUserAddressUrl,
} from "lib/app-fns/explorer";
import { LoadingOverlay } from "lib/components/LoadingOverlay";
import { DEFAULT_ADDRESS, DEFAULT_CHAIN } from "lib/data";
import { DEFAULT_ADDRESS } from "lib/data";
import { useCodeStore, useContractStore } from "lib/hooks";
import type { ChainGasPrice, Token, U } from "lib/types";
import { formatUserKey } from "lib/utils";
Expand Down Expand Up @@ -59,6 +60,7 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
appContractAddressMap,
constants,
}: AppProviderProps<ContractAddress, Constants>) => {
const router = useRouter();
const { currentChainName, currentChainRecord, setCurrentChain } = useWallet();
const { setCodeUserKey, isCodeUserKeyExist } = useCodeStore();
const { setContractUserKey, isContractUserKeyExist } = useContractStore();
Expand Down Expand Up @@ -113,9 +115,15 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
}, [currentChainName, setCodeUserKey, setContractUserKey]);

useEffect(() => {
setCurrentChain(DEFAULT_CHAIN);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
/**
* @remarks Condition checking varies by chain
*/
if (router.query.network === "mainnet") {
setCurrentChain("osmosis");
} else {
setCurrentChain("osmosistestnet");
}
}, [router.query.network, setCurrentChain]);

const AppContent = observer(() => {
if (isCodeUserKeyExist() && isContractUserKeyExist())
Expand Down
1 change: 1 addition & 0 deletions src/lib/app-provider/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./useCelatoneApp";
export * from "./useQueryCmds";
export * from "./useExecuteCmds";
export * from "./useTokensInfo";
export * from "./useInternalNavigate";
38 changes: 38 additions & 0 deletions src/lib/app-provider/hooks/useInternalNavigate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useRouter } from "next/router";
import { useCallback } from "react";

import type { Dict } from "lib/types";

interface TransitionOptions {
shallow?: boolean;
locale?: string | false;
scroll?: boolean;
unstable_skipClientCache?: boolean;
}

interface NavigationArgs {
pathname: string;
query?: Dict<string, string | number>;
options?: TransitionOptions;
}

export const useInternalNavigate = () => {
const router = useRouter();

return useCallback(
({ pathname, query = {}, options = {} }: NavigationArgs) => {
router.push(
{
pathname: `/[network]${pathname}`,
query: {
network: router.query.network === "mainnet" ? "mainnet" : "testnet",
...query,
},
},
undefined,
options
);
},
[router]
);
};
31 changes: 31 additions & 0 deletions src/lib/components/AppLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Text } from "@chakra-ui/react";
import type { LinkProps } from "next/link";
import Link from "next/link";
import { useRouter } from "next/router";

export const AppLink = ({
children,
...linkProps
}: Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, keyof LinkProps> &
LinkProps & {
children?: React.ReactNode;
} & React.RefAttributes<HTMLAnchorElement>) => {
const router = useRouter();
const componentHref = linkProps.href.toString();
return (
<Link
{...linkProps}
href={
router.query.network === "mainnet"
? `/mainnet${componentHref}`
: `/testnet${componentHref}`
}
>
{typeof children === "string" ? (
<Text color={linkProps.color}>{children}</Text>
) : (
children
)}
</Link>
);
};
6 changes: 3 additions & 3 deletions src/lib/components/ExplorerLink.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { BoxProps } from "@chakra-ui/react";
import { Box, Text } from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import Link from "next/link";

import {
getExplorerBlockUrl,
Expand All @@ -12,6 +11,7 @@ import {
import type { AddressReturnType } from "lib/hooks";
import { truncate } from "lib/utils";

import { AppLink } from "./AppLink";
import { Copier } from "./Copier";

export type LinkType =
Expand Down Expand Up @@ -101,9 +101,9 @@ const LinkRender = ({
);

return isInternal ? (
<Link href={hrefLink} passHref onClick={(e) => e.stopPropagation()}>
<AppLink href={hrefLink} passHref onClick={(e) => e.stopPropagation()}>
{textElement}
</Link>
</AppLink>
) : (
<a
href={hrefLink}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/button/InstantiateButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { ButtonProps } from "@chakra-ui/react";
import { Button, chakra, Icon, Tooltip } from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import { useRouter } from "next/router";
import { MdHowToVote, MdPerson } from "react-icons/md";

import { useInternalNavigate } from "lib/app-provider";
import type { HumanAddr, PermissionAddresses } from "lib/types";
import { InstantiatePermission } from "lib/types";

Expand Down Expand Up @@ -53,10 +53,10 @@ export const InstantiateButton = ({
codeId,
...buttonProps
}: InstantiateButtonProps) => {
const router = useRouter();
const { address } = useWallet();
const navigate = useInternalNavigate();
const goToInstantiate = () =>
router.push({ pathname: "/instantiate", query: { "code-id": codeId } });
navigate({ pathname: "/instantiate", query: { "code-id": codeId } });

const isAllowed =
permissionAddresses.includes(address as HumanAddr) ||
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/modal/list/RemoveList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { MenuItemProps } from "@chakra-ui/react";
import { MenuItem, useToast, Icon, Text } from "@chakra-ui/react";
import { useRouter } from "next/router";
import { MdDeleteForever, MdCheckCircle } from "react-icons/md";

import { useInternalNavigate } from "lib/app-provider";
import { ActionModal } from "lib/components/modal/ActionModal";
import { useContractStore, useUserKey } from "lib/hooks";
import type { LVPair } from "lib/types";
Expand All @@ -18,10 +18,10 @@ export function RemoveList({ list, menuItemProps }: ModalProps) {
const { removeList } = useContractStore();

const toast = useToast();
const router = useRouter();
const navigate = useInternalNavigate();
const handleRemove = () => {
removeList(userKey, list.value);
router.push("/contract-list");
navigate({ pathname: "/contract-list" });
// TODO: show toast after removed and redirect to /contract-list
setTimeout(() => {
toast({
Expand Down
8 changes: 4 additions & 4 deletions src/lib/components/modal/tx/ButtonSection.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Button, Icon } from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import { useRouter } from "next/router";
import { useCallback } from "react";
import { FiChevronRight } from "react-icons/fi";

import { getExplorerTxUrl } from "lib/app-fns/explorer";
import { useInternalNavigate } from "lib/app-provider";
import type { ActionVariant, TxReceipt } from "lib/types";

interface ButtonSectionProps {
Expand All @@ -18,7 +18,7 @@ export const ButtonSection = ({
onClose,
receipts,
}: ButtonSectionProps) => {
const router = useRouter();
const navigate = useInternalNavigate();
const { currentChainName } = useWallet();

const openExplorer = useCallback(() => {
Expand All @@ -40,7 +40,7 @@ export const ButtonSection = ({
<Button
variant="ghost-primary"
onClick={() => {
router.push("/codes");
navigate({ pathname: "/codes" });
onClose?.();
}}
>
Expand All @@ -53,7 +53,7 @@ export const ButtonSection = ({
}
onClick={() => {
const codeId = receipts.find((r) => r.title === "Code ID")?.value;
router.push({
navigate({
pathname: "/instantiate",
query: { "code-id": codeId },
});
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/state/ZeroState.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Flex, Button, Icon, Text } from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import { useRouter } from "next/router";
import { MdOutlineAdd, MdBookmarkBorder, MdSearch } from "react-icons/md";

import { useInternalNavigate } from "lib/app-provider";
import { SaveNewContract } from "lib/components/modal/contract";
import type { LVPair } from "lib/types";

Expand Down Expand Up @@ -56,7 +56,7 @@ export const ZeroState = ({
isReadOnly,
isInstantiatedByMe,
}: ZeroStateProps) => {
const router = useRouter();
const navigate = useInternalNavigate();
const { isWalletConnected } = useWallet();

return (
Expand All @@ -82,7 +82,7 @@ export const ZeroState = ({
<ActionSection
isInstantiatedByMe={isInstantiatedByMe}
list={list}
handleAction={() => router.push("/deploy")}
handleAction={() => navigate({ pathname: "/deploy" })}
/>
)}
</Flex>
Expand Down
27 changes: 24 additions & 3 deletions src/lib/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,35 @@ import {
} from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import { useRouter } from "next/router";
import { useCallback } from "react";
import { FiChevronDown } from "react-icons/fi";

import { useInternalNavigate } from "lib/app-provider";
import { WalletSection } from "lib/components/Wallet";
import { CHAIN_NAMES } from "lib/data";

import Searchbar from "./Searchbar";

const Header = () => {
const { currentChainRecord, setCurrentChain, getChainRecord } = useWallet();
const router = useRouter();
const navigate = useInternalNavigate();
const { currentChainRecord, setCurrentChain, getChainRecord } = useWallet();

const handleChainSelect = useCallback(
(chainName: string) => {
setCurrentChain(chainName);
navigate({
pathname: router.asPath.replace(`/${router.query.network}`, ""),
query: {
/**
* @remarks Condition checking varies by chain
*/
network: chainName === "osmosis" ? "mainnet" : "testnet",
},
});
},
[setCurrentChain, navigate, router]
);

return (
<Flex
Expand All @@ -38,7 +57,7 @@ const Header = () => {
width="115px"
mr="36px"
_hover={{ cursor: "pointer" }}
onClick={() => router.push({ pathname: "/" })}
onClick={() => navigate({ pathname: "/" })}
/>
<Searchbar />
<Flex gap={2}>
Expand Down Expand Up @@ -74,7 +93,9 @@ const Header = () => {
return (
<MenuItem
key={chainName}
onClick={() => setCurrentChain(chainName)}
onClick={() => {
handleChainSelect(chainName);
}}
flexDirection="column"
alignItems="flex-start"
>
Expand Down
10 changes: 5 additions & 5 deletions src/lib/layout/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Flex, Box, Text, Icon, Button, Spacer } from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import { observer } from "mobx-react-lite";
import Link from "next/link";
import {
MdHome,
MdCode,
Expand All @@ -15,6 +14,7 @@ import {
MdReadMore,
} from "react-icons/md";

import { AppLink } from "lib/components/AppLink";
import { CreateNewList } from "lib/components/modal";
import { INSTANTIATED_LIST_NAME, getListIcon, SAVED_LIST_NAME } from "lib/data";
import { useContractStore } from "lib/hooks";
Expand Down Expand Up @@ -144,7 +144,7 @@ const Navbar = observer(() => {
)}
</Flex>
{item.submenu.map((submenu) => (
<Link href={submenu.slug} key={submenu.slug}>
<AppLink href={submenu.slug} key={submenu.slug}>
<Flex
gap="3"
p={2}
Expand All @@ -158,7 +158,7 @@ const Navbar = observer(() => {
{submenu.name}
</Text>
</Flex>
</Link>
</AppLink>
))}
</Box>
))}
Expand All @@ -175,12 +175,12 @@ const Navbar = observer(() => {
borderTop="4px solid"
borderTopColor="background.main"
>
<Link href="/deploy">
<AppLink href="/deploy">
<Button>
<Icon as={MdOutlineAdd} boxSize="4" />
Deploy new contract
</Button>
</Link>
</AppLink>
</Flex>
</Flex>
);
Expand Down
Loading