From c523bcdf5e62a53f1a1562fae54df64245e284b7 Mon Sep 17 00:00:00 2001 From: jinoosss Date: Fri, 15 Dec 2023 22:40:25 +0900 Subject: [PATCH] feat: Add data initialization hook in background --- .../web/src/hooks/common/use-background.tsx | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/web/src/hooks/common/use-background.tsx b/packages/web/src/hooks/common/use-background.tsx index ad7d0163b..2ae3c27c2 100644 --- a/packages/web/src/hooks/common/use-background.tsx +++ b/packages/web/src/hooks/common/use-background.tsx @@ -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>((prev, current) => { + prev[current.path] = current; + return prev; + }, {}); + setTokenPrices(priceMap); + } + }, [tokenPrices]); + + useEffect(() => { + if (window.adena) { + initSession(); + } + }, [window.adena]); useEffect(() => { if (walletClient) {