Skip to content

Commit

Permalink
Merge pull request #486 from alleslabs/feat/schema-query-scroll
Browse files Browse the repository at this point in the history
Switch to schema tab when available and add msg scrolling
  • Loading branch information
evilpeach committed Aug 22, 2023
2 parents 0f1bffa + dec5f34 commit 30c2457
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 114 deletions.
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

- [#486](https://github.com/alleslabs/celatone-frontend/pull/486) Switch to schema tab when available and prefill,expand,and scroll to the prefill msg
- [#482](https://github.com/alleslabs/celatone-frontend/pull/482) Add json schema functionality to query page
- [#477](https://github.com/alleslabs/celatone-frontend/pull/477) Add json schema section on code detail page
- [#475](https://github.com/alleslabs/celatone-frontend/pull/475) Add json schema functionality to instantiate contract
Expand Down
25 changes: 2 additions & 23 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useModalTheme } from "@cosmos-kit/react";
import { GraphQLClient } from "graphql-request";
import { observer } from "mobx-react-lite";
import type { Dispatch, ReactNode, SetStateAction } from "react";
import type { ReactNode } from "react";
import {
useCallback,
useState,
Expand All @@ -21,13 +21,11 @@ import { SUPPORTED_CHAIN_IDS } from "env";
import { LoadingOverlay } from "lib/components/LoadingOverlay";
import { NetworkErrorState } from "lib/components/state/NetworkErrorState";
import { DEFAULT_ADDRESS } from "lib/data";
import { useLocalStorage } from "lib/hooks/useLocalStorage";
import {
useCodeStore,
useContractStore,
usePublicProjectStore,
} from "lib/providers/store";
import type { Option } from "lib/types";
import { formatUserKey } from "lib/utils";

interface AppProviderProps {
Expand All @@ -40,10 +38,6 @@ interface AppContextInterface {
chainConfig: ChainConfig;
indexerGraphClient: GraphQLClient;
constants: ProjectConstants;
isExpand: boolean;
isDevMode: Option<boolean>;
setIsExpand: Dispatch<SetStateAction<boolean>>;
setIsDevMode: Dispatch<SetStateAction<Option<boolean>>>;
}

const AppContext = createContext<AppContextInterface>({
Expand All @@ -52,10 +46,6 @@ const AppContext = createContext<AppContextInterface>({
chainConfig: DEFAULT_CHAIN_CONFIG,
indexerGraphClient: new GraphQLClient(DEFAULT_CHAIN_CONFIG.indexer),
constants: PROJECT_CONSTANTS,
isExpand: false,
isDevMode: undefined,
setIsExpand: () => {},
setIsDevMode: () => {},
});

export const AppProvider = observer(({ children }: AppProviderProps) => {
Expand All @@ -67,13 +57,6 @@ export const AppProvider = observer(({ children }: AppProviderProps) => {
const [currentChainName, setCurrentChainName] = useState<string>();
const [currentChainId, setCurrentChainId] = useState("");

// TODO - Revisit localstorage
const [isDevMode, setIsDevMode] = useLocalStorage<Option<boolean>>(
"devMode",
undefined
);
const [isExpand, setIsExpand] = useLocalStorage("navbar", false);

// Remark: this function is only used in useSelectChain. Do not use in other places.
const handleOnChainIdChange = useCallback((newChainId: string) => {
const config = CHAIN_CONFIGS[newChainId];
Expand All @@ -90,12 +73,8 @@ export const AppProvider = observer(({ children }: AppProviderProps) => {
chainConfig,
indexerGraphClient: new GraphQLClient(chainConfig.indexer),
constants: PROJECT_CONSTANTS,
isDevMode,
isExpand,
setIsDevMode,
setIsExpand,
};
}, [currentChainId, isDevMode, isExpand, setIsDevMode, setIsExpand]);
}, [currentChainId]);

useEffect(() => {
if (currentChainName) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/app-provider/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./app";
export * from "./nav";
42 changes: 42 additions & 0 deletions src/lib/app-provider/contexts/nav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Dispatch, ReactNode, SetStateAction } from "react";
import { useContext, createContext, useMemo } from "react";

import { useLocalStorage } from "lib/hooks/useLocalStorage";
import type { Option } from "lib/types";

interface NavContextInterface {
isExpand: boolean;
isDevMode: Option<boolean>;
setIsExpand: Dispatch<SetStateAction<boolean>>;
setIsDevMode: Dispatch<SetStateAction<Option<boolean>>>;
}
const NavContext = createContext<NavContextInterface>({
isExpand: false,
isDevMode: undefined,
setIsExpand: () => {},
setIsDevMode: () => {},
});

export const NavProvider = ({ children }: { children: ReactNode }) => {
const [isDevMode, setIsDevMode] = useLocalStorage<Option<boolean>>(
"devMode",
undefined
);
const [isExpand, setIsExpand] = useLocalStorage("navbar", false);

const states = useMemo<NavContextInterface>(
() => ({
isDevMode,
isExpand,
setIsDevMode,
setIsExpand,
}),
[isDevMode, isExpand, setIsDevMode, setIsExpand]
);

return <NavContext.Provider value={states}>{children}</NavContext.Provider>;
};

export const useNavContext = (): NavContextInterface => {
return useContext(NavContext);
};
140 changes: 72 additions & 68 deletions src/lib/components/ContractSelectSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Addr, ContractAddr, Option } from "lib/types";

import { ExplorerLink } from "./ExplorerLink";
import { CustomIcon } from "./icon";
import { LoadingOverlay } from "./LoadingOverlay";
import { EditContractDetailsModal, SaveContractDetailsModal } from "./modal";
import {
SelectContractAdmin,
Expand Down Expand Up @@ -156,7 +157,7 @@ export const ContractSelectSection = observer(
mode: "all",
});

const { refetch } = useContractDetailByContractAddress(
const { refetch, isFetching } = useContractDetailByContractAddress(
contractAddress,
(data) => {
successCallback?.(data);
Expand Down Expand Up @@ -188,77 +189,80 @@ export const ContractSelectSection = observer(
const style = modeStyle(mode);

return (
<Flex
mb={style.container}
borderWidth="thin"
borderColor="gray.800"
p={4}
borderRadius="8px"
fontSize="12px"
justify="space-between"
align="center"
width="full"
>
<Flex gap={4} width="100%" direction={{ base: "column", md: "row" }}>
<Flex
direction="column"
width={{ base: "auto", md: style.contractAddrContainer }}
>
Contract Address
{!notSelected ? (
<ExplorerLink
value={contractAddress}
type="contract_address"
showCopyOnHover
// TODO - Revisit not necessary if disable UI for mobile is implemented
textFormat={
isMobile || mode === "only-admin" ? "truncate" : "normal"
}
maxWidth="none"
minWidth={style.contractAddrW}
wordBreak="break-all"
/>
) : (
<Text color="text.disabled" variant="body2">
Not Selected
</Text>
)}
</Flex>
<Flex
direction="column"
width={{ base: "auto", md: style.contractNameContainer }}
>
Contract Name
<DisplayName
notSelected={notSelected}
isValid={contractState.isValid}
name={contractLocalInfo?.name}
label={contractState.label}
/>
</Flex>
<Flex gap={2} alignItems="center">
{mode === "all-lists" && contractState.isValid && (
<ContractDetailsButton
contractAddress={contractAddress}
contractLocalInfo={contractLocalInfo}
instantiator={contractState.instantiator as Addr}
label={contractState.label}
/>
)}
{mode === "all-lists" ? (
<SelectContractInstantiator
notSelected={notSelected}
onContractSelect={onContractSelect}
/>
) : (
<SelectContractAdmin
<>
{isFetching && <LoadingOverlay />}
<Flex
mb={style.container}
borderWidth="thin"
borderColor="gray.800"
p={4}
borderRadius="8px"
fontSize="12px"
justify="space-between"
align="center"
width="full"
>
<Flex gap={4} width="100%" direction={{ base: "column", md: "row" }}>
<Flex
direction="column"
width={{ base: "auto", md: style.contractAddrContainer }}
>
Contract Address
{!notSelected ? (
<ExplorerLink
value={contractAddress}
type="contract_address"
showCopyOnHover
// TODO - Revisit not necessary if disable UI for mobile is implemented
textFormat={
isMobile || mode === "only-admin" ? "truncate" : "normal"
}
maxWidth="none"
minWidth={style.contractAddrW}
wordBreak="break-all"
/>
) : (
<Text color="text.disabled" variant="body2">
Not Selected
</Text>
)}
</Flex>
<Flex
direction="column"
width={{ base: "auto", md: style.contractNameContainer }}
>
Contract Name
<DisplayName
notSelected={notSelected}
onContractSelect={onContractSelect}
isValid={contractState.isValid}
name={contractLocalInfo?.name}
label={contractState.label}
/>
)}
</Flex>
<Flex gap={2} alignItems="center">
{mode === "all-lists" && contractState.isValid && (
<ContractDetailsButton
contractAddress={contractAddress}
contractLocalInfo={contractLocalInfo}
instantiator={contractState.instantiator as Addr}
label={contractState.label}
/>
)}
{mode === "all-lists" ? (
<SelectContractInstantiator
notSelected={notSelected}
onContractSelect={onContractSelect}
/>
) : (
<SelectContractAdmin
notSelected={notSelected}
onContractSelect={onContractSelect}
/>
)}
</Flex>
</Flex>
</Flex>
</Flex>
</>
);
}
);
4 changes: 2 additions & 2 deletions src/lib/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRouter } from "next/router";
import type { ReactNode } from "react";
import { useEffect, useMemo } from "react";

import { useCelatoneApp, useMobile } from "lib/app-provider";
import { useMobile, useNavContext } from "lib/app-provider";
import { scrollToTop } from "lib/utils";

import Footer from "./Footer";
Expand All @@ -19,7 +19,7 @@ type LayoutProps = {
const Layout = ({ children }: LayoutProps) => {
const router = useRouter();
const isMobile = useMobile();
const { isExpand, isDevMode, setIsExpand, setIsDevMode } = useCelatoneApp();
const { isExpand, isDevMode, setIsExpand, setIsDevMode } = useNavContext();

const defaultRow = "70px 48px 1fr";
const mode = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export const CodeSchemaSection = ({
<TabList>
<StyledCustomTab>Full Schema</StyledCustomTab>
{SchemaMsgTabList.map((schemaProperty) => (
<StyledCustomTab>{capitalize(schemaProperty)}Msg</StyledCustomTab>
<StyledCustomTab key={schemaProperty}>
{capitalize(schemaProperty)}Msg
</StyledCustomTab>
))}
</TabList>
<TabPanels pl={6}>
Expand Down
8 changes: 6 additions & 2 deletions src/lib/pages/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { useRouter } from "next/router";
import { useEffect } from "react";

import { CURR_THEME } from "env";
import { useCelatoneApp, useInternalNavigate } from "lib/app-provider";
import {
useCelatoneApp,
useInternalNavigate,
useNavContext,
} from "lib/app-provider";
import { ConnectWalletAlert } from "lib/components/ConnectWalletAlert";
import { CustomIcon } from "lib/components/icon";
import PageContainer from "lib/components/PageContainer";
Expand Down Expand Up @@ -108,7 +112,7 @@ const calculateAverageBlockTime = (
const Home = () => {
const router = useRouter();
const navigate = useInternalNavigate();
const { isDevMode } = useCelatoneApp();
const { isDevMode } = useNavContext();

const {
chainConfig: { prettyName },
Expand Down
9 changes: 8 additions & 1 deletion src/lib/pages/query/components/QueryArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const QueryArea = ({

useEffect(() => {
if (!schema) setTab(MessageTabs.JSON_INPUT);
else {
setTab(MessageTabs.YOUR_SCHEMA);
}
}, [schema]);

return (
Expand Down Expand Up @@ -61,7 +64,11 @@ export const QueryArea = ({
}
schemaContent={
codeHash ? (
<SchemaQuery schema={schema} contractAddress={contractAddress} />
<SchemaQuery
schema={schema}
contractAddress={contractAddress}
initialMsg={initialMsg}
/>
) : (
<EmptyState
imageVariant="not-found"
Expand Down
Loading

0 comments on commit 30c2457

Please sign in to comment.