Skip to content

Commit

Permalink
Merge pull request #936 from alleslabs/fix/add-hydrated-in-context
Browse files Browse the repository at this point in the history
Fix/add hydrated in context
  • Loading branch information
songwongtp committed May 18, 2024
2 parents 0c69b5f + 137f8d9 commit 74554b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

- [#936](https://github.com/alleslabs/celatone-frontend/pull/936) Add isHydrated in app context for redirect behaviors
- [#921](https://github.com/alleslabs/celatone-frontend/pull/921) Add theme
- [#922](https://github.com/alleslabs/celatone-frontend/pull/922) Adjust color for latest blocks
- [#826](https://github.com/alleslabs/celatone-frontend/pull/826) Apply dynamic component theme generation
Expand Down
3 changes: 3 additions & 0 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface AppProviderProps {
}

interface AppContextInterface {
isHydrated: boolean;
availableChainIds: string[];
currentChainId: string;
chainConfig: ChainConfig;
Expand All @@ -38,6 +39,7 @@ interface AppContextInterface {
}

const DEFAULT_STATES: AppContextInterface = {
isHydrated: false,
availableChainIds: SUPPORTED_CHAIN_IDS,
currentChainId: FALLBACK_SUPPORTED_CHAIN_ID,
chainConfig: FALLBACK_CHAIN_CONFIG,
Expand Down Expand Up @@ -66,6 +68,7 @@ export const AppProvider = ({ children }: AppProviderProps) => {
changeFavicon(theme.branding.favicon);

setStates({
isHydrated: true,
availableChainIds: SUPPORTED_CHAIN_IDS,
currentChainId: newChainId,
chainConfig,
Expand Down
19 changes: 11 additions & 8 deletions src/lib/providers/network-guard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,38 @@ interface NetworkGuardProps {
}

export const NetworkGuard = observer(({ children }: NetworkGuardProps) => {
const { currentChainId, chainConfig } = useCelatoneApp();
const {
isHydrated,
currentChainId,
chainConfig: { registryChainName },
} = useCelatoneApp();
const { setAccountUserKey, isAccountUserKeyExist } = useAccountStore();
const { setCodeUserKey, isCodeUserKeyExist } = useCodeStore();
const { setContractUserKey, isContractUserKeyExist } = useContractStore();
const { setProjectUserKey, isProjectUserKeyExist } = usePublicProjectStore();

useEffect(() => {
if (chainConfig.registryChainName) {
const userKey = formatUserKey(
chainConfig.registryChainName,
DEFAULT_ADDRESS
);
if (isHydrated) {
const userKey = formatUserKey(registryChainName, DEFAULT_ADDRESS);
setAccountUserKey(userKey);
setCodeUserKey(userKey);
setContractUserKey(userKey);
setProjectUserKey(userKey);
}
}, [
chainConfig.registryChainName,
isHydrated,
registryChainName,
setAccountUserKey,
setCodeUserKey,
setContractUserKey,
setProjectUserKey,
]);

if (currentChainId && !(currentChainId in CHAIN_CONFIGS))
if (isHydrated && !(currentChainId in CHAIN_CONFIGS))
return <NetworkErrorState />;

if (
!isHydrated ||
!isAccountUserKeyExist() ||
!isCodeUserKeyExist() ||
!isContractUserKeyExist() ||
Expand Down

0 comments on commit 74554b3

Please sign in to comment.