Skip to content

Commit

Permalink
Merge pull request #259 from gnoswap-labs/GSW-446-add-data-initializa…
Browse files Browse the repository at this point in the history
…tion-hook-in-background

feat: Add data initialization hook in background
  • Loading branch information
jinoosss authored Dec 15, 2023
2 parents 39c10d5 + c523bcd commit d13c1d6
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions packages/web/src/hooks/common/use-background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@ import { useWallet } from "@hooks/wallet/use-wallet";
import { useAtom } from "jotai";
import { TokenState, WalletState } from "@states/index";
import { useTokenData } from "@hooks/token/use-token-data";
import { useRouter } from "next/router";
import { useGetTokenPrices, useGetTokensList } from "@query/token";
import { TokenPriceModel } from "@models/token/token-price-model";

export const useBackground = () => {
const router = useRouter();
const [walletClient] = useAtom(WalletState.client);
const { account, initSession, connectAccount, updateWalletEvents } = useWallet();
const [walletClient] = useAtom(WalletState.client);
const [, setTokens] = useAtom(TokenState.tokens);
const [, setTokenPrices] = useAtom(TokenState.tokenPrices);
const [, setBalances] = useAtom(TokenState.balances);
const { updateBalances } = useTokenData();

const { data: tokens } = useGetTokensList();
const { data: tokenPrices } = useGetTokenPrices();

useEffect(() => {
if (tokens) {
setTokens(tokens.tokens);
}
}, [tokens]);

useEffect(() => {
initSession();
}, [router.route]);
if (tokenPrices) {
const priceMap = tokenPrices.prices.reduce<Record<string, TokenPriceModel>>((prev, current) => {
prev[current.path] = current;
return prev;
}, {});
setTokenPrices(priceMap);
}
}, [tokenPrices]);

useEffect(() => {
if (window.adena) {
initSession();
}
}, [window.adena]);

useEffect(() => {
if (walletClient) {
Expand Down

0 comments on commit d13c1d6

Please sign in to comment.