Skip to content

Commit

Permalink
feat: chain config store, useChainConfigs, apply all
Browse files Browse the repository at this point in the history
docs: add changelog

fix: add isHydrated

feat: update test

feat: update test case
  • Loading branch information
evilpeach committed Jul 31, 2024
1 parent b347d01 commit 9b4c8db
Show file tree
Hide file tree
Showing 19 changed files with 527 additions and 86 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#1054](https://github.com/alleslabs/celatone-frontend/pull/1054) Add chain config store, useChainConfig hook, and apply it all places

### Improvements

### Bug fixes
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
"build-storybook": "storybook build"
},
"dependencies": {
"@alleslabs/shared": "1.0.0-dev1",
"@amplitude/analytics-browser": "^2.3.3",
"@amplitude/analytics-types": "^2.3.0",
"@amplitude/plugin-user-agent-enrichment-browser": "^1.0.0",
"@chain-registry/types": "0.17.0",
"@chain-registry/types": "0.45.36",
"@chakra-ui/anatomy": "^2.2.2",
"@chakra-ui/card": "^2.2.0",
"@chakra-ui/icons": "^2.1.1",
Expand Down
29 changes: 21 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/config/chain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface ChainConfig {
registryChainName: string;
prettyName: string;
logoUrl?: string;
networkType: "mainnet" | "testnet";
networkType: "mainnet" | "testnet" | "devnet" | "local";
lcd: string;
rpc: string;
indexer: string;
Expand Down
55 changes: 32 additions & 23 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
useState,
} from "react";

import { useChainConfigs } from "../hooks/useChainConfigs";
import { useNetworkChange } from "../hooks/useNetworkChange";
import { CHAIN_CONFIGS, FALLBACK_CHAIN_CONFIG } from "config/chain";
import { FALLBACK_CHAIN_CONFIG } from "config/chain";
import type { ChainConfig } from "config/chain";
import { PROJECT_CONSTANTS } from "config/project";
import type { ProjectConstants } from "config/project";
Expand All @@ -21,6 +22,8 @@ import {
HASURA_ADMIN_SECRET,
SUPPORTED_CHAIN_IDS,
} from "env";
import { LoadingOverlay } from "lib/components/LoadingOverlay";
import { useChainConfigStore } from "lib/providers/store";
import { changeFavicon } from "lib/utils";

interface AppProviderProps {
Expand Down Expand Up @@ -57,32 +60,37 @@ const AppContext = createContext<AppContextInterface>(DEFAULT_STATES);

export const AppProvider = ({ children }: AppProviderProps) => {
const { setModalTheme } = useModalTheme();
const { chainConfigs } = useChainConfigs();
const { isHydrated: isChainConfigStoreHydrated } = useChainConfigStore();

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

// Remark: this function is only used in useSelectChain. Do not use in other places.
const handleOnChainIdChange = useCallback((newChainId: string) => {
const chainConfig = CHAIN_CONFIGS[newChainId] ?? FALLBACK_CHAIN_CONFIG;

const theme = getTheme(chainConfig.chain);
changeFavicon(theme.branding.favicon);

setStates({
isHydrated: true,
availableChainIds: SUPPORTED_CHAIN_IDS,
currentChainId: newChainId,
chainConfig,
indexerGraphClient: new GraphQLClient(chainConfig.indexer, {
headers: {
"x-hasura-admin-secret": HASURA_ADMIN_SECRET,
},
}),
constants: PROJECT_CONSTANTS,
theme,
setTheme: (newTheme: ThemeConfig) =>
setStates((prev) => ({ ...prev, theme: newTheme })),
});
}, []);
const handleOnChainIdChange = useCallback(
(newChainId: string) => {
const chainConfig = chainConfigs[newChainId] ?? FALLBACK_CHAIN_CONFIG;

const theme = getTheme(chainConfig.chain);
changeFavicon(theme.branding.favicon);

setStates({
isHydrated: true,
availableChainIds: SUPPORTED_CHAIN_IDS,
currentChainId: newChainId,
chainConfig,
indexerGraphClient: new GraphQLClient(chainConfig.indexer, {
headers: {
"x-hasura-admin-secret": HASURA_ADMIN_SECRET,
},
}),
constants: PROJECT_CONSTANTS,
theme,
setTheme: (newTheme: ThemeConfig) =>
setStates((prev) => ({ ...prev, theme: newTheme })),
});
},
[chainConfigs]
);

// Disable "Leave page" alert
useEffect(() => {
Expand All @@ -102,6 +110,7 @@ export const AppProvider = ({ children }: AppProviderProps) => {

useNetworkChange(handleOnChainIdChange);

if (!isChainConfigStoreHydrated) return <LoadingOverlay />;
return <AppContext.Provider value={states}>{children}</AppContext.Provider>;
};

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 @@ -17,3 +17,4 @@ export * from "./useConvertHexAddress";
export * from "./usePlatform";
export * from "./useInitia";
export * from "./useIsConnected";
export * from "./useChainConfigs";
Loading

0 comments on commit 9b4c8db

Please sign in to comment.